<?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.TChangeTaskMapper">
  <resultMap id="BaseResultMap" type="com.goafanti.common.model.TChangeTask">
    <id column="id" jdbcType="INTEGER" property="id" />
    <result column="tid" jdbcType="INTEGER" property="tid" />
    <result column="cid" jdbcType="INTEGER" property="cid" />
    <result column="order_no" jdbcType="VARCHAR" property="orderNo" />
    <result column="main" jdbcType="INTEGER" property="main" />
    <result column="commodity_id" jdbcType="VARCHAR" property="commodityId" />
    <result column="commodity_name" jdbcType="VARCHAR" property="commodityName" />
    <result column="commodity_quantity" jdbcType="INTEGER" property="commodityQuantity" />
    <result column="commodity_price" jdbcType="DECIMAL" property="commodityPrice" />
    <result column="task_comment" jdbcType="VARCHAR" property="taskComment" />
    <result column="type" jdbcType="INTEGER" property="type" />
    <result column="create_time" jdbcType="TIMESTAMP" property="createTime" />
    <result column="split_status" jdbcType="INTEGER" property="splitStatus" />
    <result column="split_super" jdbcType="INTEGER" property="splitSuper" />
    <result column="receiver_name" jdbcType="VARCHAR" property="receiverName" />
    <result column="official_cost" jdbcType="INTEGER" property="officialCost" />
    <result column="cost_reduction" jdbcType="INTEGER" property="costReduction" />
    <result column="service_life" jdbcType="VARCHAR" property="serviceLife" />
    <result column="service_year" jdbcType="VARCHAR" property="serviceYear" />
    <result column="year_sum" jdbcType="INTEGER" property="yearSum" />
    <result column="contract_term" jdbcType="VARCHAR" property="contractTerm" />
    <result column="declaration_batch" jdbcType="INTEGER" property="declarationBatch" />
    <result column="patent_type" jdbcType="INTEGER" property="patentType" />
    <result column="change_split_super" jdbcType="INTEGER" property="changeSplitSuper" />
    <result column="last_year" jdbcType="VARCHAR" property="lastYear" />
    <result column="last_year_capital" jdbcType="DECIMAL" property="lastYearCapital" />
    <result column="last_year_income" jdbcType="DECIMAL" property="lastYearIncome" />
  </resultMap>
  <sql id="Base_Column_List">
    id, tid, cid, order_no, main, commodity_id, commodity_name, commodity_quantity, commodity_price,
    task_comment, `type`, create_time, split_status, split_super, receiver_name, official_cost,
    cost_reduction, service_life, service_year, year_sum, contract_term, declaration_batch,
    patent_type, change_split_super, last_year, last_year_capital, last_year_income
  </sql>
  <select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
    select
    <include refid="Base_Column_List" />
    from t_change_task
    where id = #{id,jdbcType=INTEGER}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
    delete from t_change_task
    where id = #{id,jdbcType=INTEGER}
  </delete>
  <insert id="insert" keyColumn="id" keyProperty="id" parameterType="com.goafanti.common.model.TChangeTask" useGeneratedKeys="true">
    insert into t_change_task (tid, cid, order_no,
      main, commodity_id, commodity_name,
      commodity_quantity, commodity_price, task_comment,
      `type`, create_time, split_status,
      split_super, receiver_name, official_cost,
      cost_reduction, service_life, service_year,
      year_sum, contract_term, declaration_batch,
      patent_type, change_split_super, last_year,
      last_year_capital, last_year_income)
    values (#{tid,jdbcType=INTEGER}, #{cid,jdbcType=INTEGER}, #{orderNo,jdbcType=VARCHAR},
      #{main,jdbcType=INTEGER}, #{commodityId,jdbcType=VARCHAR}, #{commodityName,jdbcType=VARCHAR},
      #{commodityQuantity,jdbcType=INTEGER}, #{commodityPrice,jdbcType=DECIMAL}, #{taskComment,jdbcType=VARCHAR},
      #{type,jdbcType=INTEGER}, #{createTime,jdbcType=TIMESTAMP}, #{splitStatus,jdbcType=INTEGER},
      #{splitSuper,jdbcType=INTEGER}, #{receiverName,jdbcType=VARCHAR}, #{officialCost,jdbcType=INTEGER},
      #{costReduction,jdbcType=INTEGER}, #{serviceLife,jdbcType=VARCHAR}, #{serviceYear,jdbcType=VARCHAR},
      #{yearSum,jdbcType=INTEGER}, #{contractTerm,jdbcType=VARCHAR}, #{declarationBatch,jdbcType=INTEGER},
      #{patentType,jdbcType=INTEGER}, #{changeSplitSuper,jdbcType=INTEGER}, #{lastYear,jdbcType=VARCHAR},
      #{lastYearCapital,jdbcType=DECIMAL}, #{lastYearIncome,jdbcType=DECIMAL})
  </insert>
  <insert id="insertSelective" keyColumn="id" keyProperty="id" parameterType="com.goafanti.common.model.TChangeTask" useGeneratedKeys="true">
    insert into t_change_task
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="tid != null">
        tid,
      </if>
      <if test="cid != null">
        cid,
      </if>
      <if test="orderNo != null">
        order_no,
      </if>
      <if test="main != null">
        main,
      </if>
      <if test="commodityId != null">
        commodity_id,
      </if>
      <if test="commodityName != null">
        commodity_name,
      </if>
      <if test="commodityQuantity != null">
        commodity_quantity,
      </if>
      <if test="commodityPrice != null">
        commodity_price,
      </if>
      <if test="taskComment != null">
        task_comment,
      </if>
      <if test="type != null">
        `type`,
      </if>
      <if test="createTime != null">
        create_time,
      </if>
      <if test="splitStatus != null">
        split_status,
      </if>
      <if test="splitSuper != null">
        split_super,
      </if>
      <if test="receiverName != null">
        receiver_name,
      </if>
      <if test="officialCost != null">
        official_cost,
      </if>
      <if test="costReduction != null">
        cost_reduction,
      </if>
      <if test="serviceLife != null">
        service_life,
      </if>
      <if test="serviceYear != null">
        service_year,
      </if>
      <if test="yearSum != null">
        year_sum,
      </if>
      <if test="contractTerm != null">
        contract_term,
      </if>
      <if test="declarationBatch != null">
        declaration_batch,
      </if>
      <if test="patentType != null">
        patent_type,
      </if>
      <if test="changeSplitSuper != null">
        change_split_super,
      </if>
      <if test="lastYear != null">
        last_year,
      </if>
      <if test="lastYearCapital != null">
        last_year_capital,
      </if>
      <if test="lastYearIncome != null">
        last_year_income,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
      <if test="tid != null">
        #{tid,jdbcType=INTEGER},
      </if>
      <if test="cid != null">
        #{cid,jdbcType=INTEGER},
      </if>
      <if test="orderNo != null">
        #{orderNo,jdbcType=VARCHAR},
      </if>
      <if test="main != null">
        #{main,jdbcType=INTEGER},
      </if>
      <if test="commodityId != null">
        #{commodityId,jdbcType=VARCHAR},
      </if>
      <if test="commodityName != null">
        #{commodityName,jdbcType=VARCHAR},
      </if>
      <if test="commodityQuantity != null">
        #{commodityQuantity,jdbcType=INTEGER},
      </if>
      <if test="commodityPrice != null">
        #{commodityPrice,jdbcType=DECIMAL},
      </if>
      <if test="taskComment != null">
        #{taskComment,jdbcType=VARCHAR},
      </if>
      <if test="type != null">
        #{type,jdbcType=INTEGER},
      </if>
      <if test="createTime != null">
        #{createTime,jdbcType=TIMESTAMP},
      </if>
      <if test="splitStatus != null">
        #{splitStatus,jdbcType=INTEGER},
      </if>
      <if test="splitSuper != null">
        #{splitSuper,jdbcType=INTEGER},
      </if>
      <if test="receiverName != null">
        #{receiverName,jdbcType=VARCHAR},
      </if>
      <if test="officialCost != null">
        #{officialCost,jdbcType=INTEGER},
      </if>
      <if test="costReduction != null">
        #{costReduction,jdbcType=INTEGER},
      </if>
      <if test="serviceLife != null">
        #{serviceLife,jdbcType=VARCHAR},
      </if>
      <if test="serviceYear != null">
        #{serviceYear,jdbcType=VARCHAR},
      </if>
      <if test="yearSum != null">
        #{yearSum,jdbcType=INTEGER},
      </if>
      <if test="contractTerm != null">
        #{contractTerm,jdbcType=VARCHAR},
      </if>
      <if test="declarationBatch != null">
        #{declarationBatch,jdbcType=INTEGER},
      </if>
      <if test="patentType != null">
        #{patentType,jdbcType=INTEGER},
      </if>
      <if test="changeSplitSuper != null">
        #{changeSplitSuper,jdbcType=INTEGER},
      </if>
      <if test="lastYear != null">
        #{lastYear,jdbcType=VARCHAR},
      </if>
      <if test="lastYearCapital != null">
        #{lastYearCapital,jdbcType=DECIMAL},
      </if>
      <if test="lastYearIncome != null">
        #{lastYearIncome,jdbcType=DECIMAL},
      </if>
    </trim>
  </insert>
  <update id="updateByPrimaryKeySelective" parameterType="com.goafanti.common.model.TChangeTask">
    update t_change_task
    <set>
      <if test="tid != null">
        tid = #{tid,jdbcType=INTEGER},
      </if>
      <if test="cid != null">
        cid = #{cid,jdbcType=INTEGER},
      </if>
      <if test="orderNo != null">
        order_no = #{orderNo,jdbcType=VARCHAR},
      </if>
      <if test="main != null">
        main = #{main,jdbcType=INTEGER},
      </if>
      <if test="commodityId != null">
        commodity_id = #{commodityId,jdbcType=VARCHAR},
      </if>
      <if test="commodityName != null">
        commodity_name = #{commodityName,jdbcType=VARCHAR},
      </if>
      <if test="commodityQuantity != null">
        commodity_quantity = #{commodityQuantity,jdbcType=INTEGER},
      </if>
      <if test="commodityPrice != null">
        commodity_price = #{commodityPrice,jdbcType=DECIMAL},
      </if>
      <if test="taskComment != null">
        task_comment = #{taskComment,jdbcType=VARCHAR},
      </if>
      <if test="type != null">
        `type` = #{type,jdbcType=INTEGER},
      </if>
      <if test="createTime != null">
        create_time = #{createTime,jdbcType=TIMESTAMP},
      </if>
      <if test="splitStatus != null">
        split_status = #{splitStatus,jdbcType=INTEGER},
      </if>
      <if test="splitSuper != null">
        split_super = #{splitSuper,jdbcType=INTEGER},
      </if>
      <if test="receiverName != null">
        receiver_name = #{receiverName,jdbcType=VARCHAR},
      </if>
      <if test="officialCost != null">
        official_cost = #{officialCost,jdbcType=INTEGER},
      </if>
      <if test="costReduction != null">
        cost_reduction = #{costReduction,jdbcType=INTEGER},
      </if>
      <if test="serviceLife != null">
        service_life = #{serviceLife,jdbcType=VARCHAR},
      </if>
      <if test="serviceYear != null">
        service_year = #{serviceYear,jdbcType=VARCHAR},
      </if>
      <if test="yearSum != null">
        year_sum = #{yearSum,jdbcType=INTEGER},
      </if>
      <if test="contractTerm != null">
        contract_term = #{contractTerm,jdbcType=VARCHAR},
      </if>
      <if test="declarationBatch != null">
        declaration_batch = #{declarationBatch,jdbcType=INTEGER},
      </if>
      <if test="patentType != null">
        patent_type = #{patentType,jdbcType=INTEGER},
      </if>
      <if test="changeSplitSuper != null">
        change_split_super = #{changeSplitSuper,jdbcType=INTEGER},
      </if>
      <if test="lastYear != null">
        last_year = #{lastYear,jdbcType=VARCHAR},
      </if>
      <if test="lastYearCapital != null">
        last_year_capital = #{lastYearCapital,jdbcType=DECIMAL},
      </if>
      <if test="lastYearIncome != null">
        last_year_income = #{lastYearIncome,jdbcType=DECIMAL},
      </if>
    </set>
    where id = #{id,jdbcType=INTEGER}
  </update>
  <update id="updateByPrimaryKey" parameterType="com.goafanti.common.model.TChangeTask">
    update t_change_task
    set tid = #{tid,jdbcType=INTEGER},
      cid = #{cid,jdbcType=INTEGER},
      order_no = #{orderNo,jdbcType=VARCHAR},
      main = #{main,jdbcType=INTEGER},
      commodity_id = #{commodityId,jdbcType=VARCHAR},
      commodity_name = #{commodityName,jdbcType=VARCHAR},
      commodity_quantity = #{commodityQuantity,jdbcType=INTEGER},
      commodity_price = #{commodityPrice,jdbcType=DECIMAL},
      task_comment = #{taskComment,jdbcType=VARCHAR},
      `type` = #{type,jdbcType=INTEGER},
      create_time = #{createTime,jdbcType=TIMESTAMP},
      split_status = #{splitStatus,jdbcType=INTEGER},
      split_super = #{splitSuper,jdbcType=INTEGER},
      receiver_name = #{receiverName,jdbcType=VARCHAR},
      official_cost = #{officialCost,jdbcType=INTEGER},
      cost_reduction = #{costReduction,jdbcType=INTEGER},
      service_life = #{serviceLife,jdbcType=VARCHAR},
      service_year = #{serviceYear,jdbcType=VARCHAR},
      year_sum = #{yearSum,jdbcType=INTEGER},
      contract_term = #{contractTerm,jdbcType=VARCHAR},
      declaration_batch = #{declarationBatch,jdbcType=INTEGER},
      patent_type = #{patentType,jdbcType=INTEGER},
      change_split_super = #{changeSplitSuper,jdbcType=INTEGER},
      last_year = #{lastYear,jdbcType=VARCHAR},
      last_year_capital = #{lastYearCapital,jdbcType=DECIMAL},
      last_year_income = #{lastYearIncome,jdbcType=DECIMAL}
    where id = #{id,jdbcType=INTEGER}
  </update>

  <insert id="insertSelectiveList" parameterType="java.util.List">
    insert into t_change_task ( tid, cid, split_status,
      order_no, commodity_id, commodity_name,main,
      commodity_quantity, commodity_price, task_comment,
        split_super , receiver_name, create_time,service_life,
    service_year, year_sum, contract_term,declaration_batch,patent_type,
    last_year,last_year_capital,last_year_income)
    values
    <foreach collection="list" index="index" item="item" separator=",">
      ( #{item.tid,jdbcType=INTEGER}, #{item.cid,jdbcType=INTEGER}, #{item.splitStatus,jdbcType=INTEGER},
      #{item.orderNo,jdbcType=VARCHAR}, #{item.commodityId,jdbcType=VARCHAR}, #{item.commodityName,jdbcType=VARCHAR},#{item.main},
      #{item.commodityQuantity,jdbcType=INTEGER}, #{item.commodityPrice,jdbcType=DECIMAL}, #{item.taskComment,jdbcType=VARCHAR},
       #{item.splitSuper,jdbcType=INTEGER}, #{item.receiverName,jdbcType=VARCHAR}, now(), #{item.serviceLife,jdbcType=VARCHAR},
      #{item.serviceYear,jdbcType=VARCHAR}, #{item.yearSum,jdbcType=INTEGER},#{item.contractTerm,jdbcType=VARCHAR},
      #{item.declarationBatch,jdbcType=INTEGER},#{item.patentType,jdbcType=INTEGER},
       #{item.lastYear,jdbcType=VARCHAR}, #{item.lastYearCapital,jdbcType=DECIMAL},#{item.lastYearIncome,jdbcType=DECIMAL})
    </foreach>
  </insert>
  <select id="selectByCid" resultType="com.goafanti.order.bo.TChangeTaskOut">
    select a.id,a.order_no as orderNo,a.main, c.cname,c.sort cSort,a.commodity_name as commodityName,a.official_cost officialCost,
	a.official_cost officialCost, a.commodity_quantity as commodityQuantity,i.price,a.commodity_id as commodityId,a.type,a.tid,
	a.receiver_name receiverName, a.task_comment as taskComment ,a.commodity_price as commodityPrice,a.split_status splitStatus,
	a.split_super splitSuper, a.service_life serviceLife ,a.service_year serviceYear,a.year_sum yearSum,i.patent_transfer patentTransfer,
    i.type taskType, a.declaration_batch declarationBatch,a.patent_type patentType,a.contract_term contractTerm,
    a.change_split_super changeSplitSuper,a.last_year lastYear,a.last_year_income lastYearIncome,a.last_year_capital lastYearCapital
    from t_change_task a left join business_project i on a.commodity_id=i.id
  	left join business_category c on i.cid=c.id
  	where 1=1
  	<if test="splitStatus !=null and splitStatus !=3">
  	and a.split_status= #{splitStatus}
  	</if>
  	<if test="splitStatus ==3">
  	and a.split_status in (0,1)
  	</if>
  	 and a.cid= #{id} order by a.id
  </select>
    <update id="updateByTid">
  		update t_change_task
      <set>
      <if test="type != null">
        type= #{type},
      </if>
      <if test="member ==1">
        service_life=null,year_sum=null,
      </if>
      </set>
  		where 1=1
  		<if test="tid !=null">
  		and tid= #{tid}
  		</if>
  		<if test="cid !=null">
  		and cid= #{cid}
  		</if>
  		<if test="sid !=null">
  		and split_super= #{sid}
  		</if>
  </update>

   <update id="deleteByTid">
  		delete from t_change_task where tid= #{tid};
  </update>
  <delete id="deleteByChangeSplitSuper">
    delete from t_change_task where change_split_super= #{tid};
  </delete>
  <update id="updateMemberByTid">
    update t_change_task
    <set>
      <if test="type != null">
        type= #{type},
      </if>
      <if test="serviceLife != null">
        service_life= #{serviceLife},
      </if>
      <if test="serviceYear != null">
        service_year= #{serviceYear},
      </if>
      <if test="yearSum != null">
        year_sum= #{yearSum},
      </if>
      <if test="contractTerm != null">
        contract_term= #{contractTerm},
      </if>
    </set>
    where 1=1
    <if test="cid !=null">
      and cid= #{cid}
    </if>
    <if test="sid !=null">
      and split_super= #{sid}
    </if>
  </update>
  <update id="updateTypeBatch">
    update t_change_task set type= #{type}
    where id in
    <foreach close=")" collection="list" item="id" open="(" separator=",">
      #{id}
    </foreach>
  </update>



  <select id="selectBySuper" resultType="com.goafanti.order.bo.TChangeTaskOut">
	select a.id,a.order_no as orderNo,a.main, c.cname,c.sort cSort,a.commodity_name as commodityName,
  	a.commodity_quantity as commodityQuantity,i.price,a.commodity_id as commodityId,a.type,a.tid,a.receiver_name receiverName,
	a.task_comment as taskComment ,a.commodity_price as commodityPrice,a.split_status splitStatus,a.split_super splitSuper,
    a.service_life serviceLife ,a.service_year serviceYear,a.year_sum yearSum, a.declaration_batch declarationBatch,
	a.patent_type patentType,a.contract_term contractTerm,a.change_split_super changeSplitSuper
    from t_change_task a left join business_project i on a.commodity_id=i.id
  	left join business_category c on i.cid=c.id
  	where a.split_status=2 and a.cid= #{cid}
  	  <if test="splitSuper !=null">
  	    and a.split_super= #{splitSuper}
      </if>
	<if test="changeSplitSuper !=null" >
    and a.change_split_super= #{changeSplitSuper}
    </if>
	order by a.id
  </select>



<select id="selectBytid" resultType="com.goafanti.common.model.TChangeTask">
    select tct.id,tct.tid,tct.cid,tct.order_no orderNo
    from t_change_task tct left join new_order_change noc on tct.cid =noc.id
    where noc.process_state &lt; 4 and noc.status in (0,1,2)
      and tct.tid= #{tid}
  </select>
  <update id="updateSuperServiceYear" parameterType="com.goafanti.common.model.TChangeTask">
    update t_change_task a,(select a.split_super,GROUP_CONCAT(a.service_year) counts
        from t_change_task a where a.split_status =2 and a.type in (0,1,2)
        and a.split_super=#{splitSuper} and  a.cid= #{cid}  group by a.split_super)x
    set a.service_year =x.counts
    where a.tid =x.split_super and a.cid= #{cid};
  </update>
  <update id="updateChangeSuperServiceYear" parameterType="com.goafanti.common.model.TChangeTask">
    update t_change_task a,(select a.change_split_super,GROUP_CONCAT(a.service_year) counts
        from t_change_task a where a.split_status =2 and a.type in (0,1,2)
        and a.change_split_super = #{changeSplitSuper} group by a.change_split_super)x
    set a.service_year =x.counts
    where a.id =x.change_split_super
  </update>
    <update id="updateDimissionTransfer"></update>
</mapper>
