<?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.DemandOrderMapper" >
  <resultMap id="BaseResultMap" type="com.goafanti.common.model.DemandOrder" >
    <id column="id" property="id" jdbcType="VARCHAR" />
    <result column="serial_number" property="serialNumber" jdbcType="INTEGER" />
    <result column="demand_id" property="demandId" jdbcType="VARCHAR" />
    <result column="uid" property="uid" jdbcType="VARCHAR" />
    <result column="create_time" property="createTime" jdbcType="TIMESTAMP" />
    <result column="enclosure_url" property="enclosureUrl" jdbcType="VARCHAR" />
    <result column="status" property="status" jdbcType="INTEGER" />
    <result column="intention_money" property="intentionMoney" jdbcType="INTEGER" />
    <result column="contract_money" property="contractMoney" jdbcType="INTEGER" />
    <result column="commission" property="commission" jdbcType="INTEGER" />
    <result column="service_money" property="serviceMoney" jdbcType="INTEGER" />
    <result column="refund" property="refund" jdbcType="INTEGER" />
    <result column="deleted_sign" property="deletedSign" jdbcType="INTEGER" />
  </resultMap>
  <sql id="Base_Column_List" >
    id, serial_number, demand_id, uid, create_time, enclosure_url, status, intention_money, 
    contract_money, commission, service_money, refund, deleted_sign
  </sql>
  <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.String" >
    select 
    <include refid="Base_Column_List" />
    from demand_order
    where id = #{id,jdbcType=VARCHAR}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.String" >
    delete from demand_order
    where id = #{id,jdbcType=VARCHAR}
  </delete>
  <insert id="insert" parameterType="com.goafanti.common.model.DemandOrder" >
    insert into demand_order (id, serial_number, demand_id, 
      uid, create_time, enclosure_url, 
      status, intention_money, contract_money, 
      commission, service_money, refund, 
      deleted_sign)
    values (#{id,jdbcType=VARCHAR}, #{serialNumber,jdbcType=INTEGER}, #{demandId,jdbcType=VARCHAR}, 
      #{uid,jdbcType=VARCHAR}, #{createTime,jdbcType=TIMESTAMP}, #{enclosureUrl,jdbcType=VARCHAR}, 
      #{status,jdbcType=INTEGER}, #{intentionMoney,jdbcType=INTEGER}, #{contractMoney,jdbcType=INTEGER}, 
      #{commission,jdbcType=INTEGER}, #{serviceMoney,jdbcType=INTEGER}, #{refund,jdbcType=INTEGER}, 
      #{deletedSign,jdbcType=INTEGER})
  </insert>
  <insert id="insertSelective" parameterType="com.goafanti.common.model.DemandOrder" >
    insert into demand_order
    <trim prefix="(" suffix=")" suffixOverrides="," >
      <if test="id != null" >
        id,
      </if>
      <if test="serialNumber != null" >
        serial_number,
      </if>
      <if test="demandId != null" >
        demand_id,
      </if>
      <if test="uid != null" >
        uid,
      </if>
      <if test="createTime != null" >
        create_time,
      </if>
      <if test="enclosureUrl != null" >
        enclosure_url,
      </if>
      <if test="status != null" >
        status,
      </if>
      <if test="intentionMoney != null" >
        intention_money,
      </if>
      <if test="contractMoney != null" >
        contract_money,
      </if>
      <if test="commission != null" >
        commission,
      </if>
      <if test="serviceMoney != null" >
        service_money,
      </if>
      <if test="refund != null" >
        refund,
      </if>
      <if test="deletedSign != null" >
        deleted_sign,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides="," >
      <if test="id != null" >
        #{id,jdbcType=VARCHAR},
      </if>
      <if test="serialNumber != null" >
        #{serialNumber,jdbcType=INTEGER},
      </if>
      <if test="demandId != null" >
        #{demandId,jdbcType=VARCHAR},
      </if>
      <if test="uid != null" >
        #{uid,jdbcType=VARCHAR},
      </if>
      <if test="createTime != null" >
        #{createTime,jdbcType=TIMESTAMP},
      </if>
      <if test="enclosureUrl != null" >
        #{enclosureUrl,jdbcType=VARCHAR},
      </if>
      <if test="status != null" >
        #{status,jdbcType=INTEGER},
      </if>
      <if test="intentionMoney != null" >
        #{intentionMoney,jdbcType=INTEGER},
      </if>
      <if test="contractMoney != null" >
        #{contractMoney,jdbcType=INTEGER},
      </if>
      <if test="commission != null" >
        #{commission,jdbcType=INTEGER},
      </if>
      <if test="serviceMoney != null" >
        #{serviceMoney,jdbcType=INTEGER},
      </if>
      <if test="refund != null" >
        #{refund,jdbcType=INTEGER},
      </if>
      <if test="deletedSign != null" >
        #{deletedSign,jdbcType=INTEGER},
      </if>
    </trim>
  </insert>
  <update id="updateByPrimaryKeySelective" parameterType="com.goafanti.common.model.DemandOrder" >
    update demand_order
    <set >
      <if test="serialNumber != null" >
        serial_number = #{serialNumber,jdbcType=INTEGER},
      </if>
      <if test="demandId != null" >
        demand_id = #{demandId,jdbcType=VARCHAR},
      </if>
      <if test="uid != null" >
        uid = #{uid,jdbcType=VARCHAR},
      </if>
      <if test="createTime != null" >
        create_time = #{createTime,jdbcType=TIMESTAMP},
      </if>
      <if test="enclosureUrl != null" >
        enclosure_url = #{enclosureUrl,jdbcType=VARCHAR},
      </if>
      <if test="status != null" >
        status = #{status,jdbcType=INTEGER},
      </if>
      <if test="intentionMoney != null" >
        intention_money = #{intentionMoney,jdbcType=INTEGER},
      </if>
      <if test="contractMoney != null" >
        contract_money = #{contractMoney,jdbcType=INTEGER},
      </if>
      <if test="commission != null" >
        commission = #{commission,jdbcType=INTEGER},
      </if>
      <if test="serviceMoney != null" >
        service_money = #{serviceMoney,jdbcType=INTEGER},
      </if>
      <if test="refund != null" >
        refund = #{refund,jdbcType=INTEGER},
      </if>
      <if test="deletedSign != null" >
        deleted_sign = #{deletedSign,jdbcType=INTEGER},
      </if>
    </set>
    where id = #{id,jdbcType=VARCHAR}
  </update>
  <update id="updateByPrimaryKey" parameterType="com.goafanti.common.model.DemandOrder" >
    update demand_order
    set serial_number = #{serialNumber,jdbcType=INTEGER},
      demand_id = #{demandId,jdbcType=VARCHAR},
      uid = #{uid,jdbcType=VARCHAR},
      create_time = #{createTime,jdbcType=TIMESTAMP},
      enclosure_url = #{enclosureUrl,jdbcType=VARCHAR},
      status = #{status,jdbcType=INTEGER},
      intention_money = #{intentionMoney,jdbcType=INTEGER},
      contract_money = #{contractMoney,jdbcType=INTEGER},
      commission = #{commission,jdbcType=INTEGER},
      service_money = #{serviceMoney,jdbcType=INTEGER},
      refund = #{refund,jdbcType=INTEGER},
      deleted_sign = #{deletedSign,jdbcType=INTEGER}
    where id = #{id,jdbcType=VARCHAR}
  </update>
  
   <select id="selectDemandOrderByUidAndDemandId" resultMap="BaseResultMap" >
    select 
    <include refid="Base_Column_List" />
    from demand_order
    where 
    	uid = #{0} 
    and demand_id = #{1} 
    and deleted_sign = 0 
  </select>
  
  <select id="findDemandOrderListByPage" parameterType="String" resultType="com.goafanti.demand.bo.DemandOrderListBo">
  	select 
  		do.id, do.serial_number as serialNumber, do.demand_id as demandId,
  		do.create_time as createTime, do.enclosure_url as enclosureUrl, do.status,
  		do.intention_money as intentionMoney, do.contract_money as contractMoney, do.commission,
  		do.service_money as serviceMoney, d.name, d.serial_number as demandSerialNumber,
  		d.deleted_sign as deletedSign, d.audit_status as auditStatus
  	from demand_order do
  	left join demand d on d.id = do.demand_id
  	where do.deleted_sign = 0 
  	and   d.deleted_sign = 0 
  	and   do.uid = #{uid,jdbcType=VARCHAR}
  	<if test="status != null">
  		and do.status = #{status,jdbcType=INTEGER}
  	</if>
  	order by do.serial_number desc
  	<if test="page_sql!=null">
		${page_sql}
	</if>
  </select>
  
  <select id="findDemandOrderCount" parameterType="String" resultType="java.lang.Integer">
  	select 
  		count(1)
  	from demand_order do
  	left join demand d on d.id = do.demand_id
  	where do.deleted_sign = 0 
  	and   d.deleted_sign 
  	and   do.uid = #{uid,jdbcType=VARCHAR}
  	<if test="status != null">
  		and do.status = #{status,jdbcType=INTEGER}
  	</if>
  </select>
  
  <select id="selectDemandOrderByDemandId" parameterType="java.lang.String" resultMap="BaseResultMap">
  	 select 
    <include refid="Base_Column_List" />
    from demand_order
    where demand_id = #{demandId,jdbcType=VARCHAR}
  </select>
  
   <select id="findUserDemandOrderListByPage" parameterType="String" resultType="com.goafanti.demand.bo.UserDemandOrderListBo">
  	select 
  		do.id, do.serial_number as serialNumber, do.demand_id as demandId,
  		do.create_time as createTime, do.enclosure_url as enclosureUrl, do.status,
  		do.intention_money as intentionMoney, do.contract_money as contractMoney, do.commission,
  		do.service_money as serviceMoney, d.name, d.serial_number as demandSerialNumber,
  		d.deleted_sign as deletedSign, d.audit_status as auditStatus,
  		do.uid, ui.username
  	from demand_order do
  	left join demand d on d.id = do.demand_id
  	left join user_identity ui on ui.uid = do.uid
  	where do.deleted_sign = 0 
  	and   d.deleted_sign = 0 
  	and  ui.username is not null
  	<if test="techBrokerId != null">
  		and d.tech_broker_id = #{techBrokerId,jdbcType=VARCHAR}
  	</if>
  	<if test="status != null">
  		and do.status = #{status,jdbcType=INTEGER}
  	</if>
  	<if test="uid != null">
  		and d.uid = #{uid,jdbcType=VARCHAR}
  	</if>
  	<if test="username != null">
  		and ui.username = #{username,jdbcType=VARCHAR}
  	</if>
  	order by do.serial_number desc
  	<if test="page_sql!=null">
		${page_sql}
	</if>
  </select>
  
  <select id="findUserDemandOrderCount" parameterType="String" resultType="java.lang.Integer">
  	select 
  		count(1)
  	from demand_order do
  	left join demand d on d.id = do.demand_id
  	left join user_identity ui on ui.uid = do.uid
  	where do.deleted_sign = 0 
  	and   d.deleted_sign = 0 
  	and  ui.username is not null
  	<if test="techBrokerId != null">
  		and d.tech_broker_id = #{techBrokerId,jdbcType=VARCHAR}
  	</if>
  	<if test="status != null">
  		and do.status = #{status,jdbcType=INTEGER}
  	</if>
  	<if test="uid != null">
  		and d.uid = #{uid,jdbcType=VARCHAR}
  	</if>
  	<if test="username != null">
  		and ui.username = #{username,jdbcType=VARCHAR}
  	</if>
  </select>
  
  
   <select id="findOrgDemandOrderListByPage" parameterType="String" resultType="com.goafanti.demand.bo.OrgDemandOrderListBo">
  	select 
  		do.id, do.serial_number as serialNumber, do.demand_id as demandId,
  		do.create_time as createTime, do.enclosure_url as enclosureUrl, do.status,
  		do.intention_money as intentionMoney, do.contract_money as contractMoney, do.commission,
  		do.service_money as serviceMoney, d.name, d.serial_number as demandSerialNumber,
  		d.deleted_sign as deletedSign, d.audit_status as auditStatus,
  		do.uid, oi.unit_name as unitName
  	from demand_order do
  	left join demand d on d.id = do.demand_id
  	left join organization_identity oi on oi.uid = do.uid
  	where do.deleted_sign = 0 
  	and   d.deleted_sign = 0 
  	and  oi.unit_name is not null
  	<if test="techBrokerId != null">
  		and d.tech_broker_id = #{techBrokerId,jdbcType=VARCHAR}
  	</if>
  	<if test="status != null">
  		and do.status = #{status,jdbcType=INTEGER}
  	</if>
  	<if test="uid != null">
  		and d.uid = #{uid,jdbcType=VARCHAR}
  	</if>
  	<if test="unitName != null">
  		and oi.unit_name = #{unitName,jdbcType=VARCHAR}
  	</if>
  	order by do.serial_number desc
  	<if test="page_sql!=null">
		${page_sql}
	</if>
  </select>
  
  <select id="findOrgDemandOrderCount" parameterType="String" resultType="java.lang.Integer">
  	select 
  		count(1)
  	from demand_order do
  	left join demand d on d.id = do.demand_id
  	left join organization_identity oi on oi.uid = do.uid
  	where do.deleted_sign = 0 
  	and   d.deleted_sign = 0 
  	and  oi.unit_name is not null
  	<if test="techBrokerId != null">
  		and d.tech_broker_id = #{techBrokerId,jdbcType=VARCHAR}
  	</if>
  	<if test="status != null">
  		and do.status = #{status,jdbcType=INTEGER}
  	</if>
  	<if test="uid != null">
  		and d.uid = #{uid,jdbcType=VARCHAR}
  	</if>
    <if test="unitName != null">
  		and oi.unit_name = #{unitName,jdbcType=VARCHAR}
  	</if>
  </select>

</mapper>