<?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.empty.mapper.CuMemberEmptyCodeMapper">

    <!-- 查询最新的编号 -->
    <select id="queryLastCode" resultType="string">
        select max(cme.BATCH_CODE) code
        from CU_MEMBER_EMPTY_CODE cme
    </select>

    <!-- 批量插入会员编号 -->
    <insert id="insertBatch">
        insert into CU_MEMBER_EMPTY_CODE
        (PK_ID, BATCH_CODE, MEMBER_CODE, IS_USED, PK_COUNTRY, PK_CREATOR, SYSTEM_TYPE)
        select CU_MEMBER_EMPTY_CODE_SEQ.Nextval, tmp.*
        from (
        <foreach collection="memberCodeList" item="item" separator=" union ">
            select
            #{item.batchCode} BATCH_CODE,
            #{item.memberCode} MEMBER_CODE,
            #{item.isUsed} IS_USED,
            #{item.pkCountry} PK_COUNTRY,
            #{item.pkCreator} PK_CREATOR,
            #{item.systemType} SYSTEM_TYPE
            from dual
        </foreach>
        ) tmp
    </insert>

    <!-- 清除会员编号占用数据 -->
    <update id="clearMemberEmptyCode">
        update CU_MEMBER_EMPTY_CODE cmec
        set cmec.del_flag      = 1,
            cmec.modified_time = sysdate
        where cmec.pk_id in
              (select cmec.pk_id
               from CU_MEMBER_EMPTY_CODE cmec
                        left join cu_member cm
                                  on cm.member_code = cmec.member_code
               where cmec.del_flag = 0
                 and cm.pk_id is null
                 and sysdate - 7 >= cmec.creation_time)
    </update>

</mapper>
