<?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.DistributionMapper" >
  <resultMap id="BaseResultMap" type="com.th.demo.model.ware.Distribution" >
    <id column="id" property="id" jdbcType="VARCHAR" />
    <result column="truck_no" property="truckNo" jdbcType="VARCHAR" />
    <result column="driver_name" property="driverName" jdbcType="VARCHAR" />
    <result column="driver_phone" property="driverPhone" jdbcType="VARCHAR" />
    <result column="sum_weight" property="sumWeight" jdbcType="DOUBLE" />
    <result column="sum_count" property="sumCount" jdbcType="DOUBLE" />
    <result column="suttle" property="suttle" jdbcType="DOUBLE" />
    <result column="out_flag" property="outFlag" 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="delivery_company" property="deliveryCompany" jdbcType="VARCHAR" />
    <result column="distribution_no" property="distributionNo" jdbcType="VARCHAR" />
    <result column="tally_people" property="tallyPeople" jdbcType="VARCHAR" />


    <collection property="listDetail"   column="ID"  ofType="com.th.demo.model.ware.DistributionDetail"
                select="com.th.demo.mapping.ware.DistributionDetailMapper.selectByFidDist" />

  </resultMap>
  <sql id="Base_Column_List" >
    id, truck_no, driver_name, driver_phone, sum_weight, sum_count, suttle, out_flag, 
    add_id, add_time, modify_id, modify_time, cancel_id, cancel_time, cancel_flag, belong_id,delivery_company
  </sql>
  <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.String" >
    select *
    from t_ware_distribution
    where id = #{id,jdbcType=VARCHAR}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
    delete from t_ware_distribution
    where id = #{id,jdbcType=INTEGER}
  </delete>
  <insert id="insert" parameterType="com.th.demo.model.ware.Distribution" >

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


    insert into t_ware_distribution (id, truck_no, driver_name, 
      driver_phone, sum_weight, sum_count, 
      suttle, out_flag, add_id, 
      add_time, modify_id, modify_time, 
      cancel_id, cancel_time, cancel_flag, 
      belong_id ,delivery_company)
    values (#{id,jdbcType=VARCHAR}, #{truckNo,jdbcType=VARCHAR}, #{driverName,jdbcType=VARCHAR},
      #{driverPhone,jdbcType=VARCHAR}, #{sumWeight,jdbcType=DOUBLE}, #{sumCount,jdbcType=DOUBLE}, 
      #{suttle,jdbcType=DOUBLE}, #{outFlag,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},#{deliveryCompany,jdbcType=VARCHAR})
  </insert>
  <insert id="insertSelective" parameterType="com.th.demo.model.ware.Distribution" >
    insert into t_ware_distribution
    <trim prefix="(" suffix=")" suffixOverrides="," >
      <if test="id != null" >
        id,
      </if>
      <if test="truckNo != null" >
        truck_no,
      </if>
      <if test="driverName != null" >
        driver_name,
      </if>
      <if test="driverPhone != null" >
        driver_phone,
      </if>
      <if test="sumWeight != null" >
        sum_weight,
      </if>
      <if test="sumCount != null" >
        sum_count,
      </if>
      <if test="suttle != null" >
        suttle,
      </if>
      <if test="outFlag != null" >
        out_flag,
      </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=INTEGER},
      </if>
      <if test="truckNo != null" >
        #{truckNo,jdbcType=INTEGER},
      </if>
      <if test="driverName != null" >
        #{driverName,jdbcType=VARCHAR},
      </if>
      <if test="driverPhone != null" >
        #{driverPhone,jdbcType=VARCHAR},
      </if>
      <if test="sumWeight != null" >
        #{sumWeight,jdbcType=DOUBLE},
      </if>
      <if test="sumCount != null" >
        #{sumCount,jdbcType=DOUBLE},
      </if>
      <if test="suttle != null" >
        #{suttle,jdbcType=DOUBLE},
      </if>
      <if test="outFlag != null" >
        #{outFlag,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.Distribution" >
    update t_ware_distribution
    <set >
      <if test="truckNo != null" >
        truck_no = #{truckNo,jdbcType=INTEGER},
      </if>
      <if test="driverName != null" >
        driver_name = #{driverName,jdbcType=VARCHAR},
      </if>
      <if test="driverPhone != null" >
        driver_phone = #{driverPhone,jdbcType=VARCHAR},
      </if>
      <if test="sumWeight != null" >
        sum_weight = #{sumWeight,jdbcType=DOUBLE},
      </if>
      <if test="sumCount != null" >
        sum_count = #{sumCount,jdbcType=DOUBLE},
      </if>
      <if test="suttle != null" >
        suttle = #{suttle,jdbcType=DOUBLE},
      </if>
      <if test="outFlag != null" >
        out_flag = #{outFlag,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=INTEGER}
  </update>
  <update id="updateByPrimaryKey" parameterType="com.th.demo.model.ware.Distribution" >
    update t_ware_distribution
    set truck_no = #{truckNo,jdbcType=VARCHAR},
      driver_name = #{driverName,jdbcType=VARCHAR},
      driver_phone = #{driverPhone,jdbcType=VARCHAR},
      sum_weight = #{sumWeight,jdbcType=DOUBLE},
      sum_count = #{sumCount,jdbcType=DOUBLE},
      suttle = #{suttle,jdbcType=DOUBLE},
      out_flag = #{outFlag,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},
      delivery_company = #{deliveryCompany,jdbcType=VARCHAR},
      distribution_no =ifnull(distribution_no,(select dis_no from (select concat('MJ',DATE_FORMAT(NOW(), '%Y%m%d'),IFNULL(lpad(max(substring(t.distribution_no,11,3)+1),3,'0'),'001')) as dis_no from t_ware_distribution t
where substring(t.distribution_no,3,8) = DATE_FORMAT(NOW(), '%Y%m%d')) temp))
      ,tally_people    = #{tallyPeople,jdbcType=VARCHAR}
    where id = #{id,jdbcType=VARCHAR}
  </update>



  <select id="selectWaitTrucks" resultMap="BaseResultMap" >
    select    t.*    from t_ware_distribution t
    where  ifnull(t.out_flag,'0') = '0'
    and t.belong_id=#{param2,jdbcType=VARCHAR}
    order by t.add_time desc
  </select>

  <select id="selectByTruckNo" resultMap="BaseResultMap" >
    select    t.*    from t_ware_distribution t
    where  ifnull(t.out_flag,'0') = '0'
    and t.truck_no = #{param1,jdbcType=VARCHAR}
    and t.belong_id = #{param2,jdbcType=VARCHAR}
    order by t.add_time desc
  </select>

</mapper>