<?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.NewOrderChangeMapper">
  <resultMap id="BaseResultMap" type="com.goafanti.common.model.NewOrderChange">
    <id column="id" jdbcType="INTEGER" property="id" />
    <result column="order_no" jdbcType="VARCHAR" property="orderNo" />
    <result column="process_state" jdbcType="INTEGER" property="processState" />
    <result column="status" jdbcType="INTEGER" property="status" />
    <result column="type" jdbcType="INTEGER" property="type" />
    <result column="create_time" jdbcType="TIMESTAMP" property="createTime" />
    <result column="remarks" jdbcType="VARCHAR" property="remarks" />
    <result column="voucher_url" jdbcType="VARCHAR" property="voucherUrl" />
    <result column="total_amount" jdbcType="DECIMAL" property="totalAmount" />
    <result column="settlement_amount" jdbcType="DECIMAL" property="settlementAmount" />
    <result column="change_amount" jdbcType="DECIMAL" property="changeAmount" />
    <result column="applicant" jdbcType="VARCHAR" property="applicant" />
    <result column="dep_name" jdbcType="VARCHAR" property="depName" />
    <result column="project_state" jdbcType="VARCHAR" property="projectState" />
    <result column="zxs_cost" jdbcType="DECIMAL" property="zxsCost" />
    <result column="zxs_remarks" jdbcType="VARCHAR" property="zxsRemarks" />
    <result column="payment_time" jdbcType="TIMESTAMP" property="paymentTime" />
    <result column="payment_amount" jdbcType="DECIMAL" property="paymentAmount" />
    <result column="invoice_time" jdbcType="TIMESTAMP" property="invoiceTime" />
    <result column="invoice_amount" jdbcType="DECIMAL" property="invoiceAmount" />
    <result column="invoice_type" jdbcType="VARCHAR" property="invoiceType" />
    <result column="cw_cost" jdbcType="DECIMAL" property="cwCost" />
    <result column="refundable_amount" jdbcType="DECIMAL" property="refundableAmount" />
    <result column="cw_remarks" jdbcType="VARCHAR" property="cwRemarks" />
    <result column="estimate_cost" jdbcType="DECIMAL" property="estimateCost" />
    <result column="estimate_refundable" jdbcType="DECIMAL" property="estimateRefundable" />
    <result column="refund_Invoice" jdbcType="DECIMAL" property="refundInvoice" />
    <result column="used_order" jdbcType="VARCHAR" property="usedOrder" />
    <result column="refund_status" jdbcType="INTEGER" property="refundStatus" />
    <result column="refund_url" jdbcType="VARCHAR" property="refundUrl" />
    <result column="back_status" jdbcType="INTEGER" property="backStatus" />
    <result column="attachment_url" jdbcType="VARCHAR" property="attachmentUrl" />
    <result column="contract_picture_url" jdbcType="VARCHAR" property="contractPictureUrl" />
    <result column="first_amount" jdbcType="DECIMAL" property="firstAmount" />
    <result column="examine_name" jdbcType="VARCHAR" property="examineName" />
  </resultMap>
  <sql id="Base_Column_List">
    id, order_no, process_state, `status`, `type`, create_time, remarks, voucher_url,
    total_amount, settlement_amount, change_amount, applicant, dep_name, project_state,
    zxs_cost, zxs_remarks, payment_time, payment_amount, invoice_time, invoice_amount,
    invoice_type, cw_cost, refundable_amount, cw_remarks, estimate_cost, estimate_refundable,
    refund_Invoice, used_order, refund_status, refund_url, back_status, attachment_url,
    contract_picture_url, first_amount, examine_name
  </sql>
  <select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
    select
    <include refid="Base_Column_List" />
    from new_order_change
    where id = #{id,jdbcType=INTEGER}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
    delete from new_order_change
    where id = #{id,jdbcType=INTEGER}
  </delete>
  <insert id="insert" keyColumn="id" keyProperty="id" parameterType="com.goafanti.common.model.NewOrderChange" useGeneratedKeys="true">
    insert into new_order_change (order_no, process_state, `status`,
      `type`, create_time, remarks,
      voucher_url, total_amount, settlement_amount,
      change_amount, applicant, dep_name,
      project_state, zxs_cost, zxs_remarks,
      payment_time, payment_amount, invoice_time,
      invoice_amount, invoice_type, cw_cost,
      refundable_amount, cw_remarks, estimate_cost,
      estimate_refundable, refund_Invoice, used_order,
      refund_status, refund_url, back_status,
      attachment_url, contract_picture_url, first_amount,
      examine_name)
    values (#{orderNo,jdbcType=VARCHAR}, #{processState,jdbcType=INTEGER}, #{status,jdbcType=INTEGER},
      #{type,jdbcType=INTEGER}, #{createTime,jdbcType=TIMESTAMP}, #{remarks,jdbcType=VARCHAR},
      #{voucherUrl,jdbcType=VARCHAR}, #{totalAmount,jdbcType=DECIMAL}, #{settlementAmount,jdbcType=DECIMAL},
      #{changeAmount,jdbcType=DECIMAL}, #{applicant,jdbcType=VARCHAR}, #{depName,jdbcType=VARCHAR},
      #{projectState,jdbcType=VARCHAR}, #{zxsCost,jdbcType=DECIMAL}, #{zxsRemarks,jdbcType=VARCHAR},
      #{paymentTime,jdbcType=TIMESTAMP}, #{paymentAmount,jdbcType=DECIMAL}, #{invoiceTime,jdbcType=TIMESTAMP},
      #{invoiceAmount,jdbcType=DECIMAL}, #{invoiceType,jdbcType=VARCHAR}, #{cwCost,jdbcType=DECIMAL},
      #{refundableAmount,jdbcType=DECIMAL}, #{cwRemarks,jdbcType=VARCHAR}, #{estimateCost,jdbcType=DECIMAL},
      #{estimateRefundable,jdbcType=DECIMAL}, #{refundInvoice,jdbcType=DECIMAL}, #{usedOrder,jdbcType=VARCHAR},
      #{refundStatus,jdbcType=INTEGER}, #{refundUrl,jdbcType=VARCHAR}, #{backStatus,jdbcType=INTEGER},
      #{attachmentUrl,jdbcType=VARCHAR}, #{contractPictureUrl,jdbcType=VARCHAR}, #{firstAmount,jdbcType=DECIMAL},
      #{examineName,jdbcType=VARCHAR})
  </insert>
  <insert id="insertSelective" keyColumn="id" keyProperty="id" parameterType="com.goafanti.common.model.NewOrderChange" useGeneratedKeys="true">
    insert into new_order_change
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="orderNo != null">
        order_no,
      </if>
      <if test="processState != null">
        process_state,
      </if>
      <if test="status != null">
        `status`,
      </if>
      <if test="type != null">
        `type`,
      </if>
      <if test="createTime != null">
        create_time,
      </if>
      <if test="remarks != null">
        remarks,
      </if>
      <if test="voucherUrl != null">
        voucher_url,
      </if>
      <if test="totalAmount != null">
        total_amount,
      </if>
      <if test="settlementAmount != null">
        settlement_amount,
      </if>
      <if test="changeAmount != null">
        change_amount,
      </if>
      <if test="applicant != null">
        applicant,
      </if>
      <if test="depName != null">
        dep_name,
      </if>
      <if test="projectState != null">
        project_state,
      </if>
      <if test="zxsCost != null">
        zxs_cost,
      </if>
      <if test="zxsRemarks != null">
        zxs_remarks,
      </if>
      <if test="paymentTime != null">
        payment_time,
      </if>
      <if test="paymentAmount != null">
        payment_amount,
      </if>
      <if test="invoiceTime != null">
        invoice_time,
      </if>
      <if test="invoiceAmount != null">
        invoice_amount,
      </if>
      <if test="invoiceType != null">
        invoice_type,
      </if>
      <if test="cwCost != null">
        cw_cost,
      </if>
      <if test="refundableAmount != null">
        refundable_amount,
      </if>
      <if test="cwRemarks != null">
        cw_remarks,
      </if>
      <if test="estimateCost != null">
        estimate_cost,
      </if>
      <if test="estimateRefundable != null">
        estimate_refundable,
      </if>
      <if test="refundInvoice != null">
        refund_Invoice,
      </if>
      <if test="usedOrder != null">
        used_order,
      </if>
      <if test="refundStatus != null">
        refund_status,
      </if>
      <if test="refundUrl != null">
        refund_url,
      </if>
      <if test="backStatus != null">
        back_status,
      </if>
      <if test="attachmentUrl != null">
        attachment_url,
      </if>
      <if test="contractPictureUrl != null">
        contract_picture_url,
      </if>
      <if test="firstAmount != null">
        first_amount,
      </if>
      <if test="examineName != null">
        examine_name,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
      <if test="orderNo != null">
        #{orderNo,jdbcType=VARCHAR},
      </if>
      <if test="processState != null">
        #{processState,jdbcType=INTEGER},
      </if>
      <if test="status != null">
        #{status,jdbcType=INTEGER},
      </if>
      <if test="type != null">
        #{type,jdbcType=INTEGER},
      </if>
      <if test="createTime != null">
        #{createTime,jdbcType=TIMESTAMP},
      </if>
      <if test="remarks != null">
        #{remarks,jdbcType=VARCHAR},
      </if>
      <if test="voucherUrl != null">
        #{voucherUrl,jdbcType=VARCHAR},
      </if>
      <if test="totalAmount != null">
        #{totalAmount,jdbcType=DECIMAL},
      </if>
      <if test="settlementAmount != null">
        #{settlementAmount,jdbcType=DECIMAL},
      </if>
      <if test="changeAmount != null">
        #{changeAmount,jdbcType=DECIMAL},
      </if>
      <if test="applicant != null">
        #{applicant,jdbcType=VARCHAR},
      </if>
      <if test="depName != null">
        #{depName,jdbcType=VARCHAR},
      </if>
      <if test="projectState != null">
        #{projectState,jdbcType=VARCHAR},
      </if>
      <if test="zxsCost != null">
        #{zxsCost,jdbcType=DECIMAL},
      </if>
      <if test="zxsRemarks != null">
        #{zxsRemarks,jdbcType=VARCHAR},
      </if>
      <if test="paymentTime != null">
        #{paymentTime,jdbcType=TIMESTAMP},
      </if>
      <if test="paymentAmount != null">
        #{paymentAmount,jdbcType=DECIMAL},
      </if>
      <if test="invoiceTime != null">
        #{invoiceTime,jdbcType=TIMESTAMP},
      </if>
      <if test="invoiceAmount != null">
        #{invoiceAmount,jdbcType=DECIMAL},
      </if>
      <if test="invoiceType != null">
        #{invoiceType,jdbcType=VARCHAR},
      </if>
      <if test="cwCost != null">
        #{cwCost,jdbcType=DECIMAL},
      </if>
      <if test="refundableAmount != null">
        #{refundableAmount,jdbcType=DECIMAL},
      </if>
      <if test="cwRemarks != null">
        #{cwRemarks,jdbcType=VARCHAR},
      </if>
      <if test="estimateCost != null">
        #{estimateCost,jdbcType=DECIMAL},
      </if>
      <if test="estimateRefundable != null">
        #{estimateRefundable,jdbcType=DECIMAL},
      </if>
      <if test="refundInvoice != null">
        #{refundInvoice,jdbcType=DECIMAL},
      </if>
      <if test="usedOrder != null">
        #{usedOrder,jdbcType=VARCHAR},
      </if>
      <if test="refundStatus != null">
        #{refundStatus,jdbcType=INTEGER},
      </if>
      <if test="refundUrl != null">
        #{refundUrl,jdbcType=VARCHAR},
      </if>
      <if test="backStatus != null">
        #{backStatus,jdbcType=INTEGER},
      </if>
      <if test="attachmentUrl != null">
        #{attachmentUrl,jdbcType=VARCHAR},
      </if>
      <if test="contractPictureUrl != null">
        #{contractPictureUrl,jdbcType=VARCHAR},
      </if>
      <if test="firstAmount != null">
        #{firstAmount,jdbcType=DECIMAL},
      </if>
      <if test="examineName != null">
        #{examineName,jdbcType=VARCHAR},
      </if>
    </trim>
  </insert>
  <update id="updateByPrimaryKeySelective" parameterType="com.goafanti.common.model.NewOrderChange">
    update new_order_change
    <set>
      <if test="orderNo != null">
        order_no = #{orderNo,jdbcType=VARCHAR},
      </if>
      <if test="processState != null">
        process_state = #{processState,jdbcType=INTEGER},
      </if>
      <if test="status != null">
        `status` = #{status,jdbcType=INTEGER},
      </if>
      <if test="type != null">
        `type` = #{type,jdbcType=INTEGER},
      </if>
      <if test="createTime != null">
        create_time = #{createTime,jdbcType=TIMESTAMP},
      </if>
      <if test="remarks != null">
        remarks = #{remarks,jdbcType=VARCHAR},
      </if>
      <if test="voucherUrl != null">
        voucher_url = #{voucherUrl,jdbcType=VARCHAR},
      </if>
      <if test="totalAmount != null">
        total_amount = #{totalAmount,jdbcType=DECIMAL},
      </if>
      <if test="settlementAmount != null">
        settlement_amount = #{settlementAmount,jdbcType=DECIMAL},
      </if>
      <if test="changeAmount != null">
        change_amount = #{changeAmount,jdbcType=DECIMAL},
      </if>
      <if test="applicant != null">
        applicant = #{applicant,jdbcType=VARCHAR},
      </if>
      <if test="depName != null">
        dep_name = #{depName,jdbcType=VARCHAR},
      </if>
      <if test="projectState != null">
        project_state = #{projectState,jdbcType=VARCHAR},
      </if>
      <if test="zxsCost != null">
        zxs_cost = #{zxsCost,jdbcType=DECIMAL},
      </if>
      <if test="zxsRemarks != null">
        zxs_remarks = #{zxsRemarks,jdbcType=VARCHAR},
      </if>
      <if test="paymentTime != null">
        payment_time = #{paymentTime,jdbcType=TIMESTAMP},
      </if>
      <if test="paymentAmount != null">
        payment_amount = #{paymentAmount,jdbcType=DECIMAL},
      </if>
      <if test="invoiceTime != null">
        invoice_time = #{invoiceTime,jdbcType=TIMESTAMP},
      </if>
      <if test="invoiceAmount != null">
        invoice_amount = #{invoiceAmount,jdbcType=DECIMAL},
      </if>
      <if test="invoiceType != null">
        invoice_type = #{invoiceType,jdbcType=VARCHAR},
      </if>
      <if test="cwCost != null">
        cw_cost = #{cwCost,jdbcType=DECIMAL},
      </if>
      <if test="refundableAmount != null">
        refundable_amount = #{refundableAmount,jdbcType=DECIMAL},
      </if>
      <if test="cwRemarks != null">
        cw_remarks = #{cwRemarks,jdbcType=VARCHAR},
      </if>
      <if test="estimateCost != null">
        estimate_cost = #{estimateCost,jdbcType=DECIMAL},
      </if>
      <if test="estimateRefundable != null">
        estimate_refundable = #{estimateRefundable,jdbcType=DECIMAL},
      </if>
      <if test="refundInvoice != null">
        refund_Invoice = #{refundInvoice,jdbcType=DECIMAL},
      </if>
      <if test="usedOrder != null">
        used_order = #{usedOrder,jdbcType=VARCHAR},
      </if>
      <if test="refundStatus != null">
        refund_status = #{refundStatus,jdbcType=INTEGER},
      </if>
      <if test="refundUrl != null">
        refund_url = #{refundUrl,jdbcType=VARCHAR},
      </if>
      <if test="backStatus != null">
        back_status = #{backStatus,jdbcType=INTEGER},
      </if>
      <if test="attachmentUrl != null">
        attachment_url = #{attachmentUrl,jdbcType=VARCHAR},
      </if>
      <if test="contractPictureUrl != null">
        contract_picture_url = #{contractPictureUrl,jdbcType=VARCHAR},
      </if>
      <if test="firstAmount != null">
        first_amount = #{firstAmount,jdbcType=DECIMAL},
      </if>
      <if test="examineName != null">
        examine_name = #{examineName,jdbcType=VARCHAR},
      </if>
    </set>
    where id = #{id,jdbcType=INTEGER}
  </update>
  <update id="updateByPrimaryKey" parameterType="com.goafanti.common.model.NewOrderChange">
    update new_order_change
    set order_no = #{orderNo,jdbcType=VARCHAR},
      process_state = #{processState,jdbcType=INTEGER},
      `status` = #{status,jdbcType=INTEGER},
      `type` = #{type,jdbcType=INTEGER},
      create_time = #{createTime,jdbcType=TIMESTAMP},
      remarks = #{remarks,jdbcType=VARCHAR},
      voucher_url = #{voucherUrl,jdbcType=VARCHAR},
      total_amount = #{totalAmount,jdbcType=DECIMAL},
      settlement_amount = #{settlementAmount,jdbcType=DECIMAL},
      change_amount = #{changeAmount,jdbcType=DECIMAL},
      applicant = #{applicant,jdbcType=VARCHAR},
      dep_name = #{depName,jdbcType=VARCHAR},
      project_state = #{projectState,jdbcType=VARCHAR},
      zxs_cost = #{zxsCost,jdbcType=DECIMAL},
      zxs_remarks = #{zxsRemarks,jdbcType=VARCHAR},
      payment_time = #{paymentTime,jdbcType=TIMESTAMP},
      payment_amount = #{paymentAmount,jdbcType=DECIMAL},
      invoice_time = #{invoiceTime,jdbcType=TIMESTAMP},
      invoice_amount = #{invoiceAmount,jdbcType=DECIMAL},
      invoice_type = #{invoiceType,jdbcType=VARCHAR},
      cw_cost = #{cwCost,jdbcType=DECIMAL},
      refundable_amount = #{refundableAmount,jdbcType=DECIMAL},
      cw_remarks = #{cwRemarks,jdbcType=VARCHAR},
      estimate_cost = #{estimateCost,jdbcType=DECIMAL},
      estimate_refundable = #{estimateRefundable,jdbcType=DECIMAL},
      refund_Invoice = #{refundInvoice,jdbcType=DECIMAL},
      used_order = #{usedOrder,jdbcType=VARCHAR},
      refund_status = #{refundStatus,jdbcType=INTEGER},
      refund_url = #{refundUrl,jdbcType=VARCHAR},
      back_status = #{backStatus,jdbcType=INTEGER},
      attachment_url = #{attachmentUrl,jdbcType=VARCHAR},
      contract_picture_url = #{contractPictureUrl,jdbcType=VARCHAR},
      first_amount = #{firstAmount,jdbcType=DECIMAL},
      examine_name = #{examineName,jdbcType=VARCHAR}
    where id = #{id,jdbcType=INTEGER}
  </update>
  <select id="selectByorderNo" resultType="com.goafanti.order.bo.NewOrderChangeBo">
select
	a.id, a.order_no orderNo, a.process_state processState, a.status, a.`type`,a.refundable_amount refundableAmount,
	date_format(a.create_time, '%Y-%m-%d %H:%i:%S')createTimes, a.remarks, a.voucher_url voucherUrl,a.cw_cost cwCost,
	a.total_amount totalAmount, a.settlement_amount settlementAmount, (a.total_amount-a.settlement_amount) arrears,
	a.change_amount changeAmount, a.applicant, a.dep_name depName, a.project_state projectState, a.zxs_cost zxsCost,
	a.zxs_remarks zxsRemarks, date_format(a.payment_time, '%Y-%m-%d')paymentTimes, a.payment_amount paymentAmount,
	date_format(a. invoice_time, '%Y-%m-%d') invoiceTimes, a.invoice_amount invoiceAmount,a.invoice_type invoiceType,
	a.cw_remarks cwRemarks,a.contract_picture_url contractPictureUrl,a.first_amount firstAmount,a.examine_name examineName
    from new_order_change a where 1 = 1
	and a.order_no= #{orderNo}
	order by a.create_time desc limit 1
  </select>
  <select id="selectByChangeId" resultType="com.goafanti.order.bo.NewOrderChangeBo">
select
	a.id, a.order_no orderNo, a.process_state processState, a.status, a.`type`,a.refundable_amount refundableAmount,
	date_format(a.create_time, '%Y-%m-%d %H:%i:%S')createTimes, a.remarks, a.voucher_url voucherUrl,a.cw_cost cwCost,
	a.total_amount totalAmount, a.settlement_amount settlementAmount, (a.total_amount-a.settlement_amount) arrears,
	a.change_amount changeAmount, a.applicant, a.dep_name depName, a.project_state projectState, a.zxs_cost zxsCost,
	a.zxs_remarks zxsRemarks, date_format(a.payment_time, '%Y-%m-%d')paymentTimes, a.payment_amount paymentAmount,
	date_format(a. invoice_time, '%Y-%m-%d') invoiceTimes, a.invoice_amount invoiceAmount,a.invoice_type invoiceType,
	a.cw_remarks cwRemarks,a.back_status backStatus ,a.contract_picture_url contractPictureUrl,a.first_amount firstAmount
    from new_order_change a where 1 = 1
	and a.id= #{ChangeId}
	order by a.create_time desc
  </select>

  <select id="selectByorderNoAndStatus" resultType="com.goafanti.order.bo.NewOrderChangeBo">
select
	a.id, a.order_no orderNo, a.process_state processState, a.status, a.`type`,a.refundable_amount refundableAmount,
	date_format(a.create_time, '%Y-%m-%d %H:%i:%S')createTimes, a.remarks, a.voucher_url voucherUrl,a.cw_cost cwCost,
	a.total_amount totalAmount, a.settlement_amount settlementAmount, (a.total_amount-a.settlement_amount) arrears,
	a.change_amount changeAmount, a.applicant, a.dep_name depName, a.project_state projectState, a.zxs_cost zxsCost,
	a.zxs_remarks zxsRemarks, date_format(a.payment_time, '%Y-%m-%d')paymentTimes, a.payment_amount paymentAmount,a.first_amount firstAmount,
	date_format(a. invoice_time, '%Y-%m-%d') invoiceTimes, a.invoice_amount invoiceAmount,a.invoice_type invoiceType,
	a.cw_remarks cwRemarks,a.back_status backStatus from new_order_change a where 1 = 1 and status in (0,1,2,3)
	and a.order_no= #{orderNo}
	order by a.create_time desc
  </select>

  <select id="selectById" resultType="com.goafanti.order.bo.NewOrderChangeBo">
  select
	a.id, a.order_no orderNo, a.process_state processState, a.status, a.`type`,a.refundable_amount refundableAmount,a.refund_url refundUrl,
	date_format(a.create_time, '%Y-%m-%d %H:%i:%S')createTimes, a.remarks, a.voucher_url voucherUrl,a.cw_cost cwCost,a.refund_status refundStatus,
	a.total_amount totalAmount, a.settlement_amount settlementAmount, (a.total_amount-a.settlement_amount) arrears,a.attachment_url attachmentUrl,
	a.change_amount changeAmount, a.applicant, a.dep_name depName, a.project_state projectState, a.zxs_cost zxsCost,a.first_amount firstAmount,
	a.zxs_remarks zxsRemarks, date_format(a.payment_time, '%Y-%m-%d')paymentTimes, a.payment_amount paymentAmount,a.examine_name examineName,
	date_format(a. invoice_time, '%Y-%m-%d') invoiceTimes, a.invoice_amount invoiceAmount,a.invoice_type invoiceType,
	a.cw_remarks cwRemarks,a.estimate_cost estimateCost,estimate_refundable estimateRefundable,refund_Invoice refundInvoice,a.contract_picture_url contractPictureUrl
	from new_order_change a where 1 = 1 and a.id= #{id}
  </select>
  <select id="checkOderNo" resultType="java.lang.Integer">
  select count(*) from new_order_change
	where order_no= #{orderNo} and status in (0,1,2,3,6)
  </select>
  <select id="selectOrderChangeList" resultType="com.goafanti.order.bo.NewOderCahngeListBo">
	select a.id,a.order_no orderNo,b.contract_no contractNo, date_format(a.create_time,'%Y-%m-%d %H:%i:%S')createTimes,c.nickname,b.order_type orderType,
  date_format(b.create_time,'%Y-%m-%d %H:%i:%S')establishTimes, b.order_status orderStatus,b.total_amount totalAmount,a.status ,a.examine_name examineName,
  b.settlement_amount settlementAmount,(b.total_amount-b.settlement_amount) arrears,d.name salesmanName,e.name depName,a.`type`,
  a.process_state processState,a.back_status backStatus from new_order_change  noc left join  new_order_change a on noc.id=a.id
  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 e on b.order_dep=e.id
 	<if test="processState==3">
 	left join (select order_no, aid from order_examine where`type`=0 group by order_no,aid)x on a.order_no = x.order_no
	</if>
	<if test="processState==4">
	left join (select order_no, aid from order_examine where`type`=1 group by order_no,aid)x on a.order_no = x.order_no
	</if>
	where 1=1
	<if test="processState==0">
	and b.salesman_id= #{aid}
	</if>
    <if test="processState==1">
      and d.manager_id= #{aid}
    </if>
	<if test="processState==3 and complete !=5">
	and  x.aid=#{aid}  and a.process_state &gt;= #{processState}
	</if>
	<if test="processState==4 and complete !=5">
	and x.aid=#{aid}  and a.process_state &gt;= #{processState}
	</if>
	<if test="processState==3 and complete ==5">
	and  x.aid=#{aid}  and a.back_status &gt;= #{processState}
	</if>
	<if test="processState==4 and complete ==5">
	and x.aid=#{aid}  and a.back_status &gt;= #{processState}
	</if>
	<if test="processState==6 and aid !=null">
	and e.finance_id =#{aid}
	</if>
	<if test="processState==6 and aids !=null">
	and e.finance_id in
		<foreach close=")" collection="aids" item="id" open="(" separator=",">
		#{id}
		</foreach>
	</if>
	<if test="userName !=null">
	and c.nickname like concat('%',#{userName},'%')
	</if>
	<if test="orderNo !=null">
	and a.order_no like concat('%',#{orderNo},'%')
	</if>
	<if test="contractNo !=null">
	and b.contract_no like concat('%',#{contractNo},'%')
	</if>
	<if test="type !=null">
	and a.type = #{type}
	</if>
	<if test="timeType == 0 and startTime !=null and endTime !=null">
	and a.create_time between #{startTime} and #{endTime}
	</if>
	<if test="timeType == 1 and startTime !=null and endTime !=null">
	and b.create_time between #{startTime} and #{endTime}
	</if>
	<if test="deps !=null">
	and b.order_dep in
      <foreach close=")" collection="deps" item="depId" open="(" separator=",">
        #{depId}
      </foreach>
	</if>
	<if test="salesmanName !=null">
	and d.name like concat('%',#{salesmanName},'%')
	</if>
	<if test="complete ==0 and processState != 9">
	 and (a.process_state &gt;= #{processState} or a.back_status &gt;= #{processState})
	</if>
	<if test="complete ==0 and processState == 9">
	 and (a.process_state &gt;= #{processState} and  a.process_state &lt; 10)or a.back_status &gt;= #{processState}
	</if>
	<if test="complete ==1 and processState != 6 and processState != 8 and processState != 9">
	 and a.process_state = #{processState} and a.status in (0,1,2,3)
	</if>
	<if test="complete ==1 and processState == 6">
	 and (a.process_state in(6)  and a.status in (0,1,2,3,5,6))
	</if>
	<if test="complete ==1 and processState == 8">
	 and (a.process_state = 8  and  a.status =1)
	</if>
	<if test="complete ==1 and processState == 9">
	 and (a.process_state = #{processState}  and a.status =1)
	</if>
	<if test="complete ==2 and processState != 6 and processState != 8 and processState != 9">
	 and a.process_state &gt; #{processState}
	</if>
	<if test="complete ==2 and processState == 6">
	 and a.process_state &gt; 6 and a.process_state &lt; 10
	</if>
	<if test="complete ==2 and processState == 8">
	 and ((a.process_state = 8  and  (a.status = 4 or a.status = 2))or a.process_state &gt; 8)
	</if>
	<if test="complete ==2 and processState == 9">
	 and a.process_state = 9  and   (a.status = 4 or a.status = 2)
	</if>
	<if test="complete ==3">
	 and   a.status = 2
	</if>
	<if test="complete ==4">
	 and   a.status = 4
	</if>
    <if test="complete ==5 and processState != 3 and processState != 4">
	 and a.back_status &gt;= #{processState}
	</if>
    <if test="complete ==6">
      and   a.status = 6
    </if>
	order by a.status,a.create_time desc
	<if test="page_sql != null">
			${page_sql}
	</if>
  </select>

  <select id="selectOrderChangeCount" resultType="java.lang.Integer">
  select count(*) from new_order_change noc left join  new_order_change a on noc.id=a.id
  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 e on b.order_dep=e.id
		<if test="processState==3">
 	left join (select order_no, aid from order_examine where`type`=0 group by order_no,aid)x on a.order_no = x.order_no
	</if>
	<if test="processState==4">
	left join (select order_no, aid from order_examine where`type`=1 group by order_no,aid)x on a.order_no = x.order_no
	</if>
	where 1=1
    <if test="processState==0">
      and b.salesman_id= #{aid}
    </if>
    <if test="processState==1">
      and d.manager_id= #{aid}
    </if>
	<if test="processState==3 and complete !=5">
	and  x.aid=#{aid}  and a.process_state &gt;= #{processState}
	</if>
	<if test="processState==4 and complete !=5">
	and x.aid=#{aid}  and a.process_state &gt;= #{processState}
	</if>
	<if test="processState==3 and complete ==5">
	and  x.aid=#{aid}  and a.back_status &gt;= #{processState}
	</if>
	<if test="processState==4 and complete ==5">
	and x.aid=#{aid}  and a.back_status &gt;= #{processState}
	</if>
	<if test="processState==6 and aid !=null">
	and e.finance_id =#{aid}
	</if>
	<if test="processState==6 and aids !=null">
	and e.finance_id in
		<foreach close=")" collection="aids" item="id" open="(" separator=",">
		#{id}
		</foreach>
	</if>
	<if test="userName !=null">
	and c.nickname like concat('%',#{userName},'%')
	</if>
	<if test="orderNo !=null">
	and a.order_no like concat('%',#{orderNo},'%')
	</if>
	<if test="contractNo !=null">
	and b.contract_no like concat('%',#{contractNo},'%')
	</if>
	<if test="type !=null">
	and a.type = #{type}
	</if>
	<if test="timeType == 0 and startTime !=null and endTime !=null">
	and a.create_time between #{startTime} and #{endTime}
	</if>
	<if test="timeType == 1 and startTime !=null and endTime !=null">
	and b.create_time between #{startTime} and #{endTime}
	</if>
    <if test="deps !=null">
      and b.order_dep in
      <foreach close=")" collection="deps" item="depId" open="(" separator=",">
        #{depId}
      </foreach>
    </if>
	<if test="salesmanName !=null">
	and d.name like concat('%',#{salesmanName},'%')
	</if>
	<if test="complete ==0 and processState != 9">
	 and (a.process_state &gt;= #{processState} or a.back_status &gt;= #{processState})
	</if>
	<if test="complete ==0 and processState == 9">
	 and (a.process_state &gt;= #{processState} and  a.process_state &lt; 10)or a.back_status &gt;= #{processState}
	</if>
	<if test="complete ==1 and processState != 6 and processState != 8 and processState != 9">
      and a.process_state = #{processState} and a.status in (0,1,2,3)
	</if>
	<if test="complete ==1 and processState == 6">
	 and (a.process_state in(6)  and a.status in (0,1,2,3,5,6))
	</if>
    <if test="complete ==1 and processState == 8">
      and (a.process_state = 8  and  a.status =1)
    </if>
    <if test="complete ==1 and processState == 9">
      and (a.process_state = #{processState} and   a.status =1)
    </if>
	<if test="complete ==2 and processState != 6 and processState != 8 and processState != 9">
	 and a.process_state &gt; #{processState}
	</if>
	<if test="complete ==2 and processState == 6">
	 and a.process_state &gt; 6 and a.process_state &lt; 10
	</if>
	<if test="complete ==2 and processState == 8">
	 and ((a.process_state = 8  and  (a.status = 4 or a.status = 2))or a.process_state &gt; 8)
	</if>
	<if test="complete ==2 and processState == 9">
	 and a.process_state = 9  and   (a.status = 4 or a.status = 2)
	</if>
	<if test="complete ==3">
	 and   a.status = 2
	</if>
	<if test="complete ==4">
	 and   a.status = 4
	</if>
	<if test="complete ==5 and processState != 3 and processState != 4">
	 and a.back_status &gt;= #{processState}
	</if>
    <if test="complete ==6">
      and   a.status = 6
    </if>
	</select>
	<update id="addRefundInvoice">
	update  new_order_change
	set `refund_Invoice`=ifnull(`refund_Invoice`,0)+ #{amount}
	where order_no= #{orderNo}
	</update>
	<update id="subtractRefundInvoice">
	update  new_order_change
	set `refund_Invoice`=ifnull(`refund_Invoice`,0)- #{amount}
	where order_no= #{orderNo}
	</update>

    <select id="getOrderChange" resultType="java.util.Map">
	select id,used_order usedOrder from new_order_change where  order_no= #{orderNo} order by id desc
	</select>


  <update id="updateExamineName">
    update new_order_change a , t_order_new b ,(select a.department_id depId,group_concat(a.name) name
                                                from admin a left join user_role b on a.id=b.uid
                                                             left join `role` c on b.rid =c.id
                                                where c.role_type ='9996' and a.status ='0' and a.department_id = #{depId}
                                                group by a.department_id ) c
    set a.examine_name =c.name
    where a.order_no =b.order_no and b.order_dep = #{depId} and b.order_dep =c.depId
  </update>
    <update id="updateTaskExamineName">
      update new_order_change n left join (select c.id
             from t_order_new a left join t_order_task b on a.order_no =b.order_no
                                left join new_order_change c on a.order_no =c.order_no
             where (b.consultant_id in (#{aid},#{taskReceiverId}) or b.manager_id in (#{aid},#{taskReceiverId}))
               and c.process_state in (#{type}) group by c.id)x on n.id =x.id
      left join (select a.order_no ,group_concat(DISTINCT b.name)name from order_examine a left join admin b on a.aid=b.id
                   where 1=1
                   <if test="type==3">
                   and  a.type=0
                   </if>
                  <if test="type==4">
                  and   a.type=1
                  </if>
      group by a.order_no ) a on n.order_no =a.order_no
      set n.examine_name =a.name
      where n.process_state =#{type}
    </update>
  <update id="updateExamineById">
    update new_order_change a left join t_order_new b on a.order_no =b.order_no
    <if test="type==0 or type==1">
      left join admin c on b.salesman_id =c.id
      left join admin d on c.manager_id =d.id
      set a.examine_name=d.name
      where a.process_state =1 and d.id = #{id}
    </if>
    <if test="type==1">
      left join admin c on b.salesman_id =c.id
      set a.examine_name=c.name
      where a.process_state =1 and c.manager_id = #{id}
    </if>
    <if test="type==2">
      left join (select a.department_id depId,group_concat(a.name) name
      from admin a left join admin b on a.department_id =b.department_id
      left join user_role c on a.id=c.uid left join `role` d on c.rid =d.id
      where  b.id= #{id} and d.role_type ='9996'
      and a.status ='0' group by b.department_id )x on b.order_dep=x.depId
      set a.examine_name=x.name
      where a.process_state =2
    </if>
    <if test="type==3">
      left join t_order_task c on a.order_no =c.order_no
      left join (select a.order_no ,a.`type` ,GROUP_CONCAT(distinct b.name) name
      from order_examine a left join admin b on a.aid=b.id where  a.`type` =0
      group by order_no ,type)d on a.order_no =d.order_no
      set a.examine_name =d.name
      where a.process_state =3 and c.manager_id = #{id}
    </if>
    <if test="type==4">
      left join t_order_task c on a.order_no =c.order_no
      left join (select a.order_no ,a.`type` ,GROUP_CONCAT(distinct b.name) name
      from order_examine a left join admin b on a.aid=b.id where  a.`type` =1
      group by order_no ,type)d on a.order_no =d.order_no
      set a.examine_name =d.name
      where a.process_state =3 and c.consultant_id = #{id}
    </if>
    <if test="type==6">
      left join department c on b.order_dep =c.id
      left join admin d on c.finance_id =d.id
      set a.examine_name=d.name
      where a.process_state =6 and d.id = #{id}
    </if>
    <if test="type==5 or type==7">
      left join (select group_concat(a.name) name
      from admin a left join user_role b on a.id=b.uid
      left join `role` c on b.rid =c.id
        <if test="type==5">
      where c.role_type in ('9998') and a.status ='0'
        </if>
      <if test="type==7">
      where c.role_type in ('9997') and a.status ='0'
        </if>
        )c on 1=1
      set a.examine_name =c.name
      where a.process_state = #{type}
    </if>
  </update>

  <select id="getProcessName" resultType="com.goafanti.order.bo.OutProcessName">
    select a.id,a.order_no orderNo ,a.`type`,a.change_amount changeAmount ,a.status,a.process_state processState,
           c.name yxy,mg.name yxjl, yxgly.name yxgly,zxs.name zxs,zxsjl.name zxsjl, zxsgly.name zxsgly,
           fm.name cwzy,cwgly.name cwgly, zc.name zc,dsz.name dsz,b.salesman_id salesmanId
    from new_order_change a
           left join t_order_new b on a.order_no =b.order_no
           left join admin c on b.salesman_id =c.id
           left join department dep on b.order_dep =dep.id
           left join admin fm on dep.finance_id =fm.id
           left join admin mg on c.manager_id =mg.id
           left join (select a.department_id depId,group_concat(a.name) name
                      from admin a left join user_role b on a.id=b.uid
                                   left join `role` c on b.rid =c.id
                      where c.role_type ='9996' and a.status ='0' group by a.department_id )yxgly on b.order_dep =yxgly.depId
           left join (select a.order_no ,a.`type` ,GROUP_CONCAT(distinct b.name) name
                      from order_examine a left join admin b on a.aid=b.id where a.order_no = #{orderNo} and a.`type` =0
                      group by order_no ,type)zxs on a.order_no =zxs.order_no
           left join (select a.order_no ,a.`type` ,GROUP_CONCAT(distinct b.name) name
                      from order_examine a left join admin b on a.aid=b.id where a.order_no = #{orderNo} and a.`type` =1
                      group by order_no ,type)zxsjl on a.order_no =zxsjl.order_no
           left join (select group_concat(a.name) name
                      from admin a left join user_role b on a.id=b.uid
                                   left join `role` c on b.rid =c.id
                      where c.role_type in ('99998','99988') and a.status ='0' ) zc on 1=1
           left join (select group_concat(a.name) name
                      from admin a left join user_role b on a.id=b.uid
                                   left join `role` c on b.rid =c.id
                      where c.role_type in ('99999','99989') and a.status ='0' ) dsz on 1=1
           left join (select group_concat(a.name) name
                      from admin a left join user_role b on a.id=b.uid
                                   left join `role` c on b.rid =c.id
                      where c.role_type in ('9998') and a.status ='0' ) zxsgly on 1=1
           left join (select group_concat(a.name) name
                      from admin a left join user_role b on a.id=b.uid
                                   left join `role` c on b.rid =c.id
                      where c.role_type in ('9997') and a.status ='0' ) cwgly on 1=1
    where a.id= #{id}
  </select>
    <select id="selectProcess" resultType="java.lang.Integer">
      select t0.id from (select x.id
            from (select a.id from new_order_change a left join t_order_task c on a.order_no =c.order_no
             where a.process_state= #{type}
             <if test="type==3">
             and c.consultant_id = #{aid}
             </if>
             <if test="type==4">
             and c.manager_id = #{aid}
             </if>
             group by a.id)x
                    left join new_order_change z on x.id=z.id
                    left join order_examine y on z.order_no=y.order_no
             where y.`type` =0 group by x.id)t0
    </select>
</mapper>
