<?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.core.account.v3.mapper.AccTradeWalletDetailMapper">
<!--    获取当用户月即将过期的补贴记录集合-->
    <select id="listCustMonthAccSubsidyExpirationTrade"
            resultType="com.bonus.core.account.v3.model.AccTradeWalletDetail">
        SELECT t1.*
        FROM acc_trade_wallet_detail t1
        WHERE
        t1.cust_id = #{custId}
        AND t1.trade_type = 20
        AND t1.validate_time BETWEEN #{startDateTime} AND #{endDateTime}
        AND t1.expired_clear = 2
        AND EXISTS (SELECT t2.id
                    FROM acc_trade t2
                    WHERE t2.id = t1.trade_id
                    AND t2.trade_state = 2
                    AND t2.pay_state = 3
        )
    </select>

    <!--    查询钱包记录详情-->
    <select id="listAccTradeWalletDetailForSend"
            resultType="com.bonus.core.account.v3.mq.send.model.RepAccMqDetailModel">
        SELECT
        trade_id AS flowId,
        wallet_id,
        amount,
        wallet_bal,
        trade_type AS flowType,
        frozen_balance,
        trade_time
        FROM
        acc_trade_wallet_detail
        WHERE trade_id IN
        <foreach collection="tradeIdList" item="tradeId" separator="," open="(" close=")">
            #{tradeId}
        </foreach>
    </select>


    <!--    查询补贴有效期到期的用户id-->
    <select id="selectCustIdForSubsidyExpireTime" resultType="java.lang.Long">
        SELECT t1.cust_id
          FROM acc_trade_wallet_detail t1
         WHERE t1.trade_type = 20
           AND t1.validate_time <![CDATA[ >= ]]> #{expireTime}
           AND t1.amount <![CDATA[ > ]]> t1.use_amount
           AND EXISTS (SELECT t2.id
                         FROM acc_trade t2
                        WHERE t2.id = t1.trade_id
                          AND t2.trade_state = 2
                          AND t2.pay_state = 3)
         GROUP BY t1.cust_id
    </select>


    <!--    获取一个用户当前时间开始带有效期的记录-->
    <select id="selectOneSubsidyExpirationTrade" resultType="com.bonus.core.account.v3.model.AccTradeWalletDetail">
        SELECT t1.*
          FROM acc_trade_wallet_detail t1
         WHERE t1.cust_id = #{custId}
           AND t1.trade_type = 20
           AND t1.validate_time <![CDATA[ >= ]]> #{now}
           AND t1.amount <![CDATA[ > ]]> t1.use_amount
           AND EXISTS (SELECT t2.id
                         FROM acc_trade t2
                        WHERE t2.id = t1.trade_id
                          AND t2.trade_state = 2 AND t2.pay_state = 3)
         ORDER BY t1.validate_time ASC, t1.trade_id ASC, t1.id ASC LIMIT 1
    </select>

    <select id="queryAccTradeWalletDetailByLeOrdNo" resultType="com.bonus.core.account.v3.model.AccTradeWalletDetail">
        SELECT t1.trade_id,
        t1.cust_id,
        t1.wallet_id,
        t1.amount,
        t1.trade_type
        FROM acc_trade_wallet_detail t1
        WHERE EXISTS(
        SELECT 1
        FROM acc_trade t2
        WHERE t2.id = t1.trade_id
        AND t2.le_ord_no = #{orderId}
        AND t2.trade_type in
        <foreach collection="tradeTypeList" item="tradeType" separator="," open="(" close=")">
            #{tradeType}
        </foreach>
        )
    </select>

    <select id="queryAccTradeWalletDetailByOriginTradeId"
            resultType="com.bonus.core.account.v3.model.AccTradeWalletDetail">
        SELECT t1.trade_id,
               t1.cust_id,
               t1.wallet_id,
               t1.amount,
               t1.trade_type
          FROM acc_trade_wallet_detail t1
         WHERE EXISTS(SELECT 1
                        FROM acc_trade t2
                       WHERE t2.id = t1.trade_id
                         AND t2.origin_trade_id = #{originTradeId}
                         AND t2.trade_type = #{tradeType})
    </select>

    <select id="queryAccTradeOrderDetailByLeOrderNo" resultType="com.bonus.core.account.v3.po.AccOrdTradeDetailPO">
        SELECT t1.wallet_id,
        t1.amount,
        t2.le_ord_no,
        t2.wallet_bal_total
        FROM acc_trade_wallet_detail t1
        LEFT JOIN acc_trade t2 ON t1.trade_id = t2.id
        <where>
            <if test="leOrderNo != null">
                and t2.le_ord_no = #{leOrderNo}
            </if>
            <if test="leOrderNoList != null and leOrderNoList.size() != 0">
                and t2.le_ord_no in
                <foreach collection="leOrderNoList" item="leOrderNo" separator="," open="(" close=")">
                    #{leOrderNo}
                </foreach>
            </if>
        </where>
    </select>

    <select id="queryAccTradeWalletDetailByTradeIdList"
            resultType="com.bonus.core.account.v3.model.AccTradeWalletDetail">
        SELECT trade_id,
        cust_id,
        wallet_id,
        amount,
        wallet_bal,
        trade_type,
        validate_time
        FROM acc_trade_wallet_detail
        WHERE
        trade_id in
        <foreach collection="tradeIdList" item="tradeId" separator="," open="(" close=")">
            #{tradeId}
        </foreach>
    </select>

</mapper>
