<?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.TakeStockMapper">
  <resultMap id="takeStockMap" type="com.tmerclub.cloud.product.model.TakeStock">
    <id column="take_stock_id" property="takeStockId" />
    <result column="take_stock_no" property="takeStockNo"/>
    <result column="bill_status" property="billStatus"/>
    <result column="maker" property="maker"/>
    <result column="create_time" property="createTime"/>
    <result column="stock_region_name" property="stockRegionName"/>
    <result column="remark" property="remark"/>
    <result column="shop_id" property="shopId"/>
    <result column="maker_mobile" property="makerMobile"/>
  </resultMap>

  <resultMap id="takeStockExcelMap" type="com.tmerclub.cloud.product.vo.TakeStockExcelVO">
    <result column="take_stock_no" property="takeStockNo"/>
    <result column="bill_status" property="billStatus"/>
    <result column="maker" property="maker"/>
    <result column="create_time" property="createTime"/>
    <result column="stock_region_name" property="stockRegionName"/>
    <result column="remark" property="remark"/>
<!--    <collection property="takeStockSpuExcelVOs" ofType="com.tmerclub.cloud.product.vo.TakeStockSpuExcelVO">-->
<!--      <result column="spu_name" property="spuName"/>-->
<!--      <result column="sku_name" property="skuName"/>-->
<!--      <result column="party_code" property="partyCode"/>-->
<!--      <result column="stocks" property="stocks"/>-->
<!--      <result column="total_stock" property="actualStock"/>-->
<!--      <result column="io_type" property="ioType"/>-->
<!--      <result column="remark" property="remark"/>-->
<!--    </collection>-->
  </resultMap>

  <sql id="Vo_Column_List">
    `take_stock_id`,`take_stock_no`,`bill_status`,`maker`,`create_time`,`stock_region_name`,`remark`,`shop_id`,`maker_mobile`,`warehouse_id`,`stock_point_type`
  </sql>
  <select id="list" resultMap="takeStockMap">
    select
      <include refid="Vo_Column_List"/>
    from
      take_stock
    <where>
      <if test="takeStockDTO.warehouseId != null">
        warehouse_id = #{takeStockDTO.warehouseId}
      </if>
      <if test="takeStockDTO.billStatus != null">
        AND bill_status = #{takeStockDTO.billStatus}
      </if>
    </where>
    order by
      take_stock_id desc
  </select>
  <select id="getByTakeStockId" resultMap="takeStockMap">
    select <include refid="Vo_Column_List"/> from take_stock where take_stock_id = #{takeStockId}
  </select>
  <insert id="save" useGeneratedKeys="true" keyProperty="takeStockId">
    insert into take_stock (`take_stock_no`,`bill_status`,`maker`,`stock_region_name`,`remark`,`shop_id`,`maker_mobile`, `warehouse_id`, `stock_point_type`)
    values (#{takeStock.takeStockNo},#{takeStock.billStatus},#{takeStock.maker},#{takeStock.stockRegionName},
            #{takeStock.remark},#{takeStock.shopId},#{takeStock.makerMobile},#{takeStock.warehouseId},#{takeStock.stockPointType});
  </insert>
  <update id="update">
    update take_stock
    <set>
      <if test="takeStock.takeStockNo != null">
        `take_stock_no` = #{takeStock.takeStockNo},
      </if>
      <if test="takeStock.billStatus != null">
        `bill_status` = #{takeStock.billStatus},
      </if>
      <if test="takeStock.maker != null">
        `maker` = #{takeStock.maker},
      </if>
      <if test="takeStock.stockRegionName != null">
        `stock_region_name` = #{takeStock.stockRegionName},
      </if>
      <if test="takeStock.remark != null">
        `remark` = #{takeStock.remark},
      </if>
      <if test="takeStock.shopId != null">
        `shop_id` = #{takeStock.shopId},
      </if>
      <if test="takeStock.makerMobile != null">
        `maker_mobile` = #{takeStock.makerMobile},
      </if>
    </set>
    where take_stock_id = #{takeStock.takeStockId}
  </update>
  <delete id="deleteById">
    delete from take_stock where take_stock_id = #{takeStockId}
  </delete>

  <sql id="whereCondition">
    ts.`shop_id` = #{takeStockDTO.shopId}
    <if test="takeStockDTO.spuName != null and takeStockDTO.spuName != ''">
      AND sl.`spu_name` LIKE CONCAT( "%", #{takeStockDTO.spuName}, "%" )
    </if>
    <if test="takeStockDTO.takeStockNo != null and takeStockDTO.takeStockNo != ''">
      AND ts.`take_stock_no` LIKE CONCAT( "%", #{takeStockDTO.takeStockNo}, "%" )
    </if>
    <if test="takeStockDTO.billStatus != null">
      AND ts.`bill_status` = #{takeStockDTO.billStatus}
    </if>
    <if test="takeStockDTO.createStartTime != null">
      AND ts.`create_time` &gt;= #{takeStockDTO.createStartTime}
    </if>
    <if test="takeStockDTO.createEndTime != null">
      AND ts.`create_time` &lt;= #{takeStockDTO.createEndTime}
    </if>
    <if test="takeStockDTO.maker != null">
      AND ts.`maker` = #{takeStockDTO.maker}
    </if>
    <if test="takeStockDTO.takeStockId != null">
      AND ts.`take_stock_id` = #{takeStockDTO.takeStockId}
    </if>
    <if test="takeStockDTO.partyCode != null and takeStockDTO.partyCode != '' ">
      AND s.`party_code` LIKE CONCAT( '%', #{takeStockDTO.partyCode}, '%' )
    </if>
  </sql>

  <select id="pageByParam" resultType="com.tmerclub.cloud.product.vo.TakeStockVO">
    SELECT
      ts.`take_stock_id`,
      ts.`take_stock_no`,
      ts.`bill_status`,
      ts.`create_time`,
      ts.`maker`,
      ts.`maker_mobile`,
      ts.`maker_mobile` maker_name,
      ts.`warehouse_id`,
      ts.`stock_point_type`,
      COUNT(tss.`take_stock_spu_id`) AS sku_count
    FROM `take_stock` ts
      LEFT JOIN `take_stock_spu` tss ON ts.`take_stock_id` = tss.`take_stock_id`
      LEFT JOIN `sku` s ON tss.`sku_id` = s.`sku_id`
      LEFT JOIN `spu_lang` sl ON tss.`spu_id` = sl.`spu_id`
      AND sl.`lang` = #{lang}
    WHERE
      <include refid="whereCondition"/>
    GROUP BY
      ts.`take_stock_id`
    ORDER BY
      ts.`take_stock_id` DESC
  </select>

  <select id="listTakeStockExportData" resultType="com.tmerclub.cloud.product.vo.TakeStockExcelVO">
    SELECT
      ts.`take_stock_no`,
      ts.`bill_status`,
      ts.`create_time`,
      ts.`maker_mobile`,
      ts.`stock_region_name`,
      ts.`maker`,
      ts.remark,
      ts.`warehouse_id`,
      ts.`stock_point_type`,
      spl.spu_name,
      sl.sku_name,
      s.party_code,
      tss.io_type,
      tss.stocks,
      tss.total_stock actualStock,
      tss.total_stock - tss.stocks changeStock,
      tss.remark  takeStockRemark
    FROM
      `take_stock` ts
      LEFT JOIN `take_stock_spu` tss ON ts.`take_stock_id` = tss.`take_stock_id`
      LEFT JOIN `sku` s ON tss.`sku_id` = s.`sku_id`
      LEFT JOIN `sku_lang` sl ON sl.`sku_id` = tss.`sku_id` AND sl.`lang` = #{lang}
      LEFT JOIN `spu_lang` spl ON tss.`spu_id` = spl.`spu_id` AND spl.`lang` = #{lang}
    WHERE
      <include refid="whereCondition"/>
    ORDER BY
      ts.`take_stock_id` DESC
  </select>

  <select id="countUnFinish" resultType="java.lang.Long">
    SELECT
      COUNT( * )
    FROM
      `take_stock`
    WHERE
      warehouse_id = #{warehouseId}
      AND bill_status = 1
  </select>

</mapper>
