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

    <!-- 通用查询映射结果 -->
    <resultMap id="CuMemberTrade" type="com.hzs.common.domain.member.account.CuMemberTrade">
        <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="TRADE_CODE" property="tradeCode"/>
        <result column="PK_ACCOUNT" property="pkAccount"/>
        <result column="TRADE_TYPE" property="tradeType"/>
        <result column="TRADE_CLASSIFY" property="tradeClassify"/>
        <result column="PK_RATE" property="pkRate"/>
        <result column="TRADE_AMOUNT" property="tradeAmount"/>
        <result column="TRADE_BALANCE" property="tradeBalance"/>
        <result column="TRADE_BEFORE_BALANCE" property="tradeBeforeBalance"/>
        <result column="SOURCE_CODE" property="sourceCode"/>
        <result column="TRADE_STATUE" property="tradeStatue"/>
        <result column="REMARKS" property="remarks"/>
    </resultMap>

    <sql id="select_base">
        select t.*, m.member_code as memberCode, m.member_name as memberName
        from cu_member_trade t
                 left join cu_member m
                           on t.pk_member = m.pk_id
    </sql>

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

    <sql id="list_where">
        <where>
            t.del_flag='0'
            <if test="memberName != null and memberName != ''">
                and m.member_name like #{memberName} || '%'
            </if>
            <if test="memberCode != null and memberCode != ''">
                and m.member_code like #{memberCode} || '%'
            </if>
            <if test="tradeAmount != null">
                and t.TRADE_AMOUNT = #{tradeAmount}
            </if>
            <if test="remarks != null and remarks != ''">
                and m.REMARKS like #{remarks} || '%'
            </if>
            <if test="pkAccount != null">
                and t.PK_ACCOUNT = #{pkAccount}
            </if>
            <if test="tradeType != null">
                and t.TRADE_TYPE = #{tradeType}
            </if>
            <if test="sourceCode != null and sourceCode != ''">
                and t.SOURCE_CODE = #{sourceCode}
            </if>
            <if test="pkCountry != null">
                and t.pk_country = #{pkCountry}
            </if>
            <if test="tradeClassify != null">
                and t.TRADE_CLASSIFY = #{tradeClassify}
            </if>
            <if test="startDate != null and startDate != ''">
                and t.CREATION_TIME >= to_date(#{startDate}, 'yyyy-mm-dd')
            </if>
            <if test="endDate != null and endDate != ''">
                and t.CREATION_TIME &lt;= to_date(#{endDate} || ' 23:59:59', 'yyyy-mm-dd hh24:mi:ss')
            </if>
        </where>
    </sql>

    <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>
