package com.fzm.mall.server.front.asset.sql;


import com.fzm.mall.server.front.asset.model.po.qo.CoinLogQO;
import com.fzm.mall.server.front.asset.model.po.qo.CoinQO;
import com.fzm.mall.server.front.asset.model.qo.CoinOutQO;
import com.fzm.mall.server.front.constant.TokenPrefixType;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.jdbc.SQL;

/**
 * @author tdz
 * @date 2021/03/02 14:51
 */

public class CoinSqlFactory {

	public String listCoin(CoinQO coinQO) {
		return new SQL() {
			{
				SELECT("uc.`coin`", "uc.`amount`", "uc.`frozen`", "spu.name");
				SELECT("other.t_start_time", "other.t_end_time");
				FROM("user_asset uc");
				LEFT_OUTER_JOIN("goods_sku sku on uc.coin = sku.coin and sku.is_active = 1");
				LEFT_OUTER_JOIN("goods_spu spu on sku.goods_id = spu.goods_id");
				LEFT_OUTER_JOIN("goods_sale other on sku.goods_id = other.goods_id");
				WHERE("uc.uid = #{uid}");
				if (StringUtils.isNotBlank(coinQO.getType())) {
					if (TokenPrefixType.GOOD_NUM.equals(coinQO.getType())) {
						if (StringUtils.isNotBlank(coinQO.getCoin())) {
							WHERE("uc.coin like concat('SP', '%', #{coin}, '%')");
						} else {
							WHERE("uc.coin like concat('SP', '%')");
						}
					} else if (TokenPrefixType.BONUS_POINT_NUM.equals(coinQO.getType())) {
						if (StringUtils.isNotBlank(coinQO.getCoin())) {
							WHERE("uc.coin like concat('JF', '%', #{coin}, '%')");
						} else {
							WHERE("uc.coin like concat('JF', '%')");
						}
					} else if (TokenPrefixType.ASSET.equals(coinQO.getType())) {
						if (StringUtils.isNotBlank(coinQO.getCoin())) {
							WHERE("uc.coin like concat('%', #{coin}, '%')");
						} else {
							WHERE("(uc.coin = 'CCNY' or uc.coin = 'GXL')");
						}
					}
				} else {
					if (StringUtils.isNotBlank(coinQO.getIsToken())) {
						WHERE("uc.is_token = #{isToken}");
					}
					if (StringUtils.isNotBlank(coinQO.getBlock())) {
						WHERE("uc.block = #{block}");
					}
					if (StringUtils.isNotBlank(coinQO.getCoin())) {
						WHERE("uc.coin like concat('%', #{coin}, '%')");
					}
				}
				ORDER_BY("uc.update_time desc");
			}
		}.toString() + " limit #{begin},#{size}";
	}

	public String countCoin(CoinQO coinQO) {
		return new SQL() {
			{
				SELECT("count(id)");
				FROM("user_asset");
				WHERE("uid = #{uid}");
				if (StringUtils.isNotBlank(coinQO.getType())) {
					if (TokenPrefixType.GOOD_NUM.equals(coinQO.getType())) {
						if (StringUtils.isNotBlank(coinQO.getCoin())) {
							WHERE("coin like concat('SP', '%', #{coin}, '%')");
						} else {
							WHERE("coin like concat('SP', '%')");
						}
					} else if (TokenPrefixType.BONUS_POINT_NUM.equals(coinQO.getType())) {
						if (StringUtils.isNotBlank(coinQO.getCoin())) {
							WHERE("coin like concat('JF', '%', #{coin}, '%')");
						} else {
							WHERE("coin like concat('JF', '%')");
						}
					} else if (TokenPrefixType.ASSET.equals(coinQO.getType())) {
						if (StringUtils.isNotBlank(coinQO.getCoin())) {
							WHERE("coin like concat('%', #{coin}, '%')");
						} else {
							WHERE("(coin = 'CCNY' or coin = 'GXL')");
						}
					}
				} else {
					if (StringUtils.isNotBlank(coinQO.getIsToken())) {
						WHERE("is_token = #{isToken}");
					}
					if (StringUtils.isNotBlank(coinQO.getBlock())) {
						WHERE("block = #{block}");
					}
					if (StringUtils.isNotBlank(coinQO.getCoin())) {
						WHERE("coin like concat('%', #{coin}, '%')");
					}
				}
//				if (StringUtils.isNotBlank(coinQO.getHide()) && MallConst.ALL_1.equals(coinQO.getHide())) {
//					WHERE("amount > 0");
//				}
			}
		}.toString();
	}

	public String listLog(CoinLogQO coinLogQO) {
		return new SQL() {
			{
				SELECT("log_tx_coin.id aid", "log_tx_coin.uid", "log_tx_coin.type", "log_tx_coin.is_token",
						"log_tx_coin.block", "log_tx_coin.coin", "log_tx_coin.address", "log_tx_coin.amount",
						"log_tx_coin.is_active", "log_tx_coin.create_time");
				SELECT("log_tx_coin_channel.id channel_id", "log_tx_coin_channel.name channel_name");
				SELECT("spu.name");
				FROM("log_tx_coin");
				LEFT_OUTER_JOIN("log_tx_coin_channel on log_tx_coin.channel = log_tx_coin_channel.id");
				LEFT_OUTER_JOIN("goods_sku sku on log_tx_coin.coin = sku.coin and sku.is_active = 1");
				LEFT_OUTER_JOIN("goods_spu spu on sku.goods_id = spu.goods_id");
				if (StringUtils.isNotBlank(coinLogQO.getUid())) {
					WHERE("log_tx_coin.uid = #{uid}");
				}
				if (StringUtils.isNotBlank(coinLogQO.getIsToken())) {
					WHERE("log_tx_coin.is_token = #{isToken}");
				}
				if (StringUtils.isNotBlank(coinLogQO.getType())) {
					WHERE("log_tx_coin.type = #{type}");
				}
				if (StringUtils.isNotBlank(coinLogQO.getAddress())) {
					WHERE("log_tx_coin.address = #{address}");
				}
				if (StringUtils.isNotBlank(coinLogQO.getState())) {
					WHERE("log_tx_coin.is_active = #{state}");
				}
				if (StringUtils.isNotBlank(coinLogQO.getCoin())) {
					WHERE("log_tx_coin.coin = #{coin}");
				}
				ORDER_BY("log_tx_coin.id desc");
			}
		}.toString() + " limit #{begin},#{size}";
	}

	public String countLog(CoinLogQO coinLogQO) {
		return new SQL() {
			{
				SELECT("count(id)");
				FROM("log_tx_coin");
				if (StringUtils.isNotBlank(coinLogQO.getUid())) {
					WHERE("uid = #{uid}");
				}
				if (StringUtils.isNotBlank(coinLogQO.getIsToken())) {
					WHERE("is_token = #{isToken}");
				}
				if (StringUtils.isNotBlank(coinLogQO.getType())) {
					WHERE("type = #{type}");
				}
				if (StringUtils.isNotBlank(coinLogQO.getAddress())) {
					WHERE("address = #{address}");
				}
				if (StringUtils.isNotBlank(coinLogQO.getState())) {
					WHERE("is_active = #{state}");
				}
				if (StringUtils.isNotBlank(coinLogQO.getCoin())) {
					WHERE("coin = #{coin}");
				}
			}
		}.toString();
	}

	public String listApply(CoinOutQO coinOutQO) {
		return new SQL() {
			{
				SELECT("uc.*", "spu.name");
				FROM("user_coin_out_apply uc");
				LEFT_OUTER_JOIN("goods_sku sku on uc.coin = sku.coin and sku.is_active = 1");
				LEFT_OUTER_JOIN("goods_spu spu on sku.goods_id = spu.goods_id");
				WHERE("uc.delete_state = 0");
				if (StringUtils.isNotBlank(coinOutQO.getAid())) {
					WHERE("uc.aid = #{aid}");
				}
				if (StringUtils.isNotBlank(coinOutQO.getUid())) {
					WHERE("uc.uid = #{uid}");
				}
				if (StringUtils.isNotBlank(coinOutQO.getIsToken())) {
					WHERE("uc.is_token = #{isToken}");
				}
				if (StringUtils.isNotBlank(coinOutQO.getBlock())) {
					WHERE("uc.block = #{block}");
				}
				if (StringUtils.isNotBlank(coinOutQO.getVerifyState())) {
					WHERE("uc.verify_state = #{verifyState}");
				}
				if (StringUtils.isNotBlank(coinOutQO.getChainState())) {
					WHERE("uc.chain_state = #{chainState}");
				}
				if (StringUtils.isNotBlank(coinOutQO.getActiveState())) {
					WHERE("uc.active_state = #{activeState}");
				}
				WHERE("uc.create_time between #{startTime} and #{endTime}");
				if (StringUtils.isNotBlank(coinOutQO.getCoin())) {
					WHERE("uc.coin = #{coin}");
				}
				ORDER_BY("uc.id desc");
			}
		}.toString() + " limit #{begin},#{size}";
	}

	public String countApply(CoinOutQO coinOutQO) {
		return new SQL() {
			{
				SELECT("count(id)");
				FROM("user_coin_out_apply");
				WHERE("delete_state = 0");
				if (StringUtils.isNotBlank(coinOutQO.getAid())) {
					WHERE("aid = #{aid}");
				}
				if (StringUtils.isNotBlank(coinOutQO.getUid())) {
					WHERE("uid = #{uid}");
				}
				if (StringUtils.isNotBlank(coinOutQO.getIsToken())) {
					WHERE("is_token = #{isToken}");
				}
				if (StringUtils.isNotBlank(coinOutQO.getBlock())) {
					WHERE("block = #{block}");
				}
				if (StringUtils.isNotBlank(coinOutQO.getVerifyState())) {
					WHERE("verify_state = #{verifyState}");
				}
				if (StringUtils.isNotBlank(coinOutQO.getChainState())) {
					WHERE("chain_state = #{chainState}");
				}
				if (StringUtils.isNotBlank(coinOutQO.getActiveState())) {
					WHERE("active_state = #{activeState}");
				}
				WHERE("create_time between #{startTime} and #{endTime}");
				if (StringUtils.isNotBlank(coinOutQO.getCoin())) {
					WHERE("coin = #{coin}");
				}
			}
		}.toString();
	}
}
