<?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.DeficitRecordMapper">
    <resultMap id="BaseResultMap" type="com.th.demo.model.ware.DeficitRecord">
        <id column="id" property="id" jdbcType="VARCHAR"/>
        <result column="ware_name" property="wareName" jdbcType="VARCHAR"/>
        <result column="stack_name" property="stackName" jdbcType="VARCHAR"/>
        <result column="layer" property="layer" jdbcType="INTEGER"/>
        <result column="material_name" property="materialName" jdbcType="VARCHAR"/>
        <result column="standard" property="standard" jdbcType="VARCHAR"/>
        <result column="model" property="model" jdbcType="VARCHAR"/>
        <result column="customer_name" property="customerName" 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="receive_address" property="receiveAddress" jdbcType="VARCHAR"/>
        <result column="remark" property="remark" jdbcType="VARCHAR"/>
        <result column="record_type" property="recordType" 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="store_id" property="storeId" jdbcType="VARCHAR"/>
        <result column="reason" property="reason" jdbcType="VARCHAR"/>
        <result column="back_flag" property="backFlag" 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" />

        <association column="add_id" property="addUser"
                     select="com.th.demo.mapping.system.SysUserMapper.selectByPrimaryKey"/>
    </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_name, stack_name, layer, material_name, standard, model, customer_name, 
    plate_no, count, weight, receive_address, remark, record_type, add_id, add_time, 
    modify_id, modify_time, cancel_id, cancel_time, cancel_flag, belong_id,store_id,reason,
    back_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_deficit_record
        where id = #{id,jdbcType=VARCHAR}
    </select>
    <delete id="deleteByPrimaryKey" parameterType="java.lang.String">
    delete from t_ware_deficit_record
    where id = #{id,jdbcType=VARCHAR}
  </delete>
    <insert id="insert" parameterType="com.th.demo.model.ware.DeficitRecord">

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


        insert into t_ware_deficit_record (id, ware_name, stack_name,
        layer, material_name, standard,
        model, customer_name, plate_no,
        count, weight, receive_address,
        remark, record_type, add_id,
        add_time, modify_id, modify_time,
        cancel_id, cancel_time, cancel_flag,
        belong_id,store_id,reason,back_flag
        ,wgt_dcn_mtc_cd,edge_ty,production_place)
        values (#{id,jdbcType=VARCHAR}, #{wareName,jdbcType=VARCHAR}, #{stackName,jdbcType=VARCHAR},
        #{layer,jdbcType=INTEGER}, #{materialName,jdbcType=VARCHAR}, #{standard,jdbcType=VARCHAR},
        #{model,jdbcType=VARCHAR}, #{customerName,jdbcType=VARCHAR}, #{plateNo,jdbcType=VARCHAR},
        #{count,jdbcType=DOUBLE}, #{weight,jdbcType=DOUBLE}, #{receiveAddress,jdbcType=VARCHAR},
        #{remark,jdbcType=VARCHAR}, #{recordType,jdbcType=VARCHAR}, #{addUser.id,jdbcType=VARCHAR},
        #{addTime,jdbcType=TIMESTAMP}, #{modifyId,jdbcType=VARCHAR}, #{modifyTime,jdbcType=TIMESTAMP},
        #{cancelId,jdbcType=VARCHAR}, #{cancelTime,jdbcType=TIMESTAMP}, #{cancelFlag,jdbcType=VARCHAR},
        #{belongId,jdbcType=VARCHAR},#{storeId,jdbcType=VARCHAR},#{reason,jdbcType=VARCHAR},
        #{backFlag,jdbcType=VARCHAR} ,#{wgtDcnMtcCd,jdbcType=VARCHAR},#{edgeTy,jdbcType=VARCHAR},#{productionPlace,jdbcType=VARCHAR})
    </insert>
    <insert id="insertSelective" parameterType="com.th.demo.model.ware.DeficitRecord">
        insert into t_ware_deficit_record
        <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="standard != null">
                standard,
            </if>
            <if test="model != null">
                model,
            </if>
            <if test="customerName != null">
                customer_name,
            </if>
            <if test="plateNo != null">
                plate_no,
            </if>
            <if test="count != null">
                count,
            </if>
            <if test="weight != null">
                weight,
            </if>
            <if test="receiveAddress != null">
                receive_address,
            </if>
            <if test="remark != null">
                remark,
            </if>
            <if test="recordType != null">
                record_type,
            </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="standard != null">
                #{standard,jdbcType=VARCHAR},
            </if>
            <if test="model != null">
                #{model,jdbcType=VARCHAR},
            </if>
            <if test="customerName != null">
                #{customerName,jdbcType=VARCHAR},
            </if>
            <if test="plateNo != null">
                #{plateNo,jdbcType=VARCHAR},
            </if>
            <if test="count != null">
                #{count,jdbcType=DOUBLE},
            </if>
            <if test="weight != null">
                #{weight,jdbcType=DOUBLE},
            </if>
            <if test="receiveAddress != null">
                #{receiveAddress,jdbcType=VARCHAR},
            </if>
            <if test="remark != null">
                #{remark,jdbcType=VARCHAR},
            </if>
            <if test="recordType != null">
                #{recordType,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.DeficitRecord">
        update t_ware_deficit_record
        <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="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="plateNo != null">
                plate_no = #{plateNo,jdbcType=VARCHAR},
            </if>
            <if test="count != null">
                count = #{count,jdbcType=DOUBLE},
            </if>
            <if test="weight != null">
                weight = #{weight,jdbcType=DOUBLE},
            </if>
            <if test="receiveAddress != null">
                receive_address = #{receiveAddress,jdbcType=VARCHAR},
            </if>
            <if test="remark != null">
                remark = #{remark,jdbcType=VARCHAR},
            </if>
            <if test="recordType != null">
                record_type = #{recordType,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.DeficitRecord">
    update t_ware_deficit_record
    set ware_name = #{wareName,jdbcType=VARCHAR},
      stack_name = #{stackName,jdbcType=VARCHAR},
      layer = #{layer,jdbcType=INTEGER},
      material_name = #{materialName,jdbcType=VARCHAR},
      standard = #{standard,jdbcType=VARCHAR},
      model = #{model,jdbcType=VARCHAR},
      customer_name = #{customerName,jdbcType=VARCHAR},
      plate_no = #{plateNo,jdbcType=VARCHAR},
      count = #{count,jdbcType=DOUBLE},
      weight = #{weight,jdbcType=DOUBLE},
      receive_address = #{receiveAddress,jdbcType=VARCHAR},
      remark = #{remark,jdbcType=VARCHAR},
      record_type = #{recordType,jdbcType=VARCHAR},
      add_id = #{addUser.id,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},
      store_id = #{storeId,jdbcType=VARCHAR},
      reason = #{reason,jdbcType=VARCHAR},
         back_flag = #{backFlag,jdbcType=VARCHAR}
    where id = #{id,jdbcType=VARCHAR}
  </update>


    <select id="select" resultMap="BaseResultMap">
    select
   t.*
    from t_ware_deficit_record t
     where t.ware_name  like concat('%',#{param1,jdbcType=VARCHAR},'%')
    and t.stack_name like concat('%',#{param2,jdbcType=VARCHAR},'%')
    and t.model like concat('%',#{param3,jdbcType=INTEGER},'%')
    and t.material_name like concat('%',#{param4,jdbcType=VARCHAR},'%')
    and t.standard like concat('%',#{param5,jdbcType=VARCHAR},'%')
     and t.customer_name like concat('%',#{param6,jdbcType=VARCHAR},'%')
      and t.plate_no like concat('%',#{param7,jdbcType=VARCHAR},'%')
      and t.add_time between #{param8,jdbcType=TIMESTAMP} and #{param9,jdbcType=TIMESTAMP}
     and  t.ware_name in (
    select b.name from t_maint_userware a ,t_maint_ware b  where a.ware_id = b.id
    and  a.user_id = #{param10,jdbcType=VARCHAR}  )
    and t.belong_id=#{param11,jdbcType=VARCHAR}
    order by t.add_time desc
  </select>

    <select id="selectTotal" resultMap="TotalResultMap">
    select
    sum(t.count) as total_amount
      ,sum(t.weight) as total_weight
    from t_ware_deficit_record t
     where t.ware_name  like concat('%',#{param1,jdbcType=VARCHAR},'%')
    and t.stack_name like concat('%',#{param2,jdbcType=VARCHAR},'%')
    and t.model like concat('%',#{param3,jdbcType=INTEGER},'%')
    and t.material_name like concat('%',#{param4,jdbcType=VARCHAR},'%')
    and t.standard like concat('%',#{param5,jdbcType=VARCHAR},'%')
     and t.customer_name like concat('%',#{param6,jdbcType=VARCHAR},'%')
      and t.plate_no like concat('%',#{param7,jdbcType=VARCHAR},'%')
      and t.add_time between #{param8,jdbcType=TIMESTAMP} and #{param9,jdbcType=TIMESTAMP}
     and  t.ware_name in (
    select b.name from t_maint_userware a ,t_maint_ware b  where a.ware_id = b.id
    and  a.user_id = #{param10,jdbcType=VARCHAR}  )
    and t.belong_id=#{param11,jdbcType=VARCHAR}
    order by t.add_time desc
  </select>


</mapper>