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

    <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="queryLastManualAwards" resultType="com.hzs.common.domain.member.detail.ext.CuMemberAwardsExt">
        select *
        from (select cma.*, ba.awards_value
              from cu_member_awards cma
                       left join bd_awards ba
                                 on ba.pk_id = cma.new_level
              where cma.del_flag = 0
                and cma.pk_member = #{pkMember}
                and cma.up_type = 2
              order by cma.creation_time desc)
        where rownum = 1
    </select>

</mapper>
