<?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"/>
        <result column="PERIOD" property="period"/>
    </resultMap>

    <!-- 查询会员升级列表 -->
    <select id="listMemberLevelList" resultType="com.hzs.common.domain.member.detail.ext.CuMemberLevelExt">
        select cml.*, cm.member_code, cm.member_name, bgo.grade_name grade_name_old, bgn.grade_name grade_name_new
        from cu_member_level cml
        left join cu_member cm
        on cm.pk_id = cml.pk_member
        left join bd_grade bgo
        on bgo.pk_id = cml.old_level
        left join bd_grade bgn
        on bgn.pk_id = cml.new_level
        where cml.del_flag = 0
        and cm.del_flag = 0
        <if test="param.memberCode != null and param.memberCode != ''">
            and cm.member_code like #{param.memberCode} || '%'
        </if>
        order by cml.creation_time desc
    </select>

    <!-- 查询最新一条 -->
    <select id="queryLastManualUpgrade" resultMap="CuMemberLevel">
        select *
        from (select *
        from cu_member_level cml
        where cml.del_flag = 0
        and cml.pk_member = #{pkMember}
        <if test="typeList != null and typeList.size > 0">
            and cml.up_type in
            <foreach collection="typeList" item="items" open="(" separator="," close=")">
                #{items}
            </foreach>
        </if>
        <if test="pkOrder != null">
            and cml.pk_order != #{pkOrder}
        </if>
        order by cml.upgrade_time desc)
        where rownum = 1
    </select>

</mapper>
