<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  "https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.zioinfo.mes.stocktake.mapper.StocktakeMapper">

  <resultMap id="stkMap" type="com.zioinfo.mes.stocktake.MesStocktake">
    <id property="id" column="id"/>
    <result property="stocktakeNo" column="stocktake_no"/>
    <result property="itemCode" column="item_code"/>
    <result property="warehouseCode" column="warehouse_code"/>
    <result property="locationCode" column="location_code"/>
    <result property="lotNo" column="lot_no"/>
    <result property="systemQty" column="system_qty"/>
    <result property="countedQty" column="counted_qty"/>
    <result property="diffQty" column="diff_qty"/>
    <result property="status" column="status"/>
    <result property="remark" column="remark"/>
    <result property="createdBy" column="created_by"/>
    <result property="createdAt" column="created_at"/>
  </resultMap>

  <select id="findAll" resultMap="stkMap">
    SELECT * FROM mes_stocktake
    <where>
      <if test="stocktakeNo != null and stocktakeNo != ''">AND stocktake_no = #{stocktakeNo}</if>
      <if test="status != null and status != ''">AND status = #{status}</if>
    </where>
    ORDER BY id DESC
  </select>

  <select id="findById" resultMap="stkMap">SELECT * FROM mes_stocktake WHERE id = #{id}</select>

  <insert id="insert" parameterType="com.zioinfo.mes.stocktake.MesStocktake" useGeneratedKeys="true" keyProperty="id">
    INSERT INTO mes_stocktake (stocktake_no, item_code, warehouse_code, location_code, lot_no,
      system_qty, counted_qty, diff_qty, status, remark, created_by)
    VALUES (#{stocktakeNo}, #{itemCode}, #{warehouseCode}, #{locationCode}, #{lotNo},
      #{systemQty}, #{countedQty}, #{diffQty}, #{status}, #{remark}, #{createdBy})
  </insert>

  <update id="updateStatus">UPDATE mes_stocktake SET status = #{status} WHERE id = #{id}</update>

</mapper>
