<?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.bonus.account.mapper.CuMemberAccountMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="AccountExResultMap" type="com.hzs.common.domain.member.ext.CuMemberAccountExt">
        <id column="PK_ID" property="pkId"/>
        <result column="PK_MEMBER" property="pkMember"/>
        <result column="ACCOUNT1" property="account1"/>
        <result column="LOCK_ACCOUNT1" property="lockAccount1"/>
        <result column="LOCK_TRANSFER_ACCOUNT1" property="lockTransferAccount1"/>
        <result column="ACCOUNT1_REMARKS" property="account1Remarks"/>
        <result column="ACCOUNT2" property="account2"/>
        <result column="LOCK_ACCOUNT2" property="lockAccount2"/>
        <result column="LOCK_TRANSFER_ACCOUNT2" property="lockTransferAccount2"/>
        <result column="ACCOUNT2_REMARKS" property="account2Remarks"/>
        <result column="ACCOUNT3" property="account3"/>
        <result column="LOCK_ACCOUNT3" property="lockAccount3"/>
        <result column="LOCK_TRANSFER_ACCOUNT3" property="lockTransferAccount3"/>
        <result column="ACCOUNT3_REMARKS" property="account3Remarks"/>
        <result column="ACCOUNT4" property="account4"/>
        <result column="LOCK_ACCOUNT4" property="lockAccount4"/>
        <result column="LOCK_TRANSFER_ACCOUNT4" property="lockTransferAccount4"/>
        <result column="LOCK_WITHDRAW_ACCOUNT4" property="lockWithdrawAccount4"/>
        <result column="WITHDRAW_ACCOUNT4" property="withdrawAccount4"/>
        <result column="CONSUME_ACCOUNT4" property="consumeAccount4"/>
        <result column="ACCOUNT4_REMARKS" property="account4Remarks"/>
        <result column="ACCOUNT5" property="account5"/>
        <result column="LOCK_ACCOUNT5" property="lockAccount5"/>
        <result column="LOCK_TRANSFER_ACCOUNT5" property="lockTransferAccount5"/>
        <result column="ACCOUNT5_REMARKS" property="account5Remarks"/>
        <result column="ACCOUNT6" property="account6"/>
        <result column="LOCK_ACCOUNT6" property="lockAccount6"/>
        <result column="LOCK_TRANSFER_ACCOUNT6" property="lockTransferAccount6"/>
        <result column="ACCOUNT6_REMARKS" property="account6Remarks"/>
        <result column="ACCOUNT7" property="account7"/>
        <result column="LOCK_ACCOUNT7" property="lockAccount7"/>
        <result column="LOCK_TRANSFER_ACCOUNT7" property="lockTransferAccount7"/>
        <result column="ACCOUNT7_REMARKS" property="account7Remarks"/>
        <result column="ACCOUNT8" property="account8"/>
        <result column="LOCK_ACCOUNT8" property="lockAccount8"/>
        <result column="LOCK_TRANSFER_ACCOUNT8" property="lockTransferAccount8"/>
        <result column="ACCOUNT8_REMARKS" property="account8Remarks"/>
        <result column="ACCOUNT9" property="account9"/>
        <result column="LOCK_ACCOUNT9" property="lockAccount9"/>
        <result column="LOCK_TRANSFER_ACCOUNT9" property="lockTransferAccount9"/>
        <result column="ACCOUNT9_REMARKS" property="account9Remarks"/>
        <result column="ACCOUNT10" property="account10"/>
        <result column="LOCK_ACCOUNT10" property="lockAccount10"/>
        <result column="LOCK_TRANSFER_ACCOUNT10" property="lockTransferAccount10"/>
        <result column="ACCOUNT10_REMARKS" property="account10Remarks"/>
        <result column="ACCOUNT11" property="account11"/>
        <result column="ACCOUNT12" property="account12"/>
        <result column="ACCOUNT13" property="account13"/>
        <result column="LOCK_ACCOUNT13" property="lockAccount13"/>
        <result column="ACCOUNT14" property="account14"/>
        <result column="ACCOUNT15" property="account15"/>
        <result column="ACCOUNT16" property="account16"/>
        <result column="ACCOUNT17" property="account17"/>
        <result column="ACCOUNT18" property="account18"/>
        <result column="ACCOUNT19" property="account19"/>
        <result column="ACCOUNT20" property="account20"/>
        <result column="TRANSFER_STATUS" property="transferStatus"/>
        <result column="IS_TRANSFER" property="isTransfer"/>
        <result column="IS_WITHDRAW" property="isWithdraw"/>
        <result column="IS_CONSUME" property="isConsume"/>
        <result column="PAY_PWD" property="payPwd"/>
        <result column="ACCOUNT_STATUS" property="accountStatus"/>
        <result column="PK_COUNTRY" property="pkCountry"/>
    </resultMap>

    <update id="updateMemberAccountGrantByPeriod">
        merge into cu_member_account a
            using (select pk_member,
                          sum(real_income_total * bc.out_exchange_rate)     account4,
                          sum(back_points * bc.out_exchange_rate)           account5,
                          sum(repur_coupon_subtotal * bc.out_exchange_rate) account8,
                          sum(global_points * bc.out_exchange_rate)         account9,
                          sum(car_award_points * bc.out_exchange_rate)      account10
                   from cu_member_bonus cb
                            inner join bd_currency bc
                                       on cb.pk_country = bc.pk_country
                   where cb.del_flag = 0
                     and cb.pk_country = #{pkCountry}
                     and period between #{startPeriod} and #{endPeriod}
                     and cb.grant_status = 2
                   group by pk_member
            ) b
            on (a.pk_member = b.pk_member)
            when matched then
                update set a.account4 = a.account4 + b.account4,
                    a.consume_account4 = a.consume_account4 + b.account4,
                    a.account5 = a.account5 + b.account5,
                    a.account8 = a.account8 + b.account8
    </update>

    <update id="updateMemberAccountWithdrawalByPeriod">
        merge into cu_member_account a
            using (
                select a.pk_member, (case when CONSUME_ACCOUNT4 >= a.consume_money then a.consume_money else CONSUME_ACCOUNT4 end) consume_money
                from (select ct.pk_member, sum(cd.consume_money) consume_money
                      from cu_member_trade ct
                               inner join cu_member_trade_detail cd
                                          on cd.pk_member_trade = ct.pk_id
                      where cd.del_flag = 0
                        and ct.del_flag = 0
                        and cd.trade_statue = 1
                        and cd.period between #{startPeriod} and #{endPeriod}
                        and ct.pk_country = #{pkCountry}
                      group by ct.pk_member) a
                         inner join cu_member_account ca
                                    on a.pk_member = ca.pk_member
                where ca.CONSUME_ACCOUNT4 > 0
            ) b
            on (a.pk_member = b.pk_member)
            when matched then
                update set a.withdraw_account4 = a.withdraw_account4 + b.consume_money,
                    a.consume_account4 = a.consume_account4 - b.consume_money
    </update>

</mapper>
