<?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.maint.StackMapper" >
  <resultMap id="BaseResultMap" type="com.th.demo.model.maint.Stack" >
    <id column="id" property="id" jdbcType="VARCHAR" />

    <result column="name" property="name" jdbcType="VARCHAR" />
    <result column="code" property="code" 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="stack_x" property="stackX" jdbcType="INTEGER" />
    <result column="stack_y" property="stackY" jdbcType="INTEGER" />
    <result column="width" property="width" jdbcType="INTEGER" />
    <result column="height" property="height" jdbcType="INTEGER" />
    <result column="is_layer" property="isLayer" jdbcType="VARCHAR" />
    <result column="on_stack_amt" property="onStackAmt" jdbcType="INTEGER" />
    <association  column="ware_id" property="ware" select="com.th.demo.mapping.maint.WareMapper.selectByPrimaryKey" />
  </resultMap>
  <sql id="Base_Column_List" >
    id, ware_id, name, code, add_id, add_time, modify_id, modify_time, cancel_id, cancel_time, 
    cancel_flag, belong_id, stack_x, stack_y, width, height,is_layer
  </sql>
  <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.String" >
    select 
    <include refid="Base_Column_List" />
    from t_maint_stack
    where id = #{id,jdbcType=VARCHAR}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.String" >
    delete from t_maint_stack
    where id = #{id,jdbcType=VARCHAR}
  </delete>
  <insert id="insert" parameterType="com.th.demo.model.maint.Stack" >
    <selectKey keyProperty="id" order="BEFORE" resultType="java.lang.String">
      SELECT  REPLACE(UUID(),'-','') as id from dual
    </selectKey>


    insert into t_maint_stack (id, ware_id, name, 
      code, add_id, add_time, 
      modify_id, modify_time, cancel_id, 
      cancel_time, cancel_flag, belong_id,
    stack_x, stack_y, width, height,is_layer)
    values (#{id,jdbcType=VARCHAR}, #{ware.id,jdbcType=VARCHAR}, #{name,jdbcType=VARCHAR},
      #{code,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},
    #{stackX,jdbcType=INTEGER}, #{stackY,jdbcType=INTEGER}, #{width,jdbcType=INTEGER},
    #{height,jdbcType=INTEGER},#{isLayer,jdbcType=INTEGER})
  </insert>
  <insert id="insertSelective" parameterType="com.th.demo.model.maint.Stack" >
    insert into t_maint_stack
    <trim prefix="(" suffix=")" suffixOverrides="," >
      <if test="id != null" >
        id,
      </if>
      <if test="wareId != null" >
        ware_id,
      </if>
      <if test="name != null" >
        name,
      </if>
      <if test="code != null" >
        code,
      </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="wareId != null" >
        #{wareId,jdbcType=VARCHAR},
      </if>
      <if test="name != null" >
        #{name,jdbcType=VARCHAR},
      </if>
      <if test="code != null" >
        #{code,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.maint.Stack" >
    update t_maint_stack
    <set >
      <if test="wareId != null" >
        ware_id = #{wareId,jdbcType=VARCHAR},
      </if>
      <if test="name != null" >
        name = #{name,jdbcType=VARCHAR},
      </if>
      <if test="code != null" >
        code = #{code,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.maint.Stack" >
    update t_maint_stack
    set ware_id = #{ware.id,jdbcType=VARCHAR},
      name = #{name,jdbcType=VARCHAR},
      code = #{code,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},
        stack_x = #{stackX,jdbcType=INTEGER},
      stack_y = #{stackY,jdbcType=INTEGER},
      width = #{width,jdbcType=INTEGER},
      height = #{height,jdbcType=INTEGER},
     is_layer = #{isLayer,jdbcType=INTEGER}
    where id = #{id,jdbcType=VARCHAR}
  </update>



  <select id="select" resultMap="BaseResultMap" >
    select t.*
    from t_maint_stack t ,t_maint_ware d
    where (t.name like concat('%',#{param1,jdbcType=VARCHAR},'%') or t.code like concat('%',#{param1,jdbcType=VARCHAR},'%'))
     and t.ware_id = d.id
    <if test="param2 != ''" >
      and  d.name = #{param2,jdbcType=VARCHAR}
    </if>
    and   t.belong_id = #{param3,jdbcType=VARCHAR}
    and ifnull(t.cancel_flag,'0') = '0'
    order by t.code
  </select>



  <select id="selectByWareId" resultMap="BaseResultMap" >
    select t.*
       ,b.on_stack_amt
    from t_maint_stack t
        left outer join (select temp.stack_id
                 ,count(*) as on_stack_amt
          from t_ware_store temp
          where (temp.cancel_flag is null  or temp.cancel_flag = '0')
          and (temp.out_flag is null or temp.out_flag = '0')
          group by temp.stack_id) b
    on t.id = b.stack_id
    where   t.ware_id =  #{param1,jdbcType=VARCHAR}
    and   t.belong_id =  #{param2,jdbcType=VARCHAR}
    and ifnull(t.cancel_flag,'0') = '0'
    order by t.code

  </select>


  <select id="selectByWareNameStackName" resultMap="BaseResultMap" >
    select t.*
    from t_maint_stack t ,t_maint_ware d
    where t.ware_id = d.id
    and d.id in  (
    select  a.ware_id from t_maint_userware a where  a.user_id = #{param3,jdbcType=VARCHAR}
    )  and d.name = #{param1,jdbcType=VARCHAR}
    and t.name = #{param2,jdbcType=VARCHAR}
    and   t.belong_id = #{param4,jdbcType=VARCHAR}
    and ifnull(t.cancel_flag,'0') = '0'
    order by t.code
  </select>

  <select id="selectByWareIdAreaId" resultMap="BaseResultMap" >
    select t.* ,b.on_stack_amt
    from t_maint_stack t
           left outer join (select temp.stack_id
                                 ,count(*) as on_stack_amt
                            from t_ware_store temp
                            where (temp.cancel_flag is null  or temp.cancel_flag = '0')
                              and (temp.out_flag is null or temp.out_flag = '0')
                            group by temp.stack_id) b
                           on t.id = b.stack_id
    where   t.ware_id =  #{param1,jdbcType=VARCHAR}
      and   t.belong_id =  #{param3,jdbcType=VARCHAR}
      and   t.code like concat('_',#{param2,jdbcType=VARCHAR},'%')
      and ifnull(t.cancel_flag,'0') = '0'
    order by t.code

  </select>
</mapper>