<?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.InventoryStoreMapper">
  <resultMap id="TotalResultMap" type="com.neusoft.smart.pos.inventory.persistent.domain.InventoryStoreTotal">
    <result column="goods_id" jdbcType="INTEGER" property="goodsId" />
    <result column="goods_bar_code" jdbcType="VARCHAR" property="goodsBarCode" />
    <result column="goods_name" jdbcType="VARCHAR" property="goodsName" />
    <result column="goods_category" jdbcType="VARCHAR" property="goodsCategory" />
    <result column="goods_category_id" jdbcType="INTEGER" property="goodsCategoryId" />
	<result column="supplier_id" jdbcType="INTEGER" property="supplierId" />
    <result column="supplier" jdbcType="VARCHAR" property="supplier" />
    <result column="supplierName" jdbcType="VARCHAR" property="supplierName" />
    <result column="goods_unit" jdbcType="VARCHAR" property="goodsUnit" />
    <result column="quality_date" jdbcType="DATE" property="qualityDate" />
    <result column="storage_num" jdbcType="DECIMAL" property="storageNum" />
    <result column="average_in_price" jdbcType="DECIMAL" property="averageInPrice" />
    <result column="total_inventory_amount" jdbcType="DECIMAL" property="totalInventoryAmount" />
    <result column="updated_time" jdbcType="TIMESTAMP" property="updatedTime" />
    <result column="operator" jdbcType="VARCHAR" property="operator" />
    <result column="shop_id" jdbcType="INTEGER" property="shopId" />
	  <result column="is_stock" jdbcType="INTEGER" property="isStock" />
	  <result column="scale" jdbcType="INTEGER" property="scale" />
  </resultMap>

	<resultMap id="HistoryResultMap" type="com.neusoft.smart.pos.inventory.persistent.domain.InventoryStoreHistory">
		<id column="id" jdbcType="INTEGER" property="id" />
		<result column="shop_id" jdbcType="INTEGER" property="shopId" />
		<result column="goods_id" jdbcType="INTEGER" property="goodsId" />
		<result column="goods_bar_code" jdbcType="VARCHAR" property="goodsBarCode" />
		<result column="goods_name" jdbcType="VARCHAR" property="goodsName" />
		<result column="goods_category" jdbcType="VARCHAR" property="goodsCategory" />
		<result column="residue_num" jdbcType="DECIMAL" property="residueNum" />
		<result column="var_num" jdbcType="DECIMAL" property="varNum" />
		<result column="in_price" jdbcType="DECIMAL" property="inPrice" />
		<result column="sale_price" jdbcType="DECIMAL" property="salePrice" />
		<result column="action_dire" jdbcType="INTEGER" property="actionDire" />
		<result column="action_type" jdbcType="VARCHAR" property="actionType" />
		<result column="created_time" jdbcType="TIMESTAMP" property="createdTime" />
		<result column="created_by" jdbcType="VARCHAR" property="createdBy" />
	</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>

  <select id="selectTotalByShopId" parameterType="java.lang.Integer" resultMap="TotalResultMap">
select
	*
from
	( select
		igi.id as goods_id, igi.bar_code as goods_bar_code, igi.name as goods_name, igc.name as goods_category, igi.unit as goods_unit, tmp.supplier_id as supplier_id, tmp.supplier as supplier, tmp.storage_num as storage_num, igi.is_stock, igi.scale
	from
		inventory_goods_info as igi
	left join ( select
			isb.goods_id as goods_id, supplier as supplier_id, ips.name as supplier, ifnull( s.total, 0 ) as storage_num
		from
			inventory_store_batch as isb
		inner join ( select
				sum( storage_num ) as total, max( id ) as last_record
			from
				inventory_store_batch
			where
				shop_id = #{shopId}
				and (status = 0 or status = 1)
			group by
				goods_id ) as s on
			isb.id = s.last_record
		left join inventory_product_supplier as ips on
			isb.supplier = ips.id ) as tmp on
		tmp.goods_id = igi.id
	left  join inventory_goods_category igc on igc.id = igi.cate_id
	where
		igi.shop_id = #{shopId} and igi.deleted &lt; 2 and igi.is_stock=1) as tmp
	order by tmp.goods_id desc
  </select>

  <select id="selectTotalByExample" parameterType="com.neusoft.smart.pos.inventory.persistent.domain.InventoryStoreTotalExample" resultMap="TotalResultMap">
	  select
	  *
	  from
	  ( select
	  tmp.id as id, igi.id as goods_id, igi.bar_code as goods_bar_code, igi.name as goods_name, igc.name as goods_category,igi.cate_id as goods_category_id, igi.unit as goods_unit, IF(tmp.storage_num = 0,'',tmp.supplierName) as supplierName, IF(tmp.storage_num = 0,'',tmp.supplier) as supplier, tmp.quality_date as quality_date, ifnull(tmp.storage_num, 0)/POWER(10,scale) as storage_num, if ( tmp.storage_num, tmp.in_amount_total / tmp.storage_num, 0 ) as average_in_price, if ( tmp.in_amount_total, tmp.in_amount_total , 0 )/POWER(10,scale) as total_inventory_amount, tmp.updated_time as updated_time, tmp.operator as operator, tmp.status as status, igi.is_stock
	  from
	  inventory_goods_info as igi
	  left join ( select
	  isb.id as id, isb.goods_id as goods_id, ips.name as supplierName, isb.supplier as supplier, s.quality_date as quality_date, s.total as storage_num, s.in_amount_total as in_amount_total, isb.updated_time as updated_time, isb.updated_by as operator, isb.status
	  from
	  inventory_store_batch as isb
	  inner join ( select
	  sum( storage_num ) as total, sum( storage_num * in_price ) as in_amount_total, (select id from inventory_store_batch where goods_id = bh.goods_id order by updated_time desc LIMIT 1) as last_record, min(case when status=0 then null else quality_date end) as quality_date, count( id ) as batch_count
	  from
	  inventory_store_batch bh
	  where
	  shop_id = ${shopId}
	  and (status = 1 or status = 0)
	  group by
	  goods_id ) as s on
	  isb.id = s.last_record
	  left join inventory_product_supplier as ips on isb.supplier = ips.id) as tmp on
	  tmp.goods_id = igi.id left  join inventory_goods_category igc on igc.id = igi.cate_id where igi.shop_id = ${shopId} and igi.deleted &lt; 2 ) as tmp
      <include refid="Example_Where_Clause" />
      <if test="orderByClause != null">
          order by ${orderByClause}
      </if>
      <if test="offset != null and limit != null">
          limit ${offset}, ${limit}
      </if>
  </select>

  <select id="countTotalByExample" parameterType="com.neusoft.smart.pos.inventory.persistent.domain.InventoryStoreTotalExample" resultType="java.lang.Integer">
	  select
	  count(*)
	  from
	  ( select
	  tmp.id as id, igi.id as goods_id, igi.bar_code as goods_bar_code, igi.name as goods_name, igi.cate_id as goods_category,igi.cate_id as goods_category_id, igi.unit as goods_unit, tmp.supplier as supplier, tmp.quality_date as quality_date, ifnull(tmp.storage_num, 0)/POWER(10,scale) as storage_num, if ( tmp.storage_num, tmp.in_amount_total / tmp.storage_num, 0 ) as average_in_price, if ( tmp.in_amount_total, tmp.in_amount_total, 0 )/POWER(10,scale) as total_inventory_amount, tmp.updated_time as updated_time, tmp.operator as operator, tmp.status as status
	  from
	  inventory_goods_info as igi
	  left join ( select
	  isb.id as id, isb.goods_id as goods_id, isb.supplier as supplier, s.quality_date as quality_date, s.total as storage_num, s.in_amount_total as in_amount_total, isb.updated_time as updated_time, isb.created_by as operator, isb.status
	  from
	  inventory_store_batch as isb
	  inner join ( select
	  sum( storage_num ) as total, sum( storage_num * in_price ) as in_amount_total, max( id ) as last_record, min(quality_date) as quality_date, count( id ) as batch_count
	  from
	  inventory_store_batch
	  where
	  shop_id = ${shopId}
	  and status = 1
	  group by
	  goods_id ) as s on
	  isb.id = s.last_record ) as tmp on
	  tmp.goods_id = igi.id  where igi.shop_id = ${shopId} and igi.deleted &lt; 2 ) as tmp
      <include refid="Example_Where_Clause" />
  </select>

    <select id="selectInventoryTotalAmount" parameterType="java.lang.Integer" resultType="com.neusoft.smart.pos.inventory.persistent.domain.InventoryTotalAmount">
		select
			ROUND(SUM(in_price * storage_num/ POWER(10,scale)),2) AS totalAmount,
			ROUND(SUM(storage_num/ POWER(10,scale)),scale)  AS totalStorageNum,
			goods_id
		from
			inventory_store_batch isb, inventory_goods_info igi
		where
			isb.shop_id = #{shopId} and isb.status = 1 and goods_id = igi.id
    </select>

	<select id="selectHistoryByExample" parameterType="com.neusoft.smart.pos.inventory.persistent.domain.InventoryStoreHistoryExample" resultMap="HistoryResultMap">
select
	isi.id as id,
	isi.created_time as created_time,
	igi.bar_code as goods_bar_code,
	igi.name as goods_name,
	igi.cate_id as goods_category,
	action_type,
	residue_num,
	var_num,
	sale_price,
	in_price,
	action_dire
from
	inventory_store_inout as isi
inner join inventory_goods_info as igi on
	igi.id = isi.goods_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="countHistoryByExample" parameterType="com.neusoft.smart.pos.inventory.persistent.domain.InventoryStoreHistoryExample" resultType="java.lang.Integer">
select
	count(isi.id)
from
	inventory_store_inout as isi
inner join inventory_goods_info as igi on
	igi.id = isi.goods_id
		<if test="_parameter != null">
			<include refid="Example_Where_Clause" />
		</if>
	</select>
</mapper>