<?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.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="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>
    <select id="queryMemberAccountByCondition" resultMap="AccountExResultMap">
        select cm.pay_pwd,
        cm.account_status,
        ca.pk_member,
        ca.account1,
        ca.lock_account1,
        ca.account2,
        ca.lock_account2,
        ca.lock_account3,
        ca.account3,
        ca.lock_account4,
        ca.account4,
        ca.consume_account4,
        ca.withdraw_account4,
        ca.lock_account4,
        ca.account5,
        ca.lock_account5,
        ca.account6,
        ca.lock_account6,
        ca.account7,
        ca.lock_account7,
        ca.account8,
        ca.lock_account8,
        ca.account9,
        ca.account10,
        ca.account11,
        ca.account12,
        ca.account13,
        ca.account14,
        ca.account15,
        ca.is_transfer,
        ca.is_withdraw,
        ca.is_consume,
        cm.pk_settle_country pk_country
        from cu_member cm
        inner join cu_member_account ca
        on cm.pk_id = ca.pk_member
        where cm.del_flag=0 and ca.del_flag=0
        and cm.category =0
        and cm.pk_settle_country=#{pkCountry}
        <if test="pkMember != null and pkMember != ''">
            and ca.pk_member = #{pkMember}
        </if>
    </select>


    <!-- 根据会员编码获取会员账户余额 -->
    <select id="selectBalanceByMemberCode" resultType="decimal">
        SELECT CASE
                   WHEN 1 = #{accountType} then account1
                   WHEN 2 = #{accountType} then account2
                   WHEN 3 = #{accountType} then account3
                   WHEN 4 = #{accountType} then account4
                   WHEN 5 = #{accountType} then account5
                   WHEN 6 = #{accountType} then account6
                   WHEN 7 = #{accountType} then account7
                   WHEN 8 = #{accountType} then account8
                   WHEN 9 = #{accountType} then account9
                   WHEN 10 = #{accountType} then account10
                   WHEN 11 = #{accountType} then account11
                   WHEN 12 = #{accountType} then account12
                   WHEN 13 = #{accountType} then account13
                   WHEN 14 = #{accountType} then account14
                   WHEN 15 = #{accountType} then account15
                   WHEN 16 = #{accountType} then account16
                   WHEN 17 = #{accountType} then account17
                   WHEN 18 = #{accountType} then account18
                   WHEN 19 = #{accountType} then account19
                   WHEN 20 = #{accountType} then account20
                   END balance
        FROM CU_MEMBER_ACCOUNT
        WHERE pk_member = #{pkMember}
    </select>


        <update id="updateMemberAccount">
        UPDATE CU_MEMBER_ACCOUNT
        SET
        <if test="account1 != null">
            account1 = account1 + #{account1},
        </if>
        <if test="account2 != null">
            account2 = account2 + #{account2},
        </if>
        <if test="account3 != null">
            account3 = account3 + #{account3},
        </if>
        <if test="account4 != null">
            account4 = account4 + #{account4},
        </if>
        <if test="account5 != null">
            account5 = account5 + #{account5},
        </if>
        <if test="account6 != null">
            account6 = account6 + #{account6},
        </if>
        <if test="account7 != null">
            account7 = account7 + #{account7},
        </if>
        <if test="account8 != null">
            account8 = account8 + #{account8},
        </if>
        <if test="account9 != null">
            account9 = account9 + #{account9},
        </if>
        <if test="account10 != null">
            account10 = account10 + #{account10},
        </if>
        <if test="account11 != null">
            account11 = account11 + #{account11},
        </if>
        <if test="account12 != null">
            account12 = account12 + #{account12},
        </if>
        <if test="account13 != null">
            account13 = account13 + #{account13},
        </if>
        <if test="account14 != null">
            account14 = account14 + #{account14},
        </if>
        <if test="account15 != null">
            account15 = account15 + #{account15},
        </if>
        <if test="account16 != null">
            account16 = account16 + #{account16},
        </if>
        <if test="account17 != null">
            account17 = account17 + #{account17},
        </if>
        <if test="account18 != null">
            account18 = account18 + #{account18},
        </if>
        <if test="account19 != null">
            account19 = account19 + #{account19},
        </if>
        <if test="account20 != null">
            account20 = account20 + #{account20},
        </if>
        <if test="lockAccount1 != null">
            lock_account1 = #{lockAccount1},
        </if>
        <if test="lockAccount2 != null">
            lock_account2 = #{lockAccount2},
        </if>
        <if test="lockAccount4 != null">
            lock_account4 = #{lockAccount4},
        </if>
        <if test="withdrawAccount4 != null">
            withdraw_account4 = withdraw_account4 + #{withdrawAccount4},
        </if>
        <if test="consumeAccount4 != null">
            consume_account4 = consume_account4 + #{consumeAccount4},
        </if>
        pk_modified = #{pkModified},
        modified_time = sysdate
        WHERE pk_member = #{pkMember} and pk_country = #{pkCountry} and del_flag=0
    </update>

    <select id="queryMemberAccountByPkCountry" resultType="com.hzs.common.domain.member.ext.CuMemberAccountExt">
        select a.*, b.pk_id pkBank
        from cu_member_account a,
             cu_member_bank b
        where a.pk_member = b.pk_member
          and a.del_flag = 0
          and b.del_flag = 0
          and b.default_card = 1
          and a.is_withdraw = 0
          and a.pk_country = #{pkCountry}
    </select>


    <select id="queryMemberAccountByMemberCode" resultType="com.hzs.member.account.dto.CuMemberRechargeAccountParam">
        select pk_id,
        pk_member,
        account_name,
        decode(account_name,
        'ACCOUNT4',
        WITHDRAW_ACCOUNT4 - lock_withdraw_account4,
        0) withdrawBalance,
        case
        when account_name = 'ACCOUNT1' then
        balance - lock_account1 - lock_transfer_account1
        when account_name = 'ACCOUNT2' then
        balance - lock_account2 - lock_transfer_account2
        when account_name = 'ACCOUNT3' then
        balance - lock_account3 - lock_transfer_account3
        when account_name = 'ACCOUNT4' then
        balance - lock_account4 - lock_transfer_account4 -
        lock_withdraw_account4
        when account_name = 'ACCOUNT5' then
        balance - lock_account5 - lock_transfer_account5
        when account_name = 'ACCOUNT6' then
        balance - lock_account6 - lock_transfer_account6
        when account_name = 'ACCOUNT7' then
        balance - lock_account7 - lock_transfer_account7
        when account_name = 'ACCOUNT8' then
        balance - lock_account8 - lock_transfer_account8
        else
        balance
        end availableBalance,
        replace(account_name, 'ACCOUNT', '') accountType,
        balance
        from CU_MEMBER_ACCOUNT unpivot(balance for account_name in(account1,
        account2,
        account3,
        account4,
        account5,
        account6,
        account7,
        account8,
        account9,
        account10,
        account11,
        account12,
        account13,
        account14,
        account15
        ))
        where del_flag = 0
        <if test="memberCode != null and memberCode != ''">
            and pk_member = (
            select pk_id from cu_member m where m.member_code=#{memberCode} and m.del_flag=0)
        </if>
        <if test="pkMember != null">
            and pk_member = #{pkMember}
        </if>
        and replace(account_name, 'ACCOUNT', '') in
        <foreach collection="accountTypes" item="item" open="(" close=")" separator=",">
            #{item}
        </foreach>
    </select>


    <update id="updateBatchMemberAccountByPkmember">
        MERGE INTO cu_member_account m
            USING (select *
                   from (select a.field_value,
                                r.recharge_amount,
                                pk_member,
                                r.recharge_method
                         from cu_member_recharge r
                                  left join bd_account a
                                            on r.pk_account = a.pk_id
                         where r.del_flag = 0
                           and r.batch = #{batch}) tt pivot(sum(recharge_amount)
        for field_value in('1' as account1,
        '2' as account2,
        '3' as account3,
        '4' as account4,
        '5' as account5,
        '6' as account6,
        '7' as account7,
        '8' as account8,
        '9' as account9,
        '10' as account10,
        '11' as account11,
        '12' as account12,
        '13' as account13,
        '14' as account14,
        '15' as account15
        ))) t
            ON (m.pk_member = t.pk_member)
            WHEN MATCHED THEN
                update
                    set m.account1 = NVL(t.account1, 0) + m.account1,
                        m.account2 = NVL(t.account2, 0) + m.account2,
                        m.account3 = NVL(t.account3, 0) + m.account3,
                        m.account4 = NVL(t.account4, 0) + m.account4,
                        m.WITHDRAW_ACCOUNT4 = case
                                                  when NVL(t.account4, 0) &lt; 0 and m.WITHDRAW_ACCOUNT4 + NVL(t.account4, 0) >= 0 then
                                                      NVL(t.account4, 0) + m.WITHDRAW_ACCOUNT4
                                                  when NVL(t.account4, 0) &lt; 0 and m.WITHDRAW_ACCOUNT4 + NVL(t.account4, 0) &lt; 0 then
                                                      0
                                                  else
                                                      m.WITHDRAW_ACCOUNT4
                            end,
                        m.CONSUME_ACCOUNT4 = case
                                                 when NVL(t.account4, 0) &lt; 0 and m.WITHDRAW_ACCOUNT4 + NVL(t.account4, 0) &lt; 0 then
                                                     m.WITHDRAW_ACCOUNT4 + t.account4 + m.CONSUME_ACCOUNT4
                                                 else
                                                     NVL(t.account4, 0) + m.CONSUME_ACCOUNT4
                            end,
                        m.account5 = NVL(t.account5, 0) + m.account5,
                        m.account6 = NVL(t.account6, 0) + m.account6,
                        m.account7 = NVL(t.account7, 0) + m.account7,
                        m.account8 = NVL(t.account8, 0) + m.account8,
                        m.account9 = NVL(t.account9, 0) + m.account9,
                        m.account10 = NVL(t.account10, 0) + m.account10,
                        m.account11 = NVL(t.account11, 0) + m.account11,
                        m.account12 = NVL(t.account12, 0) + m.account12,
                        m.account13 = NVL(t.account13, 0) + m.account13,
                        m.account14 = NVL(t.account14, 0) + m.account14,
                        m.account15 = NVL(t.account15, 0) + m.account15,
                        m.MODIFIED_TIME = sysdate,
                        m.PK_MODIFIED = #{pkModified}
    </update>

    <select id="queryMemberAccountByMemberCodes" resultType="com.hzs.common.domain.member.ext.CuMemberAccountExt">
        select m.member_code memberCode,
        m.member_name memberName,
        a.account1,
        a.account2,
        a.account3,
        a.account4,
        a.account5,
        a.account6,
        a.account7,
        a.account8,
        a.account9,
        a.account10,
        a.account11,
        a.account12,
        a.account13,
        a.account14,
        a.account15
        from cu_member_account a, cu_member m
        where a.pk_member = m.pk_id
        and a.del_flag = 0
        and m.del_flag = 0
        and m.member_code in
        <if test="memberCodes != null and memberCodes.size >0">
            <foreach collection="memberCodes" item="item" open="(" close=")" separator=",">
                #{item.memberCode}
            </foreach>
        </if>
        <if test="memberCodeStrs != null and memberCodeStrs.size >0">
            <foreach collection="memberCodeStrs" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
    </select>


    <select id="queryMemberAccountAllByMemberCodes" resultType="com.hzs.common.domain.member.ext.CuMemberAccountExt">
        select m.member_code memberCode,
        m.member_name memberName,
        a.*
        from cu_member_account a, cu_member m
        where a.pk_member = m.pk_id
        and a.del_flag = 0
        and m.del_flag = 0
        <if test="memberCodes != null and memberCodes.size >0">
            and m.member_code in
            <foreach collection="memberCodes" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
    </select>

    <sql id="select_base">
        with t1 as
         (select u.member_code,
                 u.member_name,
                 u.pk_id,
                 u.pk_vertex       vertex_id,
                 u.pk_team_code    team_id,
                 v.vertex_name,
                 v.pk_member,
                 u.pk_place_parent,
                 u.pk_parent,
                 t.team_name
            from cu_member u
            left join bd_vertex v
              on u.pk_vertex = v.pk_id
             and v.del_flag = 0
            left join cu_member_team t
              on u.pk_team_code = t.pk_id
             and t.del_flag = 0
           where u.del_flag = 0
             and u.pk_settle_country = #{pkCountry})
        select ss.memberCode,
               ss.memberName,
               ss.pk_id pkId,
               ss.vertexId,
               ss.teamId,
               ss.vertexName,
               ss.pk_member,
               ss.pk_place_parent,
               ss.pk_parent,
               ss.teamName,
               ss.slevel,
               pp.plevel,
               account1,
               lock_account1,
               lock_transfer_account1,
               account2,
               lock_account2,
               lock_transfer_account2,
               account3,
               lock_account3,
               lock_transfer_account3,
               account4,
               lock_account4,
               lock_transfer_account4,
               lock_withdraw_account4,
               withdraw_account4,
               consume_account4,
               account5,
               lock_account5,
               lock_transfer_account5,
               account6,
               lock_account6,
               lock_transfer_account6,
               account7,
               lock_account7,
               lock_transfer_account7,
               account8,
               lock_account8,
               lock_transfer_account8,
               account9,
               lock_account9,
               lock_transfer_account9,
               account10,
               lock_account10,
               account11,
               lock_transfer_account10,
               account12,
               account13,
               account14,
               account15
          from (select member_code     memberCode,
                       member_name     memberName,
                       pk_id,
                       vertex_id       vertexId,
                       team_id         teamId,
                       vertex_name     vertexName,
                       pk_member,
                       pk_place_parent,
                       pk_parent,
                       team_name       teamName,
                       level           slevel
                  from t1
                  <include refid="list_place_where"></include>
                 start with pk_id =
                <if test="memberCode != null and memberCode != ''">
                    (select cm.pk_id
                    from cu_member cm
                    where cm.member_code = #{memberCode})
                </if>
                <if test="memberCode == null or memberCode == ''">
                    pk_member
                </if>
                connect by prior pk_id = pk_place_parent) ss,
               (select pk_id, level plevel
                  from t1
                  <include refid="list_rem_where"></include>
                 start with pk_id =
                    <if test="memberCode != null and memberCode != ''">
                        (select cm.pk_id
                        from cu_member cm
                        where cm.member_code = #{memberCode})
                    </if>
                    <if test="memberCode == null or memberCode == ''">
                        pk_member
                    </if>
                connect by prior pk_id = pk_parent) pp,
               cu_member_account a
    </sql>

    <!--  安置条件  -->
    <sql id="list_place_where">
        <where>
            1=1
            <if test="memberName != null and memberName != ''">
                and member_name like #{memberName} || '%'
            </if>
            <if test="vertexName != null and vertexName != ''">
                and vertex_name like #{memberName} || '%'
            </if>
            <if test="teamName != null and teamName != ''">
                and team_name like #{teamName} || '%'
            </if>
            <if test="teamId != null">
                and team_id = #{teamId}
            </if>
            <if test="vertexId != null">
                and vertex_id = #{vertexId}
            </if>
            <if test="slevel != null">
                and level &lt;= #{slevel}
            </if>
        </where>
    </sql>

    <!--  推荐条件  -->
    <sql id="list_rem_where">
        <where>
            1=1
            <if test="memberName != null and memberName != ''">
                and member_name like #{memberName} || '%'
            </if>
            <if test="vertexName != null and vertexName != ''">
                and vertex_name like #{memberName} || '%'
            </if>
            <if test="teamName != null and teamName != ''">
                and team_name like #{teamName} || '%'
            </if>
            <if test="teamId != null">
                and team_id = #{teamId}
            </if>
            <if test="vertexId != null">
                and vertex_id = #{vertexId}
            </if>

            <if test="plevel != null">
                and level &lt;= #{plevel}
            </if>
        </where>
    </sql>

    <sql id="list_where">
        <where>
            ss.pk_id = pp.pk_id
            and ss.pk_id = a.pk_member
            and a.del_flag = 0
            and a.pk_country = #{pkCountry}
        </where>
    </sql>


    <sql id="ids_where">
        <where>
            ss.pk_id = pp.pk_id
            and ss.pk_id = a.pk_member
            and a.del_flag = 0
            and a.pk_country = #{pkCountry}
            and ss.pk_id in
            <foreach collection="pkIds" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </where>
    </sql>


    <select id="selectMemberAccountList" resultType="com.hzs.common.domain.member.ext.CuMemberAccountExt">
        <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 a.creation_time Desc,a.PK_ID Desc
    </select>


    <select id="selectPkMembersByMemberCode" resultType="java.lang.Long">
        select a.pk_member
        from cu_member_account a, cu_member c
        where a.pk_member = c.pk_id
        and c.del_flag = 0
        and c.account_status = 0
        and a.del_flag = 0
        and c.pk_settle_country= #{pkCountry}
        and c.member_code in
        <foreach collection="memberCodes" item="item" open="(" close=")" separator=",">
            #{item}
        </foreach>
    </select>


    <update id="updateFrozenAmountInfo">
        update cu_member_account a
        set
        <if test="lockAccount1 != null">
            a.lock_account1 = #{lockAccount1},
        </if>
        <if test="lockTransferAccount1 != null">
            a.lock_transfer_account1 = #{lockTransferAccount1},
        </if>
        <if test="account1Remarks != null and account1Remarks != ''">
            a.account1_remarks = #{account1Remarks},
        </if>

        <if test="lockAccount2 != null">
            a.lock_account2 = #{lockAccount2},
        </if>
        <if test="lockTransferAccount2 != null">
            a.lock_transfer_account2 = #{lockTransferAccount2},
        </if>
        <if test="account2Remarks != null and account2Remarks != ''">
            a.account2_remarks = #{account2Remarks},
        </if>

        <if test="lockAccount3 != null">
            a.lock_account3 = #{lockAccount3},
        </if>
        <if test="lockTransferAccount3 != null">
            a.lock_transfer_account3 = #{lockTransferAccount3},
        </if>
        <if test="account3Remarks != null and account3Remarks != ''">
            a.account3_remarks = #{account3Remarks},
        </if>

        <if test="lockAccount4 != null">
            a.lock_account4 = #{lockAccount4},
        </if>
        <if test="lockTransferAccount4 != null">
            a.lock_transfer_account4 = #{lockTransferAccount4},
        </if>
        <if test="lockWithdrawAccount4 != null">
            a.lock_withdraw_account4 = #{lockWithdrawAccount4},
        </if>
        <if test="account4Remarks != null and account4Remarks != ''">
            a.account4_remarks = #{account4Remarks},
        </if>

        <if test="lockAccount5 != null">
            a.lock_account5 = #{lockAccount5},
        </if>
        <if test="lockTransferAccount5 != null">
            a.lock_transfer_account5 = #{lockTransferAccount5},
        </if>
        <if test="account5Remarks != null and account5Remarks != ''">
            a.account5_remarks = #{account5Remarks},
        </if>

        <if test="lockAccount6 != null">
            a.lock_account6 = #{lockAccount6},
        </if>
        <if test="lockTransferAccount6 != null">
            a.lock_transfer_account6 = #{lockTransferAccount6},
        </if>
        <if test="account6Remarks != null and account6Remarks != ''">
            a.account6_remarks = #{account6Remarks},
        </if>

        <if test="lockAccount7 != null">
            a.lock_account7 = #{lockAccount7},
        </if>
        <if test="lockTransferAccount7 != null">
            a.lock_transfer_account7 = #{lockTransferAccount7},
        </if>
        <if test="account7Remarks != null and account7Remarks != ''">
            a.account7_remarks = #{account7Remarks},
        </if>

        <if test="lockAccount8 != null">
            a.lock_account8 = #{lockAccount8},
        </if>
        <if test="lockTransferAccount8 != null">
            a.lock_transfer_account8 = #{lockTransferAccount8},
        </if>
        <if test="account8Remarks != null and account8Remarks != ''">
            a.account8_remarks = #{account8Remarks},
        </if>

        <if test="lockAccount9 != null">
            a.lock_account9 = #{lockAccount9},
        </if>
        <if test="lockTransferAccount9 != null">
            a.lock_transfer_account9 = #{lockTransferAccount9},
        </if>
        <if test="account9Remarks != null and account9Remarks != ''">
            a.account9_remarks = #{account9Remarks},
        </if>

        <if test="lockAccount10 != null">
            a.lock_account10 = #{lockAccount10},
        </if>
        <if test="lockTransferAccount10 != null">
            a.lock_transfer_account10 = #{lockTransferAccount10},
        </if>
        <if test="account10Remarks != null and account10Remarks != ''">
            a.account10_remarks = #{account10Remarks},
        </if>
        a.pk_modified = #{pkModified},
        a.modified_time = sysdate
        WHERE a.pk_member in
        <foreach collection="pkMembers" item="item" open="(" close=")" separator=",">
            #{item}
        </foreach>
    </update>


    <select id="queryMemberAccountByPkMember" resultType="com.hzs.common.domain.member.ext.CuMemberAccountExt">
        select m.member_code memberCode,
        m.member_name memberName,
        a.*
        from cu_member_account a, cu_member m
        where a.pk_member = m.pk_id
        and a.del_flag = 0
        and m.del_flag = 0
        <if test="pkMembers != null and pkMembers.size >0">
            and a.pk_member in
            <foreach collection="pkMembers" item="item" open="(" close=")" separator=",">
                #{item.pkMember}
            </foreach>
        </if>
    </select>

    <update id="batchUpdateMemberAccountByPkMember">
        MERGE INTO cu_member_account m
        USING
        (
        <foreach collection="pkMemberList" item="item" index="index" separator="union">
            select #{item.pkMember} pk_member,
            <if test='pkAccount == "1"'>
                #{item.tradeAmount} account1
            </if>
            <if test='pkAccount == "2"'>
                #{item.tradeAmount} account2
            </if>
            <if test='pkAccount == "4"'>
                #{item.tradeAmount} account4
            </if>
            FROM DUAL
        </foreach>
        ) t
        ON ( t.pk_member=m.pk_member and m.pk_member in
        <foreach collection="pkMemberList" item="item" open="(" close=")" separator=",">
            #{item.pkMember}
        </foreach>)
        WHEN MATCHED THEN
        UPDATE SET
        <if test='pkAccount == "1"'>
            m.account1 = NVL(t.account1, 0) + m.account1,
        </if>
        <if test='pkAccount == "2"'>
            m.account2 = NVL(t.account2, 0) + m.account2,
        </if>
        <if test='pkAccount == "4"'>
            m.account4 = NVL(t.account4, 0) + m.account4,
        </if>
        <if test='isWithdraw != null and isWithdraw == "0"'>
            m.withdraw_account4 = NVL(t.account4, 0) + m.withdraw_account4,
        </if>
        m.MODIFIED_TIME = sysdate
    </update>


    <update id="batchUpdateMemberAccountByPkMemberCountry">
        MERGE INTO cu_member_account m
        USING
        (
        <foreach collection="pkMemberList" item="item" index="index" separator="union">
            select #{item.pkMember} pk_member,
            <if test='item.pkAccount == "1"'>
                #{item.tradeAmount} account1,0 account2,0 account4
            </if>
            <if test='item.pkAccount == "2"'>
                0 account1,#{item.tradeAmount} account2,0 account4
            </if>
            <if test='item.pkAccount == "4"'>
                0 account1,0 account2,#{item.tradeAmount} account4
            </if>
            FROM DUAL
        </foreach>
        ) t
        ON ( t.pk_member=m.pk_member and m.pk_member in
        <foreach collection="pkMemberList" item="item" open="(" close=")" separator=",">
            #{item.pkMember}
        </foreach>)
        WHEN MATCHED THEN
        UPDATE SET
                m.account1 = NVL(t.account1, 0) + m.account1,
                m.account2 = NVL(t.account2, 0) + m.account2,
                m.account4 = NVL(t.account4, 0) + m.account4,
            <if test='isWithdraw != null and isWithdraw == "0"'>
                m.withdraw_account4 = NVL(t.account4, 0) + m.withdraw_account4,
            </if>
            m.MODIFIED_TIME = sysdate
    </update>


    <update id="batchUpdateWalletStateByPkMember">
        update cu_member_account a
        <set>
            <foreach collection="businessValueList" index="index" item="value">
                <if test='value == "1"'>
                    is_transfer = 0,is_consume = 0,is_withdraw = 0,
                </if>
                <if test='value == "2"'>
                    is_transfer = 1,
                </if>
                <if test='value == "4"'>
                    is_consume = 1,
                </if>
                <if test='value == "3"'>
                    is_withdraw = 1,
                </if>
                <if test='value == "5"'>
                    transfer_status =1,
                </if>
            </foreach>
            a.MODIFIED_TIME = sysdate,
            a.pk_modified = #{updatePkMember}
        </set>
        where a.pk_member in
        <foreach collection="businessMemberList" item="item" open="(" close=")" separator=",">
            #{item.pkMember}
        </foreach>
    </update>
    <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>
    <select id="selectMemberAccountByPkMember" resultType="com.hzs.common.domain.member.account.CuMemberAccount">
        select a.pk_member,
        a.account1,
        a.account2,
        a.account3,
        a.account4,
        a.account5,
        a.account6,
        a.account7,
        a.account8,
        a.account9,
        a.account10,
        a.account11,
        a.account12,
        a.account13,
        a.account14,
        a.account15
        from cu_member_account a
        where a.del_flag = 0
        <if test="pkMembers != null and pkMembers.size >0">
            and a.pk_member in
            <foreach collection="pkMembers" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
    </select>

    <!-- 查询会员账户以及会员信息 -->
    <select id="queryAccountAndMemberInfo" resultType="com.hzs.common.domain.member.ext.CuMemberAccountExt">
        select cm.member_code,
        cm.member_name,
        cm.pk_parent,
        cm.pk_place_parent,
        cma.*
        from cu_member cm
        inner join cu_member_account cma
        on cm.pk_id = cma.pk_member
        where cm.del_flag = 0
        <if test="memberIdList != null and memberIdList.size > 0">
            and cm.pk_id in
            <foreach collection="memberIdList" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
    </select>

    <select id="queryWalletBalanceByPkMember" resultType="com.hzs.member.account.dto.CuMemberRechargeAccountParam">
        select pk_id,
               decode(account_name,'ACCOUNT4',WITHDRAW_ACCOUNT4 - lock_withdraw_account4,0) withdrawBalance,
               case
                 when account_name = 'ACCOUNT1' then
                  balance - lock_account1 - lock_transfer_account1
                 when account_name = 'ACCOUNT2' then
                  balance - lock_account2 - lock_transfer_account2
                 when account_name = 'ACCOUNT3' then
                  balance - lock_account3 - lock_transfer_account3
                 when account_name = 'ACCOUNT4' then
                  balance - lock_account4 - lock_transfer_account4 -
                  lock_withdraw_account4
                 when account_name = 'ACCOUNT5' then
                  balance - lock_account5 - lock_transfer_account5
                 when account_name = 'ACCOUNT6' then
                  balance - lock_account6 - lock_transfer_account6
                 when account_name = 'ACCOUNT7' then
                  balance - lock_account7 - lock_transfer_account7
                 when account_name = 'ACCOUNT8' then
                  balance - lock_account8 - lock_transfer_account8
                 else
                  balance
               end availableBalance,
               case
                 when account_name = 'ACCOUNT1' then
                  lock_account1
                 when account_name = 'ACCOUNT2' then
                  lock_account2
                 when account_name = 'ACCOUNT3' then
                  lock_account3
                 when account_name = 'ACCOUNT4' then
                  lock_account4
                 when account_name = 'ACCOUNT5' then
                  lock_account5
                 when account_name = 'ACCOUNT6' then
                  lock_account6
                 when account_name = 'ACCOUNT7' then
                  lock_account7
                 when account_name = 'ACCOUNT8' then
                  lock_account8
                 else
                  0
               end lockAccount,
               case
                 when account_name = 'ACCOUNT1' then
                  lock_transfer_account1
                 when account_name = 'ACCOUNT2' then
                  lock_transfer_account2
                 when account_name = 'ACCOUNT3' then
                  lock_transfer_account3
                 when account_name = 'ACCOUNT4' then
                  lock_transfer_account4
                 when account_name = 'ACCOUNT5' then
                  lock_transfer_account5
                 when account_name = 'ACCOUNT6' then
                  lock_transfer_account6
                 when account_name = 'ACCOUNT7' then
                  lock_transfer_account7
                 when account_name = 'ACCOUNT8' then
                  lock_transfer_account8
                 else
                  0
               end transferAccount,
               case
                 when account_name = 'ACCOUNT4' then
                  lock_withdraw_account4
                 else
                  0
               end withdrawAccount,
               case
                 when account_name = 'ACCOUNT1' then
                  account1_remarks
                 when account_name = 'ACCOUNT2' then
                  account2_remarks
                 when account_name = 'ACCOUNT3' then
                  account3_remarks
                 when account_name = 'ACCOUNT4' then
                  account4_remarks
                 when account_name = 'ACCOUNT5' then
                  account5_remarks
                 when account_name = 'ACCOUNT6' then
                  account6_remarks
                 when account_name = 'ACCOUNT7' then
                  account7_remarks
                 when account_name = 'ACCOUNT8' then
                  account8_remarks
                 else
                  '0'
               end remarks,
               replace(account_name, 'ACCOUNT', '') accountType,
               balance
          from CU_MEMBER_ACCOUNT unpivot(balance for account_name in(account1,
                                                                     account2,
                                                                     account3,
                                                                     account4,
                                                                     account5,
                                                                     account6,
                                                                     account7,
                                                                     account8,
                                                                     account9,
                                                                     account10,
                                                                     account11,
                                                                     account12,
                                                                     account13,
                                                                     account14,
                                                                     account15
                                                                     ))
        where pk_member = #{pkMember}
    </select>


    <select id="queryMemberAccountByMemberCodeAndName" resultType="com.hzs.common.domain.member.ext.CuMemberAccountExt">
        select cm.member_code memberCode, cm.member_name memberName, ca.*
        from cu_member cm
        inner join cu_member_account ca
        on cm.pk_id = ca.pk_member
        where cm.del_flag = 0
        and ca.del_flag = 0
        and cm.pay_status = 1
        and cm.category = 0
        and cm.pk_settle_country = #{pkCountry}
        <if test="memberName != null and memberName != ''">
            and cm.member_name like #{memberName} || '%'
        </if>
        <if test="memberCode != null and memberCode != ''">
            and cm.member_code like #{memberCode} || '%'
        </if>
    </select>


    <update id="batchUpdateBonusAccountByPkMember">
        MERGE INTO cu_member_account m
        USING
        (
        <foreach collection="bonusList" item="item" index="index" separator="union">
            select #{item.pkMember} pk_member,
            <if test='operateType == "1"'>
                #{item.account4} account4,
                #{item.consumeAccount4} consumeAccount4,
            </if>
            <if test='operateType == "2"'>
                #{item.account4} account4,
                #{item.withdrawAccount4} withdrawAccount4,
            </if>
            <if test='pkAccount == "3"'>
                #{item.account4} account4,
                #{item.consumeAccount4} consumeAccount4,
                #{item.withdrawAccount4} withdrawAccount4,
            </if>
            FROM DUAL
        </foreach>
        ) t
        ON ( t.pk_member=m.pk_member and m.pk_member in
        <foreach collection="bonusList" item="item" open="(" close=")" separator=",">
            #{item.pkMember}
        </foreach>)
        WHEN MATCHED THEN
        UPDATE SET
        <if test='operateType == "1"'>
            m.account4 = NVL(t.account4, 0) + m.account4,
            m.consume_account4 = NVL(t.consumeAccount4, 0) + m.consume_account4,
        </if>
        <if test='operateType == "2"'>
            m.account4 = NVL(t.account4, 0) + m.account4,
            m.withdraw_account4 = NVL(t.withdrawAccount4, 0) + m.withdraw_account4,
        </if>
        <if test='operateType == "3"'>
            m.account4 = NVL(t.account4, 0) + m.account4,
            m.consume_account4 = NVL(t.consumeAccount4, 0) + m.consume_account4,
            m.withdraw_account4 = NVL(t.withdrawAccount4, 0) + m.withdraw_account4,
        </if>
        m.MODIFIED_TIME = sysdate,m.pk_modified = #{pkModified}
    </update>


    <select id="selectMemberBonusAccountBySourceCode" resultMap="AccountExResultMap">
        select r.pk_member, ma.account4, ma.withdraw_account4, ma.consume_account4, ma.pk_modified, ma.pk_country
        from cu_member_recharge r
                 left join cu_member_account ma
                           on r.pk_member = ma.pk_member
                               and ma.del_flag = 0
                 left join bd_account a
                           on r.pk_account = a.pk_id
                               and a.del_flag = 0
        where r.del_flag = 0
          and a.field_value = 4
          and r.batch = #{sourceCode}
    </select>

    <!-- 查询指定数量会员账户SEQ -->
    <select id="querySeqList" resultType="long">
        SELECT CU_MEMBER_ACCOUNT_SEQ.nextval
        FROM (select level from dual connect by #{param} >= level)
    </select>

    <!-- 批量保存会员账户信息 -->
    <insert id="insertBatchCuMemberAccount">
        INSERT ALL
        <foreach collection="memberAccountList" item="item" separator=" ">
            into CU_MEMBER_ACCOUNT (
            PK_ID, PK_MEMBER, ACCOUNT4,
            PK_CREATOR, CREATION_TIME, PK_COUNTRY
            )
            values (
            #{item.pkId}, #{item.pkMember}, #{item.account4, jdbcType=NUMERIC},
            #{item.pkCreator}, #{item.creationTime}, #{item.pkCountry}
            )
        </foreach>
        SELECT 1 FROM dual
    </insert>

</mapper>
