<?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.jxy.order.mapper.OrderMapper">

	<insert id="insertAddressAndUserId">
        replace into user_address(
        user_id
        <if test="dto.address != null and dto.address != ''">,address</if>
        <if test="dto.addressBSC != null and dto.addressBSC != ''">,address_bsc</if>
        <if test="dto.addressTRX != null and dto.addressTRX != ''">,address_trx</if>
        ) values (
        #{dto.userId}
        <if test="dto.address != null and dto.address != ''">,#{dto.address}</if>
        <if test="dto.addressBSC != null and dto.addressBSC != ''">,#{dto.addressBSC}</if>
        <if test="dto.addressTRX != null and dto.addressTRX != ''">,#{dto.addressTRX}</if>
        )
    </insert>

	<insert id="insertAddCreditOrder"  useGeneratedKeys="true" keyProperty="id">
        insert into transfer(user_id,txid,address,coin,`chain`,status,update_time,create_time) values (
        #{dto.userId},#{dto.txid},#{dto.address},#{dto.coin},#{dto.chain},#{dto.status},sysdate(),#{dto.createTime}
        )
    </insert>

	<insert id="insertUserAccount">
        insert into user_account(user_id,usdt,usdc,busd) values (
        #{dto.userId},#{dto.usdt},#{dto.usdc},#{dto.busd}
        ) ON duplicate key update user_id=user_id
    </insert>

    <insert id="insertUserBalance">
        insert into user_balance(
        user_id,
        txid,
        <if test="dto.coin != null and dto.coin !=''"> coin,</if>
        <if test="dto.chain != null and dto.chain !=''">`chain`,</if>
        balance_usdt,
        balance_usdc,
        balance_busd,
        <if test="dto.message != null and dto.message !=''">message,</if>
        notes,
        update_time)
        values (
        #{dto.userId},
        #{dto.txid},
        <if test="dto.coin != null and dto.coin !=''">#{dto.coin},</if>
        <if test="dto.chain != null and dto.chain !=''">#{dto.chain},</if>
        #{dto.balanceUSDT},
        #{dto.balanceUSDC},
        #{dto.balanceBUSD},
        <if test="dto.message != null and dto.message !=''">#{dto.message}, </if>
        #{dto.notes},
        sysdate())
    </insert>

    <insert id="batchInsertTotalAddress">
        insert into total_address(address) values
        <foreach item="item" index="index" collection="list" separator=",">
            (#{item})
        </foreach>
        ON duplicate KEY UPDATE  address= VALUES(address)
    </insert>

    <insert id="batchInsertBSCTotalAddress">
        insert into total_address_bsc(address) values
        <foreach item="item" index="index" collection="list" separator=",">
            (#{item})
        </foreach>
        ON duplicate KEY UPDATE  address= VALUES(address)
    </insert>
    <insert id="batchInsertTRXTotalAddress">
        insert into total_address_trx(address) values
        <foreach item="item" index="index" collection="list" separator=",">
            (#{item})
        </foreach>
        ON duplicate KEY UPDATE  address= VALUES(address)
    </insert>


    <select id="checkTxidIfExist" resultType="java.lang.Integer">
        select count(*) from transfer where txid = #{txid}
    </select>

	<select id="getAddCreditOrderByUserId" resultType="com.jxy.order.entity.AddCreditOrder">
        select
            id,
            user_id userId,
            txid,
            address,
            coin,
            `chain`,
            amount,
            status,
            update_time updateTime,
            create_time createTime
        from
            transfer
        <where>
            user_id =#{userId}
            <if test ="status != null"> and status = #{status}</if>
        </where>
        order by update_time
    </select>

	<select id="getAddCreditOrderById" resultType="com.jxy.order.entity.AddCreditOrder">
        select
            id,
            user_id userId,
            txid,
            address,
            coin,
            `chain`,
            status,
            update_time updateTime,
            create_time createTime
        from
            transfer
        where
            id =#{id}
        order by update_time desc limit 1
    </select>

	<select id="getUserAccountByUserId" resultType="com.jxy.order.dto.UserAccountDto">
        select
            user_id userId,
            usdt,
            usdc,
            busd
        from
            user_account
        where
            user_id =#{userId}
    </select>

	<select id="getAddressByUserId" resultType="com.jxy.order.entity.UserAddress">
        select
            user_id userId,
            address,
            address_bsc as addressBSC,
            address_trx as addressTRX
        from
            user_address
        where
            user_id = #{userId} limit 1
    </select>

    <select id="getAddressNoUse" resultType="java.lang.String">
        select address from ${table} where if_use = 0 limit 1;
    </select>

    <select id="getAddCreditOrderByOrderIdAndUserId" resultType="com.jxy.order.entity.AddCreditOrder">
         select
            id,
            user_id userId,
            txid,
            address,
            coin,
            `chain`,
            status,
            update_time updateTime,
            create_time createTime
        from
            transfer
        where
            id =#{orderId}
            and user_id = #{userId}
        order by update_time desc limit 1
    </select>


    <select id="getAddCreditOrderByStatus" resultType="com.jxy.order.dto.AddCreditOrderDto">
        select
            t.id,
            t.user_id userId,
            u.email email,
            t.txid,
            t.address,
            t.coin,
            t.`chain`,
            t.amount,
            t.status,
            t.update_time updateTime,
            t.create_time createTime
        from
        transfer t
        left join jxy_cloud.sys_user u on t.user_id = u.user_id
        <where>
            <if test="vo.userId != null and vo.userId !=0"> and t.user_id = #{vo.userId}</if>
            <if test="vo.status != null"> and t.status = #{vo.status}</if>
        </where>
        order by t.update_time
    </select>
    <select id="getExpireNoHandleAddCreditOrder" resultType="com.jxy.order.entity.AddCreditOrder">
        select
        id,
        user_id userId,
        txid,
        address,
        coin,
        `chain`,
        amount,
        status,
        update_time updateTime,
        create_time createTime
        from
        transfer
        <where>
            status = 2
            <if test="vo.userId != null and vo.userId !=0"> and user_id = #{vo.userId}</if>
            <if test="vo.address != null and vo.address !=''"> and address = #{vo.address}</if>
        </where>
        order by update_time
    </select>
    <select id="getAddressNoUsedCount" resultType="java.lang.Integer">
        select count(*) from total_address where if_use = 0;
    </select>

    <select id="getAllAddress" resultType="com.jxy.order.entity.AddressInfo">
        select
            address,
            case if_use when 0 then '未使用' when 1 then '已使用' end status
        from
            total_address
        <where>
            <if test="status != null and status !=''">
                if_use = #{status}
            </if>
        </where>
    </select>
    <select id="getUserIdByEmail" resultType="java.lang.Long">
        select user_id from jxy_cloud.sys_user where email=#{email}
    </select>

    <update id="updateAddressToUse">
        update
            ${table}
        set
            if_use = 1
        where address = #{address} and if_use = 0
    </update>

    <update id="updateOrderStatusById">
        update
            transfer
        set
            status = #{vo.oper},
            update_time = sysdate()
        where id = #{vo.transferId}
    </update>

	<update id="updateUserAccount">
        update
            user_account
        set
            usdt = #{dto.usdt},
            usdc = #{dto.usdc},
            busd = #{dto.busd}
        where user_id = #{dto.userId}
            and usdt = #{usdt} and usdc =#{usdc} and busd=#{busd}
    </update>
    <update id="updateOrdersById">
         update
            transfer
        set
            txid = #{vo.txid},
            amount = #{vo.amount},
            status = #{vo.oper},
            update_time = sysdate()
        where id = #{vo.transferId}
    </update>



</mapper>
