<?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="queryMaxLevelCurrentDate" resultMap="CuMemberLevel">
        select cl.pk_id,cl.pk_member,cl.upgrade_time,cl.up_type,cl.old_level,cl.new_level from cu_member_level cl
        inner join (
        select pk_member,max(pk_id) pk_id from cu_member_level
        where upgrade_time &lt;= #{currentDate, jdbcType=DATE}
        and pk_member in
        <foreach collection="saOrderExtList" item="item" open="(" close=")" separator=",">
            #{item.pkMember}
        </foreach>
        group by pk_member) ce
        on cl.pk_id = ce.pk_id
    </select>

    <!-- 查询手动升级的最新一条 -->
    <select id="queryLastManualUpgrade" resultMap="CuMemberLevel">
        select *
        from (select *
              from cu_member_level cml
              where cml.del_flag = 0
                and cml.pk_member = #{pkMember}
                and cml.up_type = 2
                and cml.pk_country = #{pkCountry}
              order by cml.upgrade_time desc)
        where rownum = 1
    </select>

</mapper>
