<?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.goafanti.common.dao.TOrderDunMapper">
  <resultMap id="BaseResultMap" type="com.goafanti.common.model.TOrderDun">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Mon Nov 19 16:53:48 CST 2018.
    -->
    <id column="id" jdbcType="VARCHAR" property="id" />
    <result column="create_time" jdbcType="TIMESTAMP" property="createTime" />
    <result column="start_time" jdbcType="DATE" property="startTime" />
    <result column="end_time" jdbcType="DATE" property="endTime" />
    <result column="order_no" jdbcType="VARCHAR" property="orderNo" />
    <result column="dun_subject" jdbcType="INTEGER" property="dunSubject" />
    <result column="dun_status" jdbcType="INTEGER" property="dunStatus" />
    <result column="dun_by" jdbcType="VARCHAR" property="dunBy" />
    <result column="dun_target" jdbcType="VARCHAR" property="dunTarget" />
    <result column="money" jdbcType="DECIMAL" property="money" />
    
  </resultMap>
  <sql id="Example_Where_Clause">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Mon Nov 19 16:53:48 CST 2018.
    -->
    <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>
  <sql id="Update_By_Example_Where_Clause">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Mon Nov 19 16:53:48 CST 2018.
    -->
    <where>
      <foreach collection="example.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>
  <sql id="Base_Column_List">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Mon Nov 19 16:53:48 CST 2018.
    -->
    id, create_time, start_time, end_time, order_no, dun_subject, dun_status, dun_by, 
    dun_target,money
  </sql>
  <select id="selectByExample" parameterType="com.goafanti.common.model.TOrderDunExample" resultMap="BaseResultMap">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Mon Nov 19 16:53:48 CST 2018.
    -->
    select
    <if test="distinct">
      distinct
    </if>
    <include refid="Base_Column_List" />
    from t_order_dun
    <if test="_parameter != null">
      <include refid="Example_Where_Clause" />
    </if>
    <if test="orderByClause != null">
      order by ${orderByClause}
    </if>
  </select>
  <select id="selectByPrimaryKey" parameterType="java.lang.String" resultMap="BaseResultMap">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Mon Nov 19 16:53:48 CST 2018.
    -->
    select 
    <include refid="Base_Column_List" />
    from t_order_dun
    where id = #{id,jdbcType=VARCHAR}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.String">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Mon Nov 19 16:53:48 CST 2018.
    -->
    delete from t_order_dun
    where id = #{id,jdbcType=VARCHAR}
  </delete>
  <delete id="deleteByExample" parameterType="com.goafanti.common.model.TOrderDunExample">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Mon Nov 19 16:53:48 CST 2018.
    -->
    delete from t_order_dun
    <if test="_parameter != null">
      <include refid="Example_Where_Clause" />
    </if>
  </delete>
  <insert id="insert" parameterType="com.goafanti.common.model.TOrderDun">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Mon Nov 19 16:53:48 CST 2018.
    -->
    insert into t_order_dun (id, create_time, start_time, 
      end_time, order_no, dun_subject, 
      dun_status, dun_by, dun_target,money
      )
    values (#{id,jdbcType=VARCHAR}, #{createTime,jdbcType=TIMESTAMP}, #{startTime,jdbcType=DATE}, 
      #{endTime,jdbcType=DATE}, #{orderNo,jdbcType=VARCHAR}, #{dunSubject,jdbcType=INTEGER}, 
      #{dunStatus,jdbcType=INTEGER}, #{dunBy,jdbcType=VARCHAR}, #{dunTarget,jdbcType=VARCHAR},
      #{money,jdbcType=DECIMAL}
      )
  </insert>
  <insert id="insertSelective" parameterType="com.goafanti.common.model.TOrderDun">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Mon Nov 19 16:53:48 CST 2018.
    -->
    insert into t_order_dun
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="id != null">
        id,
      </if>
      <if test="createTime != null">
        create_time,
      </if>
      <if test="startTime != null">
        start_time,
      </if>
      <if test="endTime != null">
        end_time,
      </if>
      <if test="orderNo != null">
        order_no,
      </if>
      <if test="dunSubject != null">
        dun_subject,
      </if>
      <if test="dunStatus != null">
        dun_status,
      </if>
      <if test="dunBy != null">
        dun_by,
      </if>
      <if test="dunTarget != null">
        dun_target,
      </if>
      <if test="money != null">
      money,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
      <if test="id != null">
        #{id,jdbcType=VARCHAR},
      </if>
      <if test="createTime != null">
        #{createTime,jdbcType=TIMESTAMP},
      </if>
      <if test="startTime != null">
        #{startTime,jdbcType=DATE},
      </if>
      <if test="endTime != null">
        #{endTime,jdbcType=DATE},
      </if>
      <if test="orderNo != null">
        #{orderNo,jdbcType=VARCHAR},
      </if>
      <if test="dunSubject != null">
        #{dunSubject,jdbcType=INTEGER},
      </if>
      <if test="dunStatus != null">
        #{dunStatus,jdbcType=INTEGER},
      </if>
      <if test="dunBy != null">
        #{dunBy,jdbcType=VARCHAR},
      </if>
      <if test="dunTarget != null">
        #{dunTarget,jdbcType=VARCHAR},
      </if>
        <if test="money != null">
      #{money,jdbcType=DECIMAL},
      </if>
    </trim>
  </insert>
  <select id="countByExample" parameterType="com.goafanti.common.model.TOrderDunExample" resultType="java.lang.Long">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Mon Nov 19 16:53:48 CST 2018.
    -->
    select count(*) from t_order_dun
    <if test="_parameter != null">
      <include refid="Example_Where_Clause" />
    </if>
  </select>
  <update id="updateByExampleSelective" parameterType="map">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Mon Nov 19 16:53:48 CST 2018.
    -->
    update t_order_dun
    <set>
      <if test="record.id != null">
        id = #{record.id,jdbcType=VARCHAR},
      </if>
      <if test="record.createTime != null">
        create_time = #{record.createTime,jdbcType=TIMESTAMP},
      </if>
      <if test="record.startTime != null">
        start_time = #{record.startTime,jdbcType=DATE},
      </if>
      <if test="record.endTime != null">
        end_time = #{record.endTime,jdbcType=DATE},
      </if>
      <if test="record.orderNo != null">
        order_no = #{record.orderNo,jdbcType=VARCHAR},
      </if>
      <if test="record.dunSubject != null">
        dun_subject = #{record.dunSubject,jdbcType=INTEGER},
      </if>
      <if test="record.dunStatus != null">
        dun_status = #{record.dunStatus,jdbcType=INTEGER},
      </if>
      <if test="record.dunBy != null">
        dun_by = #{record.dunBy,jdbcType=VARCHAR},
      </if>
      <if test="record.dunTarget != null">
        dun_target = #{record.dunTarget,jdbcType=VARCHAR},
      </if>
      <if test="record.money != null">
      	money = #{money,jdbcType=DECIMAL},
      </if>
    </set>
    <if test="_parameter != null">
      <include refid="Update_By_Example_Where_Clause" />
    </if>
  </update>
  <update id="updateByExample" parameterType="map">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Mon Nov 19 16:53:48 CST 2018.
    -->
    update t_order_dun
    set id = #{record.id,jdbcType=VARCHAR},
      create_time = #{record.createTime,jdbcType=TIMESTAMP},
      start_time = #{record.startTime,jdbcType=DATE},
      end_time = #{record.endTime,jdbcType=DATE},
      order_no = #{record.orderNo,jdbcType=VARCHAR},
      dun_subject = #{record.dunSubject,jdbcType=INTEGER},
      dun_status = #{record.dunStatus,jdbcType=INTEGER},
      dun_by = #{record.dunBy,jdbcType=VARCHAR},
      dun_target = #{record.dunTarget,jdbcType=VARCHAR},
      money = #{money,jdbcType=DECIMAL}
    <if test="_parameter != null">
      <include refid="Update_By_Example_Where_Clause" />
    </if>
  </update>
  <update id="updateByPrimaryKeySelective" parameterType="com.goafanti.common.model.TOrderDun">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Mon Nov 19 16:53:48 CST 2018.
    -->
    update t_order_dun
    <set>
      <if test="createTime != null">
        create_time = #{createTime,jdbcType=TIMESTAMP},
      </if>
      <if test="startTime != null">
        start_time = #{startTime,jdbcType=DATE},
      </if>
      <if test="endTime != null">
        end_time = #{endTime,jdbcType=DATE},
      </if>
      <if test="orderNo != null">
        order_no = #{orderNo,jdbcType=VARCHAR},
      </if>
      <if test="dunSubject != null">
        dun_subject = #{dunSubject,jdbcType=INTEGER},
      </if>
      <if test="dunStatus != null">
        dun_status = #{dunStatus,jdbcType=INTEGER},
      </if>
      <if test="dunBy != null">
        dun_by = #{dunBy,jdbcType=VARCHAR},
      </if>
      <if test="dunTarget != null">
        dun_target = #{dunTarget,jdbcType=VARCHAR},
      </if>
       <if test="money != null">
      	money = #{money,jdbcType=DECIMAL},
      </if>
    </set>
    where id = #{id,jdbcType=VARCHAR}
  </update>
  <update id="updateByPrimaryKey" parameterType="com.goafanti.common.model.TOrderDun">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Mon Nov 19 16:53:48 CST 2018.
    -->
    update t_order_dun
    set create_time = #{createTime,jdbcType=TIMESTAMP},
      start_time = #{startTime,jdbcType=DATE},
      end_time = #{endTime,jdbcType=DATE},
      order_no = #{orderNo,jdbcType=VARCHAR},
      dun_subject = #{dunSubject,jdbcType=INTEGER},
      dun_status = #{dunStatus,jdbcType=INTEGER},
      dun_by = #{dunBy,jdbcType=VARCHAR},
      dun_target = #{dunTarget,jdbcType=VARCHAR},
      money = #{money,jdbcType=DECIMAL},
    where id = #{id,jdbcType=VARCHAR}
  </update>
  <select id="selectByOrderNoDunSubject" resultType="com.goafanti.common.model.TOrderDun">
  	select id,dun_subject as dunSubject,money,order_no as orderNo,dun_status as dunStatus from t_order_dun 
    where order_no= #{orderNo,jdbcType=VARCHAR} order by dun_subject 
  </select>
  
  <select id="selectByOrderNo" parameterType="java.lang.String" resultMap="BaseResultMap">
    select 
    <include refid="Base_Column_List" />
    from t_order_dun
    where order_no= #{orderNo,jdbcType=VARCHAR} order by dun_subject 
  </select>
  <select id="getOrderDunData" resultType="com.goafanti.order.bo.TOrderNewBo">
select a.id,a.order_no as orderNo,date_format(b.sign_time,'%Y-%m-%d') as signDate,
    c.nickname as userName,b.order_status as orderStatus,b.total_amount as totalAmount,
    b.project_status as projectStatus,b.liquidation_status as liquidationStatus,
    b.settlement_amount as settlementAmount,d.name as salesmanName,a.dun_subject as dunSubject,
    date_format(a.start_time, '%Y-%m-%d') as startDate,dep.name as depName,
    a.money as accountsReceivable,(a.money-b.settlement_amount) as uncollectedAmount
    from t_order_dun a left join t_order_new b on a.order_no=b.order_no
    left join `user` c on b.buyer_id=c.id 
    left join admin d on b.salesman_id=d.id
    left join department dep on d.department_id=dep.id
    where a.start_time is not null and b.delete_sign = 0
    <if test="name != null and name != ''">
  	and c.nickname like CONCAT('%',#{name,jdbcType=VARCHAR},'%')
  	</if>
  		<if test="adminName != null">
  	and d.name  like CONCAT('%',#{adminName,jdbcType=VARCHAR},'%')
  	</if>
  	<if test="depId != null">
  	and dep.id= #{depId,jdbcType=VARCHAR}
  	</if>
  	<if test="orderNo != null and orderNo != ''">
  	and a.order_no = #{orderNo,jdbcType=VARCHAR}
  	</if>
  	<if test="starTime !=null and endTime !=null and starTime !='' and endTime != '' ">
  	and b.sign_time between #{starTime,jdbcType=VARCHAR} and #{endTime,jdbcType=VARCHAR}
  	</if>
  	order by b.sign_time desc
  </select>
  
  <select id="getTaskAllOrderDun" resultType="com.goafanti.order.bo.OrderDunTaskBo">
    select a.id,a.order_no as orderNo,a.dun_subject as dunSubject,a.money,settlement_amount as settlementAmount,
 	b.project_status as projectStatus,b.order_status as orderStatus from t_order_dun a
 	left join t_order_new b on a.order_no=b.order_no
 	where a.dun_status=0 and b.delete_sign=0
    </select>
    <select id="checkOrderDun" resultType="java.lang.Integer">
    select count(*) from t_order_dun
    where order_no=  #{orderNo,jdbcType=VARCHAR}
    and dun_subject= #{dunSubject,jdbcType=VARCHAR}
    </select>
    <update id="deleteByOrderNo">
     delete from t_order_dun
    where order_no = #{orderNo,jdbcType=VARCHAR}
    </update>
    <insert id="insertBatch" parameterType="com.goafanti.common.model.TOrderDun">
     insert into t_order_dun (id, create_time, start_time, 
      end_time, order_no, dun_subject, 
      dun_status, dun_by, dun_target,money
      )
    values 
    <foreach item="item" index="index" collection="list" separator=",">
    (#{item.id,jdbcType=VARCHAR}, #{item.createTime,jdbcType=TIMESTAMP}, #{item.startTime,jdbcType=DATE}, 
      #{item.endTime,jdbcType=DATE}, #{item.orderNo,jdbcType=VARCHAR}, #{item.dunSubject,jdbcType=INTEGER}, 
      #{item.dunStatus,jdbcType=INTEGER}, #{item.dunBy,jdbcType=VARCHAR}, #{item.dunTarget,jdbcType=VARCHAR},
      #{item.money,jdbcType=DECIMAL}
      )
      </foreach>
    </insert>
</mapper>