<?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.detail.mapper.CuMemberAwardsMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="CuMemberAwards" type="com.hzs.common.domain.member.detail.CuMemberAwards">
        <id column="PK_ID" property="pkId"/>
        <result column="PK_MEMBER" property="pkMember"/>
        <result column="PERIOD" property="period"/>
        <result column="UP_TYPE" property="upType"/>
        <result column="OLD_LEVEL" property="oldLevel"/>
        <result column="NEW_LEVEL" property="newLevel"/>
        <result column="PURCHASE_STATUS" property="purchaseStatus"/>
        <result column="PK_ORDER" property="pkOrder"/>
        <result column="REMARK" property="remark"/>
        <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="SYSTEM_TYPE" property="systemType"/>
        <result column="AWARD_TYPE" property="awardType"/>
    </resultMap>

    <!-- 批量插入奖衔记录 -->
    <insert id="saveBatchCuMemberAwards">
        insert ALL
        <foreach item="item" index="index" collection="cuMemberAwardsList" separator=" ">
            into cu_member_awards (
            pk_member, period, up_type, old_level, new_level,
            purchase_status, award_type, pk_creator, pk_country, system_type
            )
            values (
            #{item.pkMember}, #{item.period}, #{item.upType}, #{item.oldLevel}, #{item.newLevel},
            #{item.purchaseStatus}, #{item.awardType}, #{item.pkCreator}, #{item.pkCountry}, #{item.systemType}
            )
        </foreach>
        SELECT 1 FROM dual
    </insert>

    <update id="calculateCuMemberAwardsEveryMonth">
        merge into cu_member_settle_awards a
            using (
                select #{period} period, b.*
                from (
                         select pk_member,
                                pk_country,
                                a_balance,
                                b_balance,
                                a_new_pv,
                                b_new_pv,
                                (case
                                     when a_balance = 0 and b_balance = 0 then a_new_pv + b_new_pv
                                     when a_balance = 0 then a_new_pv
                                     else b_new_pv end) new_pv
                         from (
                                  select ct.pk_member,
                                         ct.pk_country,
                                         nvl(ctb.a_balance, 0) a_balance,
                                         nvl(ctb.b_balance, 0) b_balance,
                                         ct.a_sum_pv           a_new_pv,
                                         ct.b_sum_pv           b_new_pv
                                  from (
                                           select pk_member,
                                                  pk_country,
                                                  sum(A_NEW_PV + REP_A_NEW_PV) A_SUM_PV,
                                                  sum(b_NEW_PV + REP_B_NEW_PV) b_SUM_PV,
                                                  sum(register_new_pv)         REGISTER_PV,
                                                  sum(upgrade_new_pv)          UPGRADE_PV,
                                                  sum(repurchase_new_pv)       REPURCHASE_PV,
                                                  sum(hi_fun_new_pv)           HI_FUN_PV,
                                                  sum(mall_new_pv)             MALL_PV
                                           from cu_member_achieve${month}
                                           where period &lt;= #{period}
                                             and pk_country = 1
                                           group by pk_member, pk_country
                                       ) ct
                                           left join ${beforeMontTableName} ctb
                                                     on ct.pk_member = ctb.pk_member
                              )) b
                where new_pv >= #{minAchieve}) b
            on (a.pk_member = b.pk_member and a.period = b.period)
            when matched then
                update set a.a_balance = b.a_balance,a.b_balance = b.b_balance,
                    a.a_new_pv = b.a_new_pv,a.b_new_pv = b.b_new_pv,a.new_pv = b.new_pv
            when not matched then
                insert (pk_id, period, pk_member, a_balance, b_balance, a_new_pv, b_new_pv, new_pv, pk_country, assess_status)
                    values (cu_member_settle_awards_seq.nextval, b.period, b.pk_member, b.a_balance, b.b_balance, b.a_new_pv, b.b_new_pv, b.new_pv,
                            b.pk_country, 3)
    </update>
    <update id="calculateCuMemberAwards">
        merge into cu_member_settle_awards a
        using(
        select pk_member,period,max(pk_awards) pk_awards from(
        <foreach collection="bdAwardsList" item="item" open=" " close=" " separator=" union ">
            select pk_member,period,#{item.pkId} pk_awards from cu_member_settle_awards
            where period = #{period} and pk_country=#{item.pkCountry} and new_pv>=#{item.communityCheck}
            <if test="item.communityCheckEnd != null and item.communityCheckEnd > 0">
                and new_pv &lt;#{item.communityCheckEnd}
            </if>
        </foreach>
        )
        group by pk_member,period
        ) b on (a.pk_member=b.pk_member and a.period=b.period)
        when matched then
        update set a.pk_awards=b.pk_awards
    </update>

    <update id="updateBatchCuMemberAwards">
        update cu_member_awards set del_flag = 1, modified_time = sysdate, PK_MODIFIED = 1
        where period= #{period} and pk_member in
        <foreach collection="cuMemberAwardsList" item="item" open="(" close=")" separator=",">
            #{item.pkMember}
        </foreach>
    </update>
    <update id="updateCuMemberAwardsExamine">
        merge into cu_member_settle_awards a
            using (
                select ca.pk_member, ca.period, ce.assess_target, ce.assess_status
                from cu_member_settle_awards ca
                         inner join ${assessTableName} ce
                                    on ca.pk_member = ce.pk_member
                where ca.period = #{awardPeriod}
                  and ce.period = #{assessPeriod}
            ) b on (a.pk_member = b.pk_member and a.period = b.period)
            when matched then
                update set a.assess_target = b.assess_target,a.assess_status = b.assess_status
    </update>

    <update id="calculateCuMemberRangeAwardsEveryMonth">
        merge into cu_member_settle_awards a
            using (
                select #{period} period, b.pk_member, b.max_team_pv, b.small_team_pv new_pv
                from (
                         select pk_parent                                                   pk_member,
                                max(team_month_pv + new_pv)                                 max_team_pv,
                                (sum(team_month_pv + new_pv) - max(team_month_pv + new_pv)) small_team_pv,
                                count(pk_parent)                                            dept_num
                         from ${rangeTableName}
                         group by pk_parent
                         having count(pk_parent) >= 2) b
                where b.small_team_pv >= #{minAchieve}) b
            on (a.pk_member = b.pk_member and a.period = b.period)
            when matched then
                update set a.a_new_pv = b.max_team_pv,a.b_new_pv = b.new_pv,a.new_pv = b.new_pv
            when not matched then
                insert (pk_id, period, pk_member, a_balance, b_balance, a_new_pv, b_new_pv, new_pv, pk_country, assess_status)
                    values (cu_member_settle_awards_seq.nextval, b.period, b.pk_member, 0, 0, b.max_team_pv, b.new_pv, b.new_pv, 1, 3)
    </update>

    <update id="mergeCuMemberBackAwards">
        merge into ${rangeTableName} a
            using (
                select cw.*
                from cu_member_awards cw
                         inner join(
                    select ca.pk_member, min(ca.pk_id) pk_id
                    from cu_member_awards ca
                             inner join bd_awards bw
                                        on ca.new_level = bw.pk_id
                    where ca.del_flag = 0
                      and bw.del_flag = 0
                      and bw.awards_value >= 5
                      and ca.up_type = 1
                      and ca.period = #{period}
                      and ca.pk_member in
                          (select pk_id
                           from cu_member start
                    with pk_id = #{pkMember}
                    connect by prior pk_parent = pk_id)
                group by ca.pk_member) cx
            on cw.pk_id = cx.pk_id ) b
        on (a.pk_member = b.pk_member)
        when matched then
        update set a.pk_awards = b.old_level
    </update>

    <!-- 删除自动升级的奖衔升级记录 -->
    <delete id="deleteCuMemberAwards">
        delete
        from cu_member_awards
        where period = #{period}
          and up_type = 1
    </delete>

    <delete id="deleteRepeatCuMemberAwards">
        delete
        from cu_member_settle_awards
        where pk_id in (
            select a.pk_id
            from (
                     select ct.pk_id, ct.pk_member, ba.awards_value
                     from cu_member_settle_awards ct
                              inner join bd_awards ba
                                         on ct.pk_awards = ba.pk_id and ba.del_flag = 0
                     where period = #{period}) a
                     inner join(
                select ct.*, bw.awards_value
                from (
                         select pk_member, max(pk_awards) pk_awards
                         from cu_member_settle_awards
                         where period between #{firstPeriod} and #{yesPeriod}
                         group by pk_member) ct
                         inner join bd_awards bw
                                    on ct.pk_awards = bw.pk_id and bw.del_flag = 0
            ) b
                               on a.pk_member = b.pk_member and a.awards_value &lt;= b.awards_value)
    </delete>
    <delete id="deleteCuMemberAwardsByList">
        delete from cu_member_awards where period = #{period} and del_flag=0 and up_type=1
        and pk_id in
        <foreach collection="cuMemberAwards" item="item" open="(" close=")" separator=",">
            #{item.pkId}
        </foreach>
    </delete>

    <select id="queryLastCuMemberAwards" resultMap="CuMemberAwards">
        select ca.pk_member,ba.awards_value new_level from cu_member_awards ca
        inner join (
        select pk_member,max(pk_id) pk_id from cu_member_awards
        where del_flag=0 and pk_member in
        <foreach collection="cuMemberAwardsList" item="item" open="(" close=")" separator=",">
            #{item.pkMember}
        </foreach>
        group by pk_member
        ) cm
        on ca.pk_id=cm.pk_id
        inner join bd_awards ba
        on ca.new_level = ba.pk_id
        where ca.del_flag=0 and ba.del_flag=0
    </select>
    <select id="queryCuMemberAwards" resultType="com.hzs.common.domain.member.detail.CuMemberAwards">
        select ca.* from cu_member_awards ca
        inner join bd_awards bw
        on ca.new_level=bw.pk_id
        where ca.del_flag=0 and bw.del_flag=0 and bw.awards_value>=5
        and ca.up_type=1 and ca.period=#{period} and ca.pk_member in
        <foreach collection="cuMemberRetailRangeExtList" item="item" open="(" close=")" separator=",">
            #{item.pkMember}
        </foreach>
    </select>

</mapper>
