<?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.tmerclub.cloud.user.mapper.UserWithdrawCashMapper">

    <insert id="save">
        INSERT INTO user_withdraw_cash(`user_id`, `pay_sys_type`, `order_no`, `biz_order_no`, `amount`, `bank_card_no`, `status`, `pay_fail_message`, `create_time`, `update_time`)
        VALUES
            (#{withdrawCash.userId}, #{withdrawCash.paySysType}, #{withdrawCash.orderNo}, #{withdrawCash.bizOrderNo}, #{withdrawCash.amount}, #{withdrawCash.bankCardNo}, #{withdrawCash.status}, #{withdrawCash.payFailMessage}, NOW(), NOW());
    </insert>

    <update id="update">
        UPDATE user_withdraw_cash
        SET
            <if test="withdrawCash.status != null">
                `status` = #{withdrawCash.status},
            </if>
            <if test="withdrawCash.payFailMessage != null and withdrawCash.payFailMessage != ''">
                pay_fail_message = #{withdrawCash.payFailMessage},
            </if>
            update_time = NOW()
        WHERE
            cash_id = #{withdrawCash.cashId}
    </update>

    <select id="getByOrderNo" resultType="com.tmerclub.cloud.user.model.UserWithdrawCash">
        SELECT
            *
        FROM
            user_withdraw_cash
        WHERE
            order_no = #{orderNo}
    </select>

    <select id="countWithdrawTimes" resultType="java.lang.Integer">
        SELECT
            count(*)
        FROM
            user_withdraw_cash
        WHERE
            user_id = #{userId}
            AND status = 2
            <if test="startTime != null">
                AND create_time >= #{startTime}
            </if>
    </select>

    <select id="getTotalWithdrawAmount" resultType="java.math.BigDecimal">
        SELECT
            sum(amount)
        FROM
            user_withdraw_cash
        WHERE
            user_id = #{userId}
            AND status = 2
    </select>

    <select id="listUserWithdrawCashVOList" resultType="com.tmerclub.cloud.user.vo.UserWithdrawCashVO">
        SELECT
            cash_id,
            order_no,
            amount,
            status,
            create_time,
            update_time
        FROM
            user_withdraw_cash
        WHERE
            user_id = #{userId}
        ORDER BY
            update_time DESC
    </select>
</mapper>