<?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.TTaskMidMapper">
  <resultMap id="BaseResultMap" type="com.goafanti.common.model.TTaskMid">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Fri Dec 25 14:38:43 CST 2020.
    -->
    <id column="id" jdbcType="INTEGER" property="id" />
    <result column="tid" jdbcType="INTEGER" property="tid" />
    <result column="cost_amount" jdbcType="DECIMAL" property="costAmount" />
    <result column="party_amount" jdbcType="DECIMAL" property="partyAmount" />
    <result column="create_time" jdbcType="TIMESTAMP" property="createTime" />
    <result column="certificates_count" jdbcType="INTEGER" property="certificatesCount" />
    <result column="urgent_day" jdbcType="INTEGER" property="urgentDay" />
    <result column="if_material" jdbcType="INTEGER" property="ifMaterial" />
    <result column="reject_count" jdbcType="INTEGER" property="rejectCount" />
    <result column="accept_count" jdbcType="INTEGER" property="acceptCount" />
    <result column="end_count" jdbcType="INTEGER" property="endCount" />
    <result column="authorize_count" jdbcType="INTEGER" property="authorizeCount" />
    <result column="apply_count" jdbcType="INTEGER" property="applyCount" />
  </resultMap>
  <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 Dec 25 14:38:43 CST 2020.
    -->
    id, tid, cost_amount, party_amount, create_time, certificates_count, urgent_day, 
    if_material, reject_count, accept_count, end_count, authorize_count, apply_count
  </sql>
  <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 Dec 25 14:38:43 CST 2020.
    -->
    select 
    <include refid="Base_Column_List" />
    from t_task_mid
    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 Dec 25 14:38:43 CST 2020.
    -->
    delete from t_task_mid
    where id = #{id,jdbcType=INTEGER}
  </delete>
  <insert id="insert" parameterType="com.goafanti.common.model.TTaskMid">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Fri Dec 25 14:38:43 CST 2020.
    -->
    insert into t_task_mid (id, tid, cost_amount, 
      party_amount, create_time, certificates_count, 
      urgent_day, if_material, reject_count, 
      accept_count, end_count, authorize_count, 
      apply_count)
    values (#{id,jdbcType=INTEGER}, #{tid,jdbcType=INTEGER}, #{costAmount,jdbcType=DECIMAL}, 
      #{partyAmount,jdbcType=DECIMAL}, #{createTime,jdbcType=TIMESTAMP}, #{certificatesCount,jdbcType=INTEGER}, 
      #{urgentDay,jdbcType=INTEGER}, #{ifMaterial,jdbcType=INTEGER}, #{rejectCount,jdbcType=INTEGER}, 
      #{acceptCount,jdbcType=INTEGER}, #{endCount,jdbcType=INTEGER}, #{authorizeCount,jdbcType=INTEGER}, 
      #{applyCount,jdbcType=INTEGER})
  </insert>
  <insert id="insertSelective" parameterType="com.goafanti.common.model.TTaskMid">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Fri Dec 25 14:38:43 CST 2020.
    -->
    insert into t_task_mid
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="id != null">
        id,
      </if>
      <if test="tid != null">
        tid,
      </if>
      <if test="costAmount != null">
        cost_amount,
      </if>
      <if test="partyAmount != null">
        party_amount,
      </if>
      <if test="createTime != null">
        create_time,
      </if>
      <if test="certificatesCount != null">
        certificates_count,
      </if>
      <if test="urgentDay != null">
        urgent_day,
      </if>
      <if test="ifMaterial != null">
        if_material,
      </if>
      <if test="rejectCount != null">
        reject_count,
      </if>
      <if test="acceptCount != null">
        accept_count,
      </if>
      <if test="endCount != null">
        end_count,
      </if>
      <if test="authorizeCount != null">
        authorize_count,
      </if>
      <if test="applyCount != null">
        apply_count,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
      <if test="id != null">
        #{id,jdbcType=INTEGER},
      </if>
      <if test="tid != null">
        #{tid,jdbcType=INTEGER},
      </if>
      <if test="costAmount != null">
        #{costAmount,jdbcType=DECIMAL},
      </if>
      <if test="partyAmount != null">
        #{partyAmount,jdbcType=DECIMAL},
      </if>
      <if test="createTime != null">
        #{createTime,jdbcType=TIMESTAMP},
      </if>
      <if test="certificatesCount != null">
        #{certificatesCount,jdbcType=INTEGER},
      </if>
      <if test="urgentDay != null">
        #{urgentDay,jdbcType=INTEGER},
      </if>
      <if test="ifMaterial != null">
        #{ifMaterial,jdbcType=INTEGER},
      </if>
      <if test="rejectCount != null">
        #{rejectCount,jdbcType=INTEGER},
      </if>
      <if test="acceptCount != null">
        #{acceptCount,jdbcType=INTEGER},
      </if>
      <if test="endCount != null">
        #{endCount,jdbcType=INTEGER},
      </if>
      <if test="authorizeCount != null">
        #{authorizeCount,jdbcType=INTEGER},
      </if>
      <if test="applyCount != null">
        #{applyCount,jdbcType=INTEGER},
      </if>
    </trim>
  </insert>
  <update id="updateByPrimaryKeySelective" parameterType="com.goafanti.common.model.TTaskMid">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Fri Dec 25 14:38:43 CST 2020.
    -->
    update t_task_mid
    <set>
      <if test="tid != null">
        tid = #{tid,jdbcType=INTEGER},
      </if>
      <if test="costAmount != null">
        cost_amount = #{costAmount,jdbcType=DECIMAL},
      </if>
      <if test="partyAmount != null">
        party_amount = #{partyAmount,jdbcType=DECIMAL},
      </if>
      <if test="createTime != null">
        create_time = #{createTime,jdbcType=TIMESTAMP},
      </if>
      <if test="certificatesCount != null">
        certificates_count = #{certificatesCount,jdbcType=INTEGER},
      </if>
      <if test="urgentDay != null">
        urgent_day = #{urgentDay,jdbcType=INTEGER},
      </if>
      <if test="ifMaterial != null">
        if_material = #{ifMaterial,jdbcType=INTEGER},
      </if>
      <if test="rejectCount != null">
        reject_count = #{rejectCount,jdbcType=INTEGER},
      </if>
      <if test="acceptCount != null">
        accept_count = #{acceptCount,jdbcType=INTEGER},
      </if>
      <if test="endCount != null">
        end_count = #{endCount,jdbcType=INTEGER},
      </if>
      <if test="authorizeCount != null">
        authorize_count = #{authorizeCount,jdbcType=INTEGER},
      </if>
      <if test="applyCount != null">
        apply_count = #{applyCount,jdbcType=INTEGER},
      </if>
    </set>
    where id = #{id,jdbcType=INTEGER}
  </update>
  <update id="updateByPrimaryKey" parameterType="com.goafanti.common.model.TTaskMid">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Fri Dec 25 14:38:43 CST 2020.
    -->
    update t_task_mid
    set tid = #{tid,jdbcType=INTEGER},
      cost_amount = #{costAmount,jdbcType=DECIMAL},
      party_amount = #{partyAmount,jdbcType=DECIMAL},
      create_time = #{createTime,jdbcType=TIMESTAMP},
      certificates_count = #{certificatesCount,jdbcType=INTEGER},
      urgent_day = #{urgentDay,jdbcType=INTEGER},
      if_material = #{ifMaterial,jdbcType=INTEGER},
      reject_count = #{rejectCount,jdbcType=INTEGER},
      accept_count = #{acceptCount,jdbcType=INTEGER},
      end_count = #{endCount,jdbcType=INTEGER},
      authorize_count = #{authorizeCount,jdbcType=INTEGER},
      apply_count = #{applyCount,jdbcType=INTEGER}
    where id = #{id,jdbcType=INTEGER}
  </update>
 
 
  <update id="updatePaymentAmount">
  	update t_task_mid
    <if test="type == 0">
    set party_amount = party_amount + #{paymentAmount,jdbcType=DECIMAL}
    </if>
    <if test="type == 1">
    set party_amount = party_amount - #{paymentAmount,jdbcType=DECIMAL}
    </if>
    where tid = #{tid,jdbcType=INTEGER}
  </update>
  <select id="selectListByTid" resultType="com.goafanti.common.model.TTaskMid">
	    select c.id,c.tid,c.cost_amount costAmount from t_order_task a left join t_order_task b on a.order_no=b.order_no
	left join t_task_mid c on b.id=c.tid
	where a.id= #{tid}
  </select>
  
    <update id="updateCostAmount">
      update t_order_task a ,t_task_mid c
    set c.cost_amount=#{count}
    where  a.id=c.tid and a.id= #{tid}
  </update>
  
  <update id="updateCertificatesCountByTid">
  	update t_task_mid
  	set certificates_count= #{count}
  	where tid= #{tid}
  </update>
  
  <update id="updateByTid" parameterType="com.goafanti.common.model.TTaskMid">
    update t_task_mid
    <set>
      <if test="urgentDay != null">
        urgent_Day = #{urgentDay},
      </if>
      <if test="ifMaterial != null">
        if_material = #{ifMaterial},
      </if>
      <if test="rejectCount != null">
        reject_count = #{rejectCount},
      </if>
    </set>
    where tid = #{tid,jdbcType=INTEGER}
  </update>
  <select id="selectCountBytid" resultType="com.goafanti.common.model.TTaskMid">
  select a.id,
  <if test="type ==0">
   ifnull(b.lic,0) certificatesCount
  </if>
  <if test="type ==1">
   ifnull(b.acc ,0)acceptCount, ifnull(b.lic ,0)authorizeCount,ifnull(b.lic ,0)certificatesCount,
   ifnull(b.tgc ,0)endCount,ifnull(b.bhc ,0)rejectCount
  </if>
  <if test="type ==2">
  ifnull(b.acc ,0)acceptCount,ifnull(b.lic ,0)certificatesCount,ifnull(b.sqc,0) applyCount,
  ifnull(b.bhc,0) rejectCount
  </if>
  from t_task_mid a left join (select task_id tid,
  <if test="type ==0">
  sum(if(licence_time is null,0,1))lic from task_progress where task_id= #{tid} GROUP by task_id)b  
  </if>
  <if test="type ==1">
  	<!-- 下证数与授权数一样，下证统计大于则用下证，小于或者等于用授权统计 -->
 	sum(if(if(licence_time is null,0,1) &gt; if(authorize_time is null,0,1),
		if(licence_time is null,0,1),if(authorize_time is null,0,1)))lic ,ifnull(count(accept_time),0)acc,
	 	sum(if(status=1,1,0))tgc,sum(if(status=2 ,1,0))bhc from task_progress where task_id= #{tid} GROUP by task_id )b
  </if>
  <if test="type ==2">
  	count(*)sqc,  sum(if(licence_time is null,0,1))lic , sum(if(accept_time is null,0,1))acc,
   	 sum(if(status=2 ,1,0))bhc  from task_progress where task_id= #{tid} GROUP by task_id )b
  </if>
   on a.tid =b.tid
	where a.tid= #{tid}
  </select>
  
 
</mapper>