<?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" />
    </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, pk_creator, pk_country
            )
            values (
            #{item.pkMember}, #{item.period}, #{item.upType}, #{item.oldLevel}, #{item.newLevel},
            #{item.purchaseStatus}, #{item.pkCreator}, #{item.pkCountry}
            )
        </foreach>
        SELECT 1 FROM dual
    </insert>

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

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

</mapper>
