<?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.m2pool.lease.mapper.LeasePayWithdrawMessageMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.m2pool.lease.entity.LeasePayWithdrawMessage">
        <id column="id" property="id" />
        <result column="queue_id" property="queueId" />
        <result column="from_address" property="fromAddress" />
        <result column="to_address" property="toAddress" />
        <result column="amount" property="amount" />
        <result column="from_chain" property="fromChain" />
        <result column="from_symbol" property="fromSymbol" />
        <result column="to_chain" property="toChain" />
        <result column="to_symbol" property="toSymbol" />
        <result column="create_time" property="createTime" />
        <result column="update_time" property="updateTime" />
        <result column="status" property="status" />
        <result column="del" property="del" />
    </resultMap>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        id, queue_id, from_address, to_address, amount, from_chain, from_symbol,to_chain,to_symbol, create_time, update_time, status, del
    </sql>
    <select id="balanceWithdrawList" resultType="com.m2pool.lease.dto.PayWithdrawMessageDto">
SELECT
    id,
    from_address as fromAddress,
    to_address as toAddress,
    amount,
    from_symbol as fromSymbol,
    from_chain as fromChain,
    to_symbol as toSymbol,
    to_chain as toChain,
    create_time as createTime,
    update_time as updateTime,
    tx_hash as txHash,
    status
FROM lease_pay_withdraw_message

        <where>
            (<foreach collection="walletList" item="item" separator="OR">
            (from_address = #{item.fromAddress}
            AND `from_chain` = #{item.fromChain}
            AND from_symbol = #{item.fromSymbol})
        </foreach>)
            <if test="status != null">
                AND status = #{status}
            </if>
        </where>
    </select>
    <select id="checkWithdrawOperator" resultType="java.lang.Boolean">
        SELECT IF(COUNT(*) > 0, true, false) AS has_data
        FROM lease_pay_withdraw_message
        WHERE from_address = #{fromAddress} AND create_time >= DATE_SUB(CURRENT_TIMESTAMP, INTERVAL 6 MONTH) AND del = 0;
    </select>
    <select id="checkWithdrawOperatorBatch" resultType="com.m2pool.lease.dto.CheckAddressDto">
        SELECT DISTINCT from_address as fromAddress,from_chain as fromChain,from_symbol as fromSymbol, true AS hasOperator
        FROM lease_pay_withdraw_message
        WHERE
        (<foreach collection="list" item="item" separator="OR">
             (from_address = #{item.fromAddress} AND `from_chain` = #{item.fromChain} AND from_symbol = #{item.fromSymbol})
        </foreach>)
        AND create_time >= DATE_SUB(CURRENT_TIMESTAMP, INTERVAL 6 MONTH) AND del = 0
        GROUP BY from_address, from_chain, from_symbol;
    </select>
    <select id="transactionRecord" resultType="com.m2pool.lease.dto.TransactionRecordDto">
        SELECT
        queue_id as queueId,
        from_address as fromAddress,
        to_address as toAddress,
        amount,
        from_symbol as fromSymbol,
        from_chain as fromChain,
        to_symbol as toSymbol,
        to_chain as toChain,
        create_time as createTime,
        update_time as updateTime,
        tx_hash as txHash,
        status
        FROM lease_pay_withdraw_message

        <where>
            (<foreach collection="walletList" item="item" separator="OR">
            (from_address = #{item.fromAddress}
            AND `from_chain` = #{item.fromChain}
            AND from_symbol = #{item.fromSymbol})
        </foreach>)
        </where>
        order by create_time desc
    </select>
    <select id="getRecentlyTransaction" resultType="com.m2pool.lease.dto.RecentlyTransactionDto">
        select
        amount as amount,
        update_time as updateTime,
        2 as type,
        status
        from lease_pay_withdraw_message
        <where>
            (<foreach collection="walletList" item="item" separator="OR">
            (from_address = #{item.fromAddress} AND `from_chain` = #{item.fromChain} AND from_symbol = #{item.fromSymbol})
        </foreach>)
        </where>
        order by update_time desc limit 5
    </select>
    <select id="getRecentlyOneData" resultType="java.lang.Integer">
        select status from lease_pay_withdraw_message where from_address = #{fromAddress} and `from_chain` = #{chain} order by create_time desc limit 1
    </select>
    <select id="getSellerWithdrawRecord" resultType="com.m2pool.lease.dto.v2.PayWithdrawSellerRecordDto">
        select
        id,
        from_address as fromAddress,
        to_address as toAddress,
        amount,
        from_symbol as symbol,
        from_chain as chain,
        create_time as createTime,
        update_time as updateTime,
        tx_hash as txHash,
        status
        from lease_pay_withdraw_message
        where shop_id = #{shopId}
        order by create_time desc
    </select>

</mapper>
