<?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.swing.mapper.CuMemberSwingNetLogMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="CuMemberSwingNetLog" type="com.hzs.common.domain.member.swing.CuMemberSwingNetLog">
        <id column="PK_ID" property="pkId" />
        <result column="CREATION_TIME" property="creationTime" />
        <result column="MODIFIED_TIME" property="modifiedTime" />
        <result column="DEL_FLAG" property="delFlag" />
        <result column="PK_CREATOR" property="pkCreator" />
        <result column="PK_MODIFIED" property="pkModified" />
        <result column="PK_COUNTRY" property="pkCountry" />
        <result column="PK_MEMBER" property="pkMember" />
        <result column="PERIOD" property="period" />
        <result column="PLACE_DEPT" property="placeDept" />
        <result column="NET_FIRST_ACHIEVE" property="netFirstAchieve" />
        <result column="NET_REPURCHASE_ACHIEVE" property="netRepurchaseAchieve" />
        <result column="NET_REAL_ACHIEVE" property="netRealAchieve" />
        <result column="A_SUM_PV" property="aSumPv" />
        <result column="B_SUM_PV" property="bSumPv" />
        <result column="A_SUM_REAL_PV" property="aSumRealPv" />
        <result column="B_SUM_REAL_PV" property="bSumRealPv" />
        <result column="REP_A_SUM_PV" property="repASumPv" />
        <result column="REP_B_SUM_PV" property="repBSumPv" />
    </resultMap>
    <update id="mergeCuMemberSwingNetLogByAchieve">
        merge into cu_member_swing_net_log a
        using (
            select x.*,(x.net_first_achieve+x.net_repurchase_achieve) net_real_achieve,
                   y.a_sum_pv,y.b_sum_pv,y.a_sum_real_pv,y.b_sum_real_pv,y.rep_a_new_pv rep_a_sum_pv,y.rep_b_new_pv rep_b_sum_pv,'100000000' pk_creator from(
            select pk_member,place_dept,period,pk_country,sum(net_first_achieve) net_first_achieve,
            sum(net_repurchase_achieve) net_repurchase_achieve from(
        <foreach collection="cuMemberAchieveExtList" item="item" separator=" union all">
            select
            #{item.pkMember} pk_member,#{item.placeDept} place_dept,#{item.period} period,
            #{item.netFirstAchieve} net_first_achieve,#{item.netRepurchaseAchieve} net_repurchase_achieve,
            #{item.pkCountry} pk_country from dual
        </foreach>)
            group by pk_member,place_dept,period,pk_country) x
            inner join ${settleTableName} y
            on x.pk_member=y.pk_member
            ) b
        on (a.pk_member=b.pk_member and a.period=b.period and a.place_dept=b.place_dept)
        when matched then
            update set a.net_first_achieve=b.net_first_achieve,a.net_repurchase_achieve=b.net_repurchase_achieve,
                a.net_real_achieve=b.net_real_achieve,a.a_sum_pv=b.a_sum_pv,a.b_sum_pv=b.b_sum_pv,
                a.a_sum_real_pv=b.a_sum_real_pv,a.b_sum_real_pv=b.b_sum_real_pv,
                a.rep_a_sum_pv=b.rep_a_sum_pv,a.rep_b_sum_pv=b.rep_b_sum_pv,modified_time=sysdate
        when not matched then
        insert
        (PK_ID,pk_member,period,place_dept,net_first_achieve,net_repurchase_achieve,net_real_achieve,
        a_sum_pv,b_sum_pv,a_sum_real_pv,b_sum_real_pv,rep_a_sum_pv,rep_b_sum_pv,pk_creator,pk_country)
        values (CU_MEMBER_SWING_NET_LOG_SEQ.NEXTVAL,b.pk_member,b.period,b.place_dept,b.net_first_achieve,
        b.net_repurchase_achieve,b.net_real_achieve,b.a_sum_pv,b.b_sum_pv,b.a_sum_real_pv,b.b_sum_real_pv,
        b.rep_a_sum_pv,b.rep_b_sum_pv,b.pk_creator,b.pk_country)
    </update>

</mapper>
