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

    <!-- 通用查询映射结果 -->
    <resultMap id="CuMemberLevel" type="com.hzs.common.domain.member.detail.CuMemberLevel">
        <id column="PK_ID" property="pkId"/>
        <result column="PK_MEMBER" property="pkMember"/>
        <result column="UPGRADE_TIME" property="upgradeTime"/>
        <result column="UP_TYPE" property="upType"/>
        <result column="OLD_LEVEL" property="oldLevel"/>
        <result column="NEW_LEVEL" property="newLevel"/>
        <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="PK_ORDER" property="pkOrder"/>
    </resultMap>

    <!-- 查询最新升级记录 -->
    <select id="getLastUpgrade" resultMap="CuMemberLevel">
        select *
        from (
        select *
        from CU_MEMBER_LEVEL cml
        where cml.del_flag = 0
        <if test="pkOrder != null">
            and (cml.pk_order != #{pkOrder} or cml.pk_order is null )
        </if>
        and cml.pk_member = #{pkMember}
        order by cml.upgrade_time desc
        ) t
        where rownum = 1
    </select>

    <!-- 查询所有升级记录 -->
    <select id="getUpgradeList" resultMap="CuMemberLevel">
        select *
        from cu_member_level cml
        where cml.del_flag = 0
          and cml.pk_member = #{pkMember}
        order by cml.upgrade_time desc
    </select>

    <!-- 批量保存升级记录 -->
    <update id="insertBatch">
        merge into CU_MEMBER_LEVEL cml
        using (
        <foreach item="item" collection="memberLevelList" separator=" union ">
            select
            #{item.pkMember} PK_MEMBER,
            #{item.upgradeTime} UPGRADE_TIME,
            #{item.upType} UP_TYPE,
            #{item.oldLevel} OLD_LEVEL,
            #{item.newLevel} NEW_LEVEL,
            #{item.pkCreator} PK_CREATOR,
            #{item.pkCountry} PK_COUNTRY,
            #{item.pkOrder, jdbcType=VARCHAR} PK_ORDER
            from dual
        </foreach>) tmp
        on (1 = 0)
        WHEN NOT MATCHED THEN
        insert (
        PK_ID,
        PK_MEMBER,
        UPGRADE_TIME,
        UP_TYPE,
        OLD_LEVEL,
        NEW_LEVEL,
        PK_CREATOR,
        PK_COUNTRY,
        PK_ORDER
        )
        values (
        CU_MEMBER_LEVEL_SEQ.NEXTVAL,
        tmp.PK_MEMBER,
        tmp.UPGRADE_TIME,
        tmp.UP_TYPE,
        tmp.OLD_LEVEL,
        tmp.NEW_LEVEL,
        tmp.PK_CREATOR,
        tmp.PK_COUNTRY,
        tmp.PK_ORDER
        )
    </update>

    <!-- 批量保存升级记录（新） -->
    <update id="insertGradeBatch">
        merge into CU_MEMBER_GRADE cmg
        using (
        <foreach item="item" collection="memberGradeList" separator=" union ">
            select
            #{item.pkMember} PK_MEMBER,
            #{item.period} PERIOD,
            #{item.upType} UP_TYPE,
            #{item.oldLevel} OLD_LEVEL,
            #{item.newLevel} NEW_LEVEL,
            #{item.pkCreator} PK_CREATOR,
            #{item.pkCountry} PK_COUNTRY
            from dual
        </foreach>) tmp
        on (1 = 0)
        WHEN NOT MATCHED THEN
        insert (
        PK_ID,
        PK_MEMBER,
        PERIOD,
        UP_TYPE,
        OLD_LEVEL,
        NEW_LEVEL,
        PK_CREATOR,
        PK_COUNTRY
        )
        values (
        CU_MEMBER_GRADE_SEQ.NEXTVAL,
        tmp.PK_MEMBER,
        tmp.PERIOD,
        tmp.UP_TYPE,
        tmp.OLD_LEVEL,
        tmp.NEW_LEVEL,
        tmp.PK_CREATOR,
        tmp.PK_COUNTRY
        )
    </update>

    <!-- 查询会员升级记录 -->
    <select id="getLevelByGradeValue" resultType="com.hzs.common.domain.member.detail.CuMemberLevel">
        select *
        from (select cml.pk_member, cml.old_level, cml.new_level, cml.upgrade_time
        from cu_member_level cml
        where cml.del_flag = 0
        and cml.pk_member = #{pkMember}
        <if test="null != queryTime">
            and cml.upgrade_time > #{queryTime}
        </if>
        and cml.new_level in (select bg.pk_id
        from bd_grade bg
        where bg.del_flag = 0
        and bg.pk_country = #{pkCountry}
        and bg.grade_value >= #{gradeValue})
        union
        select cmg.pk_member, cmg.old_level, cmg.new_level, cmg.creation_time
        from cu_member_grade cmg
        where cmg.del_flag = 0
        and cmg.pk_member = #{pkMember}
        <if test="null != queryTime">
            and cmg.creation_time > #{queryTime}
        </if>
        and cmg.new_level in (select bg.pk_id
        from bd_grade bg
        where bg.del_flag = 0
        and bg.pk_country = #{pkCountry}
        and bg.grade_value >= #{gradeValue})
        order by upgrade_time) t
        where rownum = 1
    </select>

</mapper>
