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

    <!-- 更新会员手动升级 -->
    <update id="updateCuMemberManualGrade">
        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>

    <!-- 查询会员升级记录 -->
    <select id="queryCuMemberLevel" resultType="com.hzs.common.domain.member.detail.CuMemberLevel">
        select *
        from cu_member_level
        where del_flag = 0
          and up_type = #{upType}
          and upgrade_time >= #{startDate,jdbcType=DATE}
          and upgrade_time &lt; #{endDate,jdbcType=DATE}
        order by upgrade_time
    </select>

</mapper>
