<?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.TOrderOutsourceMapper">
  <resultMap id="BaseResultMap" type="com.goafanti.common.model.TOrderOutsource">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Thu Aug 05 11:38:34 CST 2021.
    -->
    <id column="id" jdbcType="INTEGER" property="id" />
    <result column="order_no" jdbcType="VARCHAR" property="orderNo" />
    <result column="refund_status" jdbcType="INTEGER" property="refundStatus" />
    <result column="create_time" jdbcType="TIMESTAMP" property="createTime" />
    <result column="remarks" jdbcType="VARCHAR" property="remarks" />
    <result column="attachment_url" jdbcType="VARCHAR" property="attachmentUrl" />
    <result column="amount" jdbcType="DECIMAL" property="amount" />
    <result column="company_name" jdbcType="VARCHAR" property="companyName" />
    <result column="audit_time" jdbcType="TIMESTAMP" property="auditTime" />
    <result column="unit_price" jdbcType="DECIMAL" property="unitPrice" />
    <result column="unit_number" jdbcType="INTEGER" property="unitNumber" />
    <result column="outsource_remarks" jdbcType="VARCHAR" property="outsourceRemarks" />
    <result column="type" jdbcType="INTEGER" property="type" />
    <result column="tid" jdbcType="INTEGER" property="tid" />
    <result column="start_type" jdbcType="INTEGER" property="startType" />
    <result column="patent_type" jdbcType="INTEGER" property="patentType" />
    <result column="patent_name_type" jdbcType="INTEGER" property="patentNameType" />
    <result column="patent_name" jdbcType="VARCHAR" property="patentName" />
    <result column="actual_official" jdbcType="INTEGER" property="actualOfficial" />
  </resultMap>
  <resultMap extends="BaseResultMap" id="ResultMapWithBLOBs" type="com.goafanti.common.model.TOrderOutsource">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Thu Aug 05 11:38:34 CST 2021.
    -->
    <result column="picture_url" jdbcType="LONGVARCHAR" property="pictureUrl" />
  </resultMap>
  <sql id="Base_Column_List">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Thu Aug 05 11:38:34 CST 2021.
    -->
    id, order_no, refund_status, create_time, remarks, attachment_url, amount, company_name,
    audit_time, unit_price, unit_number, outsource_remarks, `type`, tid, start_type,
    patent_type, patent_name_type, patent_name, actual_official
  </sql>
  <sql id="Blob_Column_List">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Thu Aug 05 11:38:34 CST 2021.
    -->
    picture_url
  </sql>
  <select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="ResultMapWithBLOBs">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Thu Aug 05 11:38:34 CST 2021.
    -->
    select
    <include refid="Base_Column_List" />
    ,
    <include refid="Blob_Column_List" />
    from t_order_outsource
    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 Thu Aug 05 11:38:34 CST 2021.
    -->
    delete from t_order_outsource
    where id = #{id,jdbcType=INTEGER}
  </delete>
  <insert id="insert" parameterType="com.goafanti.common.model.TOrderOutsource">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Thu Aug 05 11:38:34 CST 2021.
    -->
    insert into t_order_outsource (id, order_no, refund_status,
      create_time, remarks, attachment_url,
      amount, company_name, audit_time,
      unit_price, unit_number, outsource_remarks,
      `type`, tid, start_type,
      patent_type, patent_name_type, patent_name,
      actual_official, picture_url)
    values (#{id,jdbcType=INTEGER}, #{orderNo,jdbcType=VARCHAR}, #{refundStatus,jdbcType=INTEGER},
      #{createTime,jdbcType=TIMESTAMP}, #{remarks,jdbcType=VARCHAR}, #{attachmentUrl,jdbcType=VARCHAR},
      #{amount,jdbcType=DECIMAL}, #{companyName,jdbcType=VARCHAR}, #{auditTime,jdbcType=TIMESTAMP},
      #{unitPrice,jdbcType=DECIMAL}, #{unitNumber,jdbcType=INTEGER}, #{outsourceRemarks,jdbcType=VARCHAR},
      #{type,jdbcType=INTEGER}, #{tid,jdbcType=INTEGER}, #{startType,jdbcType=INTEGER},
      #{patentType,jdbcType=INTEGER}, #{patentNameType,jdbcType=INTEGER}, #{patentName,jdbcType=VARCHAR},
      #{actualOfficial,jdbcType=INTEGER}, #{pictureUrl,jdbcType=LONGVARCHAR})
  </insert>
  <insert id="insertSelective" parameterType="com.goafanti.common.model.TOrderOutsource">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Thu Aug 05 11:38:34 CST 2021.
    -->
    insert into t_order_outsource
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="id != null">
        id,
      </if>
      <if test="orderNo != null">
        order_no,
      </if>
      <if test="refundStatus != null">
        refund_status,
      </if>
      <if test="createTime != null">
        create_time,
      </if>
      <if test="remarks != null">
        remarks,
      </if>
      <if test="attachmentUrl != null">
        attachment_url,
      </if>
      <if test="amount != null">
        amount,
      </if>
      <if test="companyName != null">
        company_name,
      </if>
      <if test="auditTime != null">
        audit_time,
      </if>
      <if test="unitPrice != null">
        unit_price,
      </if>
      <if test="unitNumber != null">
        unit_number,
      </if>
      <if test="outsourceRemarks != null">
        outsource_remarks,
      </if>
      <if test="type != null">
        `type`,
      </if>
      <if test="tid != null">
        tid,
      </if>
      <if test="startType != null">
        start_type,
      </if>
      <if test="patentType != null">
        patent_type,
      </if>
      <if test="patentNameType != null">
        patent_name_type,
      </if>
      <if test="patentName != null">
        patent_name,
      </if>
      <if test="actualOfficial != null">
        actual_official,
      </if>
      <if test="pictureUrl != null">
        picture_url,
      </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="refundStatus != null">
        #{refundStatus,jdbcType=INTEGER},
      </if>
      <if test="createTime != null">
        #{createTime,jdbcType=TIMESTAMP},
      </if>
      <if test="remarks != null">
        #{remarks,jdbcType=VARCHAR},
      </if>
      <if test="attachmentUrl != null">
        #{attachmentUrl,jdbcType=VARCHAR},
      </if>
      <if test="amount != null">
        #{amount,jdbcType=DECIMAL},
      </if>
      <if test="companyName != null">
        #{companyName,jdbcType=VARCHAR},
      </if>
      <if test="auditTime != null">
        #{auditTime,jdbcType=TIMESTAMP},
      </if>
      <if test="unitPrice != null">
        #{unitPrice,jdbcType=DECIMAL},
      </if>
      <if test="unitNumber != null">
        #{unitNumber,jdbcType=INTEGER},
      </if>
      <if test="outsourceRemarks != null">
        #{outsourceRemarks,jdbcType=VARCHAR},
      </if>
      <if test="type != null">
        #{type,jdbcType=INTEGER},
      </if>
      <if test="tid != null">
        #{tid,jdbcType=INTEGER},
      </if>
      <if test="startType != null">
        #{startType,jdbcType=INTEGER},
      </if>
      <if test="patentType != null">
        #{patentType,jdbcType=INTEGER},
      </if>
      <if test="patentNameType != null">
        #{patentNameType,jdbcType=INTEGER},
      </if>
      <if test="patentName != null">
        #{patentName,jdbcType=VARCHAR},
      </if>
      <if test="actualOfficial != null">
        #{actualOfficial,jdbcType=INTEGER},
      </if>
      <if test="pictureUrl != null">
        #{pictureUrl,jdbcType=LONGVARCHAR},
      </if>
    </trim>
  </insert>
  <update id="updateByPrimaryKeySelective" parameterType="com.goafanti.common.model.TOrderOutsource">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Thu Aug 05 11:38:34 CST 2021.
    -->
    update t_order_outsource
    <set>
      <if test="orderNo != null">
        order_no = #{orderNo,jdbcType=VARCHAR},
      </if>
      <if test="refundStatus != null">
        refund_status = #{refundStatus,jdbcType=INTEGER},
      </if>
      <if test="createTime != null">
        create_time = #{createTime,jdbcType=TIMESTAMP},
      </if>
      <if test="remarks != null">
        remarks = #{remarks,jdbcType=VARCHAR},
      </if>
      <if test="attachmentUrl != null">
        attachment_url = #{attachmentUrl,jdbcType=VARCHAR},
      </if>
      <if test="amount != null">
        amount = #{amount,jdbcType=DECIMAL},
      </if>
      <if test="companyName != null">
        company_name = #{companyName,jdbcType=VARCHAR},
      </if>
      <if test="auditTime != null">
        audit_time = #{auditTime,jdbcType=TIMESTAMP},
      </if>
      <if test="unitPrice != null">
        unit_price = #{unitPrice,jdbcType=DECIMAL},
      </if>
      <if test="unitNumber != null">
        unit_number = #{unitNumber,jdbcType=INTEGER},
      </if>
      <if test="outsourceRemarks != null">
        outsource_remarks = #{outsourceRemarks,jdbcType=VARCHAR},
      </if>
      <if test="type != null">
        `type` = #{type,jdbcType=INTEGER},
      </if>
      <if test="tid != null">
        tid = #{tid,jdbcType=INTEGER},
      </if>
      <if test="startType != null">
        start_type = #{startType,jdbcType=INTEGER},
      </if>
      <if test="patentType != null">
        patent_type = #{patentType,jdbcType=INTEGER},
      </if>
      <if test="patentNameType != null">
        patent_name_type = #{patentNameType,jdbcType=INTEGER},
      </if>
      <if test="patentName != null">
        patent_name = #{patentName,jdbcType=VARCHAR},
      </if>
      <if test="actualOfficial != null">
        actual_official = #{actualOfficial,jdbcType=INTEGER},
      </if>
      <if test="pictureUrl != null">
        picture_url = #{pictureUrl,jdbcType=LONGVARCHAR},
      </if>
    </set>
    where id = #{id,jdbcType=INTEGER}
  </update>
  <update id="updateByPrimaryKeyWithBLOBs" parameterType="com.goafanti.common.model.TOrderOutsource">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Thu Aug 05 11:38:34 CST 2021.
    -->
    update t_order_outsource
    set order_no = #{orderNo,jdbcType=VARCHAR},
      refund_status = #{refundStatus,jdbcType=INTEGER},
      create_time = #{createTime,jdbcType=TIMESTAMP},
      remarks = #{remarks,jdbcType=VARCHAR},
      attachment_url = #{attachmentUrl,jdbcType=VARCHAR},
      amount = #{amount,jdbcType=DECIMAL},
      company_name = #{companyName,jdbcType=VARCHAR},
      audit_time = #{auditTime,jdbcType=TIMESTAMP},
      unit_price = #{unitPrice,jdbcType=DECIMAL},
      unit_number = #{unitNumber,jdbcType=INTEGER},
      outsource_remarks = #{outsourceRemarks,jdbcType=VARCHAR},
      `type` = #{type,jdbcType=INTEGER},
      tid = #{tid,jdbcType=INTEGER},
      start_type = #{startType,jdbcType=INTEGER},
      patent_type = #{patentType,jdbcType=INTEGER},
      patent_name_type = #{patentNameType,jdbcType=INTEGER},
      patent_name = #{patentName,jdbcType=VARCHAR},
      actual_official = #{actualOfficial,jdbcType=INTEGER},
      picture_url = #{pictureUrl,jdbcType=LONGVARCHAR}
    where id = #{id,jdbcType=INTEGER}
  </update>
  <update id="updateByPrimaryKey" parameterType="com.goafanti.common.model.TOrderOutsource">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Thu Aug 05 11:38:34 CST 2021.
    -->
    update t_order_outsource
    set order_no = #{orderNo,jdbcType=VARCHAR},
      refund_status = #{refundStatus,jdbcType=INTEGER},
      create_time = #{createTime,jdbcType=TIMESTAMP},
      remarks = #{remarks,jdbcType=VARCHAR},
      attachment_url = #{attachmentUrl,jdbcType=VARCHAR},
      amount = #{amount,jdbcType=DECIMAL},
      company_name = #{companyName,jdbcType=VARCHAR},
      audit_time = #{auditTime,jdbcType=TIMESTAMP},
      unit_price = #{unitPrice,jdbcType=DECIMAL},
      unit_number = #{unitNumber,jdbcType=INTEGER},
      outsource_remarks = #{outsourceRemarks,jdbcType=VARCHAR},
      `type` = #{type,jdbcType=INTEGER},
      tid = #{tid,jdbcType=INTEGER},
      start_type = #{startType,jdbcType=INTEGER},
      patent_type = #{patentType,jdbcType=INTEGER},
      patent_name_type = #{patentNameType,jdbcType=INTEGER},
      patent_name = #{patentName,jdbcType=VARCHAR},
      actual_official = #{actualOfficial,jdbcType=INTEGER}
    where id = #{id,jdbcType=INTEGER}
  </update>
  <select id="selectByOrderNo" resultType="com.goafanti.order.bo.OrderOutsourceDtails">
     	select id, order_no orderNo, refund_status refundStatus, date_format(create_time,'%Y-%m-%d %H:%i:%S') createTimes , remarks,
	 date_format(audit_time,'%Y-%m-%d %H:%i:%S') auditTimes,attachment_url attachmentUrl, picture_url pictureUrl, amount,
	 company_name companyName ,unit_price unitPrice,unit_number unitNumber,outsource_remarks outsourceRemarks,start_type startType,
	 patent_name patentName,patent_type patentType,patent_name_type patentNameType,actual_official actualOfficial
	 from t_order_outsource
	 where 1=1
	  <if test="orderNo != null">
	  and  order_no =  #{orderNo}
	  </if>
	  <if test="tid == null">
	  and type=0
	  </if>
	  <if test="tid != null">
	  and tid =  #{tid}
	  </if>
  </select>
  <select id="orderOutsourceList" resultType="com.goafanti.order.bo.OrderOutsourceBo">
     select a.order_no orderNo,b.contract_no contractNo,date_format(b.create_time,'%Y-%m-%d %H:%i:%S') createTime,a.type,a.tid,
	date_format(b.sign_time,'%Y-%m-%d') signTime,c.nickname userName, e.name depName,b.total_amount totalAmount,
	b.liquidation_status liquidationStatus,a.refund_status refundStatus,d.name adminName,g.name financeName
	<if test="type == 1">
	,bc.cname,pa.name pname,bp.bname,h.split_id splitId,h.split_super splitSuper ,h.split_status splitStatus
	</if>
	from t_order_outsource a
	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 d.department_id=e.id
	left join department f on b.order_dep=f.id left join admin g on f.finance_id=g.id
	<if test="type == 1">
	left join t_order_task h on a.tid=h.id left join business_project bp on h.commodity_id=bp.id
	left join business_category bc on bp.cid=bc.id left join admin pa on  h.task_receiver=pa.id
	</if>
	where b.approval !=3 and start_type=0
	<if test="type != null">
	and a.type = #{type}
	</if>

	<if test="type ==1 and tid !=null">
	and (h.id= #{tid} or h.split_super= #{tid})
	</if>
	<if test="refundStatus != null">
	and a.refund_status= #{refundStatus}
	</if>
	<if test="contractNo != null">
	and b.contract_no like concat('%',#{contractNo},'%')
	</if>
	<if test="name != null">
	and c.nickname like concat('%',#{name},'%')
	</if>
	<if test="starTime != null and endTime != null">
	and b.create_time between #{starTime} and  #{endTime}
	</if>
	order by a.refund_status,b.create_time desc
  	<if test="page_sql!=null">
			${page_sql}
	</if>
  </select>
  <select id="orderOutsourceCount" resultType="java.lang.Integer">
  select count(*) from t_order_outsource a left join t_order_new b on a.order_no=b.order_no
  left join `user` c on b.buyer_id=c.id where b.approval !=3 and start_type=0
  <if test="type != null">
	and a.type = #{type}
  </if>
  <if test="type ==1 and tid !=null">
	and (h.id= #{tid} or h.split_super= #{tid})
	</if>
  <if test="refundStatus != null">
	and a.refund_status= #{refundStatus}
	</if>
	<if test="contractNo != null">
	and b.contract_no like concat('%',#{contractNo},'%')
	</if>
	<if test="name != null">
	and c.nickname like concat('%',#{name},'%')
	</if>
	<if test="starTime != null and endTime != null">
	and b.create_time between #{starTime} and  #{endTime}
	</if>
  </select>
    <select id="salesmanOrderOutsourceList" resultType="com.goafanti.order.bo.OrderOutsourceBo">
    select a.order_no orderNo,b.contract_no contractNo,date_format(b.create_time,'%Y-%m-%d %H:%i:%S') createTime,
	date_format(b.sign_time,'%Y-%m-%d') signTime,c.nickname userName, e.name depName,b.total_amount totalAmount,
	b.liquidation_status liquidationStatus,a.refund_status refundStatus,d.name adminName,f.name financeName
	from t_order_outsource a
	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
	left join admin f on e.finance_id=f.id
	where b.delete_sign in(0,2,3) and b.outsource=1
		and d.department_id in
  	<foreach close=")" collection="deps" item="deps" open="(" separator=",">
	 #{deps.id}
  	</foreach>
	<if test="refundStatus != null">
	and a.refund_status= #{refundStatus}
	</if>
	<if test="contractNo != null">
	and b.contract_no like concat('%',#{contractNo},'%')
	</if>
	<if test="name != null">
	and c.nickname like concat('%',#{name},'%')
	</if>
	<if test="starTime != null and endTime != null">
	and b.create_time between #{starTime} and  #{endTime}
	</if>
	order by b.create_time desc
  	<if test="page_sql!=null">
			${page_sql}
	</if>
  </select>
  <select id="salesmanOrderOutsourceCount" resultType="java.lang.Integer">
  select count(*) from t_order_outsource a 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
  where b.delete_sign in(0,2,3) and b.outsource=1
  	and d.department_id in
  	<foreach close=")" collection="deps" item="deps" open="(" separator=",">
	 #{deps.id}
  	</foreach>
  <if test="refundStatus != null">
	and a.refund_status= #{refundStatus}
	</if>
	<if test="contractNo != null">
	and b.contract_no like concat('%',#{contractNo},'%')
	</if>
	<if test="name != null">
	and c.nickname like concat('%',#{name},'%')
	</if>
	<if test="starTime != null and endTime != null">
	and b.create_time between #{starTime} and  #{endTime}
	</if>
  </select>
  <select id="selectByTid" parameterType="java.lang.Integer" resultType="com.goafanti.organization.bo.outProjectBo">
     select a.id,a.start_type startType,a.patent_type patentType,c.`type` projectType
    from  t_order_task b left join  t_order_outsource a on a.tid=b.id
    left join business_project c on b.commodity_id=c.id
    where b.id = #{tid}
  </select>
  <update id="updateByidNull">
  update t_order_outsource
	set unit_number=null,unit_price=NULL
	where id= #{id}
  </update>
</mapper>
