<?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.hzs.member.account.mapper.CuMemberWithdrawMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.hzs.common.domain.member.account.CuMemberWithdraw">
        <id column="PK_ID" property="pkId"/>
        <result column="DEL_FLAG" property="delFlag"/>
        <result column="PK_COUNTRY" property="pkCountry"/>
        <result column="PK_CREATOR" property="pkCreator"/>
        <result column="CREATION_TIME" property="creationTime"/>
        <result column="PK_MODIFIED" property="pkModified"/>
        <result column="MODIFIED_TIME" property="modifiedTime"/>
        <result column="PK_MEMBER" property="pkMember"/>
        <result column="WITHDRAW_CODE" property="withdrawCode"/>
        <result column="PK_ACCOUNT" property="pkAccount"/>
        <result column="PK_BANK" property="pkBank"/>
        <result column="PK_RATE" property="pkRate"/>
        <result column="CASH_AMOUNT" property="cashAmount"/>
        <result column="SERVICE_CHARGE" property="serviceCharge"/>
        <result column="INCOME_TAX" property="incomeTax"/>
        <result column="ISSUED_AMOUNT" property="issuedAmount"/>
        <result column="APPROVE_STATE" property="approveState"/>
        <result column="PAYMENT_TIME" property="paymentTime"/>
        <result column="PAYER" property="payer"/>
        <result column="REMARKS" property="remarks"/>
        <result column="APPROVER" property="approver"/>
        <result column="APPROVE_TIME" property="approveTime"/>
        <result column="PK_ROLE" property="pkRole"/>
        <result column="ROLE_NAME" property="roleName"/>
        <result column="STATUS" property="status"/>
        <result column="PAYER_REMARKS" property="payerRemarks"/>
        <result column="APPROVE_REMARKS" property="approveRemarks"/>
        <result column="BATCH" property="batch"/>
    </resultMap>

    <sql id="select_base">
        SELECT r.*,
               m.member_code             memberCode,
               m.member_name             memberName,
               bb.bank_name              bankName,
               b.sub_bank_name           subBankName,
               b.card_number             cardNumber,
               b.account_name            accountName,
               nvl(m.id_card, b.id_card) idCard,
               b.phone,
               b.pk_id                   pkBdBank,
               u.user_name               paymentName,
               su.user_name              approveName,
               ver.VERTEX_NAME           vertexName
        FROM cu_member_withdraw r
                 LEFT JOIN cu_member_bank b
                           ON r.pk_Bank = b.pk_id
                               AND b.default_card = 1
                               AND b.del_flag = 0
                 LEFT JOIN bd_bank bb
                           ON b.pk_bank = bb.pk_id
                               AND bb.del_flag = 0
                 LEFT JOIN cu_member m
                           ON r.pk_member = m.pk_id
                               AND m.del_flag = 0
                 LEFT JOIN bd_vertex ver
                           ON ver.pk_id = m.PK_VERTEX
                               AND ver.del_flag = 0
                 LEFT JOIN sys_user u
                           ON r.payer = u.user_id
                 LEFT JOIN sys_user su
                           ON r.approver = su.user_id
    </sql>


    <sql id="list_where">
        <where>
            r.del_flag='0'
            and r.status != 3
            <if test="memberName != null and memberName != ''">
                and m.member_name like '%' || #{memberName} || '%'
            </if>
            <if test="memberCodeList == null and  memberCode != null and memberCode != ''">
                and m.member_code like '%'|| #{memberCode} || '%'
            </if>
            <if test="memberCodeList != null and memberCodeList.size >0">
                and m.member_code in
                <foreach collection="memberCodeList" item="item" open="(" close=")" separator=",">
                    #{item}
                </foreach>
            </if>
            <if test="approveState != null">
                and r.APPROVE_STATE = #{approveState}
            </if>
            <if test="pkAccount != null">
                and r.PK_ACCOUNT = #{pkAccount}
            </if>
            <if test="pkCountry != null">
                and r.pk_country = #{pkCountry}
            </if>
            <if test="bankName != null and bankName != ''">
                and bb.bank_name like #{bankName} || '%'
            </if>
            <if test="cashAmount != null">
                and r.CASH_AMOUNT = #{cashAmount}
            </if>
            <if test="subBankName != null and subBankName != ''">
                and b.sub_bank_name like #{subBankName} || '%'
            </if>
            <if test="cardNumber != null and cardNumber != ''">
                and b.card_number like #{cardNumber} || '%'
            </if>
            <if test="accountName != null and accountName != ''">
                and b.account_name like #{accountName} || '%'
            </if>
            <if test="phone != null and phone != ''">
                and b.phone like #{phone} || '%'
            </if>
            <if test="remarks != null and remarks != ''">
                and r.remarks like #{remarks} || '%'
            </if>
            <if test="issuedAmount != null">
                and r.ISSUED_AMOUNT = #{issuedAmount}
            </if>
            <if test="approveName != null and approveName != ''">
                and su.user_name like #{approveName} || '%'
            </if>
            <if test="approveRemarks != null and approveRemarks != ''">
                and r.APPROVE_REMARKS like #{approveRemarks} || '%'
            </if>
            <if test="paymentName != null and paymentName != ''">
                and u.user_name like #{paymentName} || '%'
            </if>
            <if test="payerRemarks != null and payerRemarks != ''">
                and r.PAYER_REMARKS like #{payerRemarks} || '%'
            </if>
            <if test="withdrawCode != null and withdrawCode != ''">
                and r.WITHDRAW_CODE = #{withdrawCode}
            </if>
            <if test="status != null">
                and r.status = #{status}
            </if>
            <if test="vertexId != null">
                and m.PK_VERTEX=#{vertexId}
            </if>
            <if test="startDate != null and startDate != ''">
                and r.CREATION_TIME >= to_date(#{startDate}, 'yyyy-mm-dd')
            </if>
            <if test="endDate != null and endDate != ''">
                and r.CREATION_TIME &lt;= to_date(#{endDate} || ' 23:59:59', 'yyyy-mm-dd hh24:mi:ss')
            </if>
            <if test="startPaymentTime != null and startPaymentTime != ''">
                and r.APPROVE_TIME &gt;= to_date(#{startPaymentTime},'yyyy-mm-dd')
            </if>
            <if test="endPaymentTime != null and endPaymentTime != ''">
                and r.APPROVE_TIME &lt; to_date(#{endPaymentTime} || ' 23:59:59', 'yyyy-mm-dd hh24:mi:ss')
            </if>
        </where>
    </sql>


    <sql id="ids_where">
        <where>
            r.del_flag='0'
            and r.pk_id in
            <foreach collection="pkIds" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </where>
    </sql>

    <select id="getIncomeTaxByCycle" resultMap="BaseResultMap">
        select sum(w.cash_amount) cash_amount, sum(w.income_tax) income_tax
        from cu_member_withdraw w
        where w.approve_state = 5
          and w.creation_time >= trunc(sysdate, 'month')
          and w.creation_time &lt; TRUNC(ADD_MONTHS(SYSDATE, #{cycle}), 'month')
          and w.pk_member = #{pkMember}
    </select>

    <select id="getWithdrawTimes" resultMap="BaseResultMap">
        SELECT *
        FROM CU_MEMBER_WITHDRAW
        WHERE DEL_FLAG = 0
          AND PK_MEMBER = #{pkMember}
          AND (APPROVE_STATE = 1 OR APPROVE_STATE = 2 OR APPROVE_STATE = 3)
          AND STATUS = 0
    </select>

    <update id="updateWithdrawByCode" parameterType="com.hzs.common.domain.member.account.CuMemberWithdraw">
        update cu_member_withdraw
        <set>
            <if test="approveState != null">approve_state = #{approveState},</if>
            <if test="approver != null">approver = #{approver},</if>
            <if test="approveTime != null">approve_time = #{approveTime},</if>
            <if test="pkModified != null">pk_modified = #{pkModified},</if>
            <if test="pkRole != null">pk_role = #{pkRole},</if>
            <if test="roleName != null">role_name = #{roleName},</if>
            modified_time = sysdate
        </set>
        where BATCH = #{withdrawCode}
    </update>


    <select id="selectMemberWithdrawList" resultType="com.hzs.common.domain.member.account.CuMemberWithdrawExt">
        <include refid="select_base"/>
        <choose>
            <when test="pkIds != null and pkIds.size >0">
                <include refid="ids_where"></include>
            </when>
            <otherwise>
                <include refid="list_where"></include>
            </otherwise>
        </choose>
        order by r.creation_time Desc,r.payment_time Desc
    </select>


    <select id="selectMemberWithdrawListByPkMember"
            resultType="com.hzs.common.domain.member.account.CuMemberWithdrawExt">
        select r.*,
               bb.bank_name    bankName,
               b.sub_bank_name subBankName,
               b.card_number   cardNumber,
               b.account_name  accountName,
               b.id_card       idCard,
               b.pk_id         pkBdBank
        from cu_member_withdraw r
                 left join cu_member_bank b
                           on r.pk_Bank = b.pk_id
                               and b.default_card = 1
                               and b.del_flag = 0
                 left join bd_bank bb
                           on b.pk_bank = bb.pk_id
                               and bb.del_flag = 0
        where r.del_flag = 0
          and r.pk_country = #{pkCountry}
          and r.pk_member = #{pkMember}
        order by r.creation_time desc
    </select>

    <select id="selectMemberWithdrawListTotal" resultType="com.hzs.common.domain.member.account.CuMemberWithdrawExt">
        select sum(r.cash_amount) cashAmount,
        sum(r.income_tax) incomeTax,
        sum(r.service_charge) serviceCharge,
        sum(r.issued_amount) issuedAmount,
        round(sum(r.cash_amount) / c.in_exchange_rate, 4) settleCashAmount,
        round(sum(r.income_tax) / c.in_exchange_rate, 4) settleIncomeTax,
        round(sum(r.service_charge) / c.in_exchange_rate, 4) settleServiceCharge,
        round(sum(r.issued_amount) / c.in_exchange_rate, 4) settleIssuedAmount
        from cu_member_withdraw r
        left join bd_currency c
        on c.pk_id = r.pk_rate
        and c.enable_state = 0
        left join cu_member_bank b
        on r.pk_Bank = b.pk_id
        and b.default_card = 1
        and b.del_flag = 0
        left join bd_bank bb
        on b.pk_bank = bb.pk_id
        and bb.del_flag = 0
        left join cu_member m
        on r.pk_member = m.pk_id
        and m.del_flag = 0
        LEFT JOIN bd_vertex ver
        ON ver.pk_id = m.PK_VERTEX
        AND ver.del_flag = 0
        left join sys_user u
        on r.payer = u.user_id
        left join sys_user su
        on r.approver = su.user_id
        <include refid="list_where"></include>
        group by c.in_exchange_rate
    </select>

    <select id="queryCuMemberWithdrawDetail" resultType="com.hzs.common.domain.member.account.CuMemberWithdrawExt">
        <include refid="select_base"/>
        where r.pk_id = #{pkId}
    </select>

    <update id="withdrawPaymentByPkId">
        update cu_member_withdraw
        <set>
            <if test="paymentTime != null">PAYMENT_TIME = #{paymentTime},</if>
            <if test="payer != null">PAYER = #{payer},</if>
            <if test="status != null">STATUS = #{status},</if>
            <if test="payerRemarks != null">PAYER_REMARKS = #{payerRemarks},</if>

            <if test="approveState != null">approve_state = #{approveState},</if>
            <if test="batch != null and batch != ''">
                batch = #{batch},
            </if>
            <if test="pkModified != null and pkModified != ''">
                pk_modified = #{pkModified},
            </if>
            modified_time = sysdate
        </set>
        where pk_id in
        <foreach collection="pkIds" item="item" open="(" close=")" separator=",">
            #{item}
        </foreach>
    </update>


    <select id="getWithdrawApproveStateByIds" resultMap="BaseResultMap">
        select *
        from cu_member_withdraw w
        where w.approve_state != #{approveState}
        and w.pk_id in
        <foreach collection="pkIds" item="item" open="(" close=")" separator=",">
            #{item}
        </foreach>
    </select>

    <select id="showMemberWithdrawPetition" resultType="com.hzs.common.domain.member.account.CuMemberWithdrawExt">
        select t.pk_member pkMember,
        m.member_code memberCode,
        m.member_name memberName,
        t.cash_amount,
        t.service_charge,
        t.income_tax,
        t.issued_amount,
        t.withdraw_code,
        t.pk_country
        from cu_member_withdraw t
        left join cu_member m
        on t.pk_member = m.pk_id
        where t.del_flag = 0
        and m.del_flag = 0
        <if test="approveState != null">
            and t.approve_state != #{approveState}
        </if>
        and t.pk_id in
        <foreach collection="pkIds" item="item" open="(" close=")" separator=",">
            #{item}
        </foreach>
    </select>


    <update id="updateWithdrawStatusByPkId">
        update cu_member_withdraw w
        set w.status      = 3,
            pk_modified   = #{pkModified},
            modified_time = sysdate
        where w.pk_id = #{pkId}
          and w.approve_state = 1
    </update>


    <select id="selectWithdrawMember" resultType="long">
        SELECT cmw.pk_id
        FROM cu_member_withdraw cmw
        WHERE cmw.del_flag = 0
          AND cmw.status != 3
          AND cmw.pk_country = #{pkCountry}
          AND cmw.pk_member = #{pkMember}
          AND cmw.approve_state IN (1, 2, 3)
    </select>

</mapper>
