<?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.declaration.mapper.AcDeclarationGiftRecordMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.hzs.common.domain.activity.declaration.AcDeclarationGiftRecord">
        <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="PK_RULE_ID" property="pkRuleId"/>
        <result column="PK_MEMBER" property="pkMember"/>
        <result column="PK_PRODUCT" property="pkProduct"/>
        <result column="QUANTITY" property="quantity"/>
    </resultMap>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        DEL_FLAG
        ,
        CREATION_TIME,
        PK_CREATOR,
        MODIFIED_TIME,
        PK_MODIFIED,
        PK_COUNTRY,
        PK_ID, PK_BASE_ID, PK_RULE_ID, PK_MEMBER, PK_PRODUCT, QUANTITY
    </sql>
    <select id="queryList" resultType="com.hzs.activity.declaration.vo.AcDeclarationListVO">
        SELECT
        abc.ACT_NAME,
        cm.member_code,
        cm.member_name,
        adgc.REGISTER_AUTHORITY
        BG.GRADE_NAME AS specifyLevelVal,
        abc.ACT_START_DATE,
        abc.ACT_END_DATE,
        bp.PRODUCT_NAME,
        adgr.*
        FROM
        AC_DECLARATION_GIFT_RECORD adgr
        LEFT JOIN AC_DECLARATION_GIFT_CONFIG adgc ON adgr.PK_RULE_ID = adgc.PK_ID
        LEFT JOIN BD_PRODUCT bp ON adgr.PK_PRODUCT = bp.PK_ID
        LEFT JOIN AC_BASE_CONFIG abc ON adgr.PK_BASE_ID = abc.PK_ID
        LEFT JOIN CU_MEMBER cm ON adgr.PK_MEMBER = cm.PK_ID
        LEFT JOIN BD_GRADE BG ON adgr.SPECIFY_LEVEL = BG.PK_ID
        WHERE
        adgr.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="selectDeclarationList" resultType="com.hzs.activity.declaration.vo.AcDeclarationListVO">
        SELECT
        adgr.PK_RULE_ID,
        abc.ACT_NAME,
        cm.MEMBER_CODE AS referenceCode,
        cm.MEMBER_NAME AS referenceName,
        cm.REGISTER_AUTHORITY,
        cmm.MEMBER_CODE AS memberCode,
        cmm.MEMBER_NAME AS memberName,
        so.ORDER_CODE,
        so.PAY_TIME,
        bg.GRADE_NAME AS specifyLevelVal
        FROM
        AC_DECLARATION_GIFT_RECORD adgr
        LEFT JOIN AC_DECLARATION_GIFT_CONFIG adgc ON adgr.PK_RULE_ID = adgc.PK_ID
        LEFT JOIN SA_ORDER so ON adgr.ORDER_CODE = so.ORDER_CODE
        LEFT JOIN CU_MEMBER cm ON so.PK_REFERENCE = cm.PK_ID
        LEFT JOIN CU_MEMBER cmm ON so.PK_MEMBER = cmm.PK_ID
        LEFT JOIN BD_GRADE bg ON cmm.PK_SETTLE_GRADE = bg.PK_ID
        LEFT JOIN AC_BASE_CONFIG abc ON adgr.PK_BASE_ID = abc.PK_ID
        WHERE
        so.ORDER_STATUS = 1 AND so.DEL_FLAG = 0 AND cm.DEL_FLAG = 0 AND cm.CUSTOMER_TYPE = 0 AND cmm.DEL_FLAG = 0 AND cmm.CUSTOMER_TYPE = 0
        <if test="actName != null and actName != ''">
            AND abc.ACT_NAME = #{actName}
        </if>
        <if test="referenceCode != null and referenceCode != ''">
            AND cm.member_code = #{referenceCode}
        </if>
        <if test="referenceName != null and referenceName != ''">
            AND cm.member_name = #{referenceName}
        </if>
        <if test="registerAuthority != null and registerAuthority != ''">
            AND cm.REGISTER_AUTHORITY = #{registerAuthority}
        </if>
        <if test="memberCode != null and memberCode != ''">
            AND cmm.member_code = #{memberCode}
        </if>
        <if test="memberName != null and memberName != ''">
            AND cmm.member_name = #{memberName}
        </if>
        <if test="orderCode != null and orderCode != ''">
            AND so.order_code = #{orderCode}
        </if>
        <if test="creationStartTime!= null and creationStartTime != ''">
            AND adgr.CREATION_TIME >= #{creationStartTime}
        </if>
        <if test="creationEndTime!= null and creationEndTime != ''">
            AND adgr.CREATION_TIME &lt;= #{creationEndTime}
        </if>
    </select>
    <select id="selectLastRecord" resultType="com.hzs.common.domain.activity.declaration.AcDeclarationGiftRecord">
        SELECT *
        FROM (SELECT * FROM AC_DECLARATION_GIFT_RECORD WHERE DEL_FLAG = 0 ORDER BY CREATION_TIME DESC)
        WHERE ROWNUM = 1
    </select>
    <select id="queryListByRuleList" resultType="com.hzs.common.domain.activity.base.ext.AcGiftConfigExt">
        select adgd.*,
        bp.product_name,
        bp.cover
        from AC_DECLARATION_GIFT_DETAIL adgd
        inner join bd_product bp
        on bp.pk_id = adgd.pk_product
        where adgd.del_flag = 0
        and
        (adgd.pk_rule_id in
        <foreach collection="pkDataId" item="item" index="index" open="(" close=")">
            <if test="index != 0">
                <choose>
                    <when test="index % 500 == 0">) or adgd.pk_rule_id IN (</when>
                    <otherwise>,</otherwise>
                </choose>
            </if>
            #{item}
        </foreach>
        )
        and adgd.pk_country = #{pkCountry}
        order by adgd.PK_RULE_ID, adgd.pk_id
    </select>
    <select id="selectDeclarationListTotal"
            resultType="com.hzs.activity.declaration.vo.AcDeclarationTotalListVO">
        SELECT
        base.PK_ID AS PK_BASE_ID,
        base.ACT_NAME AS actName,
        adgd.PK_PRODUCT AS PRODUCT_ID,
        bp.PRODUCT_NAME AS productName,
        bp.PRODUCT_CODE AS productCode,
        adgd.SPECS_NAME as specsName,
        SUM(adgd.QUANTITY) AS quantity
        FROM
        AC_BASE_CONFIG base
        JOIN AC_DECLARATION_GIFT_RECORD adgr ON adgr.PK_BASE_ID = base.PK_ID
        JOIN AC_DECLARATION_GIFT_DETAIL adgd ON adgr.PK_RULE_ID = adgd.PK_RULE_ID
        JOIN BD_PRODUCT bp ON adgd.PK_PRODUCT = bp.PK_ID
        WHERE
        base.ACT_TYPE = 32
        AND base.DEL_FLAG = 0
        AND adgr.DEL_FLAG = 0
        AND adgd.DEL_FLAG = 0
        <if test="productCode!= null and productCode != ''">
            AND bp.PRODUCT_CODE LIKE #{productCode} || '%'
        </if>
        <if test="actName!= null and productCode != ''">
            AND base.ACT_NAME = #{actName}
        </if>
        GROUP BY
        base.PK_ID,
        base.ACT_NAME,
        adgd.PK_PRODUCT,
        bp.PRODUCT_NAME,
        bp.PRODUCT_CODE,
        adgd.SPECS_NAME
        ORDER BY
        base.PK_ID,
        adgd.PK_PRODUCT
    </select>

</mapper>
