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

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.hzs.common.domain.activity.base.AcBaseConfig">
        <id column="PK_ID" property="pkId"/>
        <result column="DEL_FLAG" property="delFlag"/>
        <result column="CREATION_TIME" property="creationTime"/>
        <result column="PK_CREATOR" property="pkCreator"/>
        <result column="MODIFIED_TIME" property="modifiedTime"/>
        <result column="PK_MODIFIED" property="pkModified"/>
        <result column="PK_COUNTRY" property="pkCountry"/>
        <result column="ACT_NAME" property="actName"/>
        <result column="ACT_TYPE" property="actType"/>
        <result column="ACT_EXPLAIN" property="actExplain"/>
        <result column="ACT_START_DATE" property="actStartDate"/>
        <result column="ACT_END_DATE" property="actEndDate"/>
        <result column="DIS_START_DATE" property="disStartDate"/>
        <result column="DIS_END_DATE" property="disEndDate"/>
        <result column="ACT_COVER" property="actCover"/>
        <result column="PAY_MONEY" property="payMoney"/>
        <result column="ATC_MODULE" property="atcModule"/>
    </resultMap>

    <resultMap id="AcBaseConfigExt" type="com.hzs.common.domain.activity.base.ext.AcBaseConfigExt">
        <id column="PK_ID" property="pkId"/>
        <result column="DEL_FLAG" property="delFlag"/>
        <result column="CREATION_TIME" property="creationTime"/>
        <result column="PK_CREATOR" property="pkCreator"/>
        <result column="MODIFIED_TIME" property="modifiedTime"/>
        <result column="PK_MODIFIED" property="pkModified"/>
        <result column="PK_COUNTRY" property="pkCountry"/>
        <result column="ACT_NAME" property="actName"/>
        <result column="ACT_TYPE" property="actType"/>
        <result column="ACT_EXPLAIN" property="actExplain"/>
        <result column="ACT_START_DATE" property="actStartDate"/>
        <result column="ACT_END_DATE" property="actEndDate"/>
        <result column="DIS_START_DATE" property="disStartDate"/>
        <result column="DIS_END_DATE" property="disEndDate"/>
        <result column="ACT_COVER" property="actCover"/>
        <result column="PAY_MONEY" property="payMoney"/>
        <result column="ACT_STATUS" property="actStatus"/>
        <result column="EXIST_DRAW_NUM" property="existDrawNum"/>
    </resultMap>

    <select id="queryBaseConfigList" resultMap="AcBaseConfigExt">
        select ac.pk_id,
               ac.act_type,
               ac.act_name,
               ac.act_cover,
               ac.act_start_date,
               ac.act_end_date,
               (case
                    when act_start_date &gt; sysdate then 0
                    when act_end_date &lt; sysdate then 2
                    else 1 end)                     ACT_STATUS,
               ac.act_explain,
               nvl(draw.exist_draw_num, 1)          exist_draw_num,
               pk_new_people,
               nvl(people.exist_new_people_gift, 1) exist_new_people_gift
        from ac_base_config ac
                 left join (
            select pk_base_id, 0 exist_draw_num
            from ac_draw_reward_num
            where del_flag = 0
              and pk_member = #{pkMember}
              and (total_surplus_num + available_surplus_num) > 0) draw
                           on ac.pk_id = draw.pk_base_id
                 left join (
            select distinct pk_base_id, pk_new_people, 0 exist_new_people_gift
            from ac_people_service
            where del_flag = 0
              and pk_member = #{pkMember}
              and plan_state = 0
        ) people
                           on ac.pk_id = people.pk_base_id
        where ac.del_flag = 0
          and ac.pk_country = #{pkCountry}
          and ac.dis_start_date &lt;= sysdate
          and ac.dis_end_date >= sysdate
        order by ACT_STATUS, ac.act_type
    </select>

    <select id="selectBaseConfigByType" resultMap="BaseResultMap">
        select *
        from AC_BASE_CONFIG b
        where b.del_flag = 0
        and b.pk_country = #{pkCountry}
        <if test="actType != null">
            and b.act_type = #{actType}
        </if>
        <if test="atcModule != null">
            and b.atc_module =#{atcModule}
        </if>
        and trunc(sysdate) between b.act_start_date and b.act_end_date
    </select>

    <!-- 特殊查询活动基础信息 -->
    <select id="queryBaseConfigSpecial" resultMap="BaseResultMap">
        select ba.*
        from AC_BASE_CONFIG ba
        where ba.del_flag = 0
          and ba.pk_country = #{pkCountry}
          and ba.act_type = #{actType}
          and sysdate >= ba.act_start_date
          and ba.act_end_date + INTERVAL '7' day >= sysdate
    </select>

</mapper>
