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

    <!-- 更新提货数量 -->
    <update id="updatePickQuantity">
        update ac_retail_pick arp
        set arp.usable_quantity = arp.usable_quantity - #{quantity},
        arp.used_quantity = arp.used_quantity + #{quantity},
        arp.pk_modified = #{pkModified},
        arp.modified_time = sysdate
        where arp.del_flag = 0
        and arp.pk_id = #{pkId}
        <if test="pkMember != null">
            and arp.pk_member = #{pkMember}
        </if>
        and arp.usable_quantity - #{quantity} >= 0
        and arp.used_quantity + #{quantity} >= 0
    </update>

    <!-- 提货批量操作 -->
    <update id="handlePick">
        merge into AC_RETAIL_PICK arp
            using (select *
                   from AC_RETAIL_PICK_LOG arpl
                   where arpl.del_flag = 0
                     and arpl.pk_base_id = #{pkBaseId}
                     and to_char(arpl.creation_time, 'yyyy-mm-dd hh24:mi:ss') = to_char(#{nowDate}, 'yyyy-mm-dd hh24:mi:ss')) tmp
            on (arp.pk_member = tmp.pk_member and arp.del_flag = 0)
            when not matched then
                insert
                    (PK_ID, PK_MEMBER, USABLE_QUANTITY, PK_CREATOR, CREATION_TIME)
                    values (AC_RETAIL_PICK_SEQ.Nextval,
                            tmp.pk_member,
                            tmp.gift_quantity,
                            tmp.PK_CREATOR,
                            tmp.CREATION_TIME)
            when matched then
                update set arp.usable_quantity = arp.usable_quantity + tmp.gift_quantity
    </update>

    <!-- 列表数据 -->
    <select id="listData" resultType="com.hzs.common.domain.activity.pick.ext.AcRetailPickExt">
        select arp.*, cm.member_code, cm.member_name
        from ac_retail_pick arp
        left join cu_member cm
        on cm.pk_id = arp.pk_member
        where arp.del_flag = 0
        and arp.pk_country = #{param.pkCountry}
        and arp.system_type = #{param.systemType}
        <if test="param.memberCode != null and param.memberCode != ''">
            and cm.member_code = #{param.memberCode}
        </if>
        order by arp.creation_time desc
    </select>

</mapper>
