<?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.add.mapper.AcAddUpgradeConfigMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.hzs.common.domain.activity.add.AcAddUpgradeConfig">
        <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="PK_BASE_ID" property="pkBaseId"/>
        <result column="ORDER_TYPE" property="orderType"/>
        <result column="PK_WARES" property="pkWares"/>
        <result column="QUANTITY" property="quantity"/>
        <result column="ORIGINAL_LEVEL" property="originalLevel"/>
        <result column="UPGRADE_LEVEL" property="upgradeLevel"/>
    </resultMap>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        DEL_FLAG
        ,
        CREATION_TIME,
        PK_CREATOR,
        MODIFIED_TIME,
        PK_MODIFIED,
        PK_COUNTRY,
        PK_ID, PK_BASE_ID, ORDER_TYPE, PK_WARES, QUANTITY, ORIGINAL_LEVEL, UPGRADE_LEVEL
    </sql>
    <select id="selectListByOrderType"
            resultType="com.hzs.activity.add.param.AcAddUpgradeRuleConfigParam">
        SELECT aauc.ORDER_TYPE,
               aauc.PK_WARES,
               aauc.QUANTITY,
               aauc.ORIGINAL_LEVEL,
               aauc.UPGRADE_LEVEL,
               bg.GRADE_NAME  AS originalLevelVal,
               bgg.GRADE_NAME AS upgradeLevelVal
        FROM AC_ADD_UPGRADE_CONFIG aauc
                 LEFT JOIN BD_GRADE bg ON aauc.ORIGINAL_LEVEL = bg.PK_ID
                 LEFT JOIN BD_GRADE bgg ON aauc.UPGRADE_LEVEL = bgg.PK_ID
        WHERE aauc.PK_BASE_ID = #{pkId}
          AND aauc.DEL_FLAG = 0
          AND aauc.ORDER_TYPE = #{orderType}
    </select>
    <select id="selectOrderTypeByBaseId" resultType="java.lang.Integer">
        SELECT DISTINCT ORDER_TYPE
        FROM AC_ADD_UPGRADE_CONFIG
        WHERE PK_BASE_ID = #{pkId}
          AND DEL_FLAG = 0
    </select>
    <select id="queryList" resultType="com.hzs.activity.add.vo.AcAddUpgradeListVO">
        SELECT
        abc.ACT_NAME,
        cm.member_code,
        cm.member_name,
        BG.GRADE_NAME AS originalLevelVal,
        BGG.GRADE_NAME AS upgradeLevelVal,
        abc.ACT_START_DATE,
        abc.ACT_END_DATE,
        aaur.*
        FROM
        AC_ADD_UPGRADE_RECORD aaur
        LEFT JOIN AC_BASE_CONFIG abc ON aaur.PK_BASE_ID = abc.PK_ID
        LEFT JOIN CU_MEMBER cm ON aaur.PK_MEMBER = cm.PK_ID
        LEFT JOIN BD_GRADE BG ON aaur.PK_ORIGIN_GRADE = BG.PK_ID
        LEFT JOIN BD_GRADE BGG ON aaur.PK_UPGRADE_GRADE = BGG.PK_ID
        WHERE
        aaur.DEL_FLAG = 0
        <if test="actName != null and actName != ''">
            AND abc.ACT_NAME = #{actName}
        </if>
        <if test="memberCode != null and memberCode != ''">
            AND cm.member_code = #{memberCode}
        </if>
        <if test="startActStartDate != null and startActStartDate != ''">
            AND abc.ACT_START_DATE >= TO_DATE(SUBSTR(#{startActStartDate},1,10), 'YYYY-MM-DD')
        </if>
        <if test="startActEndDate != null and startActEndDate != ''">
            AND abc.ACT_START_DATE &lt;= TO_DATE(SUBSTR(#{startActEndDate},1,10), 'YYYY-MM-DD')
        </if>
        <if test="endActStartDate != null and endActStartDate != ''">
            AND abc.ACT_END_DATE >= TO_DATE(SUBSTR(#{endActStartDate},1,10), 'YYYY-MM-DD')
        </if>
        <if test="endActEndDate != null and endActEndDate != ''">
            AND abc.ACT_END_DATE &lt;= TO_DATE(SUBSTR(#{endActEndDate},1,10), 'YYYY-MM-DD')
        </if>
    </select>
    <select id="selectGradeValue" resultType="com.hzs.common.domain.system.config.BdGrade">
        SELECT *
        FROM BD_GRADE
        WHERE PK_ID = #{pkId}
    </select>

    <!-- 查询符合条件的规则 -->
    <select id="getUpgradeConfig" resultType="com.hzs.common.domain.activity.add.ext.AcAddUpgradeConfigExt">
        select aauc.*,
        bg.grade_name,
        bg.grade_value,
        bg.pk_awards,
        bg.register_authority,
        bw.wares_code
        from ac_base_config abc
        left join AC_ADD_UPGRADE_CONFIG aauc
        on aauc.pk_base_id = abc.pk_id
        left join bd_wares bw
        on bw.pk_id = aauc.pk_wares
        left join bd_grade bg
        on bg.pk_id = aauc.upgrade_level
        where abc.del_flag = 0
        and abc.act_type = 31
        and sysdate >= abc.act_start_date
        and abc.act_end_date >= sysdate
        and aauc.del_flag = 0
        and aauc.order_type = #{orderType}
        and aauc.original_level = #{pkGrade}
        and bw.del_flag = 0
        order by abc.act_start_date desc, aauc.creation_time desc
    </select>

</mapper>
