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

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

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        id, user_id, from_address, balance, to_address, from_symbol, from_chain, to_symbol, to_chain, qrcode, create_time, update_time, del
    </sql>


    <!-- 根据用户 ID 查询钱包信息 -->
    <select id="selectByUserId" resultType="com.m2pool.lease.dto.UserWalletDataDto">
        SELECT
            id,
            user_id as userId,
            from_address as fromAddress,
            balance,
            blocked_balance as blockedBalance,
            qrcode,
            to_address as toAddress,
            from_symbol as fromSymbol,
            from_chain as fromChain,
            to_symbol as toSymbol,
            to_chain as toChain,
            create_time as createTime
        FROM lease_user_wallet_data
        WHERE user_id = #{userId} AND del = 0
    </select>
    <select id="getWalletInfoByAddress" resultType="com.m2pool.lease.dto.UserWalletDataDto">
        SELECT
            id,
            user_id as userId,
            from_address as fromAddress,
            balance,
            blocked_balance as blockedBalance,
            qrcode,
            to_address as toAddress,
            from_symbol as fromSymbol,
            from_chain as fromChain,
            to_symbol as toSymbol,
            to_chain as toChain,
            create_time as createTime
        FROM lease_user_wallet_data
        WHERE user_id = #{userId} AND from_symbol = #{symbol} AND from_address = #{address} AND del = 0
    </select>
    <select id="getWalletInfoByChain" resultType="com.m2pool.lease.dto.UserWalletDataDto">
        SELECT
            id,
            user_id as userId,
            from_address as fromAddress,
            balance,
            blocked_balance as blockedBalance,
            qrcode,
            to_address as toAddress,
            from_symbol as fromSymbol,
            from_chain as fromChain,
            to_symbol as toSymbol,
            to_chain as toChain,
            create_time as createTime,
            queue_id as queueId
        FROM lease_user_wallet_data
        WHERE user_id = #{userId} AND from_chain = #{chain} AND del = 0
    </select>
    <select id="getWalletForBalanceIsZero" resultType="com.m2pool.lease.entity.LeaseUserWalletData">
        SELECT
            queue_id as queueId,
        from_address as fromAddress,
        from_symbol as fromSymbol,
        from_chain as fromChain
        FROM lease_user_wallet_data
        WHERE balance = 0  AND del = 0 AND create_time <![CDATA[ <= ]]> DATE_SUB(NOW(), INTERVAL 6 MONTH) AND
        (<foreach collection="list" item="item" separator="OR">
            (`from_address` = #{item.fromAddress} AND from_symbol = #{item.fromSymbol} AND from_chain = #{item.fromChain})
        </foreach>)
    </select>
    <select id="selectWalletByChainAndCoinAndUsername" resultType="com.m2pool.lease.entity.LeaseUserWalletData">
        SELECT
            id,
            from_address as fromAddress,
            balance,
            blocked_balance as blockedBalance,
            from_symbol as fromSymbol,
            from_chain as fromChain
        FROM lease_user_wallet_data
        WHERE del = 0 AND user_id = #{username} AND (
        <foreach collection="list" item="item" separator="OR">
            (from_chain = #{item.chain} AND from_symbol = #{item.coin} )
        </foreach>
        )
    </select>


    <update id="updateBalanceAndBlockBalance">
        <foreach collection="list" item="item" separator=";">
            UPDATE lease_user_wallet_data
            SET balance = balance - #{item.realAmount},
            blocked_balance = blocked_balance - #{item.blockAmount}
            WHERE from_address = #{item.fromAddress} AND from_symbol = #{item.fromSymbol} AND from_chain = #{item.fromChain} AND del = false
        </foreach>
    </update>

</mapper>
