<?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.base.mapper.CuMemberExpireHistoryMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.hzs.common.domain.member.base.CuMemberExpireHistory">
        <id column="PK_ID" property="pkId"/>
        <result column="DEL_FLAG" property="delFlag"/>
        <result column="PK_CREATOR" property="pkCreator"/>
        <result column="CREATION_TIME" property="creationTime"/>
        <result column="PK_MODIFIED" property="pkModified"/>
        <result column="MODIFIED_TIME" property="modifiedTime"/>
        <result column="PK_COUNTRY" property="pkCountry"/>
        <result column="MEMBER_NAME" property="memberName"/>
        <result column="PHONE" property="phone"/>
        <result column="EXPIRE_DATE" property="expireDate"/>
        <result column="STOP_DATE" property="stopDate"/>
    </resultMap>

    <!-- 记录历史最后一次有效期、休止期 -->
    <insert id="saveExpireHistory">
        insert into CU_MEMBER_EXPIRE_HISTORY
        (pk_id,
         member_name,
         phone,
         expire_date,
         stop_date,
         pk_creator,
         pk_country)
        select CU_MEMBER_EXPIRE_HISTORY_SEQ.Nextval,
               cm.member_name,
               cm.phone,
               cm.expire_date,
               cm.stop_date,
               #{pkMember},
               #{pkCountry}
        from (select cm.*, rownum
              from cu_member cm
              where cm.del_flag = 0
                and cm.category = 0
                and cm.pk_settle_country = #{pkCountry}
                and cm.member_name = #{memberName}
                and cm.phone = #{phone}
                and cm.pk_id != #{pkMember}
              order by cm.expire_date desc) cm
        where rownum = 1
    </insert>

    <!-- 查询当前会员产生的最新一次时间历史记录 -->
    <select id="queryLastData" resultMap="BaseResultMap">
        select *
        from (select cmeh.*, rownum
              from cu_member_expire_history cmeh
              where cmeh.del_flag = 0
                and cmeh.pk_country = #{pkCountry}
                and cmeh.member_name = #{memberName}
                and cmeh.phone = #{phone}
                and cmeh.pk_creator = #{pkMember}
              order by cmeh.creation_time desc) cmeh
        where rownum = 1
    </select>

</mapper>
