<?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.CuMemberGradeMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="CuMemberGrade" type="com.hzs.common.domain.member.detail.CuMemberGrade">
        <id column="PK_ID" property="pkId"/>
        <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="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="REMARK" property="remark"/>
    </resultMap>

    <update id="updateCuMemberGrade">
        merge into ${rangeTableName} a
            using (
                select a.pk_member, max(a.new_level) pk_grade
                from cu_member_grade a
                         inner join(
                    select pk_member, max(nvl(modified_time, creation_time)) creation_time
                    from cu_member_grade
                    where period = #{period}
                      and del_flag = 0
                      and up_type = 2
                    group by pk_member) b
                                   on a.pk_member = b.pk_member and nvl(a.modified_time, a.creation_time) = b.creation_time
                group by a.pk_member
            ) b
            on (a.pk_member = b.pk_member)
            when matched then
                update set
                    a.pk_grade = b.pk_grade
    </update>

    <!-- 查询会员最新手动升级记录，更新秒结表会员等级小于手动升级的会员等级 -->
    <update id="updateCuMemberGradeSecond">
        merge into ${secondRangeTableName} a
            using (
                select cs.pk_member, b.pk_grade
                from ${secondRangeTableName} cs
                         inner join (
                    select a.pk_member, max(a.new_level) pk_grade
                    from cu_member_grade a
                             inner join (
                        select pk_member, max(nvl(modified_time, creation_time)) creation_time
                        from cu_member_grade
                        where period &lt;= #{period}
                          and del_flag = 0
                          and up_type = 2
                        group by pk_member
                    ) b on a.pk_member = b.pk_member and nvl(a.modified_time, a.creation_time) = b.creation_time
                    group by a.pk_member
                ) b on cs.pk_member = b.pk_member
                where cs.pk_grade &lt; b.pk_grade
            ) b
            on (a.pk_member = b.pk_member)
            when matched then
                update set
                    a.pk_grade = b.pk_grade
    </update>

    <!-- 回退会员等级 -->
    <update id="updateCuMemberGradeLevel">
        merge into ${rangeTableName} a
        using (
        select pk_member, max(new_level) pk_grade
        from cu_member_level
        where del_flag = 0 and up_type = 4
        <if test="pkOrder != null">
            and pk_order = #{pkOrder}
        </if>
        group by pk_member
        ) b
        on (a.pk_member = b.pk_member)
        when matched then
        update set
        a.pk_grade = b.pk_grade
    </update>

    <!-- 查询订单升级记录 -->
    <select id="getMemberLevel" resultType="com.hzs.common.domain.member.achieve.ext.CuMemberRetailRangeExt">
        select cml.pk_member, bg.pk_id pk_grade, bg.grade_value
        from cu_member_level cml
                 left join bd_grade bg
                           on bg.pk_id = cml.new_level
        where cml.del_flag = 0
          and cml.up_type = 4
          and cml.pk_member = #{pkMember}
          and cml.pk_order = #{pkOrder}
    </select>

    <!-- 删除等级 -->
    <delete id="deleteCuMemberGrade">
        update cu_member_grade
        set del_flag = 1,
        modified_time = sysdate
        where period = #{period}
          and del_flag = 0
          and up_type = 1

        <!--
        delete
        from cu_member_grade
        where period = #{period}
          and del_flag = 0
          and up_type = 1
          -->
    </delete>

    <!-- 删除等级升级记录 -->
    <delete id="deleteCuMemberGradeByList">
        update cu_member_grade
        set del_flag = 1,
        modified_time = sysdate
        where period = #{period}
        and del_flag = 0
        and up_type = 1
        and pk_id in
        <foreach collection="cuMemberGradeList" item="item" open="(" close=")" separator=",">
            #{item.pkId}
        </foreach>

        <!--
        delete from cu_member_grade
        where period = #{period} and del_flag=0 and up_type=1
        and pk_id in
        <foreach collection="cuMemberGradeList" item="item" open="(" close=")" separator=",">
            #{item.pkId}
        </foreach>
        -->
    </delete>

    <select id="queryCuMemberGrade" resultMap="CuMemberGrade">
        select cg.*
        from cu_member_grade cg
        inner join bd_grade bg
        on cg.new_level = bg.pk_id
        where bg.del_flag = 0
        and cg.del_flag = 0
        and bg.grade_value >= 60
        and cg.up_type = 1
        and cg.period = #{period}
        and cg.pk_member in
        <foreach collection="cuMemberRetailRangeExtList" item="item" open="(" close=")" separator=",">
            #{item.pkMember}
        </foreach>
    </select>

    <select id="queryCuMemberLevel" resultType="com.hzs.common.domain.member.detail.CuMemberLevel">
        select *
        from cu_member_level
        where del_flag = 0
          and up_type = 4
          and upgrade_time >= #{startDate,jdbcType=DATE}
          and upgrade_time &lt; #{endDate,jdbcType=DATE}
        order by upgrade_time
    </select>

    <select id="queryCuMemberGradeByList" resultType="com.hzs.common.domain.member.detail.CuMemberGrade">
        select a.* from cu_member_grade a
        inner join (
        select pk_member, max(nvl(modified_time,creation_time)) creation_time
        from cu_member_grade
        where period &lt;= #{period}
        and del_flag = 0
        and up_type = 2
        group by pk_member) b
        on a.pk_member = b.pk_member and nvl(a.modified_time,a.creation_time) = b.creation_time
        and a.pk_member in
        <foreach collection="memberRetailRangeExtList" item="item" open="(" close=")" separator=",">
            #{item.pkMember}
        </foreach>
    </select>

</mapper>
