<?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.WithdrawMapper">
    <insert id="insertWithdraw">
        insert into withdraw(user_id,applied_amount,applied_coin,applied_chain,applied_address,applied_time,status) values (
        #{vo.userId},#{vo.amount},#{vo.coin},#{vo.chain},#{vo.address},sysdate(),2
        )
    </insert>

    <update id="updateWithdraw">
        update
            withdraw
        <set>
            <if test="vo.msg != null and vo.msg != ''">
                message = #{vo.msg},
            </if>
            <if test="vo.refundTxid != null and vo.refundTxid != ''">
                refund_txid = #{vo.refundTxid},
            </if>
            <if test="vo.refundAddress != null and vo.refundAddress != ''">
                refund_address = #{vo.refundAddress},
            </if>
            refund_time = sysdate(),
            status = #{vo.oper}
        </set>
        where id = #{vo.id}
    </update>

    <update id="batchUpdateExpireWithdraw">
        update withdraw set status = 0 where id in
        <foreach item="item" index="index" collection="list" open="(" separator="," close=")">
            #{item.id}
        </foreach>
        and status = 2
    </update>


    <select id="getWithdrawByUserId" resultType="com.jxy.order.dto.WithdrawPraviteDto">
        select
            id,
            user_id userId,
            applied_amount amount,
            applied_coin coin,
            applied_chain chain,
            applied_address userAddress,
            applied_time appliedTime,
            status,
            refund,
            refund_time refundTime,
            refund_txid refundTxid,
            message
        from
            withdraw
        <where>
            user_id =#{userId}
            <if test="status != null "> and status = #{status}</if>
        </where>
        order by applied_time desc
    </select>

    <select id="getWithdrawByStatus" resultType="com.jxy.order.dto.WithdrawDto">
        select
            w.id,
            w.user_id userId,
            u.email email,
            a.address userAddress,
            w.applied_amount appliedAmount,
            w.applied_coin coin,
            w.applied_chain chain,
            w.applied_address appliedAddress,
            w.applied_time appliedTime,
            w.refund_address refundAddress,
            w.refund_txid refundTxid,
            w.status,
            ac.usdt usdt,
            ac.usdc usdc,
            ac.busd busd
        from
            withdraw w left join user_address a on w.user_id = a.user_id
            left join jxy_cloud.sys_user u on u.user_id = w.user_id
            left join user_account ac on w.user_id = ac.user_id
        <where>
            <if test="status != null "> and w.status = #{status}</if>
            <if test="email != null "> and u.email = #{email}</if>
        </where>
        order by w.applied_time desc
    </select>

    <select id="getWithdrawDetailById" resultType="com.jxy.order.dto.WithdrawDetailDto">
         select
            w.id,
            w.user_id userId,
            u.email email,
            w.applied_amount amount,
            w.applied_coin coin,
            w.applied_chain chain,
            a.address userAddress,
            w.applied_address appliedAddress,
            w.applied_time appliedTime,
            w.status,
            w.refund,
            w.refund_time refundTime,
            w.refund_txid refundTxid,
            w.message
        from
            withdraw w
            left join jxy_cloud.sys_user u on u.user_id = w.user_id
            left join user_address a on w.user_id = a.user_id
        where
            w.id = #{id}
    </select>
    <select id="getExpireNoHandleWithdrawOrder" resultType="com.jxy.order.dto.WithdrawPraviteDto">
        select
            id,
            user_id userId,
            applied_amount amount,
            applied_coin coin,
            applied_chain chain
        from
            withdraw
        where
            status = 2 and applied_time &lt;= #{date}
    </select>


</mapper>

