<?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.neusoft.smart.pos.inventory.persistent.mapper.InventoryCheckStockDtlMapper">
  <resultMap id="BaseResultMap" type="com.neusoft.smart.pos.inventory.persistent.domain.InventoryCheckStockDtl">
    <id column="id" jdbcType="INTEGER" property="id" />
    <result column="mst_id" jdbcType="INTEGER" property="mstId" />
    <result column="goods_id" jdbcType="INTEGER" property="goodsId" />
    <result column="supplier" jdbcType="VARCHAR" property="supplier" />
    <result column="stock_num" jdbcType="DECIMAL" property="stockNum" />
    <result column="check_num" jdbcType="DECIMAL" property="checkNum" />
    <result column="diff_num" jdbcType="DECIMAL" property="diffNum" />
    <result column="remark" jdbcType="VARCHAR" property="remark" />
    <result column="goods_name" jdbcType="VARCHAR" property="goodsName" />
    <result column="goods_bar_code" jdbcType="VARCHAR" property="goodsBarCode" />
    <result column="goods_category" jdbcType="VARCHAR" property="goodsCategory" />
    <result column="goods_unit" jdbcType="VARCHAR" property="goodsUnit" />
  </resultMap>
  <sql id="Example_Where_Clause">
    <where>
      <foreach collection="oredCriteria" item="criteria" separator="or">
        <if test="criteria.valid">
          <trim prefix="(" prefixOverrides="and" suffix=")">
            <foreach collection="criteria.criteria" item="criterion">
              <choose>
                <when test="criterion.noValue">
                  and ${criterion.condition}
                </when>
                <when test="criterion.singleValue">
                  and ${criterion.condition} #{criterion.value}
                </when>
                <when test="criterion.betweenValue">
                  and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
                </when>
                <when test="criterion.listValue">
                  and ${criterion.condition}
                  <foreach close=")" collection="criterion.value" item="listItem" open="(" separator=",">
                    #{listItem}
                  </foreach>
                </when>
              </choose>
            </foreach>
          </trim>
        </if>
      </foreach>
    </where>
  </sql>
  <sql id="Update_By_Example_Where_Clause">
    <where>
      <foreach collection="example.oredCriteria" item="criteria" separator="or">
        <if test="criteria.valid">
          <trim prefix="(" prefixOverrides="and" suffix=")">
            <foreach collection="criteria.criteria" item="criterion">
              <choose>
                <when test="criterion.noValue">
                  and ${criterion.condition}
                </when>
                <when test="criterion.singleValue">
                  and ${criterion.condition} #{criterion.value}
                </when>
                <when test="criterion.betweenValue">
                  and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
                </when>
                <when test="criterion.listValue">
                  and ${criterion.condition}
                  <foreach close=")" collection="criterion.value" item="listItem" open="(" separator=",">
                    #{listItem}
                  </foreach>
                </when>
              </choose>
            </foreach>
          </trim>
        </if>
      </foreach>
    </where>
  </sql>
  <sql id="Base_Column_List">
    icsd.id as id, mst_id, goods_id, supplier, stock_num, check_num, diff_num, remark, gr.bar_code as goods_bar_code, gr.name as goods_name, gr.main_cate as goods_category, gr.unit as goods_unit
  </sql>
  <select id="selectByExample" parameterType="com.neusoft.smart.pos.inventory.persistent.domain.InventoryCheckStockDtlExample" resultMap="BaseResultMap">
    select
    <if test="distinct">
      distinct
    </if>
    <include refid="Base_Column_List" />
    from inventory_check_stock_dtl as icsd INNER JOIN goods_retail as gr ON icsd.goods_id = gr.id
    <if test="_parameter != null">
      <include refid="Example_Where_Clause" />
    </if>
    <if test="orderByClause != null">
      order by ${orderByClause}
    </if>
    <if test="offset != null and limit != null">
      limit ${offset}, ${limit}
    </if>
  </select>
  <select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
    select 
    <include refid="Base_Column_List" />
    from inventory_check_stock_dtl
    where id = #{id,jdbcType=INTEGER}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
    delete from inventory_check_stock_dtl
    where id = #{id,jdbcType=INTEGER}
  </delete>
  <delete id="deleteByExample" parameterType="com.neusoft.smart.pos.inventory.persistent.domain.InventoryCheckStockDtlExample">
    delete from inventory_check_stock_dtl
    <if test="_parameter != null">
      <include refid="Example_Where_Clause" />
    </if>
  </delete>
  <insert id="insert" keyColumn="id" keyProperty="id" parameterType="com.neusoft.smart.pos.inventory.persistent.domain.InventoryCheckStockDtl" useGeneratedKeys="true">
    insert into inventory_check_stock_dtl (mst_id, goods_id, supplier, 
      stock_num, check_num, diff_num, 
      remark)
    values (#{mstId,jdbcType=INTEGER}, #{goodsId,jdbcType=INTEGER}, #{supplier,jdbcType=VARCHAR}, 
      #{stockNum,jdbcType=DECIMAL}, #{checkNum,jdbcType=DECIMAL}, #{diffNum,jdbcType=DECIMAL}, 
      #{remark,jdbcType=VARCHAR})
  </insert>
  <insert id="insertSelective" keyColumn="id" keyProperty="id" parameterType="com.neusoft.smart.pos.inventory.persistent.domain.InventoryCheckStockDtl" useGeneratedKeys="true">
    insert into inventory_check_stock_dtl
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="mstId != null">
        mst_id,
      </if>
      <if test="goodsId != null">
        goods_id,
      </if>
      <if test="supplier != null">
        supplier,
      </if>
      <if test="stockNum != null">
        stock_num,
      </if>
      <if test="checkNum != null">
        check_num,
      </if>
      <if test="diffNum != null">
        diff_num,
      </if>
      <if test="remark != null">
        remark,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
      <if test="mstId != null">
        #{mstId,jdbcType=INTEGER},
      </if>
      <if test="goodsId != null">
        #{goodsId,jdbcType=INTEGER},
      </if>
      <if test="supplier != null">
        #{supplier,jdbcType=VARCHAR},
      </if>
      <if test="stockNum != null">
        #{stockNum,jdbcType=DECIMAL},
      </if>
      <if test="checkNum != null">
        #{checkNum,jdbcType=DECIMAL},
      </if>
      <if test="diffNum != null">
        #{diffNum,jdbcType=DECIMAL},
      </if>
      <if test="remark != null">
        #{remark,jdbcType=VARCHAR},
      </if>
    </trim>
  </insert>
  <select id="countByExample" parameterType="com.neusoft.smart.pos.inventory.persistent.domain.InventoryCheckStockDtlExample" resultType="java.lang.Integer">
    select count(*) from inventory_check_stock_dtl
    <if test="_parameter != null">
      <include refid="Example_Where_Clause" />
    </if>
  </select>
  <update id="updateByExampleSelective" parameterType="map">
    update inventory_check_stock_dtl
    <set>
      <if test="record.id != null">
        id = #{record.id,jdbcType=INTEGER},
      </if>
      <if test="record.mstId != null">
        mst_id = #{record.mstId,jdbcType=INTEGER},
      </if>
      <if test="record.goodsId != null">
        goods_id = #{record.goodsId,jdbcType=INTEGER},
      </if>
      <if test="record.supplier != null">
        supplier = #{record.supplier,jdbcType=VARCHAR},
      </if>
      <if test="record.stockNum != null">
        stock_num = #{record.stockNum,jdbcType=DECIMAL},
      </if>
      <if test="record.checkNum != null">
        check_num = #{record.checkNum,jdbcType=DECIMAL},
      </if>
      <if test="record.diffNum != null">
        diff_num = #{record.diffNum,jdbcType=DECIMAL},
      </if>
      <if test="record.remark != null">
        remark = #{record.remark,jdbcType=VARCHAR},
      </if>
    </set>
    <if test="_parameter != null">
      <include refid="Update_By_Example_Where_Clause" />
    </if>
  </update>
  <update id="updateByExample" parameterType="map">
    update inventory_check_stock_dtl
    set id = #{record.id,jdbcType=INTEGER},
      mst_id = #{record.mstId,jdbcType=INTEGER},
      goods_id = #{record.goodsId,jdbcType=INTEGER},
      supplier = #{record.supplier,jdbcType=VARCHAR},
      stock_num = #{record.stockNum,jdbcType=DECIMAL},
      check_num = #{record.checkNum,jdbcType=DECIMAL},
      diff_num = #{record.diffNum,jdbcType=DECIMAL},
      remark = #{record.remark,jdbcType=VARCHAR}
    <if test="_parameter != null">
      <include refid="Update_By_Example_Where_Clause" />
    </if>
  </update>
  <update id="updateByPrimaryKeySelective" parameterType="com.neusoft.smart.pos.inventory.persistent.domain.InventoryCheckStockDtl">
    update inventory_check_stock_dtl
    <set>
      <if test="mstId != null">
        mst_id = #{mstId,jdbcType=INTEGER},
      </if>
      <if test="goodsId != null">
        goods_id = #{goodsId,jdbcType=INTEGER},
      </if>
      <if test="supplier != null">
        supplier = #{supplier,jdbcType=VARCHAR},
      </if>
      <if test="stockNum != null">
        stock_num = #{stockNum,jdbcType=DECIMAL},
      </if>
      <if test="checkNum != null">
        check_num = #{checkNum,jdbcType=DECIMAL},
      </if>
      <if test="diffNum != null">
        diff_num = #{diffNum,jdbcType=DECIMAL},
      </if>
      <if test="remark != null">
        remark = #{remark,jdbcType=VARCHAR},
      </if>
    </set>
    where id = #{id,jdbcType=INTEGER}
  </update>
  <update id="updateByPrimaryKey" parameterType="com.neusoft.smart.pos.inventory.persistent.domain.InventoryCheckStockDtl">
    update inventory_check_stock_dtl
    set mst_id = #{mstId,jdbcType=INTEGER},
      goods_id = #{goodsId,jdbcType=INTEGER},
      supplier = #{supplier,jdbcType=VARCHAR},
      stock_num = #{stockNum,jdbcType=DECIMAL},
      check_num = #{checkNum,jdbcType=DECIMAL},
      diff_num = #{diffNum,jdbcType=DECIMAL},
      remark = #{remark,jdbcType=VARCHAR}
    where id = #{id,jdbcType=INTEGER}
  </update>
  <insert id="batchInsert">
    <if test="items.get(0) != null">
            insert into inventory_check_stock_dtl  (mst_id, goods_id, supplier, 
        stock_num, check_num, diff_num, 
        remark)
      values
      <foreach collection="items" item="item" index="index" separator=",">
      (#{item.mstId,jdbcType=INTEGER}, #{item.goodsId,jdbcType=INTEGER}, #{item.supplier,jdbcType=VARCHAR}, 
        #{item.stockNum,jdbcType=DECIMAL}, #{item.checkNum,jdbcType=DECIMAL}, #{item.diffNum,jdbcType=DECIMAL}, 
        #{item.remark,jdbcType=VARCHAR})
      </foreach>
    </if>
  </insert>

  <select id="selectSummary" parameterType="java.lang.Integer" resultType="com.neusoft.smart.pos.inventory.persistent.domain.InventoryCheckStockSummary">
    <![CDATA[
select
    sum( diff_num > 0 ) as win,
    sum( diff_num < 0 ) as loss,
    sum( diff_num = 0 ) as normal
    from
    inventory_check_stock_dtl
    where
    mst_id = #{checkId}
]]>
  </select>
</mapper>