<?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.bonus.mapper.CuMemberShareDetailMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="CuMemberShareDetail" type="com.hzs.common.domain.member.bonus.CuMemberShareDetail">
        <id column="PK_ID" property="pkId" />
        <result column="PK_SHARE_BONUS" property="pkShareBonus" />
        <result column="DEL_FLAG" property="delFlag" />
        <result column="CREATION_TIME" property="creationTime" />
        <result column="MODIFIED_TIME" property="modifiedTime" />
        <result column="PK_COUNTRY" property="pkCountry" />
        <result column="PK_CREATOR" property="pkCreator" />
        <result column="PK_MODIFIED" property="pkModified" />
        <result column="PK_AWARDS" property="pkAwards" />
        <result column="PROMOTE_TOTAL" property="promoteTotal" />
        <result column="REACH_TOTAL" property="reachTotal" />
        <result column="EXPECT_SHARE_INCOME" property="expectShareIncome" />
        <result column="EXPECT_SHARE_SUBTOTAL" property="expectShareSubtotal" />
        <result column="REAL_SHARE_INCOME" property="realShareIncome" />
        <result column="REAL_SHARE_SUBTOTAL" property="realShareSubtotal" />
    </resultMap>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        DEL_FLAG,
        CREATION_TIME,
        MODIFIED_TIME,
        PK_COUNTRY,
        PK_CREATOR,
        PK_MODIFIED,
        PK_ID, PK_AWARDS,PK_SHARE_BONUS, PROMOTE_TOTAL, REACH_TOTAL, EXPECT_SHARE_INCOME, EXPECT_SHARE_SUBTOTAL, REAL_SHARE_INCOME, REAL_SHARE_SUBTOTAL
    </sql>
    <update id="saveMemberShareBonus">
        merge into cu_member_share_detail a
        using(
            <foreach collection="cuMemberShareDetailList" item="item" separator=" union ">
                select #{item.pkAwards} pk_awards,#{item.pkShareBonus} pk_share_bonus,#{item.promoteTotal} promote_total,#{item.reachTotal} reach_total,
                #{item.expectShareIncome} expect_share_income,#{item.expectShareSubtotal} expect_share_subtotal,
                #{item.realShareIncome} real_share_income,#{item.realShareSubtotal} real_share_subtotal,
                #{item.creationTime, jdbcType=TIMESTAMP} creation_time,#{item.pkCountry} pk_country,#{item.pkCreator,jdbcType=BIGINT} pk_creator,
                #{item.modifiedTime, jdbcType=TIMESTAMP} modified_time,#{item.pkModified,jdbcType=BIGINT} pk_modified from dual
            </foreach>
            ) b
        on (a.pk_share_bonus=b.pk_share_bonus and a.pk_awards=b.pk_awards)
        WHEN NOT MATCHED THEN
        insert(pk_awards,pk_share_bonus, promote_total, reach_total, expect_share_income, expect_share_subtotal, real_share_income,
               real_share_subtotal,creation_time,pk_country,pk_creator)
        values
        (b.pk_awards,b.pk_share_bonus,b.promote_total,b.reach_total, b.expect_share_income, b.expect_share_subtotal, b.real_share_income,
        b.real_share_subtotal,b.creation_time,b.pk_country,b.pk_creator)
        when matched then
        update set a.promote_total=b.promote_total,a.reach_total=b.reach_total,a.expect_share_income=b.expect_share_income,
            a.expect_share_subtotal=b.expect_share_subtotal,a.real_share_income=b.real_share_income,
            a.real_share_subtotal=b.real_share_subtotal,a.modified_time=b.modified_time,a.pk_modified=b.pk_modified
    </update>
    <select id="queryCuMemberShareBonusByPeriod" resultMap="CuMemberShareDetail">
        select cd.* from CU_MEMBER_SHARE_BONUS ch
        inner join CU_MEMBER_SHARE_DETAIL cd
        on ch.pk_id =cd.pk_share_bonus
        where ch.del_flag=0 and cd.del_flag=0
        and ch.period=#{period}
    </select>

</mapper>
