<?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.NewOrderDunMapper">
  <resultMap id="BaseResultMap" type="com.goafanti.common.model.NewOrderDun">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Fri May 15 09:46:56 CST 2020.
    -->
    <id column="id" jdbcType="INTEGER" property="id" />
    <result column="order_no" jdbcType="VARCHAR" property="orderNo" />
    <result column="tid" jdbcType="INTEGER" property="tid" />
    <result column="project_type" jdbcType="INTEGER" property="projectType" />
    <result column="dun_type" jdbcType="INTEGER" property="dunType" />
    <result column="wait_day" jdbcType="INTEGER" property="waitDay" />
    <result column="effective_count" jdbcType="INTEGER" property="effectiveCount" />
    <result column="aid" jdbcType="VARCHAR" property="aid" />
    <result column="status" jdbcType="INTEGER" property="status" />
    <result column="create_time" jdbcType="TIMESTAMP" property="createTime" />
    <result column="start_time" jdbcType="TIMESTAMP" property="startTime" />
    <result column="money" jdbcType="DECIMAL" property="money" />
    <result column="appropriation_ratio" jdbcType="DECIMAL" property="appropriationRatio" />
    <result column="customize_name" jdbcType="VARCHAR" property="customizeName" />
    <result column="customize_time" jdbcType="TIMESTAMP" property="customizeTime" />
  </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 Fri May 15 09:46:56 CST 2020.
    -->
    <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 Fri May 15 09:46:56 CST 2020.
    -->
    <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 Fri May 15 09:46:56 CST 2020.
    -->
    id, order_no, tid, project_type, dun_type, wait_day, effective_count, aid, status,
    create_time, start_time, money, appropriation_ratio, customize_name, customize_time
  </sql>
  <select id="selectByExample" parameterType="com.goafanti.common.model.NewOrderDunExample" resultMap="BaseResultMap">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Fri May 15 09:46:56 CST 2020.
    -->
    select
    <if test="distinct">
      distinct
    </if>
    <include refid="Base_Column_List" />
    from new_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.Integer" resultMap="BaseResultMap">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Fri May 15 09:46:56 CST 2020.
    -->
    select
    <include refid="Base_Column_List" />
    from new_order_dun
    where id = #{id,jdbcType=INTEGER}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Fri May 15 09:46:56 CST 2020.
    -->
    delete from new_order_dun
    where id = #{id,jdbcType=INTEGER}
  </delete>
  <delete id="deleteByExample" parameterType="com.goafanti.common.model.NewOrderDunExample">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Fri May 15 09:46:56 CST 2020.
    -->
    delete from new_order_dun
    <if test="_parameter != null">
      <include refid="Example_Where_Clause" />
    </if>
  </delete>
  <insert id="insert" parameterType="com.goafanti.common.model.NewOrderDun">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Fri May 15 09:46:56 CST 2020.
    -->
    insert into new_order_dun (id, order_no, tid,
      project_type, dun_type, wait_day,
      effective_count, aid, status,
      create_time, start_time, money,
      appropriation_ratio, customize_name, customize_time
      )
    values (#{id,jdbcType=INTEGER}, #{orderNo,jdbcType=VARCHAR}, #{tid,jdbcType=INTEGER},
      #{projectType,jdbcType=INTEGER}, #{dunType,jdbcType=INTEGER}, #{waitDay,jdbcType=INTEGER},
      #{effectiveCount,jdbcType=INTEGER}, #{aid,jdbcType=VARCHAR}, #{status,jdbcType=INTEGER},
      #{createTime,jdbcType=TIMESTAMP}, #{startTime,jdbcType=TIMESTAMP}, #{money,jdbcType=DECIMAL},
      #{appropriationRatio,jdbcType=DECIMAL}, #{customizeName,jdbcType=VARCHAR}, #{customizeTime,jdbcType=TIMESTAMP}
      )
  </insert>
  <insert id="insertSelective" parameterType="com.goafanti.common.model.NewOrderDun">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Fri May 15 09:46:56 CST 2020.
    -->
    insert into new_order_dun
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="id != null">
        id,
      </if>
      <if test="orderNo != null">
        order_no,
      </if>
      <if test="tid != null">
        tid,
      </if>
      <if test="projectType != null">
        project_type,
      </if>
      <if test="dunType != null">
        dun_type,
      </if>
      <if test="waitDay != null">
        wait_day,
      </if>
      <if test="effectiveCount != null">
        effective_count,
      </if>
      <if test="aid != null">
        aid,
      </if>
      <if test="status != null">
        status,
      </if>
      <if test="createTime != null">
        create_time,
      </if>
      <if test="startTime != null">
        start_time,
      </if>
      <if test="money != null">
        money,
      </if>
      <if test="appropriationRatio != null">
        appropriation_ratio,
      </if>
      <if test="customizeName != null">
        customize_name,
      </if>
      <if test="customizeTime != null">
        customize_time,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
      <if test="id != null">
        #{id,jdbcType=INTEGER},
      </if>
      <if test="orderNo != null">
        #{orderNo,jdbcType=VARCHAR},
      </if>
      <if test="tid != null">
        #{tid,jdbcType=INTEGER},
      </if>
      <if test="projectType != null">
        #{projectType,jdbcType=INTEGER},
      </if>
      <if test="dunType != null">
        #{dunType,jdbcType=INTEGER},
      </if>
      <if test="waitDay != null">
        #{waitDay,jdbcType=INTEGER},
      </if>
      <if test="effectiveCount != null">
        #{effectiveCount,jdbcType=INTEGER},
      </if>
      <if test="aid != null">
        #{aid,jdbcType=VARCHAR},
      </if>
      <if test="status != null">
        #{status,jdbcType=INTEGER},
      </if>
      <if test="createTime != null">
        #{createTime,jdbcType=TIMESTAMP},
      </if>
      <if test="startTime != null">
        #{startTime,jdbcType=TIMESTAMP},
      </if>
      <if test="money != null">
        #{money,jdbcType=DECIMAL},
      </if>
      <if test="appropriationRatio != null">
        #{appropriationRatio,jdbcType=DECIMAL},
      </if>
      <if test="customizeName != null">
        #{customizeName,jdbcType=VARCHAR},
      </if>
      <if test="customizeTime != null">
        #{customizeTime,jdbcType=TIMESTAMP},
      </if>
    </trim>
  </insert>
  <select id="countByExample" parameterType="com.goafanti.common.model.NewOrderDunExample" resultType="java.lang.Long">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Fri May 15 09:46:56 CST 2020.
    -->
    select count(*) from new_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 Fri May 15 09:46:56 CST 2020.
    -->
    update new_order_dun
    <set>
      <if test="record.id != null">
        id = #{record.id,jdbcType=INTEGER},
      </if>
      <if test="record.orderNo != null">
        order_no = #{record.orderNo,jdbcType=VARCHAR},
      </if>
      <if test="record.tid != null">
        tid = #{record.tid,jdbcType=INTEGER},
      </if>
      <if test="record.projectType != null">
        project_type = #{record.projectType,jdbcType=INTEGER},
      </if>
      <if test="record.dunType != null">
        dun_type = #{record.dunType,jdbcType=INTEGER},
      </if>
      <if test="record.waitDay != null">
        wait_day = #{record.waitDay,jdbcType=INTEGER},
      </if>
      <if test="record.effectiveCount != null">
        effective_count = #{record.effectiveCount,jdbcType=INTEGER},
      </if>
      <if test="record.aid != null">
        aid = #{record.aid,jdbcType=VARCHAR},
      </if>
      <if test="record.status != null">
        status = #{record.status,jdbcType=INTEGER},
      </if>
      <if test="record.createTime != null">
        create_time = #{record.createTime,jdbcType=TIMESTAMP},
      </if>
      <if test="record.startTime != null">
        start_time = #{record.startTime,jdbcType=TIMESTAMP},
      </if>
      <if test="record.money != null">
        money = #{record.money,jdbcType=DECIMAL},
      </if>
      <if test="record.appropriationRatio != null">
        appropriation_ratio = #{record.appropriationRatio,jdbcType=DECIMAL},
      </if>
      <if test="record.customizeName != null">
        customize_name = #{record.customizeName,jdbcType=VARCHAR},
      </if>
      <if test="record.customizeTime != null">
        customize_time = #{record.customizeTime,jdbcType=TIMESTAMP},
      </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 Fri May 15 09:46:56 CST 2020.
    -->
    update new_order_dun
    set id = #{record.id,jdbcType=INTEGER},
      order_no = #{record.orderNo,jdbcType=VARCHAR},
      tid = #{record.tid,jdbcType=INTEGER},
      project_type = #{record.projectType,jdbcType=INTEGER},
      dun_type = #{record.dunType,jdbcType=INTEGER},
      wait_day = #{record.waitDay,jdbcType=INTEGER},
      effective_count = #{record.effectiveCount,jdbcType=INTEGER},
      aid = #{record.aid,jdbcType=VARCHAR},
      status = #{record.status,jdbcType=INTEGER},
      create_time = #{record.createTime,jdbcType=TIMESTAMP},
      start_time = #{record.startTime,jdbcType=TIMESTAMP},
      money = #{record.money,jdbcType=DECIMAL},
      appropriation_ratio = #{record.appropriationRatio,jdbcType=DECIMAL},
      customize_name = #{record.customizeName,jdbcType=VARCHAR},
      customize_time = #{record.customizeTime,jdbcType=TIMESTAMP}
    <if test="_parameter != null">
      <include refid="Update_By_Example_Where_Clause" />
    </if>
  </update>
  <update id="updateByPrimaryKeySelective" parameterType="com.goafanti.common.model.NewOrderDun">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Fri May 15 09:46:56 CST 2020.
    -->
    update new_order_dun
    <set>
      <if test="orderNo != null">
        order_no = #{orderNo,jdbcType=VARCHAR},
      </if>
      <if test="tid != null">
        tid = #{tid,jdbcType=INTEGER},
      </if>
      <if test="projectType != null">
        project_type = #{projectType,jdbcType=INTEGER},
      </if>
      <if test="dunType != null">
        dun_type = #{dunType,jdbcType=INTEGER},
      </if>
      <if test="waitDay != null">
        wait_day = #{waitDay,jdbcType=INTEGER},
      </if>
      <if test="effectiveCount != null">
        effective_count = #{effectiveCount,jdbcType=INTEGER},
      </if>
      <if test="aid != null">
        aid = #{aid,jdbcType=VARCHAR},
      </if>
      <if test="status != null">
        status = #{status,jdbcType=INTEGER},
      </if>
      <if test="createTime != null">
        create_time = #{createTime,jdbcType=TIMESTAMP},
      </if>
      <if test="startTime != null">
        start_time = #{startTime,jdbcType=TIMESTAMP},
      </if>
      <if test="money != null">
        money = #{money,jdbcType=DECIMAL},
      </if>
      <if test="appropriationRatio != null">
        appropriation_ratio = #{appropriationRatio,jdbcType=DECIMAL},
      </if>
      <if test="customizeName != null">
        customize_name = #{customizeName,jdbcType=VARCHAR},
      </if>
      <if test="customizeTime != null">
        customize_time = #{customizeTime,jdbcType=TIMESTAMP},
      </if>
    </set>
    where id = #{id,jdbcType=INTEGER}
  </update>
  <update id="updateByPrimaryKey" parameterType="com.goafanti.common.model.NewOrderDun">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Fri May 15 09:46:56 CST 2020.
    -->
    update new_order_dun
    set order_no = #{orderNo,jdbcType=VARCHAR},
      tid = #{tid,jdbcType=INTEGER},
      project_type = #{projectType,jdbcType=INTEGER},
      dun_type = #{dunType,jdbcType=INTEGER},
      wait_day = #{waitDay,jdbcType=INTEGER},
      effective_count = #{effectiveCount,jdbcType=INTEGER},
      aid = #{aid,jdbcType=VARCHAR},
      status = #{status,jdbcType=INTEGER},
      create_time = #{createTime,jdbcType=TIMESTAMP},
      start_time = #{startTime,jdbcType=TIMESTAMP},
      money = #{money,jdbcType=DECIMAL},
      appropriation_ratio = #{appropriationRatio,jdbcType=DECIMAL},
      customize_name = #{customizeName,jdbcType=VARCHAR},
      customize_time = #{customizeTime,jdbcType=TIMESTAMP}
    where id = #{id,jdbcType=INTEGER}
  </update>
  <delete id="deleteByTid" parameterType="java.lang.Integer">

    delete from new_order_dun
    where tid = #{tid,jdbcType=INTEGER}
  </delete>
  <select id="selectListNewOrderDun" resultType="com.goafanti.order.bo.OutNewOrderDunBo">
  select a.id,a.order_no orderNo, a.tid, a.project_type projectType, a.dun_type dunType, a.wait_day waitDay, b.set_up_amount setUpAmount,
  a.effective_count effectiveCount, a.aid, a.create_time createTime ,b.commodity_name commodityName,a.money,a.status,a.customize_time customizeTime,
  a.appropriation_ratio appropriationRatio,date_format(a.customize_time,'%Y-%m-%d') customizeTimes, a.customize_name customizeName
  from new_order_dun a left join t_order_task b on a.tid=b.id where 1=1
  <if test="orderNo != null">
  and  a.order_no= #{orderNo}
  </if>
  <if test="status != null">
  and  a.status= #{status}
  </if>
   order by project_type,dun_type
  </select>
  <select id="selectAllOrderDun" resultType="com.goafanti.order.bo.OutNewOrderDunBo">
    select a.id, a.order_no orderNo, a.tid, a.project_type projectType, a.dun_type dunType, a.wait_day waitDay,
     a.effective_count effectiveCount, a.aid, a.create_time createTime,b.approval,a.customize_time customizeTime,
    date_format(a.customize_time,'%Y-%m-%d') customizeTimes, a.customize_name customizeName,a.money ,c.project_type memberType
    from new_order_dun a left join  t_order_new b on a.order_no=b.order_no left join t_order_mid c on a.order_no =c.order_no
    where 1=1 and a.status=0 and b.process_status &gt; 4 and b.delete_sign in (0,2,3)
  </select>
  <select id="selectByTid" resultType="com.goafanti.order.bo.OutNewOrderDunBo">
    select a.id, a.order_no orderNo, a.tid, a.project_type projectType, a.dun_type dunType, a.wait_day waitDay,
  	a.effective_count effectiveCount, a.aid, a.money,a.create_time createTime
  	,date_format(a.customize_time,'%Y-%m-%d') customizeTimes, a.customize_name customizeName
  	from new_order_dun a
    where tid = #{tid} and status=0
  </select>

  <insert id="insertBatch" parameterType="com.goafanti.order.bo.OutNewOrderDunBo">
  insert into new_order_dun ( order_no, tid,
      project_type, dun_type, wait_day,
      effective_count, aid, status,
      create_time, start_time, money,
      appropriation_ratio, customize_name, customize_time
      )
    values
    <foreach item="item" index="index" collection="list" separator=",">
     (#{item.orderNo,jdbcType=VARCHAR}, #{item.tid,jdbcType=INTEGER},
      #{item.projectType,jdbcType=INTEGER}, #{item.dunType,jdbcType=INTEGER}, #{item.waitDay,jdbcType=INTEGER},
      #{item.effectiveCount,jdbcType=INTEGER}, #{item.aid,jdbcType=VARCHAR}, #{item.status,jdbcType=INTEGER},
      #{item.createTime,jdbcType=TIMESTAMP}, #{item.startTime,jdbcType=TIMESTAMP}, #{item.money,jdbcType=DECIMAL},
      #{item.appropriationRatio,jdbcType=DECIMAL}, #{item.customizeName,jdbcType=VARCHAR}, #{item.customizeTime,jdbcType=TIMESTAMP}
      )
      </foreach>
  </insert>
  <select id="selectOrderDunAll" resultType="com.goafanti.order.bo.OutSelectOrderDunAll">
    select dg.name provinceName,z.province,z.quantity,z.totalAmount,z.receivables
    from (select y.province,sum(1)quantity,sum(y.total_amount)totalAmount,sum(y.receivables)receivables
    from (select c.province ,x.order_no,x.money receivables,a.total_amount
    from (select order_no ,sum(money)money from new_order_dun nod where status=1
     <if test="startDate!=null and endDate!=null">
      and start_time between #{startDate} and #{endDate}
     </if>
      group by order_no)x left join t_order_new a on x.order_no=a.order_no left join admin b on a.salesman_id =b.id
       left join department c on b.department_id =c.id where  a.delete_sign in(0,2)
    <if test="depId!=null">
      and b.department_id = #{depId}
    </if>
     )y group by y.province)z left join district_glossory dg on z.province=dg.id
    order by
    <if test="sort==0"> z.receivables </if>
    <if test="sort==1"> z.quantity </if>
    <if test="sort==2"> z.totalAmount </if>
     desc
  </select>


  <select id="selectOrderDun" resultType="com.goafanti.order.bo.OutSelectOrderDun">
    select  z.depName,z.aName,z.totalAmount,z.money,z.aid
    from( select y.depName,y.id aid,y.name aName,sum(y.total_amount)totalAmount, sum(y.money)money
    from (select c.name depName ,b.name ,b.id,x.money ,x.quantity,a.total_amount
    from (select order_no ,sum(money)money,1 quantity from new_order_dun nod where status=1
    <if test="startDate!=null and endDate!=null">
    and start_time between #{startDate} and #{endDate}
    </if>
    group by order_no)x left join t_order_new a on x.order_no=a.order_no left join admin b on a.salesman_id =b.id
    left join department c on b.department_id =c.id where  a.delete_sign in(0,2)
    <if test="depId !=null">
    and b.department_id = #{depId}
    </if>
      <if test="province !=null">
    and c.province= #{province}
      </if>
      <if test="aid !=null">
    and b.id= #{aid}
      </if>
    )y group by y.depName,y.name,y.id)z order by
    <if test="sort==0"> z.totalAmount </if>
    <if test="sort==1"> z.money </if>
    desc
  </select>
</mapper>
