<?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.TakeStockSpuMapper">
  <resultMap id="takeStockSpuMap" type="com.tmerclub.cloud.product.model.TakeStockSpu">
    <id column="take_stock_spu_id" property="takeStockSpuId" />
    <result column="take_stock_id" property="takeStockId"/>
    <result column="spu_id" property="spuId"/>
    <result column="sku_id" property="skuId"/>
    <result column="stocks" property="stocks"/>
    <result column="total_stock" property="totalStock"/>
    <result column="io_type" property="ioType"/>
    <result column="remark" property="remark"/>
    <result column="exception_reason" property="exceptionReason"/>
  </resultMap>
  <sql id="Vo_Column_List">
    `take_stock_spu_id`,`take_stock_id`,`spu_id`,`sku_id`,`stocks`,`total_stock`,`io_type`,`remark`,`exception_reason`
  </sql>
  <select id="list" resultMap="takeStockSpuMap">
    select <include refid="Vo_Column_List"/> from take_stock_spu order by take_stock_spu_id desc
  </select>
  <select id="getByTakeStockSpuId" resultMap="takeStockSpuMap">
    select <include refid="Vo_Column_List"/> from take_stock_spu where take_stock_spu_id = #{takeStockSpuId}
  </select>

  <select id="listByTakeStockId" resultType="com.tmerclub.cloud.product.vo.TakeStockSpuVO">
    SELECT
      tss.*,
      s.party_code,
      sp.main_img_url pic,
      sl.sku_name,
      spl.spu_name
    FROM
      `take_stock_spu` tss
      LEFT JOIN `sku` s ON tss.sku_id = s.sku_id
      LEFT JOIN `spu` sp ON sp.spu_id = tss.spu_id
      LEFT JOIN `sku_lang` sl ON sl.sku_id = tss.sku_id
      AND sl.lang = #{lang}
      LEFT JOIN `spu_lang` spl ON spl.spu_id = tss.spu_id
      AND spl.lang = #{lang}
    WHERE
      tss.take_stock_id = #{takeStockId}
  </select>

  <insert id="save">
    insert into take_stock_spu (`take_stock_id`,`spu_id`,`sku_id`,`stocks`,`total_stock`,`io_type`,`remark`,`exception_reason`)
    values (#{takeStockSpu.takeStockId},#{takeStockSpu.spuId},#{takeStockSpu.skuId},#{takeStockSpu.stocks},#{takeStockSpu.totalStock},#{takeStockSpu.ioType},#{takeStockSpu.remark},#{takeStockSpu.exceptionReason});
  </insert>

  <insert id="saveBatch">
    INSERT INTO `take_stock_spu` (`take_stock_id`,`spu_id`,`sku_id`,`stocks`,`total_stock`,`io_type`,`remark`,`exception_reason`)
    VALUES
      <foreach collection="takeStockSpuList" item="takeStockSpu" open="" separator="," close="">
        (
          #{takeStockSpu.takeStockId},
          #{takeStockSpu.spuId},
          #{takeStockSpu.skuId},
          #{takeStockSpu.stocks},
          #{takeStockSpu.totalStock},
          #{takeStockSpu.ioType},
          #{takeStockSpu.remark},
          #{takeStockSpu.exceptionReason}
        )
      </foreach>
  </insert>

  <update id="update">
    update take_stock_spu
    <set>
      <if test="takeStockSpu.takeStockId != null">
        `take_stock_id` = #{takeStockSpu.takeStockId},
      </if>
      <if test="takeStockSpu.spuId != null">
        `spu_id` = #{takeStockSpu.spuId},
      </if>
      <if test="takeStockSpu.skuId != null">
        `sku_id` = #{takeStockSpu.skuId},
      </if>
      <if test="takeStockSpu.stocks != null">
        `stocks` = #{takeStockSpu.stocks},
      </if>
      <if test="takeStockSpu.totalStock != null">
        `total_stock` = #{takeStockSpu.totalStock},
      </if>
      <if test="takeStockSpu.ioType != null">
        `io_type` = #{takeStockSpu.ioType},
      </if>
      <if test="takeStockSpu.remark != null">
        `remark` = #{takeStockSpu.remark},
      </if>
      <if test="takeStockSpu.exceptionReason != null">
        `exception_reason` = #{takeStockSpu.exceptionReason},
      </if>
    </set>
    where take_stock_spu_id = #{takeStockSpu.takeStockSpuId}
  </update>

  <delete id="deleteById">
    delete from take_stock_spu where take_stock_spu_id = #{takeStockSpuId}
  </delete>

  <delete id="deleteByTackStockId">
    DELETE
    FROM
      `take_stock_spu`
    WHERE
      take_stock_id = #{takeStockId}
  </delete>

</mapper>
