<?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">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Fri Apr 10 15:06:35 CST 2020.
    -->
    <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" />
  </resultMap>
  <sql id="Example_Where_Clause">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Fri Apr 10 15:06:35 CST 2020.
    -->
    <where>
      <foreach collection="oredCriteria" item="criteria" separator="or">
        <if test="criteria.valid">
          <trim prefix="(" prefixOverrides="and" suffix=")">
            <foreach collection="criteria.criteria" item="criterion">
              <choose>
                <when test="criterion.noValue">
                  and ${criterion.condition}
                </when>
                <when test="criterion.singleValue">
                  and ${criterion.condition} #{criterion.value}
                </when>
                <when test="criterion.betweenValue">
                  and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
                </when>
                <when test="criterion.listValue">
                  and ${criterion.condition}
                  <foreach close=")" collection="criterion.value" item="listItem" open="(" separator=",">
                    #{listItem}
                  </foreach>
                </when>
              </choose>
            </foreach>
          </trim>
        </if>
      </foreach>
    </where>
  </sql>
  <sql id="Update_By_Example_Where_Clause">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Fri Apr 10 15:06:35 CST 2020.
    -->
    <where>
      <foreach collection="example.oredCriteria" item="criteria" separator="or">
        <if test="criteria.valid">
          <trim prefix="(" prefixOverrides="and" suffix=")">
            <foreach collection="criteria.criteria" item="criterion">
              <choose>
                <when test="criterion.noValue">
                  and ${criterion.condition}
                </when>
                <when test="criterion.singleValue">
                  and ${criterion.condition} #{criterion.value}
                </when>
                <when test="criterion.betweenValue">
                  and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
                </when>
                <when test="criterion.listValue">
                  and ${criterion.condition}
                  <foreach close=")" collection="criterion.value" item="listItem" open="(" separator=",">
                    #{listItem}
                  </foreach>
                </when>
              </choose>
            </foreach>
          </trim>
        </if>
      </foreach>
    </where>
  </sql>
  <sql id="Base_Column_List">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Fri Apr 10 15:06:35 CST 2020.
    -->
    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
  </sql>
  <select id="selectByExample" parameterType="com.goafanti.common.model.TOrderMidExample" resultMap="BaseResultMap">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Fri Apr 10 15:06:35 CST 2020.
    -->
    select
    <if test="distinct">
      distinct
    </if>
    <include refid="Base_Column_List" />
    from t_order_mid
    <if test="_parameter != null">
      <include refid="Example_Where_Clause" />
    </if>
    <if test="orderByClause != null">
      order by ${orderByClause}
    </if>
  </select>
  <select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Fri Apr 10 15:06:35 CST 2020.
    -->
    select 
    <include refid="Base_Column_List" />
    from t_order_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 Apr 10 15:06:35 CST 2020.
    -->
    delete from t_order_mid
    where id = #{id,jdbcType=INTEGER}
  </delete>
  <delete id="deleteByExample" parameterType="com.goafanti.common.model.TOrderMidExample">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Fri Apr 10 15:06:35 CST 2020.
    -->
    delete from t_order_mid
    <if test="_parameter != null">
      <include refid="Example_Where_Clause" />
    </if>
  </delete>
  <insert id="insert" parameterType="com.goafanti.common.model.TOrderMid">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Fri Apr 10 15:06:35 CST 2020.
    -->
    insert into t_order_mid (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)
    values (#{id,jdbcType=INTEGER}, #{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})
  </insert>
  <insert id="insertSelective" parameterType="com.goafanti.common.model.TOrderMid">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Fri Apr 10 15:06:35 CST 2020.
    -->
    insert into t_order_mid
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="id != null">
        id,
      </if>
      <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>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
      <if test="id != null">
        #{id,jdbcType=INTEGER},
      </if>
      <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>
    </trim>
  </insert>
  <select id="countByExample" parameterType="com.goafanti.common.model.TOrderMidExample" resultType="java.lang.Long">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Fri Apr 10 15:06:35 CST 2020.
    -->
    select count(*) from t_order_mid
    <if test="_parameter != null">
      <include refid="Example_Where_Clause" />
    </if>
  </select>
  <update id="updateByExampleSelective" parameterType="map">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Fri Apr 10 15:06:35 CST 2020.
    -->
    update t_order_mid
    <set>
      <if test="record.id != null">
        id = #{record.id,jdbcType=INTEGER},
      </if>
      <if test="record.orderNo != null">
        order_no = #{record.orderNo,jdbcType=VARCHAR},
      </if>
      <if test="record.salesmanId != null">
        salesman_id = #{record.salesmanId,jdbcType=VARCHAR},
      </if>
      <if test="record.financeId != null">
        finance_id = #{record.financeId,jdbcType=VARCHAR},
      </if>
      <if test="record.salesmanName != null">
        salesman_name = #{record.salesmanName,jdbcType=VARCHAR},
      </if>
      <if test="record.financeName != null">
        finance_name = #{record.financeName,jdbcType=VARCHAR},
      </if>
      <if test="record.finalReceivables != null">
        final_receivables = #{record.finalReceivables,jdbcType=DECIMAL},
      </if>
      <if test="record.finalReceivablesTime != null">
        final_Receivables_time = #{record.finalReceivablesTime,jdbcType=TIMESTAMP},
      </if>
      <if test="record.invoiceAmount != null">
        invoice_amount = #{record.invoiceAmount,jdbcType=DECIMAL},
      </if>
      <if test="record.createTime != null">
        create_time = #{record.createTime,jdbcType=TIMESTAMP},
      </if>
      <if test="record.buyerName != null">
        buyer_name = #{record.buyerName,jdbcType=VARCHAR},
      </if>
      <if test="record.orderReceivables != null">
        order_receivables = #{record.orderReceivables,jdbcType=DECIMAL},
      </if>
      <if test="record.orderArrears != null">
        order_arrears = #{record.orderArrears,jdbcType=DECIMAL},
      </if>
    </set>
    <if test="_parameter != null">
      <include refid="Update_By_Example_Where_Clause" />
    </if>
  </update>
  <update id="updateByExample" parameterType="map">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Fri Apr 10 15:06:35 CST 2020.
    -->
    update t_order_mid
    set id = #{record.id,jdbcType=INTEGER},
      order_no = #{record.orderNo,jdbcType=VARCHAR},
      salesman_id = #{record.salesmanId,jdbcType=VARCHAR},
      finance_id = #{record.financeId,jdbcType=VARCHAR},
      salesman_name = #{record.salesmanName,jdbcType=VARCHAR},
      finance_name = #{record.financeName,jdbcType=VARCHAR},
      final_receivables = #{record.finalReceivables,jdbcType=DECIMAL},
      final_Receivables_time = #{record.finalReceivablesTime,jdbcType=TIMESTAMP},
      invoice_amount = #{record.invoiceAmount,jdbcType=DECIMAL},
      create_time = #{record.createTime,jdbcType=TIMESTAMP},
      buyer_name = #{record.buyerName,jdbcType=VARCHAR},
      order_receivables = #{record.orderReceivables,jdbcType=DECIMAL},
      order_arrears = #{record.orderArrears,jdbcType=DECIMAL}
    <if test="_parameter != null">
      <include refid="Update_By_Example_Where_Clause" />
    </if>
  </update>
  <update id="updateByPrimaryKeySelective" parameterType="com.goafanti.common.model.TOrderMid">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Fri Apr 10 15:06:35 CST 2020.
    -->
    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>
    </set>
    where id = #{id,jdbcType=INTEGER}
  </update>
  <update id="updateByPrimaryKey" parameterType="com.goafanti.common.model.TOrderMid">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Fri Apr 10 15:06:35 CST 2020.
    -->
    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}
    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})
	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>
  <update id="addOrderReceivables"> 
  update t_order_mid  set order_receivables=order_receivables+#{money},
  order_arrears=order_arrears+#{money}
	where order_no=#{orderNo}
  </update>
</mapper>