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

    <!-- 通用查询映射结果 -->
    <resultMap id="CuMemberTradeDetail" type="com.hzs.common.domain.member.account.CuMemberTradeDetail">
        <id column="PK_ID" property="pkId" />
        <result column="DEL_FLAG" property="delFlag" />
        <result column="PK_COUNTRY" property="pkCountry" />
        <result column="CREATION_TIME" property="creationTime" />
        <result column="MODIFIED_TIME" property="modifiedTime" />
        <result column="PK_CREATOR" property="pkCreator" />
        <result column="PK_MODIFIED" property="pkModified" />
        <result column="PERIOD" property="period" />
        <result column="PK_MEMBER_TRADE" property="pkMemberTrade" />
        <result column="GRANT_MONEY" property="grantMoney" />
        <result column="CONSUME_MONEY" property="consumeMoney" />
    </resultMap>
    <insert id="batchInsertCuMemberTradeDetail">
        insert ALL
        <foreach item="item" index="index" collection="cuMemberTradeDetailList" separator=" " >
            into CU_MEMBER_TRADE_DETAIL(period,pk_member_trade,consume_money,pk_country,creation_time,pk_creator) values
            (
            #{item.period},#{item.pkMemberTrade},#{item.consumeMoney,jdbcType=NUMERIC},#{item.pkCountry},
            #{item.creationTime, jdbcType=TIMESTAMP},#{item.pkCreator}
            )
        </foreach>
        SELECT 1 FROM dual
    </insert>
    <update id="updateCuMemberTradeDetailByPeriod">
        merge into cu_member_trade_detail a
        using (
        select cd.pk_id,cd.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}
        )b
        on (a.pk_id = b.pk_id)
        when matched then
        update set a.consume_money = 0
    </update>
    <update id="updateCuMemberTradeDetail">
        merge into cu_member_trade_detail a
        using (
        <foreach collection="cuMemberTradeDetailList" item="item" open=" " separator=" union all " close=" ">
            select #{item.pkId} pk_id,#{item.grantMoney,jdbcType=NUMERIC} grant_money,#{item.consumeMoney,jdbcType=NUMERIC} consume_money from dual
        </foreach>
        ) b
        on (a.pk_id = b.pk_id)
        when matched then
        update set a.grant_money = b.grant_money,
            a.consume_money = b.consume_money
    </update>

    <update id="updateAddCuMemberTradeDetail">
        merge into cu_member_trade_detail a
        using (
        <foreach collection="cuMemberTradeDetailList" item="item" open=" " separator=" union all " close=" ">
            select #{item.pkId} pk_id,#{item.grantMoney,jdbcType=NUMERIC} grant_money,#{item.consumeMoney,jdbcType=NUMERIC} consume_money from dual
        </foreach>
        ) b
        on (a.pk_id = b.pk_id)
        when matched then
        update set a.grant_money = a.grant_money + b.grant_money,
        a.consume_money = a.consume_money + b.consume_money
    </update>



    <update id="batchInsertMemberTradeDetailFromTrade">
        merge into cu_member_trade_detail d
        using (select t.pk_id, t.trade_amount, t.pk_country, t.pk_creator
                 from cu_member_trade t
                 left join bd_account a
                   on t.pk_account = a.pk_id
                  and a.del_flag = 0
                where t.del_flag = 0
                  and a.field_value = 4
                  and t.trade_amount > 0
                  and t.source_code = #{sourceCode}) tt
        on (1 = 0)
        when not matched then
          insert
            (d.period,
             d.pk_member_trade,
             d.consume_money,
             d.pk_country,
             d.pk_creator)
          values
            (#{period},tt.pk_id, tt.trade_amount, tt.pk_country, tt.pk_creator)
    </update>

    <select id="selectMemberTradeDetailByPkMember" resultMap="CuMemberTradeDetail">
        select d.pk_id, d.period, d.consume_money,d.pk_country
          from cu_member_trade t
         inner join cu_member_trade_detail d
            on t.pk_id = d.pk_member_trade
           and d.del_flag = 0
         where t.del_flag = 0
           and t.pk_member = #{pkMember}
           and d.consume_money > 0
         order by d.creation_time
    </select>
    <select id="queryCuMemberTradeDetail" resultMap="CuMemberTradeDetail">
        SELECT cd.* FROM CU_MEMBER_TRADE_DETAIL cd
        inner join CU_MEMBER_TRADE cb
        on cd.pk_member_trade = cb.pk_id
        WHERE  cb.DEL_FLAG=0 and cb.del_flag=0
        and cb.pk_member=#{pkMember}
        and CONSUME_MONEY >0
        order by period
    </select>

</mapper>
