<?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.bonus.achieve.mapper.CuMemberSettlePeriodMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="CuMemberSettlePeriodExt" type="com.hzs.common.domain.member.achieve.ext.CuMemberSettlePeriodExt">
        <id column="PK_ID" property="pkId"/>
        <result column="DEL_FLAG" property="delFlag"/>
        <result column="CREATION_TIME" property="creationTime"/>
        <result column="MODIFIED_TIME" property="modifiedTime"/>
        <result column="SETTLE_DATE" property="settleDate"/>
        <result column="SETTLE_CYCLE" property="settleCycle"/>
        <result column="START_PERIOD" property="startPeriod"/>
        <result column="END_PERIOD" property="endPeriod"/>
        <result column="IS_SETTLE" property="isSettle"/>
        <result column="PUBLISH_DATE" property="publishDate"/>
        <result column="IS_PUBLISH" property="isPublish"/>
        <result column="GRANT_DATE" property="grantDate"/>
        <result column="IS_GRANT" property="isGrant"/>
        <result column="WITHDRAWAL_DATE" property="withdrawalDate"/>
        <result column="IS_WITHDRAWAL" property="isWithdrawal"/>
        <result column="IS_RETRY" property="isRetry"/>
        <result column="PK_COUNTRY" property="pkCountry"/>
    </resultMap>

    <sql id="memberPeriod">
        select cp.pk_id,
               cp.settle_date,
               cd.settle_cycle,
               cd.start_period,
               cd.end_period,
               cd.is_settle,
               cd.publish_date,
               cd.is_publish,
               cd.grant_date,
               cd.is_grant,
               cd.withdrawal_date,
               cd.is_withdrawal,
               cd.is_retry,
               cd.pk_country
        from cu_member_settle_period cp
                 inner join cu_member_settle_period_detail cd
                            on cp.pk_id >= cd.start_period and cp.pk_id &lt;= cd.end_period
        where cp.del_flag = 0
          and cd.del_flag = 0
    </sql>

    <select id="queryMemberSettlePeriod" resultMap="CuMemberSettlePeriodExt">
        <include refid="memberPeriod"></include>
        and cd.pk_country=#{pkCountry}
        and cp.settle_date = #{settleDate, jdbcType=DATE}
    </select>

    <select id="queryMemberSettlePeriodByCondition" resultMap="CuMemberSettlePeriodExt">
        <include refid="memberPeriod"></include>
        and cd.pk_country=#{pkCountry}
        <if test="startDate != null">
            and cp.settle_date >= #{startDate, jdbcType=DATE}
        </if>
        <if test="endDate != null">
            and cp.settle_date &lt; #{endDate, jdbcType=DATE}
        </if>
        <if test="period != null">
            and cp.pk_id = #{period}
        </if>
        <if test="isSettle != null">
            and cd.is_settle = #{isSettle}
        </if>
        <if test="isPublish != null">
            and cd.is_publish = #{isPublish}
        </if>
        <if test="isGrant != null">
            and cd.is_grant = #{isGrant}
        </if>
        <if test="isWithdrawal != null">
            and cd.is_withdrawal = #{isWithdrawal}
        </if>
        <if test="isRetry != null">
            and cd.is_retry = #{isRetry}
        </if>
        and cp.settle_date &lt; sysdate-1
        order by pk_id desc
    </select>

    <!-- 根据日期列表查询期间列表 -->
    <select id="queryPeriodList" resultMap="CuMemberSettlePeriodExt">
        select cmsr.*
        from CU_MEMBER_SETTLE_PERIOD cmsr
        where cmsr.del_flag = 0
        and to_char(cmsr.settle_date, 'yyyy-mm-dd') in
        <foreach collection="dateList" item="item" open="(" close=")" separator=",">
            #{item}
        </foreach>
    </select>

</mapper>
