<?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.tuling.tulingmall.ordercurr.dao.PortalOrderDao">
    <resultMap id="orderDetailMap" type="com.tuling.tulingmall.ordercurr.domain.OmsOrderDetail"
               extends="com.tuling.tulingmall.ordercurr.mapper.OmsOrderMapper.BaseResultMap">
        <collection property="orderItemList" columnPrefix="ot_"
                    resultMap="com.tuling.tulingmall.ordercurr.mapper.OmsOrderItemMapper.BaseResultMap">
        </collection>
    </resultMap>
    <select id="getDetail" resultMap="orderDetailMap">
        SELECT
        o.id,
        o.status,
        o.total_amount,
        o.pay_amount,
        o.order_sn,
        o.member_id,
        o.pay_type,
        ot.id ot_id,
        ot.product_id ot_product_id,
        ot.product_name ot_product_name,
        ot.product_brand ot_product_brand,
        ot.product_pic ot_product_pic,
        ot.product_price ot_product_price,
        ot.product_sku_id ot_product_sku_id,
        ot.product_sku_code ot_product_sku_code,
        ot.product_quantity ot_product_quantity,
        ot.product_attr ot_product_attr
        FROM
        oms_order o
        LEFT JOIN
        oms_order_item ot ON o.id = ot.order_id
        WHERE
           o.delete_status = 0 and 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
        FROM
            oms_order o
            LEFT JOIN oms_order_item ot ON o.id = ot.order_id
        WHERE
            o.status = 0
            AND o.create_time &lt; date_add(NOW(), INTERVAL -#{minute} MINUTE);
    </select>
    <update id="updateOrderStatus">
        update oms_order
        set status=#{status},gmt_modified=now()
        where id in
        <foreach collection="ids" item="id" separator="," open="(" close=")">
            #{id}
        </foreach>
    </update>
    <update id="deleteOrder">
        update oms_order
        set delete_status = 1,gmt_modified=now()
        WHERE
        status >= 3 and delete_status = 0 and id=#{orderId}
    </update>
    <select id="findMemberOrderList" resultMap="orderDetailMap">
        SELECT
		o.id,
		o.status,
		o.total_amount,
		o.pay_amount,
		o.order_sn,
		o.member_id,
		ot.id ot_id,
		ot.product_id ot_product_id,
		ot.product_name ot_product_name,
		ot.product_pic ot_product_pic,
		ot.product_price ot_product_price,
		ot.product_sku_id ot_product_sku_id,
		ot.product_sku_code ot_product_sku_code,
		ot.product_quantity ot_product_quantity,
		ot.product_attr ot_product_attr
        FROM
		  oms_order o
		LEFT JOIN
		  oms_order_item ot ON o.id = ot.order_id
        WHERE
          o.delete_status = 0 and o.member_id=#{memberId}
        <if test="status != null">
            and o.status=#{status}
        </if>
          ORDER BY o.create_time desc
    </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,
        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>
</mapper>