<?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.sale.order.mapper.SaOrderWaresLimitMapper">

    <!-- 查询会员已经购买的限购数据 -->
    <select id="listWaresList" resultType="com.hzs.common.domain.sale.ext.SaOrderWaresLimitExt">
        select sowl.pk_wares, sowl.wares_quantity, bw.wares_code, bw.wares_name
        from sa_order_wares_limit sowl
        inner join bd_wares bw
        on bw.special_area = sowl.order_type
        and bw.pk_id = sowl.pk_wares
        and bw.pk_country = sowl.pk_country
        and bw.del_flag = 0
        where sowl.del_flag = 0
        and sowl.pk_member = #{pkMember}
        and sowl.order_type = #{orderType}
        and sowl.limit_date = #{limitDate}
        and bw.wares_code in
        <foreach collection="waresCodeList" item="item" open="(" close=")" separator=",">
            #{item}
        </foreach>
    </select>

    <!-- 批量插入或更新会员商品限购 -->
    <insert id="saveBatchLimit">
        merge into sa_order_wares_limit sowl
        using (
        <foreach collection="dataList" item="item" separator=" union all ">
            select #{item.pkMember} PK_MEMBER,
            #{item.orderType} ORDER_TYPE,
            #{item.pkWares} PK_WARES,
            #{item.waresQuantity} WARES_QUANTITY,
            #{item.limitDate} LIMIT_DATE,
            #{item.pkCountry} PK_COUNTRY
            from dual
        </foreach>
        ) tmp
        on (sowl.pk_member = tmp.pk_member and sowl.order_type = tmp.order_type and sowl.pk_wares = tmp.pk_wares and sowl.limit_date = tmp.limit_date and sowl.del_flag = 0)
        WHEN MATCHED THEN
        update set sowl.WARES_QUANTITY = sowl.WARES_QUANTITY + tmp.WARES_QUANTITY
        WHEN NOT MATCHED THEN
        insert
        (PK_ID,
        PK_MEMBER,
        ORDER_TYPE,
        PK_WARES,
        WARES_QUANTITY,
        LIMIT_DATE,
        PK_COUNTRY,
        PK_CREATOR)
        values
        (sa_order_wares_limit_seq.nextval,
        tmp.pk_member,
        tmp.order_type,
        tmp.pk_wares,
        tmp.wares_quantity,
        tmp.limit_date,
        tmp.pk_country,
        tmp.pk_member)
    </insert>

    <!-- 撤单更新限购数量 -->
    <update id="updateCancelOrder">
        merge into SA_ORDER_WARES_LIMIT sowl
        using (select so.pk_member PK_MEMBER,
        so.order_type ORDER_TYPE,
        soi.pk_wares PK_WARES,
        sum(soi.wares_quantity) WARES_QUANTITY,
        to_char(so.pay_time, 'yyyymm') LIMIT_DATE
        from sa_order so
        left join sa_order_items soi
        on soi.pk_order = so.pk_id
        and soi.is_gift = 1
        and to_char(soi.modified_time, 'yyyy-mm-dd hh24:mi:ss') = to_char(#{modifiedTime}, 'yyyy-mm-dd hh24:mi:ss')
        left join bd_wares bw
        on bw.pk_id = soi.pk_wares
        where so.order_status = 1
        and so.pk_id = #{pkOrder}
        and so.pk_member = #{pkMember}
        and to_char(so.modified_time, 'yyyy-mm-dd hh24:mi:ss') = to_char(#{modifiedTime}, 'yyyy-mm-dd hh24:mi:ss')
        and bw.wares_code in
        <foreach collection="waresCodeList" item="item" open="(" close=")" separator=",">
            #{item}
        </foreach>
        group by so.pk_member, so.order_type, soi.pk_wares, so.pay_time) tmp
        on (sowl.pk_member = tmp.pk_member and sowl.order_type = tmp.order_type and sowl.pk_wares = tmp.pk_wares and sowl.limit_date = tmp.limit_date and sowl.del_flag = 0)
        when matched then
        update set sowl.wares_quantity = sowl.wares_quantity - tmp.wares_quantity
    </update>

</mapper>
