<?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.tmerclub.cloud.order.mapper.PurchaseOrderItemMapper">
  <resultMap id="purchaseOrderItemMap" type="com.tmerclub.cloud.order.model.PurchaseOrderItem">
    <id column="purchase_item_id" property="purchaseItemId" />
    <result column="spu_id" property="spuId"/>
    <result column="sku_id" property="skuId"/>
    <result column="purchase_amount" property="purchaseAmount"/>
    <result column="purchase_price" property="purchasePrice"/>
    <result column="purchase_stock" property="purchaseStock"/>
    <result column="actual_stock" property="actualStock"/>
    <result column="delivery_type" property="deliveryType"/>
    <result column="status" property="status"/>
    <result column="be_delivered_num" property="beDeliveredNum"/>
  </resultMap>
  <sql id="Vo_Column_List">
    `purchase_item_id`,`supplier_spu_id`,`spu_id`,`sku_id`,`purchase_amount`,`purchase_price`,`purchase_stock`,`actual_stock`,`delivery_type`,`status`,`be_delivered_num`
  </sql>
  <sql id="Join_Column_List">
    poi.`purchase_item_id`,
    poi.`create_time` ,
    poi.`update_time`,
    poi.`spu_id`,
    poi.`sku_id`,
    poi.`spu_code`,
    poi.`party_code`,
    poi.`pic`,
    poi.`purchase_amount`,
    poi.`purchase_price`,
    poi.`purchase_stock`,
    poi.`actual_stock`,
    poi.`delivery_type`,
    poi.`status`,
    poi.`be_delivered_num`
  </sql>
  <select id="list" resultMap="purchaseOrderItemMap">
    select <include refid="Vo_Column_List"/> from purchase_order_item order by purchase_spu_id desc
  </select>
  <select id="getByPurchaseSpuId" resultMap="purchaseOrderItemMap">
    select <include refid="Vo_Column_List"/> from purchase_order_item where purchase_spu_id = #{purchaseSpuId}
  </select>
  <insert id="save">
    insert into purchase_order_item (`purchase_item_id`,`purchase_order_id`,`spu_id`,`sku_id`,`purchase_amount`,`purchase_price`,`purchase_stock`,`actual_stock`,`delivery_type`,`status`,`be_delivered_num`)
    values (#{purchaseOrderItem.purchaseItemId},#{purchaseOrderItem.purchaseOrderId},#{purchaseOrderItem.spuId},#{purchaseOrderItem.skuId},#{purchaseOrderItem.purchaseAmount},#{purchaseOrderItem.purchasePrice},#{purchaseOrderItem.purchaseStock},#{purchaseOrderItem.actualStock},#{purchaseOrderItem.deliveryType},#{purchaseOrderItem.status},#{purchaseOrderItem.beDeliveredNum});
  </insert>
  <insert id="saveBatch" useGeneratedKeys="true" keyProperty="purchaseItemId">
    insert into purchase_order_item (`purchase_item_id`,`purchase_order_id`,`spu_id`,`sku_id`,`spu_code`,`party_code`,`pic`,`purchase_amount`,`purchase_price`,`purchase_stock`,`actual_stock`,`delivery_type`,`status`,`be_delivered_num`) values
    <foreach collection="purchaseOrderItemList" item="purchaseOrderItem" separator=",">
      (#{purchaseOrderItem.purchaseItemId},#{purchaseOrderItem.purchaseOrderId},#{purchaseOrderItem.spuId},#{purchaseOrderItem.skuId},#{purchaseOrderItem.spuCode},#{purchaseOrderItem.partyCode},#{purchaseOrderItem.pic},#{purchaseOrderItem.purchaseAmount},#{purchaseOrderItem.purchasePrice},#{purchaseOrderItem.purchaseStock},#{purchaseOrderItem.actualStock},#{purchaseOrderItem.deliveryType},#{purchaseOrderItem.status},#{purchaseOrderItem.beDeliveredNum})
    </foreach>
  </insert>
  <update id="update">
    update purchase_order_item
    <set>
      <if test="purchaseOrderItem.spuId != null">
        `spu_id` = #{purchaseOrderItem.spuId},
      </if>
      <if test="purchaseOrderItem.skuId != null">
        `sku_id` = #{purchaseOrderItem.skuId},
      </if>
      <if test="purchaseOrderItem.purchaseAmount != null">
        `purchase_amount` = #{purchaseOrderItem.purchaseAmount},
      </if>
      <if test="purchaseOrderItem.purchasePrice != null">
        `purchase_price` = #{purchaseOrderItem.purchasePrice},
      </if>
      <if test="purchaseOrderItem.purchaseStock != null">
        `purchase_stock` = #{purchaseOrderItem.purchaseStock},
      </if>
      <if test="purchaseOrderItem.actualStock != null">
        `actual_stock` = #{purchaseOrderItem.actualStock},
      </if>
      <if test="purchaseOrderItem.deliveryType != null">
        `delivery_type` = #{purchaseOrderItem.deliveryType},
      </if>
      <if test="purchaseOrderItem.status != null">
        `status` = #{purchaseOrderItem.status},
      </if>
      <if test="purchaseOrderItem.beDeliveredNum != null">
        `be_delivered_num` = #{purchaseOrderItem.beDeliveredNum},
      </if>
    </set>
    where purchase_item_id = #{purchaseOrderItem.purchaseItemId}
  </update>

  <select id="getDevTypeByOrderItemId" resultType="int">
    SELECT ifnull(delivery_type,-1) FROM `purchase_order_item` WHERE purchase_item_id = #{orderItemId}
  </select>

  <update id="updateByDelivery">
    UPDATE purchase_order_item set be_delivered_num=be_delivered_num - #{orderItem.changeNum},delivery_type = #{deliveryType} WHERE purchase_item_id = #{orderItem.orderItemId}
  </update>

  <select id="countUnDeliveryNumByOrderId" resultType="int">
    select sum(be_delivered_num) from purchase_order_item where be_delivered_num != -1 and purchase_order_id = #{orderId}
  </select>

  <select id="getDevTypeByOrderId" resultType="java.lang.Integer">
    SELECT delivery_type FROM `purchase_order_item` WHERE purchase_order_id = #{orderId}
  </select>

  <select id="listItemByPurchaseOrderId" resultType="com.tmerclub.cloud.order.model.PurchaseOrderItem">
    SELECT
      *
    FROM
      `purchase_order_item`
    WHERE
      purchase_order_id = #{purchaseOrderId}
  </select>

  <update id="updateStatusByPurchaseOrderId">
    update purchase_order_item
    set `status` = #{status}
    where
    <foreach collection="purchaseOrderIds" item="purchaseOrderId" open="(" close=")" separator="or">
      purchase_order_id = #{purchaseOrderId}
    </foreach>
  </update>
</mapper>
