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

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.m2pool.lease.entity.LeasePayRechargeMessage">
        <id column="id" property="id" />
        <result column="address" property="address" />
        <result column="amount" property="amount" />
        <result column="chain" property="chain" />
        <result column="symbol" property="symbol" />
        <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, address, amount, symbol,`chain`, create_time, update_time, status, del
    </sql>

    <select id="balanceRechargeList" resultType="com.m2pool.lease.dto.PayRechargeMessageDto">
select id, address, amount, chain as fromChain, symbol as fromSymbol, create_time as createTime, status,    tx_hash as txHash
from lease_pay_recharge_message
        <where>
            (<foreach collection="walletList" item="item" separator="OR">
                (address = #{item.fromAddress}
                AND `chain` = #{item.fromChain}
                AND symbol = #{item.fromSymbol})
            </foreach>)
            <if test="status != null">
                AND status = #{status}
            </if>
        </where>
    </select>
    <select id="checkRechargeOperator" resultType="java.lang.Boolean">
        SELECT IF(COUNT(*) > 0, true, false) AS has_data
        FROM lease_pay_recharge_message
        WHERE address = #{fromAddress} AND create_time >= DATE_SUB(CURRENT_TIMESTAMP, INTERVAL 6 MONTH) AND del = 0;
    </select>
    <select id="checkRechargeOperatorBatch" resultType="com.m2pool.lease.dto.CheckAddressDto">
        SELECT DISTINCT address as fromAddress,chain as fromChain,symbol as fromSymbol, true AS hasOperator
        FROM lease_pay_recharge_message
        WHERE
            (<foreach collection="list" item="item" separator="OR">
        (address = #{item.fromAddress} AND `chain` = #{item.fromChain} AND symbol = #{item.fromSymbol})
        </foreach>)
        AND create_time >= DATE_SUB(CURRENT_TIMESTAMP, INTERVAL 6 MONTH) AND del = 0
        GROUP BY address,`chain`,symbol;
    </select>
    <select id="transactionRecord" resultType="com.m2pool.lease.dto.TransactionRecordDto">
        select queue_id as queueId,address as fromAddress, amount, chain as fromChain, symbol as fromSymbol, create_time as createTime, status,    tx_hash as txHash
        from lease_pay_recharge_message
        <where>
            (<foreach collection="walletList" item="item" separator="OR">
                (address = #{item.fromAddress} AND `chain` = #{item.fromChain} AND 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,
        1 as type,status
        from lease_pay_recharge_message
        <where>
            (<foreach collection="walletList" item="item" separator="OR">
            (address = #{item.fromAddress} AND `chain` = #{item.fromChain} AND symbol = #{item.fromSymbol})
        </foreach>)
        </where>
        order by update_time desc limit 5
    </select>

    <insert id="saveOrUpdateByIndex">
        insert into lease_pay_recharge_message (queue_id,address, amount, chain, symbol, status,tx_hash)
        values (#{leasePayRechargeMessage.queueId},#{leasePayRechargeMessage.address},
                #{leasePayRechargeMessage.amount}, #{leasePayRechargeMessage.chain},
                #{leasePayRechargeMessage.symbol},#{leasePayRechargeMessage.status},#{leasePayRechargeMessage.txHash})
        ON DUPLICATE KEY UPDATE
        `queue_id` = VALUES(`queue_id`),
         `address` = VALUES(`address`),
        `amount` = VALUES(`amount`),
        `chain` = VALUES(`chain`),
        `symbol` = VALUES(`symbol`),
        `status` = VALUES(`status`),
        `tx_hash` = VALUES(`tx_hash`)
    </insert>
</mapper>
