<?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.pick.mapper.AcPickMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.hzs.common.domain.activity.pick.AcPick">
        <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_MEMBER" property="pkMember"/>
        <result column="PICK_TYPE" property="pickType"/>
        <result column="PK_BASE_CONFIG" property="pkBaseConfig"/>
        <result column="PK_DATA_ID" property="pkDataId"/>
        <result column="SPECS_NAME" property="specsName"/>
        <result column="SPECS_NAME_ID" property="specsNameId"/>
        <result column="USABLE_QUANTITY" property="usableQuantity"/>
        <result column="USED_QUANTITY" property="usedQuantity"/>
        <result column="PICK_COVER" property="pickCover"/>
        <result column="RECEIVE_TIME" property="receiveTime"/>
        <result column="IS_FREE_MAIL" property="isFreeMail"/>
        <result column="PK_POSTAGE" property="pkPostage"/>
        <result column="BASE_QUANTITY" property="baseQuantity"/>
        <result column="PK_BASE_ID" property="pkBaseId"/>
    </resultMap>

    <!-- 更新提货数量 -->
    <update id="addQuantityByActivity">
        update AC_PICK ap
        set ap.USABLE_QUANTITY = ap.USABLE_QUANTITY + #{quantity},
        <if test="receiveTime != null">
            ap.RECEIVE_TIME = #{receiveTime},
        </if>
        <choose>
            <when test="pkPostage != null">
                ap.IS_FREE_MAIL = 1,
                ap.PK_POSTAGE = #{pkPostage},
            </when>
            <otherwise>
                ap.PK_POSTAGE = 0,
            </otherwise>
        </choose>
        ap.pk_modified = #{pkModified},
        ap.modified_time = sysdate
        where ap.del_flag = 0
        and ap.PICK_TYPE = #{pickType}
        <if test="pkBaseConfig != null">
            and ap.PK_BASE_CONFIG = #{pkBaseConfig}
        </if>
        and ap.PK_DATA_ID = #{pkDataId}
        and ap.PK_MEMBER = #{pkMember}
        and ap.USABLE_QUANTITY + #{quantity} > 0
    </update>

    <!-- 更新使用数量 -->
    <update id="updatePickUse">
        update AC_PICK ap
        set ap.USABLE_QUANTITY = ap.USABLE_QUANTITY - #{quantity},
        ap.USED_QUANTITY = ap.USED_QUANTITY + #{quantity},
        ap.pk_modified = #{pkModified},
        ap.modified_time = sysdate
        where ap.del_flag = 0
        and ap.PICK_TYPE = #{pickType}
        <if test="pkBaseConfig != null">
            and ap.PK_BASE_CONFIG = #{pkBaseConfig}
        </if>
        and ap.PK_DATA_ID = #{pkDataId}
        and ap.PK_MEMBER = #{pkMember}
        and ap.USABLE_QUANTITY - #{quantity} >= 0
    </update>

    <!-- 根据主键更新提货数据 -->
    <update id="updatePickByPkId">
        update AC_PICK ap
        set ap.USABLE_QUANTITY = ap.USABLE_QUANTITY + #{acPick.usableQuantity},
        <if test="null != acPick.receiveTime">
            ap.receive_time = #{acPick.receiveTime},
        </if>
        <if test="null != acPick.isFreeMail">
            ap.is_free_mail = #{acPick.isFreeMail},
            <choose>
                <when test="1 == acPick.isFreeMail">
                    <!-- 不包邮，需要更新邮费模板 -->
                    ap.pk_postage = #{acPick.pkPostage},
                </when>
                <otherwise>
                    <!-- 包邮，清空运费模板 -->
                    ap.pk_postage = null,
                </otherwise>
            </choose>
        </if>
        <if test="null != acPick.baseQuantity">
            ap.BASE_QUANTITY = #{acPick.baseQuantity},
        </if>
        ap.pk_modified = #{acPick.pkModified},
        ap.modified_time = sysdate
        where ap.del_flag = 0
        and ap.PK_ID = #{acPick.pkId}
    </update>

    <!-- 查询提货列表 -->
    <select id="queryList" resultType="com.hzs.common.domain.activity.pick.ext.AcPickExt">
        SELECT ap.*,
        cm.member_code,
        cm.member_name,
        nvl(bp.retail_price, 0) retail_price,
        bp.product_name || nvl2(ap.specs_name, '(' || ap.specs_name || ')', '') AS product_name,
        abc.act_name
        FROM AC_PICK ap
        INNER JOIN cu_member cm ON cm.pk_id = ap.pk_member
        LEFT JOIN bd_product bp ON bp.pk_id = ap.pk_data_id AND ap.pick_type in(0, 8)
        left join ac_base_config abc on abc.pk_id = ap.pk_base_id
        WHERE ap.del_flag = 0
        and ap.pk_country = #{pkCountry}
        <if test="param.pkMember != null ">
            and ap.pk_member=#{param.pkMember}
        </if>
        <if test="param.memberCode != null and param.memberCode != ''">
            and cm.member_code like #{param.memberCode} || '%'
        </if>
        <if test="param.memberName != null and param.memberName != ''">
            and cm.member_name like #{param.memberName} || '%'
        </if>
        <if test="param.pickType != null">
            and ap.pick_type = #{param.pickType}
        </if>
        <if test="param.creationTime != null">
            and ap.creation_time >= #{param.creationTime[0]}
            and #{param.creationTime[1]} >= ap.creation_time
        </if>
        <if test="param.modifiedTime != null">
            and ap.modified_time >= #{param.modifiedTime[0]}
            and #{param.modifiedTime[1]} >= ap.modified_time
        </if>
        <if test="param.receiveTime != null">
            and ap.receive_time >= #{param.receiveTime[0]}
            and #{param.receiveTime[1]} >= ap.receive_time
        </if>
        order by ap.creation_time desc
    </select>

    <!-- 查询提货详情 -->
    <select id="queryDetail" resultType="com.hzs.common.domain.activity.pick.ext.AcPickExt">
        select ap.*,
               cm.member_code,
               cm.member_name,
               case
                   when ap.pick_type = 0 then
                       (select bp.product_name
                        from bd_product bp
                        where bp.pk_id = PK_DATA_ID)
                   end product_name
        from AC_PICK ap
                 inner join cu_member cm
                            on cm.pk_id = ap.pk_member
        where ap.del_flag = 0
          and ap.pk_id = #{pkId}
          and ap.pk_country = #{pkCountry}
    </select>

    <!-- 会员提货列表 -->
    <select id="pickList" resultType="com.hzs.activity.pick.vo.ApiPickListVO">
        <!-- 提货产品 -->
        select ap.pk_id,
        ap.pick_type,
        case
        when ap.pick_cover is not null then
        ap.pick_cover
        else
        (select bp.cover from bd_product bp where bp.pk_id = ap.pk_data_id)
        end pick_cover,
        ap.usable_quantity,
        ap.receive_time,
        0 pick_flag,
        1 IS_WHOLE,
        ap.base_quantity,
        ap.creation_time
        from AC_PICK ap
        inner join bd_product bp
        on bp.pk_id = ap.pk_data_id
        where ap.del_flag = 0
        and ap.pk_country = #{pkCountry}
        and ap.pk_member = #{pkMember}
        and ap.pick_type = 0
        and ap.usable_quantity > 0
        and (ap.receive_time is null or to_char(ap.receive_time, 'yyyy-mm-dd') >= to_char(sysdate, 'yyyy-mm-dd'))
        <!-- 迁移抽奖、新抽奖 -->
        union all
        select ap.pk_id,
        ap.pick_type,
        case
        when ap.pick_cover is not null then
        ap.pick_cover
        else
        (select bp.cover from bd_product bp where bp.pk_id = ap.pk_data_id)
        end pick_cover,
        ap.usable_quantity,
        ap.receive_time,
        0 pick_flag,
        0 IS_WHOLE,
        ap.base_quantity,
        ap.creation_time
        from AC_PICK ap
        where ap.del_flag = 0
        and ap.pk_country = #{pkCountry}
        and ap.pk_member = #{pkMember}
        and ap.pick_type in (1, 17)
        and ap.usable_quantity > 0
        and (ap.receive_time is null or to_char(ap.receive_time, 'yyyy-mm-dd hh24:mi:ss') >= to_char(sysdate, 'yyyy-mm-dd hh24:mi:ss'))
        <!-- 迁移创客沙棘提货、复购提货 -->
        union all
        select ap.pk_id,
        ap.pick_type,
        case
        when ap.pick_cover is not null then
        ap.pick_cover
        else
        (select bp.cover from bd_product bp where bp.pk_id = ap.pk_data_id)
        end pick_cover,
        ap.usable_quantity,
        ap.receive_time,
        0 pick_flag,
        1 IS_WHOLE,
        ap.base_quantity,
        ap.creation_time
        from AC_PICK ap
        where ap.del_flag = 0
        and ap.pk_country = #{pkCountry}
        and ap.pk_member = #{pkMember}
        and ap.pick_type in (5, 7)
        and ap.usable_quantity > 0
        and (ap.receive_time is null or to_char(ap.receive_time, 'yyyy-mm-dd') >= to_char(sysdate, 'yyyy-mm-dd'))
        <!-- 迁移乐学易考代金券 -->
        union all
        select ap.pk_id,
        ap.pick_type,
        case
        when ap.pick_cover is not null then
        ap.pick_cover
        else
        (select bp.cover from bd_product bp where bp.pk_id = ap.pk_data_id)
        end pick_cover,
        ap.usable_quantity,
        ap.receive_time,
        1 pick_flag,
        0 IS_WHOLE,
        ap.base_quantity,
        ap.creation_time
        from AC_PICK ap
        where ap.del_flag = 0
        and ap.pk_country = #{pkCountry}
        and ap.pk_member = #{pkMember}
        and ap.pick_type = 8
        and ap.usable_quantity > 0
        and (ap.receive_time is null or to_char(ap.receive_time, 'yyyy-mm-dd') >= to_char(sysdate, 'yyyy-mm-dd'))
        <!-- 消费赠送、直推赠送、超级工作室赠送、节日赠送 -->
        union all
        select ap.pk_id,
        ap.pick_type,
        ap.pick_cover,
        ap.usable_quantity,
        ap.receive_time,
        0 pick_flag,
        nvl(apuc.is_whole, 1) is_whole,
        ap.base_quantity,
        ap.creation_time
        from AC_PICK ap
        left join AC_PICK_UP_CONFIG apuc
        on apuc.pk_base_id = ap.pk_base_config
        and apuc.del_flag = 0
        where ap.del_flag = 0
        and ap.pk_country = #{pkCountry}
        and ap.pk_member = #{pkMember}
        and ap.pick_type in (12, 13, 14, 15)
        and ap.usable_quantity > 0
        and (ap.receive_time is null or to_char(ap.receive_time, 'yyyy-mm-dd') >= to_char(sysdate, 'yyyy-mm-dd'))
        <!-- 排序 -->
        order by creation_time
    </select>

</mapper>
