<?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.product.mapper.StockPointSkuMapper">
  <resultMap id="stockPointSkuMap" type="com.tmerclub.cloud.product.model.StockPointSku">
    <id column="stock_point_sku_id" property="stockPointSkuId" />
    <result column="stock_point_id" property="stockPointId"/>
    <result column="stock_point_type" property="stockPointType"/>
    <result column="spu_id" property="spuId"/>
    <result column="sku_id" property="skuId"/>
    <result column="supplier_sku_id" property="supplierSkuId"/>
    <result column="status" property="status"/>
    <result column="create_time" property="createTime"/>
    <result column="update_time" property="updateTime"/>
    <result column="type" property="type"/>
    <result column="stock_mode" property="stockMode"/>
  </resultMap>
  <sql id="Vo_Column_List">
    `stock_point_sku_id`,`stock_point_id`,`stock_point_type`,`spu_id`,`sku_id`,`supplier_sku_id`,`status`,`create_time`,`update_time`,`type`,`stock_mode`
  </sql>
  <select id="list" resultMap="stockPointSkuMap">
    select <include refid="Vo_Column_List"/> from stock_point_sku order by stock_point_sku_id desc
  </select>
  <select id="getByStockPointSkuId" resultMap="stockPointSkuMap">
    select <include refid="Vo_Column_List"/> from stock_point_sku where stock_point_sku_id = #{stockPointSkuId}
  </select>
  <insert id="save">
    insert into stock_point_sku (`stock_point_id`,`stock_point_type`,`spu_id`,`sku_id`,`supplier_sku_id`,`status`,`stock_mode`)
    values (#{stockPointSku.stockPointId},#{stockPointSku.stockPointType},#{stockPointSku.spuId},#{stockPointSku.skuId},#{stockPointSku.supplierSkuId},#{stockPointSku.status},#{stockPointSku.stockMode});
  </insert>
  <update id="update">
    update stock_point_sku
    <set>
      <if test="stockPointSku.skuId != null">
        `sku_id` = #{stockPointSku.skuId},
      </if>
      <if test="stockPointSku.supplierSkuId != null">
        `supplier_sku_id` = #{stockPointSku.supplierSkuId},
      </if>
      <if test="stockPointSku.status != null">
        `status` = #{stockPointSku.status},
      </if>
      <if test="stockPointSku.stockMode != null">
        `stock_mode` = #{stockPointSku.stockMode},
      </if>
    </set>
    where stock_point_sku_id = #{stockPointSku.stockPointSkuId}
  </update>

  <delete id="deleteById">
    delete from stock_point_sku where stock_point_sku_id = #{stockPointSkuId}
  </delete>

  <delete id="deleteByIds">
    DELETE FROM
      `stock_point_sku`
    WHERE
      `stock_point_sku_id` IN
        <foreach collection="stockPointSkuIds" item="stockPointSkuId" open="(" separator="," close=")">
          #{stockPointSkuId}
        </foreach>
  </delete>

  <delete id="deleteBySpuId">
    delete from stock_point_sku where spu_id = #{spuId}
  </delete>

  <select id="batchSave">
    insert into stock_point_sku (`stock_point_id`,`stock_point_type`,`spu_id`,`sku_id`,`supplier_sku_id`,`status`,`type`,`stock_mode`) values
    <foreach collection="stockPointSkuList" item="stockPointSku" separator=",">
      (#{stockPointSku.stockPointId},#{stockPointSku.stockPointType},#{stockPointSku.spuId},#{stockPointSku.skuId},#{stockPointSku.supplierSkuId},#{stockPointSku.status}, #{stockPointSku.type},#{stockPointSku.stockMode})
    </foreach>
  </select>

  <delete id="batchDeleteByStockPointSkuIdList">
    delete from stock_point_sku where stock_point_sku_id in
    <foreach collection="stockPointSkuIdList" item="stockPointSkuId" open="(" close=")" separator=",">
      #{stockPointSkuId}
    </foreach>
  </delete>

  <select id="listBySkuIds" resultType="com.tmerclub.cloud.common.product.vo.StockPointSkuVO">
    select * from stock_point_sku sks
    where status != -1 and sku_id in
    <foreach collection="skuIds" item="skuId" open="(" close=")" separator=",">
      #{skuId}
    </foreach>
  </select>

  <select id="listByPointId" resultType="com.tmerclub.cloud.common.product.vo.StockPointSkuVO">
    select * from stock_point_sku sks
    where status != -1 and stock_point_id = #{stockPointId}
  </select>

    <delete id="removeBySkuIdAndPointId">
    delete from stock_point_sku where sku_id = #{skuId} and stock_point_id = #{stockPointId}
  </delete>

  <select id="listPointBySkuIds" resultType="com.tmerclub.cloud.common.product.vo.StockPointSkuVO">
    SELECT stock_point_id,stock_point_type,spu_id,sku_id,supplier_sku_id,`type`,`stock_mode`
    FROM `stock_point_sku`
    WHERE `status` = 1 AND sku_id IN
    <foreach collection="skuIds" item="skuId" separator="," open="(" close=")">
      #{skuId}
    </foreach>
  </select>

  <select id="listStockPointSku" resultType="com.tmerclub.cloud.common.product.vo.StockPointSkuVO">
    SELECT
      *
    FROM
      `stock_point_sku`
    WHERE
      `status` != -1
      <if test="stockPointSkuDTO.stockPointId != null">
        AND stock_point_id = #{stockPointSkuDTO.stockPointId}
      </if>
      <if test="stockPointSkuDTO.type != null">
        AND type = #{stockPointSkuDTO.type}
      </if>
      <if test="stockPointSkuDTO.stockPointIds != null and stockPointSkuDTO.stockPointIds.size() > 0">
        AND stock_point_id IN
          <foreach collection="stockPointSkuDTO.stockPointIds" item="stockPointId" open="(" separator="," close=")">
            #{stockPointId}
          </foreach>
      </if>
      <if test="stockPointSkuDTO.skuIds != null and stockPointSkuDTO.skuIds.size() > 0">
        AND sku_id IN
          <foreach collection="stockPointSkuDTO.skuIds" item="skuId" open="(" separator="," close=")">
            #{skuId}
          </foreach>
      </if>
  </select>

  <update id="updateStockMode">
    update stock_point_sku set stock_mode = #{stockMode} where stock_point_id = #{stockPointId}
  </update>
</mapper>
