<?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.th.demo.mapping.ware.StoreMapper" >
  <resultMap id="BaseResultMap" type="com.th.demo.model.ware.Store" >
    <id column="id" property="id" jdbcType="VARCHAR" />
    <result column="layer" property="layer" jdbcType="INTEGER" />
    <result column="model" property="model" jdbcType="VARCHAR" />
    <result column="plate_no" property="plateNo" jdbcType="VARCHAR" />
    <result column="count" property="count" jdbcType="DOUBLE" />
    <result column="weight" property="weight" jdbcType="DOUBLE" />
    <result column="remark" property="remark" jdbcType="VARCHAR" />
    <result column="add_id" property="addId" jdbcType="VARCHAR" />
    <result column="add_time" property="addTime" jdbcType="TIMESTAMP" />
    <result column="modify_id" property="modifyId" jdbcType="VARCHAR" />
    <result column="modify_time" property="modifyTime" jdbcType="TIMESTAMP" />
    <result column="cancel_id" property="cancelId" jdbcType="VARCHAR" />
    <result column="cancel_time" property="cancelTime" jdbcType="TIMESTAMP" />
    <result column="cancel_flag" property="cancelFlag" jdbcType="VARCHAR" />
    <result column="belong_id" property="belongId" jdbcType="VARCHAR" />
    <result column="in_id" property="inId" jdbcType="VARCHAR" />
    <result column="out_id" property="outId" jdbcType="VARCHAR" />
    <result column="receive_address" property="receiveAddress" jdbcType="VARCHAR" />
    <result column="out_flag" property="outFlag" jdbcType="VARCHAR" />
    <result column="lock_flag" property="lockFlag" jdbcType="VARCHAR" />

    <result column="wgt_dcn_mtc_cd" property="wgtDcnMtcCd" jdbcType="VARCHAR" />
    <result column="edge_ty" property="edgeTy" jdbcType="VARCHAR" />
    <result column="production_place" property="productionPlace" jdbcType="VARCHAR" />
    <result column="ord_no" property="ordNo" jdbcType="VARCHAR" />
    <result column="car_no" property="carNo" jdbcType="VARCHAR" />
    <result column="tally_people" property="tallyPeople" jdbcType="VARCHAR" />
    <result column="fk_compony_id" property="fkComponyId" jdbcType="VARCHAR" />

    <result column="invoice_price" property="invoicePrice" jdbcType="DOUBLE" />


    <association  column="ware_id" property="ware" select="com.th.demo.mapping.maint.WareMapper.selectByPrimaryKey" />
    <association  column="stack_id" property="stack" select="com.th.demo.mapping.maint.StackMapper.selectByPrimaryKey" />
    <association  column="material_id" property="material" select="com.th.demo.mapping.maint.MaterialMapper.selectByPrimaryKey" />
    <association  column="customer_id" property="customer" select="com.th.demo.mapping.maint.CustomerMapper.selectByPrimaryKey" />
    <association  column="add_id" property="addUser" select="com.th.demo.mapping.system.SysUserMapper.selectByPrimaryKey" />
  </resultMap>



  <resultMap id="StoreOfWareStack" type="com.th.demo.model.ware.StoreOfWareStack" >
    <result column="ware_id" property="wareId" jdbcType="VARCHAR" />
    <result column="stack_id" property="stackId" jdbcType="VARCHAR" />
  </resultMap>

  <resultMap id="TotalResultMap" type="com.th.demo.model.ware.TotalRecord">
    <result column="total_amount" property="totalAmount" jdbcType="DOUBLE" />
    <result column="total_weight" property="totalWeight" jdbcType="DOUBLE" />
  </resultMap>

  <sql id="Base_Column_List" >
    id, ware_id, stack_id, layer, material_id, model, plate_no, customer_id, count, weight,
    remark, add_id, add_time, modify_id, modify_time, cancel_id, cancel_time, cancel_flag,
    belong_id, in_id, out_id, receive_address, out_flag, lock_flag,wgt_dcn_mtc_cd,edge_ty,production_place
  </sql>


  <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.String" >
    select 
    <include refid="Base_Column_List" />
    from t_ware_store
    where id = #{id,jdbcType=VARCHAR}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.String" >
    delete from t_ware_store
    where id = #{id,jdbcType=VARCHAR}
  </delete>

  <insert id="insert" parameterType="com.th.demo.model.ware.Store" >

   <selectKey keyProperty="id" order="BEFORE" resultType="java.lang.String">
     SELECT  REPLACE(UUID(),'-','') as id from dual
   </selectKey>

    insert into t_ware_store (id, ware_id, stack_id,
      layer, material_id, model,
      plate_no, customer_id, count,
      weight, remark, add_id,
      add_time, modify_id, modify_time,
      cancel_id, cancel_time, cancel_flag,
      belong_id, in_id, out_id,
      receive_address, out_flag, lock_flag
      ,wgt_dcn_mtc_cd,edge_ty,production_place,ord_no
      ,car_no,tally_people,fk_compony_id,invoice_price
    )
    values (#{id,jdbcType=VARCHAR}, #{ware.id,jdbcType=VARCHAR}, #{stack.id,jdbcType=VARCHAR},
      #{layer,jdbcType=INTEGER}, #{material.id,jdbcType=VARCHAR}, #{model,jdbcType=VARCHAR},
      #{plateNo,jdbcType=VARCHAR}, #{customer.id,jdbcType=VARCHAR}, #{count,jdbcType=DOUBLE},
      #{weight,jdbcType=DOUBLE}, #{remark,jdbcType=VARCHAR}, #{addId,jdbcType=VARCHAR},
      #{addTime,jdbcType=TIMESTAMP}, #{modifyId,jdbcType=VARCHAR}, #{modifyTime,jdbcType=TIMESTAMP},
      #{cancelId,jdbcType=VARCHAR}, #{cancelTime,jdbcType=TIMESTAMP}, #{cancelFlag,jdbcType=VARCHAR},
      #{belongId,jdbcType=VARCHAR}, #{inId,jdbcType=VARCHAR}, #{outId,jdbcType=VARCHAR},
      #{receiveAddress,jdbcType=VARCHAR}, #{outFlag,jdbcType=VARCHAR}, #{lockFlag,jdbcType=VARCHAR}
      ,#{wgtDcnMtcCd,jdbcType=VARCHAR},#{edgeTy,jdbcType=VARCHAR},#{productionPlace,jdbcType=VARCHAR},#{ordNo,jdbcType=VARCHAR}
     ,#{carNo,jdbcType=VARCHAR},#{tallyPeople,jdbcType=VARCHAR},#{fkComponyId,jdbcType=VARCHAR},#{invoicePrice,jdbcType=DOUBLE}
      )
  </insert>

  <insert id="insertSelective" parameterType="com.th.demo.model.ware.Store" >
    insert into t_ware_store
    <trim prefix="(" suffix=")" suffixOverrides="," >
      <if test="id != null" >
        id,
      </if>
      <if test="wareName != null" >
        ware_name,
      </if>
      <if test="stackName != null" >
        stack_name,
      </if>
      <if test="layer != null" >
        layer,
      </if>
      <if test="materialName != null" >
        material_name,
      </if>
      <if test="materialNo != null" >
        material_no,
      </if>
      <if test="standard != null" >
        standard,
      </if>
      <if test="model != null" >
        model,
      </if>
      <if test="customerName != null" >
        customer_name,
      </if>
      <if test="customerNo != null" >
        customer_no,
      </if>
      <if test="count != null" >
        count,
      </if>
      <if test="weight != null" >
        weight,
      </if>
      <if test="remark != null" >
        remark,
      </if>
      <if test="addId != null" >
        add_id,
      </if>
      <if test="addTime != null" >
        add_time,
      </if>
      <if test="modifyId != null" >
        modify_id,
      </if>
      <if test="modifyTime != null" >
        modify_time,
      </if>
      <if test="cancelId != null" >
        cancel_id,
      </if>
      <if test="cancelTime != null" >
        cancel_time,
      </if>
      <if test="cancelFlag != null" >
        cancel_flag,
      </if>
      <if test="belongId != null" >
        belong_id,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides="," >
      <if test="id != null" >
        #{id,jdbcType=VARCHAR},
      </if>
      <if test="wareName != null" >
        #{wareName,jdbcType=VARCHAR},
      </if>
      <if test="stackName != null" >
        #{stackName,jdbcType=VARCHAR},
      </if>
      <if test="layer != null" >
        #{layer,jdbcType=INTEGER},
      </if>
      <if test="materialName != null" >
        #{materialName,jdbcType=VARCHAR},
      </if>
      <if test="materialNo != null" >
        #{materialNo,jdbcType=VARCHAR},
      </if>
      <if test="standard != null" >
        #{standard,jdbcType=VARCHAR},
      </if>
      <if test="model != null" >
        #{model,jdbcType=VARCHAR},
      </if>
      <if test="customerName != null" >
        #{customerName,jdbcType=VARCHAR},
      </if>
      <if test="customerNo != null" >
        #{customerNo,jdbcType=VARCHAR},
      </if>
      <if test="count != null" >
        #{count,jdbcType=DOUBLE},
      </if>
      <if test="weight != null" >
        #{weight,jdbcType=DOUBLE},
      </if>
      <if test="remark != null" >
        #{remark,jdbcType=VARCHAR},
      </if>
      <if test="addId != null" >
        #{addId,jdbcType=VARCHAR},
      </if>
      <if test="addTime != null" >
        #{addTime,jdbcType=TIMESTAMP},
      </if>
      <if test="modifyId != null" >
        #{modifyId,jdbcType=VARCHAR},
      </if>
      <if test="modifyTime != null" >
        #{modifyTime,jdbcType=TIMESTAMP},
      </if>
      <if test="cancelId != null" >
        #{cancelId,jdbcType=VARCHAR},
      </if>
      <if test="cancelTime != null" >
        #{cancelTime,jdbcType=TIMESTAMP},
      </if>
      <if test="cancelFlag != null" >
        #{cancelFlag,jdbcType=VARCHAR},
      </if>
      <if test="belongId != null" >
        #{belongId,jdbcType=VARCHAR},
      </if>
    </trim>
  </insert>
  <update id="updateByPrimaryKeySelective" parameterType="com.th.demo.model.ware.Store" >
    update t_ware_store
    <set >
      <if test="wareName != null" >
        ware_name = #{wareName,jdbcType=VARCHAR},
      </if>
      <if test="stackName != null" >
        stack_name = #{stackName,jdbcType=VARCHAR},
      </if>
      <if test="layer != null" >
        layer = #{layer,jdbcType=INTEGER},
      </if>
      <if test="materialName != null" >
        material_name = #{materialName,jdbcType=VARCHAR},
      </if>
      <if test="materialNo != null" >
        material_no = #{materialNo,jdbcType=VARCHAR},
      </if>
      <if test="standard != null" >
        standard = #{standard,jdbcType=VARCHAR},
      </if>
      <if test="model != null" >
        model = #{model,jdbcType=VARCHAR},
      </if>
      <if test="customerName != null" >
        customer_name = #{customerName,jdbcType=VARCHAR},
      </if>
      <if test="customerNo != null" >
        customer_no = #{customerNo,jdbcType=VARCHAR},
      </if>
      <if test="count != null" >
        count = #{count,jdbcType=DOUBLE},
      </if>
      <if test="weight != null" >
        weight = #{weight,jdbcType=DOUBLE},
      </if>
      <if test="remark != null" >
        remark = #{remark,jdbcType=VARCHAR},
      </if>
      <if test="addId != null" >
        add_id = #{addId,jdbcType=VARCHAR},
      </if>
      <if test="addTime != null" >
        add_time = #{addTime,jdbcType=TIMESTAMP},
      </if>
      <if test="modifyId != null" >
        modify_id = #{modifyId,jdbcType=VARCHAR},
      </if>
      <if test="modifyTime != null" >
        modify_time = #{modifyTime,jdbcType=TIMESTAMP},
      </if>
      <if test="cancelId != null" >
        cancel_id = #{cancelId,jdbcType=VARCHAR},
      </if>
      <if test="cancelTime != null" >
        cancel_time = #{cancelTime,jdbcType=TIMESTAMP},
      </if>
      <if test="cancelFlag != null" >
        cancel_flag = #{cancelFlag,jdbcType=VARCHAR},
      </if>
      <if test="belongId != null" >
        belong_id = #{belongId,jdbcType=VARCHAR},
      </if>
    </set>
    where id = #{id,jdbcType=VARCHAR}
  </update>
  <update id="updateByPrimaryKey" parameterType="com.th.demo.model.ware.Store" >
    update t_ware_store
    set ware_id = #{ware.id,jdbcType=VARCHAR},
      stack_id = #{stack.id,jdbcType=VARCHAR},
      layer = #{layer,jdbcType=INTEGER},
      material_id = #{material.id,jdbcType=VARCHAR},
      model = #{model,jdbcType=VARCHAR},
      plate_no = #{plateNo,jdbcType=VARCHAR},
      customer_id = #{customer.id,jdbcType=VARCHAR},
      count = #{count,jdbcType=DOUBLE},
      weight = #{weight,jdbcType=DOUBLE},
      remark = #{remark,jdbcType=VARCHAR},
      add_id = #{addId,jdbcType=VARCHAR},
      add_time = #{addTime,jdbcType=TIMESTAMP},
      modify_id = #{modifyId,jdbcType=VARCHAR},
      modify_time = #{modifyTime,jdbcType=TIMESTAMP},
      cancel_id = #{cancelId,jdbcType=VARCHAR},
      cancel_time = #{cancelTime,jdbcType=TIMESTAMP},
      cancel_flag = #{cancelFlag,jdbcType=VARCHAR},
      belong_id = #{belongId,jdbcType=VARCHAR},
      in_id = #{inId,jdbcType=VARCHAR},
      out_id = #{outId,jdbcType=VARCHAR},
      receive_address = #{receiveAddress,jdbcType=VARCHAR},
      out_flag = #{outFlag,jdbcType=VARCHAR},
      lock_flag = #{lockFlag,jdbcType=VARCHAR}
    where id = #{id,jdbcType=VARCHAR}
  </update>



  <select id="selectLayerByStackId" resultType="java.lang.String" >
  SELECT ifnull(max(t.layer),0) from t_ware_store t
  where t.stack_id =#{param1,jdbcType=VARCHAR}
  and t.belong_id = #{param2,jdbcType=VARCHAR}
  and ifnull(t.cancel_flag,'0') = '0'
      and ifnull(t.out_flag,'0') = '0'
  </select>

  <select id="selectByWareIdStackId"  resultMap="BaseResultMap" >
  SELECT b.name as fk_compony_id,t.* from t_ware_store t left join t_maint_customer b
  on t.fk_compony_id = b.id
  where t.ware_id =  #{param1,jdbcType=VARCHAR}
  and t.stack_id =  #{param2,jdbcType=VARCHAR}
  and t.belong_id = #{param3,jdbcType=VARCHAR}
    and ifnull(t.cancel_flag,'0') = '0'
    and ifnull(t.out_flag,'0') = '0'
  order by layer desc
  </select>

  <select id="select"  resultMap="BaseResultMap" >

select t.* from t_ware_store t where t.ware_name in (
select b.name from t_maint_userware a ,t_maint_ware b  where a.ware_id = b.id
and  user_id = #{param3,jdbcType=VARCHAR}  )  and t.belong_id = #{param4,jdbcType=VARCHAR}
    <if test="param1 != ''" >
      and  t.ware_name = #{param1,jdbcType=VARCHAR}
    </if>
    <if test="param2 != ''" >
      and  t.stack_name = #{param2,jdbcType=VARCHAR}
    </if>
    and ifnull(t.cancel_flag,'0') = '0'
    and ifnull(t.out_flag,'0') = '0'
order by t.stack_name asc , t.layer desc

  </select>


  <select id="selectForUpDown"   resultMap="BaseResultMap"  >
select t.*  from  t_ware_store t
    where t.ware_id in (
    select a.ware_id from t_maint_userware a  where a.user_id = #{param3,jdbcType=VARCHAR}
     )  and t.belong_id = #{param4,jdbcType=VARCHAR}
    and  t.ware_id = #{param1,jdbcType=VARCHAR}
    and  t.stack_id = #{param2,jdbcType=VARCHAR}
    and t.layer &gt;=  #{param5,jdbcType=INTEGER}
    and t.layer &lt;=  #{param6,jdbcType=INTEGER}
      and ifnull(t.cancel_flag,'0') = '0'
          and ifnull(t.out_flag,'0') = '0'
  </select>

  <update id="updateForUpDownByStep"    >
update t_ware_store t set t.layer = t.layer +  #{param7,jdbcType=INTEGER}
    where t.ware_id in (
    select a.ware_id from t_maint_userware a  where a.user_id = #{param3,jdbcType=VARCHAR}
     )  and t.belong_id = #{param4,jdbcType=VARCHAR}
    and  t.ware_id = #{param1,jdbcType=VARCHAR}
    and  t.stack_id = #{param2,jdbcType=VARCHAR}
    and t.layer &gt;=  #{param5,jdbcType=INTEGER}
    and t.layer &lt;=  #{param6,jdbcType=INTEGER}
      and ifnull(t.cancel_flag,'0') = '0'
          and ifnull(t.out_flag,'0') = '0'
  </update>

  <update id="updateForUpDownByLayer"    >
update t_ware_store t set t.layer =   #{param7,jdbcType=INTEGER}
      where t.ware_id in (
    select a.ware_id from t_maint_userware a  where a.user_id = #{param3,jdbcType=VARCHAR}
     )  and t.belong_id = #{param4,jdbcType=VARCHAR}
    and  t.ware_id = #{param1,jdbcType=VARCHAR}
    and  t.stack_id = #{param2,jdbcType=VARCHAR}
    and t.layer &gt;=  #{param5,jdbcType=INTEGER}
    and t.layer &lt;=  #{param6,jdbcType=INTEGER}
      and ifnull(t.cancel_flag,'0') = '0'
          and ifnull(t.out_flag,'0') = '0'
  </update>

  <select id="selectByOutId"   resultMap="BaseResultMap"  >
    select t.*  from  t_ware_store t
      where t.ware_id in (
    select a.ware_id from t_maint_userware a  where a.user_id = #{param3,jdbcType=VARCHAR}
     )  and t.belong_id = #{param4,jdbcType=VARCHAR}
    and  t.out_id = #{param1,jdbcType=VARCHAR}
  </select>


  <update id="updateByOutId"  >
    update  t_ware_store t set t.cancel_flag = '1'
      where t.ware_id in (
    select a.ware_id from t_maint_userware a  where a.user_id = #{param3,jdbcType=VARCHAR}
     )  and t.belong_id = #{param4,jdbcType=VARCHAR}
    and  t.out_id = #{param1,jdbcType=VARCHAR}
  </update>


  <select id="selectStore" resultMap="BaseResultMap" >
    select t.* from t_ware_store t, t_maint_ware a, t_maint_stack b, t_maint_material c,t_maint_customer d
    where t.ware_id = a.id
    and t.stack_id = b.id
    and t.material_id  = c.id
    and t.customer_id = d.id
    and  a.name like concat('%',#{param1,jdbcType=VARCHAR},'%')
    and  b.name like concat('%',#{param2,jdbcType=VARCHAR},'%')
    and  t.model like concat('%',#{param3,jdbcType=VARCHAR},'%')
    and  c.name like concat('%',#{param4,jdbcType=VARCHAR},'%')
    and  c.standard like concat('%',#{param5,jdbcType=VARCHAR},'%')
    and  d.name like concat('%',#{param6,jdbcType=VARCHAR},'%')
    and t.belong_id = #{param8,jdbcType=VARCHAR}
    and t.cancel_flag = '0'
    and t.out_flag = '0'
    and t.ware_id in (
    select m.ware_id from t_maint_userware m  where m.user_id = #{param7,jdbcType=VARCHAR}
     )
    order by b.code,t.layer desc
  </select>
  <select id="selectWareStackByDistributionStore" resultMap="StoreOfWareStack" >
    select distinct  t.ware_id ,t.stack_id
    from t_ware_store t where t.id in
    <foreach item="item" index="index" collection="storeId"
             open="(" separator="," close=")">
      #{item}
    </foreach>

  </select>


  <select id="selectByWareIdStackIdInStoreId" resultMap="BaseResultMap" >
    select *
    from t_ware_store t where t.id in
    <foreach item="item" index="index" collection="storeId"
             open="(" separator="," close=")">
      #{item}
    </foreach>
    and t.ware_id like concat('%',#{wareId,jdbcType=VARCHAR},'%')
    and t.stack_id like concat('%',#{stackId,jdbcType=VARCHAR},'%')
  </select>


  <update id="updateDistributionById"  >
    update  t_ware_store t
    set t.lock_flag = #{lockFlag,jdbcType=VARCHAR} where t.id in
    <foreach item="item" index="index" collection="storeId"
             open="(" separator="," close=")">
      #{item}
    </foreach>

  </update>




  <select id="selectForCustomer" resultMap="BaseResultMap" >
    select e.name as fk_compony_id,t.*
    from t_ware_store t left join t_maint_customer e
		on t.fk_compony_id = e.id
		, t_maint_ware a, t_maint_stack b, t_maint_material c,t_maint_customer d
    where t.ware_id = a.id
    and t.stack_id = b.id
    and t.material_id  = c.id
    and t.customer_id = d.id
    and  a.name like concat('%',#{param1,jdbcType=VARCHAR},'%')
    and  b.name like concat('%',#{param2,jdbcType=VARCHAR},'%')
    and  t.model like concat('%',#{param3,jdbcType=VARCHAR},'%')
    and  c.name like concat('%',#{param4,jdbcType=VARCHAR},'%')
    and  c.standard like concat('%',#{param5,jdbcType=VARCHAR},'%')
    <if test="param6 != null and param6 != ''" >
      and  d.name = #{param6,jdbcType=VARCHAR}
    </if>
    and  t.plate_no like concat('%',#{param7,jdbcType=VARCHAR},'%')
    and t.cancel_flag = '0'
    and t.out_flag = '0'
    and IFNULL(t.ord_no,'*') like  concat('%',#{param11,jdbcType=VARCHAR},'%')
    and d.name like CONCAT((select attr3 from sys_user temp where temp.id=#{param10,jdbcType=VARCHAR}),'%')
  </select>
  <select id="queryTotal" resultMap="TotalResultMap" >
    select sum(t.weight) as total_weight,
			 sum(t.count) as total_amount
	from t_ware_store t, t_maint_ware a, t_maint_stack b, t_maint_material c,t_maint_customer d
    where t.ware_id = a.id
    and t.stack_id = b.id
    and t.material_id  = c.id
    and t.customer_id = d.id
    and  a.name like concat('%',#{param1,jdbcType=VARCHAR},'%')
    and  b.name like concat('%',#{param2,jdbcType=VARCHAR},'%')
    and  t.model like concat('%',#{param3,jdbcType=VARCHAR},'%')
    and  c.name like concat('%',#{param4,jdbcType=VARCHAR},'%')
    and  c.standard like concat('%',#{param5,jdbcType=VARCHAR},'%')
    and  d.name like concat('%',#{param6,jdbcType=VARCHAR},'%')
    and  t.plate_no like concat('%',#{param7,jdbcType=VARCHAR},'%')
    and t.cancel_flag = '0'
    and t.out_flag = '0'
    and IFNULL(t.ord_no,'*') like  concat('%',#{param11,jdbcType=VARCHAR},'%')
    and d.name like CONCAT((select attr3 from sys_user temp where temp.id=#{param10,jdbcType=VARCHAR}),'%')
  </select>

  <select id="selEffStore" resultType="java.lang.Integer" parameterType="java.lang.String">
      select count(*)
      from t_ware_store t
      where t.cancel_flag = '0'
      and t.plate_no = #{param1,jdbcType=VARCHAR};

  </select>
</mapper>