<?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.CuMemberTradeMapper">

    <update id="batchInsertCuMemberTrade">
        merge into cu_member_trade a
        using (
        select m.pk_id, m.pk_member, m.trade_code, m.pk_account, m.trade_type,m.trade_classify, m.pk_rate, m.trade_amount,
        (case when ba.field_value=4 then ca.account4 + m.trade_amount when ba.field_value=5 then ca.account5 + m.trade_amount
        when ba.field_value=8 then ca.account8 + m.trade_amount when ba.field_value=9 then ca.account9 + m.trade_amount
        else ca.account10 + m.trade_amount end) trade_balance,
        (case when ba.field_value=4 then ca.account4 when ba.field_value=5 then ca.account5 when ba.field_value=8 then ca.account8
        when ba.field_value=9 then ca.account9 else ca.account10 end) trade_before_balance,
        m.source_code, m.trade_statue, m.remarks, m.pk_country, m.creation_time, m.pk_creator from(
        <foreach collection="cuMemberTradeList" item="item" open=" " separator=" union all " close=" ">
            select #{item.pkId} pk_id,#{item.tradeCode} trade_code, #{item.tradeClassify} trade_classify,
            #{item.pkMember} pk_member,#{item.pkAccount} pk_account,#{item.pkRate} pk_rate,#{item.tradeAmount} trade_amount,
            #{item.sourceCode} source_code,#{item.tradeStatue} trade_statue,#{item.remarks} remarks,
            #{item.tradeType} trade_type,#{item.pkCountry} pk_country,#{item.pkCreator} pk_creator,
            #{item.creationTime} creation_time from dual
        </foreach>) m
        inner join cu_member_account ca
        on m.pk_member = ca.pk_member
        inner join bd_account ba
        on ba.pk_id = m.pk_account
        ) b
        on (a.pk_id = b.pk_id)
        when not matched then
        insert (pk_id, pk_member, trade_code, pk_account, trade_type,
        trade_classify, pk_rate, trade_amount, trade_balance,trade_before_balance,
        source_code, trade_statue, remarks, pk_country, creation_time, pk_creator)
        values(b.pk_id,b.pk_member,b.trade_code,b.pk_account,b.trade_type,
        b.trade_classify,b.pk_rate,b.trade_amount,b.trade_balance,b.trade_before_balance,
        b.source_code,b.trade_statue,b.remarks,b.pk_country,b.creation_time,b.pk_creator)
    </update>

    <select id="batchQueryCuMemberTradeSeq" resultType="java.lang.Long">
        select cu_member_trade_seq.nextval
        from dual connect by rownum &lt;= #{rowNum}
    </select>


</mapper>
