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

    <!-- 插入提货数据 -->
    <insert id="insertPickLog">
        merge into AC_RETAIL_PICK_LOG arpl
        using (SELECT sum(soi.WARES_QUANTITY) quantity, so.pk_member pk_member
        FROM sa_order so
        INNER JOIN SA_ORDER_ITEMS soi
        ON so.pk_id = soi.pk_order
        WHERE so.del_flag = 0
        and so.order_status = 1
        and so.order_type in (41, 42, 43)
        and so.system_type = #{systemType}
        and soi.del_flag = 0
        and soi.is_gift = 1
        and soi.pk_wares in
        <foreach collection="pkWaresList" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
        and so.pay_time >= #{startDate, jdbcType=DATE}
        and so.pay_time &lt; #{endDate, jdbcType=DATE}
        group by so.pk_member
        having sum(soi.WARES_QUANTITY) >= #{waresQuantity}) tmp
        on (arpl.del_flag = 0 and arpl.PK_MEMBER = tmp.pk_member and arpl.PK_BASE_ID = #{pkBaseId})
        when not matched then
        insert (
        PK_ID,
        PK_BASE_ID,
        PK_MEMBER,
        WARES_QUANTITY,
        GIFT_QUANTITY,
        PK_CREATOR,
        CREATION_TIME)
        values (
        AC_RETAIL_PICK_LOG_SEQ.Nextval,
        #{pkBaseId},
        tmp.pk_member,
        tmp.quantity,
        trunc(tmp.quantity / #{waresQuantity}) * #{giftQuantity},
        100000000,
        #{createDate})
    </insert>

</mapper>
