<?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>

    <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>

    <!-- 批量插入奖衔记录 -->
    <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
            )
            values (
            #{item.pkMember}, #{item.period}, #{item.upType}, #{item.oldLevel}, #{item.newLevel},
            #{item.purchaseStatus}, #{item.awardType}, #{item.pkCreator}, #{item.pkCountry}
            )
        </foreach>
        SELECT 1 FROM dual
    </insert>

    <!-- 删除自动升级的奖衔升级记录 -->
    <delete id="deleteCuMemberAwards">
        update cu_member_awards cma
        set cma.del_flag      = 1,
            cma.modified_time = sysdate
        where period = #{period}
          and up_type = 1
          and cma.del_flag = 0
    </delete>

    <!-- 删除升级的奖衔 -->
    <delete id="deleteCuMemberAwardsByList">
        update cu_member_awards cma
        set cma.del_flag = 1,
        cma.modified_time = sysdate
        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>

    <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
        and bw.del_flag = 0
        and bw.awards_value >= 5
        where ca.del_flag = 0
        and ca.up_type = 1
        and ca.period = #{period}
        and ca.award_type = #{awardType}
        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 where cw.del_flag = 0
        ) b on (a.pk_member = b.pk_member)
        when matched then
        update set
        <if test="awardType == 1">
            a.pk_awards = b.old_level
        </if>
    </update>

    <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>
