<?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.yonge.cooleshow.portal.dao.PortalOrderDao">
    <resultMap id="orderDetailMap" type="com.yonge.cooleshow.portal.domain.OmsOrderDetail"
               extends="com.yonge.cooleshow.mbg.mapper.OmsOrderMapper.BaseResultMap">
        <collection property="orderItemList" columnPrefix="ot_"
                    resultMap="com.yonge.cooleshow.mbg.mapper.OmsOrderItemMapper.BaseResultMap">
        </collection>
    </resultMap>
    <select id="getDetail" resultMap="orderDetailMap">
        SELECT
            o.*,
            ot.id ot_id,
        ot.product_id ot_product_id,
            ot.product_name ot_product_name,
            ot.product_sku_id ot_product_sku_id,
            ot.product_sku_code ot_product_sku_code,
            ot.product_quantity ot_product_quantity,
        o.status
        FROM
            oms_order o
            LEFT JOIN oms_order_item ot ON o.id = ot.order_id
        WHERE
            o.id = #{orderId}
    </select>

    <select id="getTimeOutOrders" resultMap="orderDetailMap">
        SELECT
            o.id,
            o.order_sn,
            o.coupon_id,
            o.integration,
            o.member_id,
            o.use_integration,
            ot.id               ot_id,
            ot.product_name     ot_product_name,
            ot.product_sku_id   ot_product_sku_id,
            ot.product_sku_code ot_product_sku_code,
            ot.product_quantity ot_product_quantity,
            o.status
        FROM
            oms_order o
            LEFT JOIN oms_order_item ot ON o.id = ot.order_id
        left join user_order_payment uop on uop.order_no_ = o.order_sn and uop.status_ = 'pending'
        WHERE
            (o.status = 6 AND o.create_time &lt; date_add(NOW(), INTERVAL -#{orderMinute} MINUTE))
            or (o.status = 0 and uop.create_time_ &lt; date_add(NOW(), INTERVAL -#{payMinute} MINUTE));
    </select>

    <update id="updateSkuStock">
        UPDATE pms_sku_stock
        SET
            stock = CASE id
            <foreach collection="itemList" item="item">
              WHEN #{item.productSkuId} THEN stock - #{item.productQuantity}
            </foreach>
            END,
            sale = CASE id
            <foreach collection="itemList" item="item">
                WHEN #{item.productSkuId} THEN pms_sku_stock.sale + #{item.productQuantity}
            </foreach>
            END,
            lock_stock = CASE id
            <foreach collection="itemList" item="item">
              WHEN #{item.productSkuId} THEN lock_stock - #{item.productQuantity}
            </foreach>
            END
        WHERE
            id IN
        <foreach collection="itemList" item="item" separator="," open="(" close=")">
            #{item.productSkuId}
        </foreach>
    </update>
    <update id="updateOrderStatus">
        update oms_order
        set status=#{status}
        where id in
        <foreach collection="ids" item="id" separator="," open="(" close=")">
            #{id}
        </foreach>
    </update>
    <update id="releaseSkuStockLock">
        UPDATE pms_sku_stock
        SET
        lock_stock = CASE id
        <foreach collection="itemList" item="item">
            WHEN #{item.productSkuId} THEN lock_stock - #{item.productQuantity}
        </foreach>
        END
        WHERE
        id IN
        <foreach collection="itemList" item="item" separator="," open="(" close=")">
            #{item.productSkuId}
        </foreach>
    </update>


    <update id="releaseProductStockLock">
        UPDATE pms_product
        SET
        lock_stock = CASE id
        <foreach collection="itemList" item="item">
            WHEN #{item.productId} THEN lock_stock - #{item.stock}
        </foreach>
        END
        WHERE
        id IN
        <foreach collection="itemList" item="item" separator="," open="(" close=")">
            #{item.productId}
        </foreach>
    </update>

    <update id="updateRefundSkuStock">
        UPDATE pms_sku_stock
        SET
        stock = CASE id
        <foreach collection="itemList" item="item">
            WHEN #{item.productSkuId} THEN stock + #{item.productQuantity}
        </foreach>
        END,
        sale = CASE id
        <foreach collection="itemList" item="item">
            WHEN #{item.productSkuId} THEN pms_sku_stock.sale - #{item.productQuantity}
        </foreach>
        END
        WHERE
        id IN
        <foreach collection="itemList" item="item" separator="," open="(" close=")">
            #{item.productSkuId}
        </foreach>
    </update>
</mapper>