<?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.AcDeclarationGiftConfigMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.hzs.common.domain.activity.declaration.AcDeclarationGiftConfig">
        <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="REGISTER_AUTHORITY" property="registerAuthority"/>
        <result column="PK_PRODUCT" property="pkProduct"/>
        <result column="SPECS_NAME" property="specsName"/>
        <result column="SPECS_NAME_ID" property="specsNameId"/>
        <result column="QUANTITY" property="quantity"/>
        <result column="LEVEL" property="level"/>
    </resultMap>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        DEL_FLAG
        ,
        CREATION_TIME,
        PK_CREATOR,
        MODIFIED_TIME,
        PK_MODIFIED,
        PK_COUNTRY,
        PK_ID, PK_BASE_ID, REGISTER_AUTHORITY, PK_PRODUCT, SPECS_NAME, SPECS_NAME_ID, QUANTITY, LEVEL
    </sql>
    <update id="updateOrInsertPick">
    DECLARE
    v_error_count NUMBER := 0;
    BEGIN
    BEGIN
    MERGE INTO AC_PICK tgt
        USING (SELECT adgr.PK_BASE_ID,
                      so.PK_CREATOR      AS PK_MEMBER,
                      32                 AS PICK_TYPE,
                      adgd.PK_PRODUCT    AS PK_DATA_ID,
                      adgd.SPECS_NAME,
                      adgd.SPECS_NAME_ID,
                      SUM(adgd.QUANTITY) AS USABLE_QUANTITY,
                      bp.COVER           AS PICK_COVER,
                      0                  AS IS_FREE_MAIL,
                      0                  AS DEL_FLAG,
                      bpe.DELIVERY_QUANTITY AS BASE_QUANTITY,
                      SYSDATE            AS CREATION_TIME,
                      100000000          AS PK_CREATOR,
                      #{pkCountry}       AS PK_COUNTRY
               FROM AC_DECLARATION_GIFT_RECORD adgr
                        LEFT JOIN AC_DECLARATION_GIFT_CONFIG adgc ON adgr.PK_RULE_ID = adgc.PK_ID
                        LEFT JOIN AC_DECLARATION_GIFT_DETAIL adgd ON adgc.PK_ID = adgd.PK_RULE_ID
                        LEFT JOIN BD_PRODUCT bp ON adgd.PK_PRODUCT = bp.PK_ID
                        LEFT JOIN BD_PRODUCT_EXTEND bpe ON bp.PK_ID = bpe.PK_PRODUCT
                        LEFT JOIN SA_ORDER so ON adgr.ORDER_CODE = so.ORDER_CODE
               WHERE adgr.SYNC_STATUS IN (0, 2)
               GROUP BY adgr.PK_BASE_ID,
                        so.PK_CREATOR,
                        adgd.PK_PRODUCT,
                        adgd.SPECS_NAME,
                        adgd.SPECS_NAME_ID,
                        bpe.DELIVERY_QUANTITY,
                        bp.COVER) src
        ON (
            tgt.PICK_TYPE = src.PICK_TYPE
                AND tgt.PK_DATA_ID = src.PK_DATA_ID
                AND tgt.SPECS_NAME = src.SPECS_NAME
                AND tgt.SPECS_NAME_ID = src.SPECS_NAME_ID
                AND tgt.PK_BASE_ID = src.PK_BASE_ID
                AND tgt.DEL_FLAG = src.DEL_FLAG
            )
        WHEN MATCHED THEN
            UPDATE SET
                tgt.USABLE_QUANTITY = tgt.USABLE_QUANTITY + src.USABLE_QUANTITY,
                tgt.MODIFIED_TIME = SYSDATE,
                tgt.PK_MODIFIED = src.PK_CREATOR
        WHEN NOT MATCHED THEN
            INSERT (
                    PK_ID,
                    PK_MEMBER,
                    PICK_TYPE,
                    PK_BASE_CONFIG,
                    PK_DATA_ID,
                    SPECS_NAME,
                    SPECS_NAME_ID,
                    USABLE_QUANTITY,
                    PICK_COVER,
                    IS_FREE_MAIL,
                    DEL_FLAG,
                    BASE_QUANTITY,
                    PK_BASE_ID,
                    CREATION_TIME,
                    PK_CREATOR,
                    PK_COUNTRY
                )
                VALUES (AC_PICK_SEQ.NEXTVAL,
                        src.PK_MEMBER,
                        src.PICK_TYPE,
                        0,
                        src.PK_DATA_ID,
                        src.SPECS_NAME,
                        src.SPECS_NAME_ID,
                        src.USABLE_QUANTITY,
                        src.PICK_COVER,
                        src.IS_FREE_MAIL,
                        src.DEL_FLAG,
                        src.BASE_QUANTITY,
                        src.PK_BASE_ID,
                        src.CREATION_TIME,
                        src.PK_CREATOR,
                        src.PK_COUNTRY);

    UPDATE AC_DECLARATION_GIFT_RECORD
    SET SYNC_STATUS = 1
    WHERE SYNC_STATUS IN (0, 2);

    EXCEPTION
        WHEN OTHERS THEN
            v_error_count := SQL%ROWCOUNT;
    UPDATE AC_DECLARATION_GIFT_RECORD
    SET SYNC_STATUS = 2
    WHERE SYNC_STATUS IN (0, 2);
    END;

    INSERT INTO AC_PICK_LOG (PK_ID, PK_PICK, PK_USER, QUANTITY, PK_COUNTRY)
    SELECT AC_PICK_LOG_SEQ.NEXTVAL,
           p.PK_ID,
           p.PK_CREATOR,
           p.USABLE_QUANTITY,
           p.PK_COUNTRY
    FROM AC_PICK p
    WHERE TRUNC(p.CREATION_TIME) = TRUNC(SYSDATE)
       OR TRUNC(p.MODIFIED_TIME) = TRUNC(SYSDATE);

    COMMIT;
    END;
    </update>
    <select id="selectAcPickSevenDay" resultType="com.hzs.common.domain.activity.pick.AcPick">
        SELECT adgr.PK_MEMBER,
               32                    AS PICK_TYPE,
               adgc.PK_ID            as PK_BASE_CONFIG,
               adgr.PK_PRODUCT       AS PK_DATA_ID,
               adgc.SPECS_NAME       AS SPECS_NAME,
               adgc.SPECS_NAME_ID    AS SPECS_NAME_ID,
               adgr.QUANTITY         AS USABLE_QUANTITY,
               bp.COVER              AS PICK_COVER,
               0                     AS IS_FREE_MAIL,
               0                     AS DEL_FLAG,
               bpe.DELIVERY_QUANTITY AS BASE_QUANTITY,
               adgr.PK_BASE_ID       AS PK_BASE_ID
        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 BD_PRODUCT_EXTEND bpe ON bp.PK_ID = bpe.PK_PRODUCT
        WHERE TRUNC(adgr.CREATION_TIME) = TRUNC(SYSDATE) - 7;
    </select>

</mapper>
