<?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.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,modified_time = sysdate
    </update>

</mapper>
