<?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.bailuntec.mapper.DcMidTransitMapper">
  <resultMap id="BaseResultMap" type="com.bailuntec.domain.entity.DcMidTransit">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
    -->
    <id column="id" jdbcType="INTEGER" property="id" />
    <result column="bailun_sku" jdbcType="VARCHAR" property="bailunSku" />
    <result column="warehouse_code" jdbcType="VARCHAR" property="warehouseCode" />
    <result column="quantity_purchase" jdbcType="INTEGER" property="quantityPurchase" />
    <result column="gmt_purchase_modified" jdbcType="TIMESTAMP" property="gmtPurchaseModified" />
    <result column="quantity_transfer" jdbcType="INTEGER" property="quantityTransfer" />
    <result column="gmt_transfer_modified" jdbcType="TIMESTAMP" property="gmtTransferModified" />
    <result column="quantity_out_stock" jdbcType="INTEGER" property="quantityOutStock" />
    <result column="gmt_out_stock_modified" jdbcType="TIMESTAMP" property="gmtOutStockModified" />
  </resultMap>
  <sql id="Example_Where_Clause">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
    -->
    <where>
      <foreach collection="oredCriteria" item="criteria" separator="or">
        <if test="criteria.valid">
          <trim prefix="(" prefixOverrides="and" suffix=")">
            <foreach collection="criteria.criteria" item="criterion">
              <choose>
                <when test="criterion.noValue">
                  and ${criterion.condition}
                </when>
                <when test="criterion.singleValue">
                  and ${criterion.condition} #{criterion.value}
                </when>
                <when test="criterion.betweenValue">
                  and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
                </when>
                <when test="criterion.listValue">
                  and ${criterion.condition}
                  <foreach close=")" collection="criterion.value" item="listItem" open="(" separator=",">
                    #{listItem}
                  </foreach>
                </when>
              </choose>
            </foreach>
          </trim>
        </if>
      </foreach>
    </where>
  </sql>
  <sql id="Update_By_Example_Where_Clause">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
    -->
    <where>
      <foreach collection="example.oredCriteria" item="criteria" separator="or">
        <if test="criteria.valid">
          <trim prefix="(" prefixOverrides="and" suffix=")">
            <foreach collection="criteria.criteria" item="criterion">
              <choose>
                <when test="criterion.noValue">
                  and ${criterion.condition}
                </when>
                <when test="criterion.singleValue">
                  and ${criterion.condition} #{criterion.value}
                </when>
                <when test="criterion.betweenValue">
                  and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
                </when>
                <when test="criterion.listValue">
                  and ${criterion.condition}
                  <foreach close=")" collection="criterion.value" item="listItem" open="(" separator=",">
                    #{listItem}
                  </foreach>
                </when>
              </choose>
            </foreach>
          </trim>
        </if>
      </foreach>
    </where>
  </sql>
  <sql id="Base_Column_List">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
    -->
    id, bailun_sku, warehouse_code, quantity_purchase, gmt_purchase_modified, quantity_transfer, 
    gmt_transfer_modified, quantity_out_stock, gmt_out_stock_modified
  </sql>
  <select id="selectByExample" parameterType="com.bailuntec.domain.example.DcMidTransitExample" resultMap="BaseResultMap">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
    -->
    select
    <if test="distinct">
      distinct
    </if>
    <include refid="Base_Column_List" />
    from dc_mid_transit
    <if test="_parameter != null">
      <include refid="Example_Where_Clause" />
    </if>
    <if test="orderByClause != null">
      order by ${orderByClause}
    </if>
    <if test="rows != null">
      <if test="offset != null">
        limit ${offset}, ${rows}
      </if>
      <if test="offset == null">
        limit ${rows}
      </if>
    </if>
  </select>
  <select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
    -->
    select 
    <include refid="Base_Column_List" />
    from dc_mid_transit
    where id = #{id,jdbcType=INTEGER}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
    -->
    delete from dc_mid_transit
    where id = #{id,jdbcType=INTEGER}
  </delete>
  <delete id="deleteByExample" parameterType="com.bailuntec.domain.example.DcMidTransitExample">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
    -->
    delete from dc_mid_transit
    <if test="_parameter != null">
      <include refid="Example_Where_Clause" />
    </if>
  </delete>
  <insert id="insert" parameterType="com.bailuntec.domain.entity.DcMidTransit">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
    -->
    insert into dc_mid_transit (id, bailun_sku, warehouse_code, 
      quantity_purchase, gmt_purchase_modified, 
      quantity_transfer, gmt_transfer_modified, 
      quantity_out_stock, gmt_out_stock_modified
      )
    values (#{id,jdbcType=INTEGER}, #{bailunSku,jdbcType=VARCHAR}, #{warehouseCode,jdbcType=VARCHAR}, 
      #{quantityPurchase,jdbcType=INTEGER}, #{gmtPurchaseModified,jdbcType=TIMESTAMP},
      #{quantityTransfer,jdbcType=INTEGER}, #{gmtTransferModified,jdbcType=TIMESTAMP},
      #{quantityOutStock,jdbcType=INTEGER}, #{gmtOutStockModified,jdbcType=TIMESTAMP}
      )
  </insert>
  <insert id="insertSelective" parameterType="com.bailuntec.domain.entity.DcMidTransit">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
    -->
    insert into dc_mid_transit
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="id != null">
        id,
      </if>
      <if test="bailunSku != null">
        bailun_sku,
      </if>
      <if test="warehouseCode != null">
        warehouse_code,
      </if>
      <if test="quantityPurchase != null">
        quantity_purchase,
      </if>
      <if test="gmtPurchaseModified != null">
        gmt_purchase_modified,
      </if>
      <if test="quantityTransfer != null">
        quantity_transfer,
      </if>
      <if test="gmtTransferModified != null">
        gmt_transfer_modified,
      </if>
      <if test="quantityOutStock != null">
        quantity_out_stock,
      </if>
      <if test="gmtOutStockModified != null">
        gmt_out_stock_modified,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
      <if test="id != null">
        #{id,jdbcType=INTEGER},
      </if>
      <if test="bailunSku != null">
        #{bailunSku,jdbcType=VARCHAR},
      </if>
      <if test="warehouseCode != null">
        #{warehouseCode,jdbcType=VARCHAR},
      </if>
      <if test="quantityPurchase != null">
        #{quantityPurchase,jdbcType=INTEGER},
      </if>
      <if test="gmtPurchaseModified != null">
        #{gmtPurchaseModified,jdbcType=TIMESTAMP},
      </if>
      <if test="quantityTransfer != null">
        #{quantityTransfer,jdbcType=INTEGER},
      </if>
      <if test="gmtTransferModified != null">
        #{gmtTransferModified,jdbcType=TIMESTAMP},
      </if>
      <if test="quantityOutStock != null">
        #{quantityOutStock,jdbcType=INTEGER},
      </if>
      <if test="gmtOutStockModified != null">
        #{gmtOutStockModified,jdbcType=TIMESTAMP},
      </if>
    </trim>
  </insert>
  <select id="countByExample" parameterType="com.bailuntec.domain.example.DcMidTransitExample" resultType="java.lang.Long">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
    -->
    select count(*) from dc_mid_transit
    <if test="_parameter != null">
      <include refid="Example_Where_Clause" />
    </if>
  </select>
  <update id="updateByExampleSelective" parameterType="map">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
    -->
    update dc_mid_transit
    <set>
      <if test="record.id != null">
        id = #{record.id,jdbcType=INTEGER},
      </if>
      <if test="record.bailunSku != null">
        bailun_sku = #{record.bailunSku,jdbcType=VARCHAR},
      </if>
      <if test="record.warehouseCode != null">
        warehouse_code = #{record.warehouseCode,jdbcType=VARCHAR},
      </if>
      <if test="record.quantityPurchase != null">
        quantity_purchase = #{record.quantityPurchase,jdbcType=INTEGER},
      </if>
      <if test="record.gmtPurchaseModified != null">
        gmt_purchase_modified = #{record.gmtPurchaseModified,jdbcType=TIMESTAMP},
      </if>
      <if test="record.quantityTransfer != null">
        quantity_transfer = #{record.quantityTransfer,jdbcType=INTEGER},
      </if>
      <if test="record.gmtTransferModified != null">
        gmt_transfer_modified = #{record.gmtTransferModified,jdbcType=TIMESTAMP},
      </if>
      <if test="record.quantityOutStock != null">
        quantity_out_stock = #{record.quantityOutStock,jdbcType=INTEGER},
      </if>
      <if test="record.gmtOutStockModified != null">
        gmt_out_stock_modified = #{record.gmtOutStockModified,jdbcType=TIMESTAMP},
      </if>
    </set>
    <if test="_parameter != null">
      <include refid="Update_By_Example_Where_Clause" />
    </if>
  </update>
  <update id="updateByExample" parameterType="map">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
    -->
    update dc_mid_transit
    set id = #{record.id,jdbcType=INTEGER},
      bailun_sku = #{record.bailunSku,jdbcType=VARCHAR},
      warehouse_code = #{record.warehouseCode,jdbcType=VARCHAR},
      quantity_purchase = #{record.quantityPurchase,jdbcType=INTEGER},
      gmt_purchase_modified = #{record.gmtPurchaseModified,jdbcType=TIMESTAMP},
      quantity_transfer = #{record.quantityTransfer,jdbcType=INTEGER},
      gmt_transfer_modified = #{record.gmtTransferModified,jdbcType=TIMESTAMP},
      quantity_out_stock = #{record.quantityOutStock,jdbcType=INTEGER},
      gmt_out_stock_modified = #{record.gmtOutStockModified,jdbcType=TIMESTAMP}
    <if test="_parameter != null">
      <include refid="Update_By_Example_Where_Clause" />
    </if>
  </update>
  <update id="updateByPrimaryKeySelective" parameterType="com.bailuntec.domain.entity.DcMidTransit">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
    -->
    update dc_mid_transit
    <set>
      <if test="bailunSku != null">
        bailun_sku = #{bailunSku,jdbcType=VARCHAR},
      </if>
      <if test="warehouseCode != null">
        warehouse_code = #{warehouseCode,jdbcType=VARCHAR},
      </if>
      <if test="quantityPurchase != null">
        quantity_purchase = #{quantityPurchase,jdbcType=INTEGER},
      </if>
      <if test="gmtPurchaseModified != null">
        gmt_purchase_modified = #{gmtPurchaseModified,jdbcType=TIMESTAMP},
      </if>
      <if test="quantityTransfer != null">
        quantity_transfer = #{quantityTransfer,jdbcType=INTEGER},
      </if>
      <if test="gmtTransferModified != null">
        gmt_transfer_modified = #{gmtTransferModified,jdbcType=TIMESTAMP},
      </if>
      <if test="quantityOutStock != null">
        quantity_out_stock = #{quantityOutStock,jdbcType=INTEGER},
      </if>
      <if test="gmtOutStockModified != null">
        gmt_out_stock_modified = #{gmtOutStockModified,jdbcType=TIMESTAMP},
      </if>
    </set>
    where id = #{id,jdbcType=INTEGER}
  </update>
  <update id="updateByPrimaryKey" parameterType="com.bailuntec.domain.entity.DcMidTransit">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
    -->
    update dc_mid_transit
    set bailun_sku = #{bailunSku,jdbcType=VARCHAR},
      warehouse_code = #{warehouseCode,jdbcType=VARCHAR},
      quantity_purchase = #{quantityPurchase,jdbcType=INTEGER},
      gmt_purchase_modified = #{gmtPurchaseModified,jdbcType=TIMESTAMP},
      quantity_transfer = #{quantityTransfer,jdbcType=INTEGER},
      gmt_transfer_modified = #{gmtTransferModified,jdbcType=TIMESTAMP},
      quantity_out_stock = #{quantityOutStock,jdbcType=INTEGER},
      gmt_out_stock_modified = #{gmtOutStockModified,jdbcType=TIMESTAMP}
    where id = #{id,jdbcType=INTEGER}
  </update>
  <insert id="upsertSelective" parameterType="com.bailuntec.domain.entity.DcMidTransit">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      @project https://github.com/itfsw/mybatis-generator-plugin
    -->
    insert into dc_mid_transit
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="id != null">
        id,
      </if>
      <if test="bailunSku != null">
        bailun_sku,
      </if>
      <if test="warehouseCode != null">
        warehouse_code,
      </if>
      <if test="quantityPurchase != null">
        quantity_purchase,
      </if>
      <if test="gmtPurchaseModified != null">
        gmt_purchase_modified,
      </if>
      <if test="quantityTransfer != null">
        quantity_transfer,
      </if>
      <if test="gmtTransferModified != null">
        gmt_transfer_modified,
      </if>
      <if test="quantityOutStock != null">
        quantity_out_stock,
      </if>
      <if test="gmtOutStockModified != null">
        gmt_out_stock_modified,
      </if>
    </trim>
    values
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="id != null">
        #{id,jdbcType=INTEGER},
      </if>
      <if test="bailunSku != null">
        #{bailunSku,jdbcType=VARCHAR},
      </if>
      <if test="warehouseCode != null">
        #{warehouseCode,jdbcType=VARCHAR},
      </if>
      <if test="quantityPurchase != null">
        #{quantityPurchase,jdbcType=INTEGER},
      </if>
      <if test="gmtPurchaseModified != null">
        #{gmtPurchaseModified,jdbcType=TIMESTAMP},
      </if>
      <if test="quantityTransfer != null">
        #{quantityTransfer,jdbcType=INTEGER},
      </if>
      <if test="gmtTransferModified != null">
        #{gmtTransferModified,jdbcType=TIMESTAMP},
      </if>
      <if test="quantityOutStock != null">
        #{quantityOutStock,jdbcType=INTEGER},
      </if>
      <if test="gmtOutStockModified != null">
        #{gmtOutStockModified,jdbcType=TIMESTAMP},
      </if>
    </trim>
    on duplicate key update 
    <trim suffixOverrides=",">
      <if test="id != null">
        id = #{id,jdbcType=INTEGER},
      </if>
      <if test="bailunSku != null">
        bailun_sku = #{bailunSku,jdbcType=VARCHAR},
      </if>
      <if test="warehouseCode != null">
        warehouse_code = #{warehouseCode,jdbcType=VARCHAR},
      </if>
      <if test="quantityPurchase != null">
        quantity_purchase = #{quantityPurchase,jdbcType=INTEGER},
      </if>
      <if test="gmtPurchaseModified != null">
        gmt_purchase_modified = #{gmtPurchaseModified,jdbcType=TIMESTAMP},
      </if>
      <if test="quantityTransfer != null">
        quantity_transfer = #{quantityTransfer,jdbcType=INTEGER},
      </if>
      <if test="gmtTransferModified != null">
        gmt_transfer_modified = #{gmtTransferModified,jdbcType=TIMESTAMP},
      </if>
      <if test="quantityOutStock != null">
        quantity_out_stock = #{quantityOutStock,jdbcType=INTEGER},
      </if>
      <if test="gmtOutStockModified != null">
        gmt_out_stock_modified = #{gmtOutStockModified,jdbcType=TIMESTAMP},
      </if>
    </trim>
  </insert>
  <insert id="upsert" parameterType="com.bailuntec.domain.entity.DcMidTransit">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      @project https://github.com/itfsw/mybatis-generator-plugin
    -->
    insert into dc_mid_transit
    (id, bailun_sku, warehouse_code, quantity_purchase, gmt_purchase_modified, quantity_transfer, 
      gmt_transfer_modified, quantity_out_stock, gmt_out_stock_modified)
    values
    (#{id,jdbcType=INTEGER}, #{bailunSku,jdbcType=VARCHAR}, #{warehouseCode,jdbcType=VARCHAR}, 
      #{quantityPurchase,jdbcType=INTEGER}, #{gmtPurchaseModified,jdbcType=TIMESTAMP},
      #{quantityTransfer,jdbcType=INTEGER}, #{gmtTransferModified,jdbcType=TIMESTAMP},
      #{quantityOutStock,jdbcType=INTEGER}, #{gmtOutStockModified,jdbcType=TIMESTAMP}
      )
    on duplicate key update 
    id = #{id,jdbcType=INTEGER}, 
    bailun_sku = #{bailunSku,jdbcType=VARCHAR}, 
    warehouse_code = #{warehouseCode,jdbcType=VARCHAR}, 
    quantity_purchase = #{quantityPurchase,jdbcType=INTEGER}, 
    gmt_purchase_modified = #{gmtPurchaseModified,jdbcType=TIMESTAMP},
    quantity_transfer = #{quantityTransfer,jdbcType=INTEGER}, 
    gmt_transfer_modified = #{gmtTransferModified,jdbcType=TIMESTAMP},
    quantity_out_stock = #{quantityOutStock,jdbcType=INTEGER}, 
    gmt_out_stock_modified = #{gmtOutStockModified,jdbcType=TIMESTAMP}
  </insert>
  <select id="selectOneByExample" parameterType="com.bailuntec.domain.example.DcMidTransitExample" resultMap="BaseResultMap">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      @project https://github.com/itfsw/mybatis-generator-plugin
    -->
    select
    <include refid="Base_Column_List" />
    from dc_mid_transit
    <if test="_parameter != null">
      <include refid="Example_Where_Clause" />
    </if>
    <if test="orderByClause != null">
      order by ${orderByClause}
    </if>
    limit 1
  </select>


  <insert id="calculateTransferTransit">
     insert into dc_mid_transit(bailun_sku, warehouse_code, quantity_transfer,gmt_transfer_modified)
      SELECT
          tb5.bailun_sku bailun_sku,
          tb5.warehouse_code warehouse_code,
          ( tb5.count - IFNULL( tb4.count, 0 ) ) AS count,
          CURRENT_TIMESTAMP
      FROM
          (
          SELECT
              tb1.bailun_sku,
              tb1.warehouse_code,
              sum( tb1.count ) count
          FROM
              dc_base_transfer_verify tb1
              LEFT JOIN dc_base_transfer_complete tb2 ON tb1.deleted = tb2.deleted
              AND tb1.bailun_sku = tb2.bailun_sku
              AND tb1.warehouse_code = tb2.warehouse_code
              AND tb1.transfer_order_id = tb2.transfer_order_id
          WHERE
              tb2.bailun_sku IS NULL
              AND tb1.deleted = 0
          GROUP BY
              bailun_sku,
              warehouse_code
          ) tb5
          LEFT JOIN (
          SELECT
              bailun_sku,
              warehouse_code,
              sum( count ) count
          FROM
              (
              SELECT
                  tb1.bailun_sku,
                  tb1.warehouse_code,
                  max( tb1.count ) count,
                  tb1.transfer_order_id
              FROM
                  dc_base_transfer_inbound tb1
                  LEFT JOIN dc_base_transfer_complete tb2 ON tb2.deleted = 0
                  AND tb1.bailun_sku = tb2.bailun_sku
                  AND tb1.warehouse_code = tb2.warehouse_code
                  AND tb1.transfer_order_id = tb2.transfer_order_id
              WHERE
                  tb2.bailun_sku IS NULL
                  AND tb1.deleted = 0
              GROUP BY
                  bailun_sku,
                  warehouse_code,
                  transfer_order_id
              ) tb3
          GROUP BY
              bailun_sku,
              warehouse_code
          ) tb4
          ON tb5.bailun_sku = tb4.bailun_sku
          AND tb5.warehouse_code = tb4.warehouse_code
          WHERE ( tb5.count - IFNULL( tb4.count, 0 ) ) > 0
        ON DUPLICATE KEY 	UPDATE   quantity_transfer = VALUES(quantity_transfer),gmt_transfer_modified =  VALUES(gmt_transfer_modified)
  </insert>

  <insert id="calculatePurchaseTransit">
    insert into dc_mid_transit(bailun_sku, warehouse_code, quantity_purchase,gmt_purchase_modified)
      SELECT tb1.bailun_sku as bailun_sku,tb1.warehouse_code as warehouse_code,sum((tb1.count - IFNULL(tb2.count,0))) as count,CURRENT_TIMESTAMP FROM
      (SELECT bailun_sku, warehouse_code, parent_id, sum(count) as count FROM dc_base_purchase WHERE buy_status  IN ( 0, 1, 2, 3 ) and has_delete=0   GROUP BY bailun_sku,parent_id) tb1
          LEFT JOIN (SELECT bailun_sku, warehouse_code, parent_id, sum(count) as count FROM dc_base_purchase_inbound  GROUP BY bailun_sku,parent_id) tb2
      on  tb1.bailun_sku = tb2.bailun_sku
      AND tb1.parent_id = tb2.parent_id
       GROUP BY tb1.bailun_sku, tb1.warehouse_code HAVING count>0
    ON DUPLICATE KEY UPDATE  quantity_purchase = VALUES(quantity_purchase), gmt_purchase_modified =  VALUES(gmt_purchase_modified)
  </insert>

  <insert id="insertMidDailyTransit">
    INSERT into dc_daily_transit (
    `bailun_sku` ,
    `warehouse_code`,
    `quantity_purchase` ,
    `quantity_transfer`,
      `record_time`)
      SELECT  `bailun_sku` ,
    `warehouse_code`,
    `quantity_purchase` ,
    `quantity_transfer`, DATE_SUB( CURDATE( ), INTERVAL 1 DAY )
      FROM dc_mid_transit
  </insert>

  <insert id="calculateOutStock">
    insert into dc_mid_transit(bailun_sku, warehouse_code, quantity_out_stock,gmt_out_stock_modified)
    SELECT
    t1.bailun_sku,
    t1.warehouse_code,
		t1.bailun_sku_quantity_ordered - t1.bailun_sku_quantity_picked - t2.usable_stock,
    CURRENT_TIMESTAMP
    FROM
    (
    SELECT
    bailun_sku,warehouse_code,SUM(bailun_sku_quantity_ordered) AS bailun_sku_quantity_ordered,SUM(bailun_sku_quantity_picked) AS bailun_sku_quantity_picked
    FROM
    dc_base_oms_sku
    WHERE
        bailun_order_status = 'Handling'
        and bailun_interception_status in('None','Failed')
		and warehouse_code != ''
		and platform_type != 'FBA'
		and has_delete = 0
		and has_scalp = 0
        and paid_time >= '2019-03-01 12:00:00'
	    and bailun_sku_quantity_ordered > bailun_sku_quantity_picked
    GROUP BY
    bailun_sku,
    warehouse_code
    ) t1 INNER JOIN dc_base_stock t2 ON t1.bailun_sku=t2.bailun_sku and t1.warehouse_code=t2.warehouse_code
	WHERE (t1.bailun_sku_quantity_ordered - t1.bailun_sku_quantity_picked - t2.usable_stock) >= 0
    ON DUPLICATE KEY 	UPDATE  quantity_out_stock = VALUES(quantity_out_stock),gmt_out_stock_modified =  VALUES(gmt_out_stock_modified)
  </insert>
  <update id="updateOldOutStockToZero">
    update  dc_mid_transit set quantity_out_stock =0 where gmt_out_stock_modified &lt; #{outStockModified,jdbcType=TIMESTAMP}
  </update>
  <update id="updateOldTransferToZero">
    update  dc_mid_transit set quantity_transfer =0 where gmt_transfer_modified &lt; #{transferModified,jdbcType=TIMESTAMP}
  </update>
  <update id="updateOldPurchaseToZero">
    update  dc_mid_transit set quantity_purchase =0 where gmt_purchase_modified &lt; #{purchaseModified,jdbcType=TIMESTAMP}
  </update>
  <update id="updateOldUnshippedToZero">
    update  dc_mid_transit set quantity_unshipped =0 where gmt_unshipped_modified &lt; #{unshippedModified,jdbcType=TIMESTAMP}
  </update>
  <select id="selectMaxOutStockModified" resultType="java.time.LocalDateTime">
    select  max(gmt_out_stock_modified) from  dc_mid_transit
  </select>
  <select id="selectMaxTransferModified" resultType="java.time.LocalDateTime">
    select  max(gmt_transfer_modified) from  dc_mid_transit
  </select>
  <select id="selectMaxPurchaseModified" resultType="java.time.LocalDateTime">
    select  max(gmt_purchase_modified) from  dc_mid_transit
  </select>
  <select id="selectMaxUnshippedModified" resultType="java.time.LocalDateTime">
    select  max(gmt_unshipped_modified) from  dc_mid_transit
  </select>

  <insert id="calculateUnshipped">
    insert into dc_mid_transit(bailun_sku, warehouse_code, quantity_unshipped,gmt_unshipped_modified)
    SELECT
    t1.bailun_sku,
    t1.warehouse_code,
		t1.bailun_sku_quantity_ordered - t1.bailun_sku_quantity_shipped,
    CURRENT_TIMESTAMP
    FROM
    (
    SELECT
    bailun_sku,warehouse_code,SUM(bailun_sku_quantity_ordered) AS bailun_sku_quantity_ordered,SUM(bailun_sku_quantity_shipped) AS bailun_sku_quantity_shipped
    FROM
    dc_base_oms_sku
    WHERE
    bailun_order_status = 'Handling'
    and bailun_interception_status in('None','Failed')
		and warehouse_code != ''
		and platform_type != 'FBA'
		and has_delete = 0
        and has_scalp = 0
        and paid_time >= '2019-03-01 12:00:00'
	    and bailun_sku_quantity_ordered > bailun_sku_quantity_shipped
    GROUP BY
    bailun_sku,
    warehouse_code
    ) t1
	WHERE (t1.bailun_sku_quantity_ordered - t1.bailun_sku_quantity_shipped) >= 0
    ON DUPLICATE KEY 	UPDATE  quantity_unshipped = VALUES(quantity_unshipped),gmt_unshipped_modified =  VALUES(gmt_unshipped_modified)
  </insert>
</mapper>