<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.bonus.canteen.core.account.v3.mapper.AccTradeMapper">
    <!--   移动端分页查询充值记录 -->
    <select id="queryTradeAppPage" resultType="com.bonus.canteen.core.account.v3.app.vo.AppAccTradePageVO">
        SELECT
        id AS tradeId,
        amount,
        acc_all_bal as walletBalTotal,
        trade_type,
        trade_time,
        pay_state,
        pay_type,
        remark,
        fail_reason
        FROM acc_trade
        <where>
            cust_id = #{infoParam.custId}
            <if test="infoParam.startDateTime != null">
                and trade_time <![CDATA[ >= ]]> #{infoParam.startDateTime}
            </if>
            <if test="infoParam.endDateTime != null">
                and trade_time <![CDATA[ <= ]]> #{infoParam.endDateTime}
            </if>
            <if test="infoParam.tradeTypeList != null and infoParam.tradeTypeList.size()>0 ">
                and trade_type in
                <foreach collection="infoParam.tradeTypeList" item="tradeType" separator="," open="(" close=")">
                    #{tradeType}
                </foreach>
            </if>
            <if test="infoParam.payStateList != null and infoParam.payStateList.size()>0 ">
                and pay_state in
                <foreach collection="infoParam.payStateList" item="payState" separator="," open="(" close=")">
                    #{payState}
                </foreach>
            </if>
        </where>
        ORDER BY trade_time DESC
    </select>
    <!--    * 查询交易记录 场景：发送mq-->
    <select id="listAccTradeForSend" resultType="com.bonus.canteen.core.account.v3.mq.send.model.RepAccMqModel">
        SELECT
        atr.id AS flowId,
        atr.cust_id,
        ci.cust_num,
        ci.cust_name,
        atr.org_id,
        co.org_name,
        co.org_full_name,
        atr.psn_type,
        cpt.psn_type_name,
        atr.amount AS flowAmount,
        atr.actual_amount AS flowRealAmount,
        atr.manage_cost AS manageCost,
        atr.wallet_bal_total AS accTotalBal,
        atr.acc_all_bal AS accAllBal,
        atr.trade_time AS payTime,
        atr.trade_type AS flowType,
        atr.remark,
        atr.crby AS optname,
        atr.recharge_source AS rechargeSource,
        atr.machine_type AS machineType,
        atr.pay_channel AS payChannel,
        atr.pay_type AS payType,
        atr.batch_num AS batchNum,
        atr.recharge_operate AS rechargeOperate,
        atr.machine_sn As mchSn,
        atr.machine_sn,
        atr.withdraw_source,
        atr.operate_source,
        atr.origin_trade_id
        FROM
        acc_trade AS atr
        LEFT JOIN cust_info AS ci ON atr.cust_id = ci.cust_id
        LEFT JOIN cust_org AS co ON atr.org_id = co.org_id
        LEFT JOIN cust_psn_type AS cpt ON atr.psn_type = cpt.psn_type
        WHERE atr.id IN
        <foreach collection="tradeIdList" item="tradeId" separator="," open="(" close=")">
            #{tradeId}
        </foreach>
        ORDER BY atr.trade_time,atr.id
    </select>


    <!--    获取个人钱包充值汇总-->
    <select id="getAccRechargeSum" resultType="com.bonus.canteen.core.account.v3.api.vo.AccRechargeSumApiVO">
        SELECT
        count(*) AS sumCount,
        SUM( amount ) AS sumAmount
        FROM
        acc_trade t1
        <where>
            t1.trade_type = 10 and t1.pay_state = 3 and t1.trade_state = 2
            <if test="param.custId != null">
                and t1.cust_id = #{param.custId}
            </if>
            <if test="param.startDateTime != null">
                and t1.trade_time <![CDATA[ >= ]]> #{param.startDateTime}
            </if>
            <if test="param.endDateTime != null">
                and t1.trade_time <![CDATA[ <= ]]> #{param.endDateTime}
            </if>
            <if test="param.payChannellList != null and param.payChannellList.size() > 0">
                and t1.pay_channel in
                <foreach collection="param.payChannellList" item="payChannel" separator="," open="(" close=")">
                    #{payChannel}
                </foreach>
            </if>
        </where>
    </select>

    <!--    移动端根据交易id获取交易记录-->
    <select id="getOneTradeApp" resultType="com.bonus.canteen.core.account.v3.app.vo.AppAccTradePageVO">
        SELECT
            id AS tradeId,
            amount,
            acc_all_bal as walletBalTotal,
            trade_type,
            trade_time,
            pay_state,
            pay_type,
            remark
          FROM acc_trade
         WHERE id = #{tradeId}
    </select>

    <!--    查询用户补贴交易明细-->
    <select id="listAccSubsidyTrade" resultType="com.bonus.canteen.core.account.v3.model.AccTradeWalletDetail">
        SELECT *
        FROM
        acc_trade_wallet_detail
        <where>
            wallet_id = 2
            and cust_id = #{custId}
            <if test="tradeTypeList != null and tradeTypeList.size() > 0">
                and trade_type in
                <foreach collection="tradeTypeList" item="tradeType" separator="," open="(" close=")">
                    #{tradeType}
                </foreach>
            </if>
        </where>
        ORDER BY trade_time DESC,trade_id DESC
    </select>

</mapper>
