<?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.LeasePayRecordMessageMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.m2pool.lease.entity.LeasePayRecordMessage">
        <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="to_chain" property="toChain" />
        <result column="to_symbol" property="toSymbol" />
        <result column="from_chain" property="fromChain" />
        <result column="from_symbol" property="fromSymbol" />
        <result column="order_id" property="orderId" />
        <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, `to_chain`, to_symbol,from_chain,from_symbol, order_id,create_time, status, del
    </sql>

    <select id="checkPayOperator" resultType="java.lang.Boolean">
        SELECT IF(COUNT(*) > 0, true, false) AS has_data
        FROM lease_pay_record_message
        WHERE from_address = #{fromAddress} AND create_time >= DATE_SUB(CURRENT_TIMESTAMP, INTERVAL 6 MONTH) AND del = 0;
    </select>
    <select id="checkPayOperatorBatch" 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_record_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
     UNION
        SELECT DISTINCT from_address as fromAddress,from_chain as fromChain,from_symbol as fromSymbol, true AS hasOperator
        FROM lease_pay_record_message_info
        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="balancePayList" resultType="com.m2pool.lease.dto.PayRecordMessageDto">
        select
            queue_id as queueId,
            from_address as fromAddress,
            to_address as toAddress,
            amount as amount,
            real_amount as realAmount,
            to_symbol as toSymbol,
            to_chain as toChain,
            from_symbol as fromSymbol,
            from_chain as fromChain,
            tx_hash as txHash,
            create_time as createTime,
            update_time as updateTime,
            status as status,
            order_number as orderId
        from lease_pay_record_message
        <where>
            (<foreach collection="walletList" item="item" separator="OR">
            (
                shop_id = #{item.shopId}
            AND  to_address = #{item.payAddress}
            AND `to_chain` = #{item.chain}
            AND to_symbol = #{item.payCoin})
        </foreach>)
            <if test="status != null">
                AND status = #{status}
            </if>
        </where>
        order by update_time desc
    </select>
    <select id="transactionRecord" resultType="com.m2pool.lease.dto.TransactionRecordDto">
        select
        queue_id as queueId,
        from_address as fromAddress,
        to_address as toAddress,
        amount as amount,
        real_amount as realAmount,
        block_amount as blockAmount,
        to_symbol as toSymbol,
        to_chain as toChain,
        from_symbol as fromSymbol,
        from_chain as fromChain,
        tx_hash as txHash,
        order_number as orderId,
        create_time as createTime,
        update_time as updateTime,
        status as status
        from lease_pay_record_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
            case when real_amount = 0 then amount else real_amount end as amount,
            update_time as updateTime,
            0 as type,
            status
        from lease_pay_record_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="getQueueIdByInfoIds" resultType="java.util.Map">
        select
            DISTINCT order_id as orderId,
            queue_id as queueId
        from lease_pay_record_message
        where order_id in
        <foreach item="item" index="index" collection="infoIds" open="(" close=")" separator=",">
            #{item}
        </foreach>
    </select>

    <update id="updateByTxHash">
        update lease_pay_record_message
        set
        status = #{leasePayRecordMessage.status},
        update_time = #{leasePayRecordMessage.updateTime},
        tx_hash = #{leasePayRecordMessage.txHash},
        block_height = #{leasePayRecordMessage.blockHeight},
        real_amount = #{leasePayRecordMessage.realAmount}
        where id = #{leasePayRecordMessage.id} and (tx_hash = "" or tx_hash = #{txHash})
    </update>

</mapper>
