<?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.TOrderMidMapper">
  <resultMap id="BaseResultMap" type="com.goafanti.common.model.TOrderMid">
    <id column="id" jdbcType="INTEGER" property="id" />
    <result column="order_no" jdbcType="VARCHAR" property="orderNo" />
    <result column="salesman_id" jdbcType="VARCHAR" property="salesmanId" />
    <result column="finance_id" jdbcType="VARCHAR" property="financeId" />
    <result column="salesman_name" jdbcType="VARCHAR" property="salesmanName" />
    <result column="finance_name" jdbcType="VARCHAR" property="financeName" />
    <result column="final_receivables" jdbcType="DECIMAL" property="finalReceivables" />
    <result column="final_Receivables_time" jdbcType="TIMESTAMP" property="finalReceivablesTime" />
    <result column="invoice_amount" jdbcType="DECIMAL" property="invoiceAmount" />
    <result column="create_time" jdbcType="TIMESTAMP" property="createTime" />
    <result column="buyer_name" jdbcType="VARCHAR" property="buyerName" />
    <result column="order_receivables" jdbcType="DECIMAL" property="orderReceivables" />
    <result column="order_arrears" jdbcType="DECIMAL" property="orderArrears" />
    <result column="payment_amount" jdbcType="DECIMAL" property="paymentAmount" />
    <result column="cost_amount" jdbcType="DECIMAL" property="costAmount" />
    <result column="dep_name" jdbcType="VARCHAR" property="depName" />
    <result column="province_name" jdbcType="VARCHAR" property="provinceName" />
    <result column="legal_affairs" jdbcType="INTEGER" property="legalAffairs" />
    <result column="dun_start_time" jdbcType="TIMESTAMP" property="dunStartTime" />
    <result column="legal_affairs_type" jdbcType="INTEGER" property="legalAffairsType" />
    <result column="operation_time" jdbcType="DATE" property="operationTime" />
    <result column="legal_affairs_status" jdbcType="INTEGER" property="legalAffairsStatus" />
    <result column="project_type" jdbcType="INTEGER" property="projectType" />
    <result column="service_type" jdbcType="VARCHAR" property="serviceType" />
    <result column="service_project" jdbcType="VARCHAR" property="serviceProject" />
    <result column="patent_cost" jdbcType="DECIMAL" property="patentCost" />
    <result column="patent_cost_actual" jdbcType="DECIMAL" property="patentCostActual" />
    <result column="soft_cost" jdbcType="DECIMAL" property="softCost" />
    <result column="soft_cost_actual" jdbcType="DECIMAL" property="softCostActual" />
    <result column="audit_cost" jdbcType="DECIMAL" property="auditCost" />
    <result column="audit_cost_actual" jdbcType="DECIMAL" property="auditCostActual" />
    <result column="other_cost" jdbcType="DECIMAL" property="otherCost" />
    <result column="other_cost_actual" jdbcType="DECIMAL" property="otherCostActual" />
    <result column="expect_profit" jdbcType="DECIMAL" property="expectProfit" />
    <result column="actual_profit" jdbcType="DECIMAL" property="actualProfit" />
    <result column="stop_project" jdbcType="INTEGER" property="stopProject" />
  </resultMap>
  <sql id="Base_Column_List">
    id, order_no, salesman_id, finance_id, salesman_name, finance_name, final_receivables,
    final_Receivables_time, invoice_amount, create_time, buyer_name, order_receivables,
    order_arrears, payment_amount, cost_amount, dep_name, province_name, legal_affairs,
    dun_start_time, legal_affairs_type, operation_time, legal_affairs_status, project_type,
    service_type, service_project, patent_cost, patent_cost_actual, soft_cost, soft_cost_actual,
    audit_cost, audit_cost_actual, other_cost, other_cost_actual, expect_profit, actual_profit,
    stop_project
  </sql>
  <select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
    select
    <include refid="Base_Column_List" />
    from t_order_mid
    where id = #{id,jdbcType=INTEGER}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
    delete from t_order_mid
    where id = #{id,jdbcType=INTEGER}
  </delete>
  <insert id="insert" keyColumn="id" keyProperty="id" parameterType="com.goafanti.common.model.TOrderMid" useGeneratedKeys="true">
    insert into t_order_mid (order_no, salesman_id, finance_id,
      salesman_name, finance_name, final_receivables,
      final_Receivables_time, invoice_amount,
      create_time, buyer_name, order_receivables,
      order_arrears, payment_amount, cost_amount,
      dep_name, province_name, legal_affairs,
      dun_start_time, legal_affairs_type, operation_time,
      legal_affairs_status, project_type, service_type,
      service_project, patent_cost, patent_cost_actual,
      soft_cost, soft_cost_actual, audit_cost,
      audit_cost_actual, other_cost, other_cost_actual,
      expect_profit, actual_profit, stop_project
      )
    values (#{orderNo,jdbcType=VARCHAR}, #{salesmanId,jdbcType=VARCHAR}, #{financeId,jdbcType=VARCHAR},
      #{salesmanName,jdbcType=VARCHAR}, #{financeName,jdbcType=VARCHAR}, #{finalReceivables,jdbcType=DECIMAL},
      #{finalReceivablesTime,jdbcType=TIMESTAMP}, #{invoiceAmount,jdbcType=DECIMAL},
      #{createTime,jdbcType=TIMESTAMP}, #{buyerName,jdbcType=VARCHAR}, #{orderReceivables,jdbcType=DECIMAL},
      #{orderArrears,jdbcType=DECIMAL}, #{paymentAmount,jdbcType=DECIMAL}, #{costAmount,jdbcType=DECIMAL},
      #{depName,jdbcType=VARCHAR}, #{provinceName,jdbcType=VARCHAR}, #{legalAffairs,jdbcType=INTEGER},
      #{dunStartTime,jdbcType=TIMESTAMP}, #{legalAffairsType,jdbcType=INTEGER}, #{operationTime,jdbcType=DATE},
      #{legalAffairsStatus,jdbcType=INTEGER}, #{projectType,jdbcType=INTEGER}, #{serviceType,jdbcType=VARCHAR},
      #{serviceProject,jdbcType=VARCHAR}, #{patentCost,jdbcType=DECIMAL}, #{patentCostActual,jdbcType=DECIMAL},
      #{softCost,jdbcType=DECIMAL}, #{softCostActual,jdbcType=DECIMAL}, #{auditCost,jdbcType=DECIMAL},
      #{auditCostActual,jdbcType=DECIMAL}, #{otherCost,jdbcType=DECIMAL}, #{otherCostActual,jdbcType=DECIMAL},
      #{expectProfit,jdbcType=DECIMAL}, #{actualProfit,jdbcType=DECIMAL}, #{stopProject,jdbcType=INTEGER}
      )
  </insert>
  <insert id="insertSelective" keyColumn="id" keyProperty="id" parameterType="com.goafanti.common.model.TOrderMid" useGeneratedKeys="true">
    insert into t_order_mid
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="orderNo != null">
        order_no,
      </if>
      <if test="salesmanId != null">
        salesman_id,
      </if>
      <if test="financeId != null">
        finance_id,
      </if>
      <if test="salesmanName != null">
        salesman_name,
      </if>
      <if test="financeName != null">
        finance_name,
      </if>
      <if test="finalReceivables != null">
        final_receivables,
      </if>
      <if test="finalReceivablesTime != null">
        final_Receivables_time,
      </if>
      <if test="invoiceAmount != null">
        invoice_amount,
      </if>
      <if test="createTime != null">
        create_time,
      </if>
      <if test="buyerName != null">
        buyer_name,
      </if>
      <if test="orderReceivables != null">
        order_receivables,
      </if>
      <if test="orderArrears != null">
        order_arrears,
      </if>
      <if test="paymentAmount != null">
        payment_amount,
      </if>
      <if test="costAmount != null">
        cost_amount,
      </if>
      <if test="depName != null">
        dep_name,
      </if>
      <if test="provinceName != null">
        province_name,
      </if>
      <if test="legalAffairs != null">
        legal_affairs,
      </if>
      <if test="dunStartTime != null">
        dun_start_time,
      </if>
      <if test="legalAffairsType != null">
        legal_affairs_type,
      </if>
      <if test="operationTime != null">
        operation_time,
      </if>
      <if test="legalAffairsStatus != null">
        legal_affairs_status,
      </if>
      <if test="projectType != null">
        project_type,
      </if>
      <if test="serviceType != null">
        service_type,
      </if>
      <if test="serviceProject != null">
        service_project,
      </if>
      <if test="patentCost != null">
        patent_cost,
      </if>
      <if test="patentCostActual != null">
        patent_cost_actual,
      </if>
      <if test="softCost != null">
        soft_cost,
      </if>
      <if test="softCostActual != null">
        soft_cost_actual,
      </if>
      <if test="auditCost != null">
        audit_cost,
      </if>
      <if test="auditCostActual != null">
        audit_cost_actual,
      </if>
      <if test="otherCost != null">
        other_cost,
      </if>
      <if test="otherCostActual != null">
        other_cost_actual,
      </if>
      <if test="expectProfit != null">
        expect_profit,
      </if>
      <if test="actualProfit != null">
        actual_profit,
      </if>
      <if test="stopProject != null">
        stop_project,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
      <if test="orderNo != null">
        #{orderNo,jdbcType=VARCHAR},
      </if>
      <if test="salesmanId != null">
        #{salesmanId,jdbcType=VARCHAR},
      </if>
      <if test="financeId != null">
        #{financeId,jdbcType=VARCHAR},
      </if>
      <if test="salesmanName != null">
        #{salesmanName,jdbcType=VARCHAR},
      </if>
      <if test="financeName != null">
        #{financeName,jdbcType=VARCHAR},
      </if>
      <if test="finalReceivables != null">
        #{finalReceivables,jdbcType=DECIMAL},
      </if>
      <if test="finalReceivablesTime != null">
        #{finalReceivablesTime,jdbcType=TIMESTAMP},
      </if>
      <if test="invoiceAmount != null">
        #{invoiceAmount,jdbcType=DECIMAL},
      </if>
      <if test="createTime != null">
        #{createTime,jdbcType=TIMESTAMP},
      </if>
      <if test="buyerName != null">
        #{buyerName,jdbcType=VARCHAR},
      </if>
      <if test="orderReceivables != null">
        #{orderReceivables,jdbcType=DECIMAL},
      </if>
      <if test="orderArrears != null">
        #{orderArrears,jdbcType=DECIMAL},
      </if>
      <if test="paymentAmount != null">
        #{paymentAmount,jdbcType=DECIMAL},
      </if>
      <if test="costAmount != null">
        #{costAmount,jdbcType=DECIMAL},
      </if>
      <if test="depName != null">
        #{depName,jdbcType=VARCHAR},
      </if>
      <if test="provinceName != null">
        #{provinceName,jdbcType=VARCHAR},
      </if>
      <if test="legalAffairs != null">
        #{legalAffairs,jdbcType=INTEGER},
      </if>
      <if test="dunStartTime != null">
        #{dunStartTime,jdbcType=TIMESTAMP},
      </if>
      <if test="legalAffairsType != null">
        #{legalAffairsType,jdbcType=INTEGER},
      </if>
      <if test="operationTime != null">
        #{operationTime,jdbcType=DATE},
      </if>
      <if test="legalAffairsStatus != null">
        #{legalAffairsStatus,jdbcType=INTEGER},
      </if>
      <if test="projectType != null">
        #{projectType,jdbcType=INTEGER},
      </if>
      <if test="serviceType != null">
        #{serviceType,jdbcType=VARCHAR},
      </if>
      <if test="serviceProject != null">
        #{serviceProject,jdbcType=VARCHAR},
      </if>
      <if test="patentCost != null">
        #{patentCost,jdbcType=DECIMAL},
      </if>
      <if test="patentCostActual != null">
        #{patentCostActual,jdbcType=DECIMAL},
      </if>
      <if test="softCost != null">
        #{softCost,jdbcType=DECIMAL},
      </if>
      <if test="softCostActual != null">
        #{softCostActual,jdbcType=DECIMAL},
      </if>
      <if test="auditCost != null">
        #{auditCost,jdbcType=DECIMAL},
      </if>
      <if test="auditCostActual != null">
        #{auditCostActual,jdbcType=DECIMAL},
      </if>
      <if test="otherCost != null">
        #{otherCost,jdbcType=DECIMAL},
      </if>
      <if test="otherCostActual != null">
        #{otherCostActual,jdbcType=DECIMAL},
      </if>
      <if test="expectProfit != null">
        #{expectProfit,jdbcType=DECIMAL},
      </if>
      <if test="actualProfit != null">
        #{actualProfit,jdbcType=DECIMAL},
      </if>
      <if test="stopProject != null">
        #{stopProject,jdbcType=INTEGER},
      </if>
    </trim>
  </insert>
  <update id="updateByPrimaryKeySelective" parameterType="com.goafanti.common.model.TOrderMid">
    update t_order_mid
    <set>
      <if test="orderNo != null">
        order_no = #{orderNo,jdbcType=VARCHAR},
      </if>
      <if test="salesmanId != null">
        salesman_id = #{salesmanId,jdbcType=VARCHAR},
      </if>
      <if test="financeId != null">
        finance_id = #{financeId,jdbcType=VARCHAR},
      </if>
      <if test="salesmanName != null">
        salesman_name = #{salesmanName,jdbcType=VARCHAR},
      </if>
      <if test="financeName != null">
        finance_name = #{financeName,jdbcType=VARCHAR},
      </if>
      <if test="finalReceivables != null">
        final_receivables = #{finalReceivables,jdbcType=DECIMAL},
      </if>
      <if test="finalReceivablesTime != null">
        final_Receivables_time = #{finalReceivablesTime,jdbcType=TIMESTAMP},
      </if>
      <if test="invoiceAmount != null">
        invoice_amount = #{invoiceAmount,jdbcType=DECIMAL},
      </if>
      <if test="createTime != null">
        create_time = #{createTime,jdbcType=TIMESTAMP},
      </if>
      <if test="buyerName != null">
        buyer_name = #{buyerName,jdbcType=VARCHAR},
      </if>
      <if test="orderReceivables != null">
        order_receivables = #{orderReceivables,jdbcType=DECIMAL},
      </if>
      <if test="orderArrears != null">
        order_arrears = #{orderArrears,jdbcType=DECIMAL},
      </if>
      <if test="paymentAmount != null">
        payment_amount = #{paymentAmount,jdbcType=DECIMAL},
      </if>
      <if test="costAmount != null">
        cost_amount = #{costAmount,jdbcType=DECIMAL},
      </if>
      <if test="depName != null">
        dep_name = #{depName,jdbcType=VARCHAR},
      </if>
      <if test="provinceName != null">
        province_name = #{provinceName,jdbcType=VARCHAR},
      </if>
      <if test="legalAffairs != null">
        legal_affairs = #{legalAffairs,jdbcType=INTEGER},
      </if>
      <if test="dunStartTime != null">
        dun_start_time = #{dunStartTime,jdbcType=TIMESTAMP},
      </if>
      <if test="legalAffairsType != null">
        legal_affairs_type = #{legalAffairsType,jdbcType=INTEGER},
      </if>
      <if test="operationTime != null">
        operation_time = #{operationTime,jdbcType=DATE},
      </if>
      <if test="legalAffairsStatus != null">
        legal_affairs_status = #{legalAffairsStatus,jdbcType=INTEGER},
      </if>
      <if test="projectType != null">
        project_type = #{projectType,jdbcType=INTEGER},
      </if>
      <if test="serviceType != null">
        service_type = #{serviceType,jdbcType=VARCHAR},
      </if>
      <if test="serviceProject != null">
        service_project = #{serviceProject,jdbcType=VARCHAR},
      </if>
      <if test="patentCost != null">
        patent_cost = #{patentCost,jdbcType=DECIMAL},
      </if>
      <if test="patentCostActual != null">
        patent_cost_actual = #{patentCostActual,jdbcType=DECIMAL},
      </if>
      <if test="softCost != null">
        soft_cost = #{softCost,jdbcType=DECIMAL},
      </if>
      <if test="softCostActual != null">
        soft_cost_actual = #{softCostActual,jdbcType=DECIMAL},
      </if>
      <if test="auditCost != null">
        audit_cost = #{auditCost,jdbcType=DECIMAL},
      </if>
      <if test="auditCostActual != null">
        audit_cost_actual = #{auditCostActual,jdbcType=DECIMAL},
      </if>
      <if test="otherCost != null">
        other_cost = #{otherCost,jdbcType=DECIMAL},
      </if>
      <if test="otherCostActual != null">
        other_cost_actual = #{otherCostActual,jdbcType=DECIMAL},
      </if>
      <if test="expectProfit != null">
        expect_profit = #{expectProfit,jdbcType=DECIMAL},
      </if>
      <if test="actualProfit != null">
        actual_profit = #{actualProfit,jdbcType=DECIMAL},
      </if>
      <if test="stopProject != null">
        stop_project = #{stopProject,jdbcType=INTEGER},
      </if>
    </set>
    where id = #{id,jdbcType=INTEGER}
  </update>
  <update id="updateByPrimaryKey" parameterType="com.goafanti.common.model.TOrderMid">
    update t_order_mid
    set order_no = #{orderNo,jdbcType=VARCHAR},
      salesman_id = #{salesmanId,jdbcType=VARCHAR},
      finance_id = #{financeId,jdbcType=VARCHAR},
      salesman_name = #{salesmanName,jdbcType=VARCHAR},
      finance_name = #{financeName,jdbcType=VARCHAR},
      final_receivables = #{finalReceivables,jdbcType=DECIMAL},
      final_Receivables_time = #{finalReceivablesTime,jdbcType=TIMESTAMP},
      invoice_amount = #{invoiceAmount,jdbcType=DECIMAL},
      create_time = #{createTime,jdbcType=TIMESTAMP},
      buyer_name = #{buyerName,jdbcType=VARCHAR},
      order_receivables = #{orderReceivables,jdbcType=DECIMAL},
      order_arrears = #{orderArrears,jdbcType=DECIMAL},
      payment_amount = #{paymentAmount,jdbcType=DECIMAL},
      cost_amount = #{costAmount,jdbcType=DECIMAL},
      dep_name = #{depName,jdbcType=VARCHAR},
      province_name = #{provinceName,jdbcType=VARCHAR},
      legal_affairs = #{legalAffairs,jdbcType=INTEGER},
      dun_start_time = #{dunStartTime,jdbcType=TIMESTAMP},
      legal_affairs_type = #{legalAffairsType,jdbcType=INTEGER},
      operation_time = #{operationTime,jdbcType=DATE},
      legal_affairs_status = #{legalAffairsStatus,jdbcType=INTEGER},
      project_type = #{projectType,jdbcType=INTEGER},
      service_type = #{serviceType,jdbcType=VARCHAR},
      service_project = #{serviceProject,jdbcType=VARCHAR},
      patent_cost = #{patentCost,jdbcType=DECIMAL},
      patent_cost_actual = #{patentCostActual,jdbcType=DECIMAL},
      soft_cost = #{softCost,jdbcType=DECIMAL},
      soft_cost_actual = #{softCostActual,jdbcType=DECIMAL},
      audit_cost = #{auditCost,jdbcType=DECIMAL},
      audit_cost_actual = #{auditCostActual,jdbcType=DECIMAL},
      other_cost = #{otherCost,jdbcType=DECIMAL},
      other_cost_actual = #{otherCostActual,jdbcType=DECIMAL},
      expect_profit = #{expectProfit,jdbcType=DECIMAL},
      actual_profit = #{actualProfit,jdbcType=DECIMAL},
      stop_project = #{stopProject,jdbcType=INTEGER}
    where id = #{id,jdbcType=INTEGER}
  </update>

  <select id="selectByOrderNo" resultMap="BaseResultMap">
    select
    <include refid="Base_Column_List" />
    from t_order_mid
    where order_no = #{id,jdbcType=INTEGER}
  </select>
  <update id="updateSalesmanId">
    update t_order_mid a ,admin b set a.salesman_id=#{transferId},a.salesman_name=b.name
    where b.id=#{transferId} and a.salesman_id=#{aid}
  </update>
  <update id="updateFinanceId">
    update t_order_new a ,t_order_mid b,admin c  set b.finance_id=#{financeId},b.finance_name=c.name
    where a.order_dep=#{id} and a.order_no=b.order_no and c.id= #{financeId}
  </update>
  <update id="updatefinalReceivables">
    update t_order_mid set final_receivables= #{amount},`final_Receivables_time`= #{receivablesTime},
      order_arrears=if(order_arrears- #{amount} &lt; 0,0,order_arrears- #{amount}),
      actual_profit=#{count}-payment_amount
    where order_no= #{orderNo}
  </update>
  <update id="updateInvoice">
    update t_order_mid m,(select order_no,sum(amount)amount from t_order_invoice where status=2 group by order_no)b
    set m.invoice_amount=b.amount where m.order_no= #{orderNo} and  m.order_no=b.order_no
  </update>
  <select id="selectDepNameByDepid" resultType="java.lang.String">
    select name from department where id= #{orderDep}
  </select>


  <update id="updateCostAmount">
    update t_order_mid x,(select x.order_no,sum(cost)cost,sum(party)party, sum(if(type = 1, cost, 0))zlc, sum(if(type = 1, party, 0))zlf,
     sum(if(type = 2, cost, 0))rzc, sum(if(type = 2, party, 0))rzf, sum(if(type = 3, cost, 0))sjc, sum(if(type = 3, party, 0))sjf,
     sum(if(type = 0 or type>3, cost, 0))qtc, sum(if(type = 0 or type>3, party, 0))qtf
    from(select b.order_no, c.`type`, sum(cost_amount)cost, sum(party_amount)party
         from t_task_mid a left join t_order_task b on a.tid = b.id left join business_project c on b.commodity_id = c.id
                           left join t_order_new d on b.order_no = d.order_no left join t_order_outsource too on a.tid =too.tid
         where   too.`type` =1 and too.refund_status =1 and d.delete_sign in (0,2) and b.order_no = #{orderNo}
         group by b.order_no , c.`type`)x group by x.order_no)y,t_order_new z
    set x.cost_amount =y.cost,x.payment_amount =y.party,x.patent_cost =y.zlc,x.patent_cost_actual =y.zlf,
        x.soft_cost =y.rzc,x.soft_cost_actual =y.rzf,x.audit_cost =y.sjc,x.audit_cost_actual =y.sjf,
        x.other_cost =qtc,x.other_cost_actual =y.qtf,x.expect_profit =z.total_amount-y.cost,
        x.actual_profit =z.settlement_amount-y.party
    where x.order_no =y.order_no and x.order_no=z.order_no
  </update>


  <update id="updateByOrderNo">
    update t_order_mid
    <set>
      <if test="salesmanId != null">
        salesman_id = #{salesmanId,jdbcType=VARCHAR},
      </if>
      <if test="financeId != null">
        finance_id = #{financeId,jdbcType=VARCHAR},
      </if>
      <if test="salesmanName != null">
        salesman_name = #{salesmanName,jdbcType=VARCHAR},
      </if>
      <if test="financeName != null">
        finance_name = #{financeName,jdbcType=VARCHAR},
      </if>
      <if test="finalReceivables != null">
        final_receivables = #{finalReceivables,jdbcType=DECIMAL},
      </if>
      <if test="finalReceivablesTime != null">
        final_Receivables_time = #{finalReceivablesTime,jdbcType=TIMESTAMP},
      </if>
      <if test="invoiceAmount != null">
        invoice_amount = #{invoiceAmount,jdbcType=DECIMAL},
      </if>
      <if test="createTime != null">
        create_time = #{createTime,jdbcType=TIMESTAMP},
      </if>
      <if test="buyerName != null">
        buyer_name = #{buyerName,jdbcType=VARCHAR},
      </if>
      <if test="orderReceivables != null">
        order_receivables = #{orderReceivables,jdbcType=DECIMAL},
      </if>
      <if test="orderArrears != null">
        order_arrears = #{orderArrears,jdbcType=DECIMAL},
      </if>
      <if test="paymentAmount != null">
        payment_amount = #{paymentAmount,jdbcType=DECIMAL},
      </if>
      <if test="costAmount != null">
        cost_amount = #{costAmount,jdbcType=DECIMAL},
      </if>
      <if test="depName != null">
        dep_name = #{depName,jdbcType=VARCHAR},
      </if>
      <if test="provinceName != null">
        province_name = #{provinceName,jdbcType=VARCHAR},
      </if>
      <if test="legalAffairs != null">
        legal_affairs = #{legalAffairs,jdbcType=INTEGER},
      </if>
      <if test="dunStartTime != null">
        dun_start_time = #{dunStartTime,jdbcType=TIMESTAMP},
      </if>
      <if test="legalAffairsType != null">
        legal_affairs_type = #{legalAffairsType,jdbcType=INTEGER},
      </if>
      <if test="operationTime != null">
        operation_time = #{operationTime,jdbcType=DATE},
      </if>
      <if test="legalAffairsStatus != null">
        legal_affairs_status = #{legalAffairsStatus,jdbcType=INTEGER},
      </if>
      <if test="projectType != null">
        project_type = #{projectType,jdbcType=INTEGER},
      </if>
      <if test="serviceType != null">
        service_type = #{serviceType,jdbcType=VARCHAR},
      </if>
      <if test="serviceProject != null">
        service_project = #{serviceProject,jdbcType=VARCHAR},
      </if>
      <if test="patentCost != null">
        patent_cost = #{patentCost,jdbcType=DECIMAL},
      </if>
      <if test="patentCostActual != null">
        patent_cost_actual = #{patentCostActual,jdbcType=DECIMAL},
      </if>
      <if test="softCost != null">
        soft_cost = #{softCost,jdbcType=DECIMAL},
      </if>
      <if test="softCostActual != null">
        soft_cost_actual = #{softCostActual,jdbcType=DECIMAL},
      </if>
      <if test="auditCost != null">
        audit_cost = #{auditCost,jdbcType=DECIMAL},
      </if>
      <if test="auditCostActual != null">
        audit_cost_actual = #{auditCostActual,jdbcType=DECIMAL},
      </if>
      <if test="otherCost != null">
        other_cost = #{otherCost,jdbcType=DECIMAL},
      </if>
      <if test="otherCostActual != null">
        other_cost_actual = #{otherCostActual,jdbcType=DECIMAL},
      </if>
      <if test="expectProfit != null">
        expect_profit = #{expectProfit,jdbcType=DECIMAL},
      </if>
      <if test="actualProfit != null">
        actual_profit = #{actualProfit,jdbcType=DECIMAL},
      </if>
      <if test="stopProject != null">
        stop_project = #{stopProject,jdbcType=INTEGER},
      </if>
    </set>
    where order_no = #{orderNo,jdbcType=VARCHAR}
  </update>

  <select id="legalAffairsOrderList" resultType="com.goafanti.order.bo.OutOrderLegalAffairsListBo">
    select a.order_no orderNo,a.contract_no contractNo,b.buyer_name buyerName,b.salesman_name salesmanName,b.dep_name depName,
    a.total_amount totalAmount ,a.settlement_amount  settlementAmount ,b.order_receivables orderReceivables,b.legal_affairs legalAffairs,
    b.order_arrears orderArrears,b.dun_start_time dunStartTime,date_format(b.dun_start_time,'%Y-%m-%d %H:%i:%S')dunStartTimes
    <if test="type==1 or type==2 or type==3">
      ,b.operation_time operationTime,date_format(b.operation_time,'%Y-%m-%d')operationDate,b.legal_affairs_type `type`,
      b.legal_affairs_status status
    </if>
    from t_order_new a left join t_order_mid b on a.order_no =b.order_no
    <if test="shiro == 2">
      left join admin c on a.salesman_id =c.id
    </if>
    where 1=1 and a.delete_sign in (0,2)
    <if test="type==1 or type==2 or type==3">
      and b.legal_affairs =1
      and b.legal_affairs_type = (#{type}-1)
    </if>
    <if test="type==0">
      and b.order_receivables >0
    </if>
    <if test="status != null">
      and b.legal_affairs_status = #{status}
    </if>
    <if test="shiro ==2">
      and  c.superior_id = #{sid}
    </if>
    <if test="userName != null">
      and b.buyer_name like concat('%',#{userName},'%')
    </if>
    <if test="legalAffairs != null">
      and b.legal_affairs = #{legalAffairs}
    </if>
    <if test="contractNo != null">
      and a.contract_no like  concat('%',#{contractNo},'%')
    </if>
    <if test="aid != null">
      and b.salesman_id = #{aid}
    </if>
    <if test="deps != null">
      and a.order_dep in
      <foreach close=")" collection="deps" item="dep" open="(" separator=",">
        #{dep}
      </foreach>
    </if>
    <if test="type==0">
      <if test="startTime != null and endTime != null">
        and b.dun_start_time between #{startTime} and #{endTime}
      </if>
    </if>
    <if test="type==1 or type==2 or type==3">
      <if test="startTime != null and endTime != null">
        and b.operation_time between #{startTime} and #{endTime}
      </if>
    </if>
    order by b.dun_start_time
    <if test="page_sql != null">
      ${page_sql}
    </if>
  </select>
  <select id="legalAffairsOrderCount" resultType="integer">
    select count(*)
    from t_order_new a left join t_order_mid b on a.order_no =b.order_no
    <if test="shiro == 2">
      left join admin c on a.salesman_id =c.id
    </if>
    where 1=1 and a.delete_sign in (0,2)
    <if test="type==1 or type==2 or type==3">
      and b.legal_affairs =1
      and b.legal_affairs_type = (#{type}-1)
    </if>
    <if test="type==0">
      and b.order_receivables >0
    </if>
    <if test="status != null">
      and b.legal_affairs_status = #{status}
    </if>
    <if test="shiro ==2">
      and  c.superior_id = #{sid}
    </if>
    <if test="userName != null">
      and b.buyer_name like concat('%',#{userName},'%')
    </if>
    <if test="legalAffairs != null">
      and b.legal_affairs = #{legalAffairs}
    </if>
    <if test="contractNo != null">
      and a.contract_no like  concat('%',#{contractNo},'%')
    </if>
    <if test="aid != null">
      and b.salesman_id = #{aid}
    </if>
    <if test="deps != null">
      and a.order_dep in
      <foreach close=")" collection="deps" item="dep" open="(" separator=",">
        #{dep}
      </foreach>
    </if>
    <if test="type==0">
      <if test="startTime != null and endTime != null">
        and b.dun_start_time between #{startTime} and #{endTime}
      </if>
    </if>
    <if test="type==1 or type==2 or type==3">
      <if test="startTime != null and endTime != null">
        and b.operation_time between #{startTime} and #{endTime}
      </if>
    </if>
  </select>

  <update id="updateByList">
    update t_order_mid
    set legal_affairs=1,legal_affairs_type =0
    where order_no in(
    <foreach collection="list" index="index" item="item" separator=",">
      #{item}
    </foreach>
    )
  </update>


  <select id="selectErrorDun" resultType="com.goafanti.common.bo.ErrorDunListBo">
    select a.order_no orderNo,a.order_receivables orderReceivables ,a.order_arrears orderArrears,b.money,a.create_time createTime
    from t_order_mid a left join (select order_no ,ifnull(sum(money),0)money from new_order_dun where status =1 group by order_no)b
                                 on a.order_no =b.order_no
    where a.order_receivables !=b.money order by a.create_time;
  </select>
  <update id="updateErrorDun">
    update t_order_mid a ,(select order_no ,ifnull(sum(money),0)money from new_order_dun where status =1 group by order_no)b,
      t_order_new c
    set a.order_receivables=b.money,a.order_arrears =(b.money-c.settlement_amount)
    where a.order_no =b.order_no and a.order_no =c.order_no and a.order_receivables !=b.money;
  </update>


  <select id="judgeProjectType" resultType="java.lang.Integer">
    select count(*) from t_order_new a left join t_order_task b on a.order_no =b.order_no
                                       left join business_project c on b.commodity_id =c.id left join business_category d on c.cid =d.id
    where d.sort =6 and a.order_no = #{orderNo}
  </select>

  <select id="selectAll" resultType="com.goafanti.common.model.TOrderMid">
    select order_no orderNo
    from t_order_mid
  </select>

  <update id="updateStopProjectByOrderNo">
    update t_order_mid a,(select x.order_no,max(ps)ps from (select a.order_no ,if(a.project_status=29,1,0)ps
        from t_order_task  a where a.order_no = #{orderNo} )x group by order_no) b
    set a.stop_project =b.ps
    where a.order_no =b.order_no
    </update>

</mapper>
