<?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.yonge.cooleshow.biz.dal.dao.UserWithdrawalDao">

    <resultMap id="BaseResultMap" type="com.yonge.cooleshow.biz.dal.entity.UserWithdrawal">
        <result column="id_" property="id" />
        <result column="user_id_" property="userId" />
        <result column="amount_" property="amount" />
        <result column="actual_amount_" property="actualAmount" />
        <result column="plantform_fee_" property="plantformFee" />
        <result column="auth_status_" property="authStatus" />
        <result column="auth_user_id_" property="authUserId" />
        <result column="auth_time_" property="authTime" />
        <result column="reason_" property="reason" />
        <result column="bank_card_id_" property="bankCardId" />
        <result column="open_type_" property="openType" />
        <result column="trans_no_" property="transNo" />
        <result column="trade_status_" property="tradeStatus" />
        <result column="trade_no_" property="tradeNo" />
        <result column="callbak_amount_" property="callbakAmount" />
        <result column="transfer_time_" property="transferTime" />
        <result column="error_code_" property="errorCode" />
        <result column="error_msg_" property="errorMsg" />
        <result column="create_time_" property="createTime" />
        <result column="update_time_" property="updateTime" />
    </resultMap>

    <!-- 表字段 -->
    <sql id="baseColumns">
        t.id_ as id
        , t.user_id_ as userId
        , t.amount_ as amount
        , t.actual_amount_ as actualAmount
        , t.plantform_fee_ as plantformFee
        , t.auth_status_ as authStatus
        , t.auth_user_id_ as authUserId
        , t.auth_time_ as authTime
        , t.reason_ as reason
        , t.bank_card_id_ as bankCardId
        , t.open_type_ as openType
        , t.trans_no_ as transNo
        , t.trade_status_ as tradeStatus
        , t.trade_no_ as tradeNo
        , t.callbak_amount_ as callbakAmount
        , t.transfer_time_ as transferTime
        , t.error_code_ as errorCode
        , t.error_msg_ as errorMsg
        , t.create_time_ as createTime
        , t.update_time_ as updateTime
        , t.status_ as status
    </sql>

    <select id="detail" resultType="com.yonge.cooleshow.biz.dal.vo.UserWithdrawalVo">
        SELECT
            <include refid="baseColumns"/>,
            bc.name_ as realName,
            bc.bank_name_ as bankName,
            bc.bank_card_ as bankCard,
            bc.phone_ as phone,
            u.username_  as withdrawaUser
        FROM user_withdrawal t
        LEFT JOIN user_bank_card bc on t.bank_card_id_ = bc.id_ and t.user_id_ = bc.user_id_
        left join sys_user u on t.user_id_ = u.id_
        where t.id_ = #{id}
    </select>

    <select id="detailByTransNo" resultType="com.yonge.cooleshow.biz.dal.vo.UserWithdrawalVo">
        SELECT
            <include refid="baseColumns"/>,
            bc.name_ as realName,
            bc.bank_name_ as bankName,
            bc.bank_card_ as bankCard,
            bc.phone_ as phone,
            u.username_  as withdrawaUser
        FROM user_withdrawal t
        LEFT JOIN user_bank_card bc on t.bank_card_id_ = bc.id_ and t.user_id_ = bc.user_id_
        left join sys_user u on t.user_id_ = u.id_
        where t.trans_no_ = #{transNo}
    </select>

    <sql id="selectSql">
        SELECT
            <include refid="baseColumns" />,
            bc.name_ as realName,
            bc.bank_name_ as bankName,
            bc.bank_card_ as bankCard,
            ifnull(u.real_name_,u.username_)  as withdrawaUser,
            ifnull(au.real_name_,au.username_) as verifyUser
        FROM user_withdrawal t
        LEFT JOIN user_bank_card bc on t.bank_card_id_ = bc.id_ and t.user_id_ = bc.user_id_
        left join sys_user u on t.user_id_ = u.id_
            left join sys_user au on t.auth_user_id_ = au.id_
        <where>
            u.del_flag_ = 0
            <if test="param.userId !=null">
                AND t.user_id_ = #{param.userId}
            </if>
            <if test="null != param.search and '' != param.search">
                AND (
                    t.user_id_ LIKE CONCAT('%', #{param.search}, '%') or
                    u.phone_ LIKE CONCAT('%', #{param.search}, '%') or
                    u.username_ LIKE CONCAT('%', #{param.search}, '%') or
                    u.real_name_ LIKE CONCAT('%', #{param.search}, '%')
                )
            </if>
            <if test="null != param.username and '' != param.username">
                AND (
                    u.username_ LIKE CONCAT('%', #{param.username}, '%') or
                    u.real_name_ LIKE CONCAT('%', #{param.username}, '%')
                )
            </if>
            <if test="null != param.authStatus">
                AND t.auth_status_ = #{param.authStatus}
            </if>
            <if test="null != param.tradeStatus">
                AND t.trade_status_ = #{param.tradeStatus}
            </if>
            <if test="null != param.verifyUser and '' != param.verifyUser">
                AND (
                    au.username_ LIKE CONCAT('%', #{param.verifyUser}, '%') or
                    au.real_name_ LIKE CONCAT('%', #{param.verifyUser}, '%')
                )
            </if>
            <if test="null != param.transNo and '' != param.transNo">
                AND t.trans_no_ LIKE CONCAT('%', #{param.transNo}, '%')
            </if>
            <if test="param.startTime !=null">
                <![CDATA[AND t.create_time_ >= #{param.startTime} ]]>
            </if>
            <if test="param.endTime !=null">
                <![CDATA[AND t.create_time_ < #{param.endTime} ]]>
            </if>
            <if test="param.status != null and param.status == 'DOING'">
                AND (t.auth_status_ = 'DOING' OR t.trade_status_ = 'pending')
            </if>
            <if test="param.status != null and param.status == 'PASS'">
                AND t.trade_status_ = 'succeeded'
            </if>
            <if test="param.status != null and param.status == 'UNPASS'">
                AND t.auth_status_ != 'DOING'
                AND t.trade_status_ is null or t.trade_status_ not in('pending','succeeded')
            </if>
        </where>
        order by t.create_time_ desc
    </sql>

    <select id="selectPage" resultType="com.yonge.cooleshow.biz.dal.vo.UserWithdrawalVo">
        <include refid="selectSql"/>
    </select>

    <select id="selectAllList" resultType="com.yonge.cooleshow.biz.dal.vo.UserWithdrawalVo">
        <include refid="selectSql"/>
    </select>

    <select id="getNowDayWithdrawalCount" resultType="java.lang.Integer">
        select count(1) from user_withdrawal
        where user_id_ = #{userId}
        and to_days(create_time_) = to_days(now())
    </select>

    <select id="totalAmount" resultType="com.yonge.cooleshow.biz.dal.vo.UserWithdrawalVo">
        select sum(actual_amount_) as actual_amount_ from user_withdrawal
        where auth_status_ = 'DOING'
    </select>
</mapper>