<?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.TOrderBonusMapper">
  <resultMap id="BaseResultMap" type="com.goafanti.common.model.TOrderBonus">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Tue Nov 27 11:15:28 CST 2018.
    -->
    <id column="id" jdbcType="VARCHAR" property="id" />
    <result column="create_time" jdbcType="TIMESTAMP" property="createTime" />
    <result column="order_no" jdbcType="VARCHAR" property="orderNo" />
    <result column="bonus_subject" jdbcType="INTEGER" property="bonusSubject" />
    <result column="grant_status" jdbcType="INTEGER" property="grantStatus" />
    <result column="grant_target" jdbcType="VARCHAR" property="grantTarget" />
    <result column="grant_by" jdbcType="VARCHAR" property="grantBy" />
    <result column="grant_time" jdbcType="DATE" property="grantTime" />
    <result column="grant_type" jdbcType="INTEGER" property="grantType" />
    <result column="task_id" jdbcType="INTEGER" property="taskId" />
  </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 Tue Nov 27 11:15:28 CST 2018.
    -->
    <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 Tue Nov 27 11:15:28 CST 2018.
    -->
    <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 Tue Nov 27 11:15:28 CST 2018.
    -->
    id, create_time, order_no, bonus_subject, grant_status, grant_target, grant_by, grant_time,
    grant_type, task_id
  </sql>
  <select id="selectByExample" parameterType="com.goafanti.common.model.TOrderBonusExample" resultMap="BaseResultMap">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Tue Nov 27 11:15:28 CST 2018.
    -->
    select
    <if test="distinct">
      distinct
    </if>
    <include refid="Base_Column_List" />
    from t_order_bonus
    <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.String" resultMap="BaseResultMap">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Tue Nov 27 11:15:28 CST 2018.
    -->
    select
    <include refid="Base_Column_List" />
    from t_order_bonus
    where id = #{id,jdbcType=VARCHAR}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.String">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Tue Nov 27 11:15:28 CST 2018.
    -->
    delete from t_order_bonus
    where id = #{id,jdbcType=VARCHAR}
  </delete>
  <delete id="deleteByExample" parameterType="com.goafanti.common.model.TOrderBonusExample">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Tue Nov 27 11:15:28 CST 2018.
    -->
    delete from t_order_bonus
    <if test="_parameter != null">
      <include refid="Example_Where_Clause" />
    </if>
  </delete>
  <insert id="insert" parameterType="com.goafanti.common.model.TOrderBonus">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Tue Nov 27 11:15:28 CST 2018.
    -->
    insert into t_order_bonus (id, create_time, order_no,
      bonus_subject, grant_status, grant_target,
      grant_by, grant_time, grant_type, task_id
      )
    values (#{id,jdbcType=VARCHAR}, #{createTime,jdbcType=TIMESTAMP}, #{orderNo,jdbcType=VARCHAR},
      #{bonusSubject,jdbcType=INTEGER}, #{grantStatus,jdbcType=INTEGER}, #{grantTarget,jdbcType=VARCHAR},
      #{grantBy,jdbcType=VARCHAR}, #{grantTime,jdbcType=DATE}, #{grantType,jdbcType=INTEGER}, #{taskId,jdbcType=INTEGER}
      )
  </insert>
  <insert id="insertSelective" parameterType="com.goafanti.common.model.TOrderBonus">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Tue Nov 27 11:15:28 CST 2018.
    -->
    insert into t_order_bonus
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="id != null">
        id,
      </if>
      <if test="createTime != null">
        create_time,
      </if>
      <if test="orderNo != null">
        order_no,
      </if>
      <if test="bonusSubject != null">
        bonus_subject,
      </if>
      <if test="grantStatus != null">
        grant_status,
      </if>
      <if test="grantTarget != null">
        grant_target,
      </if>
      <if test="grantBy != null">
        grant_by,
      </if>
      <if test="grantTime != null">
        grant_time,
      </if>
      <if test="grantType != null">
        grant_type,
      </if>
      <if test="taskId != null">
        task_id,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
      <if test="id != null">
        #{id,jdbcType=VARCHAR},
      </if>
      <if test="createTime != null">
        #{createTime,jdbcType=TIMESTAMP},
      </if>
      <if test="orderNo != null">
        #{orderNo,jdbcType=VARCHAR},
      </if>
      <if test="bonusSubject != null">
        #{bonusSubject,jdbcType=INTEGER},
      </if>
      <if test="grantStatus != null">
        #{grantStatus,jdbcType=INTEGER},
      </if>
      <if test="grantTarget != null">
        #{grantTarget,jdbcType=VARCHAR},
      </if>
      <if test="grantBy != null">
        #{grantBy,jdbcType=VARCHAR},
      </if>
      <if test="grantTime != null">
        #{grantTime,jdbcType=DATE},
      </if>
      <if test="grantType != null">
        #{grantType,jdbcType=INTEGER},
      </if>
      <if test="taskId != null">
        #{taskId,jdbcType=INTEGER},
      </if>
    </trim>
  </insert>
  <select id="countByExample" parameterType="com.goafanti.common.model.TOrderBonusExample" resultType="java.lang.Long">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Tue Nov 27 11:15:28 CST 2018.
    -->
    select count(*) from t_order_bonus
    <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 Tue Nov 27 11:15:28 CST 2018.
    -->
    update t_order_bonus
    <set>
      <if test="record.id != null">
        id = #{record.id,jdbcType=VARCHAR},
      </if>
      <if test="record.createTime != null">
        create_time = #{record.createTime,jdbcType=TIMESTAMP},
      </if>
      <if test="record.orderNo != null">
        order_no = #{record.orderNo,jdbcType=VARCHAR},
      </if>
      <if test="record.bonusSubject != null">
        bonus_subject = #{record.bonusSubject,jdbcType=INTEGER},
      </if>
      <if test="record.grantStatus != null">
        grant_status = #{record.grantStatus,jdbcType=INTEGER},
      </if>
      <if test="record.grantTarget != null">
        grant_target = #{record.grantTarget,jdbcType=VARCHAR},
      </if>
      <if test="record.grantBy != null">
        grant_by = #{record.grantBy,jdbcType=VARCHAR},
      </if>
      <if test="record.grantTime != null">
        grant_time = #{record.grantTime,jdbcType=DATE},
      </if>
      <if test="record.grantType != null">
        grant_type = #{record.grantType,jdbcType=INTEGER},
      </if>
      <if test="record.taskId != null">
        task_id = #{record.taskId,jdbcType=INTEGER},
      </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 Tue Nov 27 11:15:28 CST 2018.
    -->
    update t_order_bonus
    set id = #{record.id,jdbcType=VARCHAR},
      create_time = #{record.createTime,jdbcType=TIMESTAMP},
      order_no = #{record.orderNo,jdbcType=VARCHAR},
      bonus_subject = #{record.bonusSubject,jdbcType=INTEGER},
      grant_status = #{record.grantStatus,jdbcType=INTEGER},
      grant_target = #{record.grantTarget,jdbcType=VARCHAR},
      grant_by = #{record.grantBy,jdbcType=VARCHAR},
      grant_time = #{record.grantTime,jdbcType=DATE},
      grant_type = #{record.grantType,jdbcType=INTEGER},
      task_id = #{record.taskId,jdbcType=INTEGER}
    <if test="_parameter != null">
      <include refid="Update_By_Example_Where_Clause" />
    </if>
  </update>
  <update id="updateByPrimaryKeySelective" parameterType="com.goafanti.common.model.TOrderBonus">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Tue Nov 27 11:15:28 CST 2018.
    -->
    update t_order_bonus
    <set>
      <if test="createTime != null">
        create_time = #{createTime,jdbcType=TIMESTAMP},
      </if>
      <if test="orderNo != null">
        order_no = #{orderNo,jdbcType=VARCHAR},
      </if>
      <if test="bonusSubject != null">
        bonus_subject = #{bonusSubject,jdbcType=INTEGER},
      </if>
      <if test="grantStatus != null">
        grant_status = #{grantStatus,jdbcType=INTEGER},
      </if>
      <if test="grantTarget != null">
        grant_target = #{grantTarget,jdbcType=VARCHAR},
      </if>
      <if test="grantBy != null">
        grant_by = #{grantBy,jdbcType=VARCHAR},
      </if>
      <if test="grantTime != null">
        grant_time = #{grantTime,jdbcType=DATE},
      </if>
      <if test="grantType != null">
        grant_type = #{grantType,jdbcType=INTEGER},
      </if>
      <if test="taskId != null">
        task_id = #{taskId,jdbcType=INTEGER},
      </if>
    </set>
    where id = #{id,jdbcType=VARCHAR}
  </update>
  <update id="updateByPrimaryKey" parameterType="com.goafanti.common.model.TOrderBonus">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Tue Nov 27 11:15:28 CST 2018.
    -->
    update t_order_bonus
    set create_time = #{createTime,jdbcType=TIMESTAMP},
      order_no = #{orderNo,jdbcType=VARCHAR},
      bonus_subject = #{bonusSubject,jdbcType=INTEGER},
      grant_status = #{grantStatus,jdbcType=INTEGER},
      grant_target = #{grantTarget,jdbcType=VARCHAR},
      grant_by = #{grantBy,jdbcType=VARCHAR},
      grant_time = #{grantTime,jdbcType=DATE},
      grant_type = #{grantType,jdbcType=INTEGER},
      task_id = #{taskId,jdbcType=INTEGER}
    where id = #{id,jdbcType=VARCHAR}
  </update>
  <!-- 财务专员统计技术员的奖金 -->
  <select id="technicianBonusList" parameterType="Map" resultType="com.goafanti.order.bo.TOrderBonusBo">
   select b.id, t.id as tid, b.order_no as orderNo, t.task_receiver as taskReceiver, t.task_status as taskStatus,
  		t.commodity_quantity as commodityQuantity, t.project_status as projectStatus, t.commodity_name as commodityName,
  		accept_time as acceptTime, review_time as reviewTime, licence_time as licenceTime,
  		o.liquidation_status as liquidationStatus, ga.name as byName,
  		bonus_subject as bonusSubject, grant_status as grantStatus, a.name as targetName
  		, b.create_time as startTime1
  		from t_order_bonus b
  		left join t_order_task t on b.task_id = t.id
  		left join t_order_new o on o.order_no = t.order_no
  		left join admin a on a.id = t.task_receiver
  		left join admin oa on oa.id = o.salesman_id
  		left join admin ga on ga.id = b.grant_by
  		where  b.grant_type = 1
  		<if test="fids != null and fids.size > 0">
  		and b.grant_by in
  		<foreach collection="fids" item="id" open="(" separator="," close=")">
  		 #{id}
  		</foreach>
  		</if>
  		<if test='b.targetName != null and b.targetName != ""'>
  		<bind name="tn" value="'%' + b.targetName + '%'" />
  		and a.name like #{tn,jdbcType=VARCHAR}
  		</if>
  		<if test='b.salesName != null and b.salesName != ""'>
  		<bind name="sn" value="'%' + b.salesName + '%'" />
  		and oa.name like #{sn,jdbcType=VARCHAR}
  		</if>
  		<if test='b.departmentId != null and b.departmentId != ""'>
  		and oa.department_id = #{b.departmentId,jdbcType=VARCHAR}
  		</if>
  		<if test="b.grantStatus != null">
		and b.grant_status = #{b.grantStatus,jdbcType=INTEGER}
		</if>
		<if test='b.orderNo != null and b.orderNo !=""'>
		and b.order_no = #{b.orderNo,jdbcType=VARCHAR}
		</if>
  		<if test='b.startTime != null and b.startTime != "" and b.endTime != null and b.endTime != ""'>
  		<bind name="a" value="b.startTime +'-01'"/>
  		<bind name="e" value="b.endTime +'-31'"/>
  		and o.sign_time between #{a,jdbcType=VARCHAR} and #{e,jdbcType=VARCHAR}
  		</if>
  		<if test='b.startTime1 != null and b.startTime1 != "" and b.endTime1 != null and b.endTime1 != ""'>
  		<bind name="a1" value="b.startTime1 +' 00:00:00'"/>
  		<bind name="e1" value="b.endTime1 +' 23:59:59'"/>
  		and b.create_time between #{a1,jdbcType=VARCHAR} and #{e1,jdbcType=VARCHAR}
  		</if>
  		order by b.grant_status ,b.create_time
  		<if test="page_sql != null">
			${page_sql}
		</if>
  </select>
  <select id="technicianBonusCount" parameterType="Map" resultType="java.lang.Integer">
  select count(*)
  from t_order_bonus b
  		left join t_order_task t on b.task_id = t.id
  		left join t_order_new o on o.order_no = t.order_no
  		left join admin a on a.id = t.task_receiver
  		left join admin oa on oa.id = o.salesman_id
  		where b.grant_type = 1
  		<if test="fids != null">
  		and b.grant_by in
  		<foreach collection="fids" item="id" open="(" separator="," close=")">
  		 #{id}
  		</foreach>
  		</if>
  		<if test='b.targetName != null and b.targetName != ""'>
  		<bind name="tn" value="'%' + b.targetName + '%'" />
  		and a.name like #{tn,jdbcType=VARCHAR}
  		</if>
  		<if test='b.salesName != null and b.salesName != ""'>
  		<bind name="sn" value="'%' + b.salesName + '%'" />
  		and oa.name like #{sn,jdbcType=VARCHAR}
  		</if>
  		<if test='b.departmentId != null and b.departmentId != ""'>
  		and oa.department_id = #{b.departmentId,jdbcType=VARCHAR}
  		</if>
  		<if test="b.grantStatus != null">
		and b.grant_status = #{b.grantStatus,jdbcType=INTEGER}
		</if>
  		<if test='b.orderNo != null and b.orderNo !=""'>
		and b.order_no = #{b.orderNo,jdbcType=VARCHAR}
		</if>

  		<if test='b.startTime != null and b.startTime != "" and b.endTime != null and b.endTime != ""'>
  		<bind name="a" value="b.startTime +'-01'"/>
  		<bind name="e" value="b.endTime +'-31'"/>
  		and o.sign_time between #{a,jdbcType=VARCHAR} and #{e,jdbcType=VARCHAR}
  		</if>
  		<if test='b.startTime1 != null and b.startTime1 != "" and b.endTime1 != null and b.endTime1 != ""'>
  		<bind name="a1" value="b.startTime1 +' 00:00:00'"/>
  		<bind name="e1" value="b.endTime1 +' 23:59:59'"/>
  		and b.create_time between #{a1,jdbcType=VARCHAR} and #{e1,jdbcType=VARCHAR}
  		</if>
  </select>
  <!-- 财务专员统计营销员奖金 -->
  <select id="saleBonusList" parameterType="Map" resultType="com.goafanti.order.bo.TOrderBonusBo">
  select b.id, o.order_no as orderNo, o.salesman_id as salesmanId, o.sign_time as signTime,
	o.buyer_id as buyerId, u.nickname as buyName, o.total_amount as totalAmount,
	o.first_amount as firstAmount, o.settlement_amount as settlementAmount, o.refund_amount as refundAmount,
	o.order_status as orderStatus, o.project_status as projectStatus, b.bonus_subject as bonusSubject,
	o.proof_count as proofCount, b.grant_status as grantStatus, a.name as salesName, o.proof_status as proofStatus,
	ad.name as byName, b.grant_target as grantTarget, b.create_time as startTime1
	from t_order_bonus b
	left join t_order_new o on o.order_no = b.order_no
	left join admin a on o.salesman_id = a.id
	left join `user` u on o.buyer_id = u.id
	left join admin ad on b.grant_by = ad.id
	where b.grant_type = 0
	<if test="fids != null and fids.size > 0">
  		and b.grant_by in
  		<foreach collection="fids" item="id" open="(" separator="," close=")">
  		 #{id}
  		</foreach>
  	</if>
	<if test="b.grantBy != null">
	and b.grant_by = #{b.grantBy,jdbcType=VARCHAR}
	</if>
	<if test='b.salesName != null and b.salesName != ""'>
	<bind name="sn" value="'%' + b.salesName + '%'" />
	and a.name like #{sn,jdbcType=VARCHAR}
	</if>
	<if test='deps !=null'>
	and a.department_id in
        <foreach close=")" collection="deps" item="depId" open="(" separator=",">
            #{depId}
        </foreach>
	</if>
	<if test="b.grantStatus != null">
	and b.grant_status = #{b.grantStatus,jdbcType=INTEGER}
	</if>
	<if test='b.orderNo != null and b.orderNo !=""'>
	and b.order_no = #{b.orderNo,jdbcType=VARCHAR}
	</if>
	<if test='b.startTime != null and b.startTime != "" and b.endTime != null and b.endTime != ""'>
	<bind name="a" value="b.startTime +'-01'"/>
	<bind name="e" value="b.endTime +'-31'"/>
	and o.sign_time between #{a,jdbcType=VARCHAR} and #{e,jdbcType=VARCHAR}
	</if>
	<if test='b.startTime1 != null and b.startTime1 != "" and b.endTime1 != null and b.endTime1 != ""'>
	<bind name="a1" value="b.startTime1 +' 00:00:00'"/>
	<bind name="e1" value="b.endTime1 +' 23:59:59'"/>
	and b.create_time between #{a1,jdbcType=VARCHAR} and #{e1,jdbcType=VARCHAR}
	</if>
	order by b.grant_status , b.create_time desc
	<if test="page_sql != null">
		${page_sql}
	</if>
  </select>
  <select id="saleBonusCount" parameterType="Map" resultType="java.lang.Integer">
  select count(*)

	from t_order_bonus b
	left join t_order_new o on o.order_no = b.order_no
	left join admin a on o.salesman_id = a.id
	left join `user` u on o.buyer_id = u.id
	where b.grant_type = 0
	<if test="b.grantBy != null">
	and b.grant_by = #{b.grantBy,jdbcType=VARCHAR}
	</if>
	<if test='b.salesName != null and b.salesName != ""'>
	<bind name="sn" value="'%' + b.salesName + '%'" />
	and a.name like #{sn,jdbcType=VARCHAR}
	</if>
      <if test='deps !=null'>
          and a.department_id in
          <foreach close=")" collection="deps" item="depId" open="(" separator=",">
              #{depId}
          </foreach>
      </if>
	<if test="b.grantStatus != null">
	and b.grant_status = #{b.grantStatus,jdbcType=INTEGER}
	</if>
	<if test='b.orderNo != null and b.orderNo !=""'>
	and b.order_no = #{b.orderNo,jdbcType=VARCHAR}
	</if>
	<if test='b.startTime != null and b.startTime != "" and b.endTime != null and b.endTime != ""'>
	<bind name="a" value="b.startTime +'-01'"/>
	<bind name="e" value="b.endTime +'-31'"/>
	and o.sign_time between #{a,jdbcType=VARCHAR} and #{e,jdbcType=VARCHAR}
	</if>
	<if test='b.startTime1 != null and b.startTime1 != "" and b.endTime1 != null and b.endTime1 != ""'>
	<bind name="a1" value="b.startTime1 +' 00:00:00'"/>
	<bind name="e1" value="b.endTime1 +' 23:59:59'"/>
	and b.create_time between #{a1,jdbcType=VARCHAR} and #{e1,jdbcType=VARCHAR}
	</if>
  </select>
  <!-- 获得技术员有奖金数据的部门 -->
  <select id="getDeptBySome" parameterType="com.goafanti.order.bo.TOrderBonusBo" resultType="com.goafanti.order.bo.TOrderBonusBo">
    select distinct
	  	d.id as departmentId, d.name as departmentName
	  	from t_order_bonus b
	left join t_order_new o on o.order_no = b.order_no
	left join admin a on b.grant_target = a.id
	left join `user` u on o.buyer_id = u.id
	left join department d on d.id = a.department_id
	where  b.grant_type = #{grantType,jdbcType=INTEGER}
	<if test="grantBy != null">
	and b.grant_by = #{grantBy,jdbcType=VARCHAR}
	</if>
	<if test='salesName != null and salesName != ""'>
	<bind name="sn" value="'%' + salesName + '%'" />
	and a.name like #{sn,jdbcType=VARCHAR}
	</if>
	<if test='deps !=null '>
	and a.department_id in
    <foreach close=")" collection="deps" item="depId" open="(" separator=",">
        #{depId}
    </foreach>
    </if>
	<if test="grantStatus != null">
	and b.grant_status = #{grantStatus,jdbcType=INTEGER}
	</if>
	<if test='startTime != null and startTime != "" and endTime != null and endTime != ""'>
	<bind name="a" value="startTime +'-01'"/>
 	<bind name="e" value="endTime +'-31'"/>
 	and o.sign_time between #{a,jdbcType=VARCHAR} and #{e,jdbcType=VARCHAR}
	</if>
	<if test='startTime1 != null and startTime1 != "" and endTime1 != null and endTime1 != ""'>
	<bind name="a1" value="startTime1 +' 00:00:00'"/>
	<bind name="e1" value="endTime1 +' 23:59:59'"/>
	and b.create_time between #{a1,jdbcType=VARCHAR} and #{e1,jdbcType=VARCHAR}
	</if>
  </select>
  <!-- 获得及技术员奖金数据 -->
  <select id="getBonusData" parameterType="com.goafanti.order.bo.TOrderBonusBo" resultType="com.goafanti.order.bo.TOrderBonusBo">
  select b.id, t.id as tid, b.order_no as orderNo, t.task_receiver as taskReceiver, t.task_status as taskStatus,
  		t.commodity_quantity as commodityQuantity, t.project_status as projectStatus, t.commodity_name as commodityName,
  		accept_time as acceptTime, review_time as reviewTime, licence_time as licenceTime,
  		o.liquidation_status as liquidationStatus, ga.name as byName,
  		bonus_subject as bonusSubject, grant_status as grantStatus, a.name as targetName
  		,b.create_time as startTime1
  		from t_order_bonus b
  		left join t_order_task t on b.task_id = t.id
  		left join t_order_new o on o.order_no = t.order_no
  		left join admin a on a.id = t.task_receiver
  		left join admin oa on oa.id = o.salesman_id
  		left join admin ga on ga.id = b.grant_by
  		where b.grant_type = 1
  		<if test="grantBy != null">
  		and b.grant_by = #{grantBy,jdbcType=VARCHAR}
  		</if>
  		<if test='targetName != null and targetName != ""'>
  		<bind name="tn" value="'%' + targetName + '%'" />
  		and a.name like #{tn,jdbcType=VARCHAR}
  		</if>
  		<if test='salesName != null and salesName != ""'>
  		<bind name="sn" value="'%' + salesName + '%'" />
  		and oa.name like #{sn,jdbcType=VARCHAR}
  		</if>
  		<if test='departmentId != null and departmentId !=""'>
  		and a.department_id = #{departmentId,jdbcType=VARCHAR}
  		</if>
  		<if test="grantStatus != null">
		and b.grant_status = #{grantStatus,jdbcType=INTEGER}
		</if>
  		<if test='startTime != null and startTime != "" and endTime != null and endTime != ""'>
  		<bind name="a" value="startTime +'-01'"/>
	 	<bind name="e" value="endTime +'-31'"/>
	 	and o.sign_time between #{a,jdbcType=VARCHAR} and #{e,jdbcType=VARCHAR}
  		</if>
  		<if test='startTime1 != null and startTime1 != "" and endTime1 != null and endTime1 != ""'>
  		<bind name="a1" value="startTime1 +' 00:00:00'"/>
  		<bind name="e1" value="endTime1 +' 23:59:59'"/>
  		and b.create_time between #{a1,jdbcType=VARCHAR} and #{e1,jdbcType=VARCHAR}
  		</if>
  		order by b.grant_status ,b.create_time desc
  </select>
  <!-- 查看营销员的奖金数据 -->
  <select id="getSaleBounsList" parameterType="com.goafanti.order.bo.TOrderBonusBo" resultType="com.goafanti.order.bo.TOrderBonusBo">
  select b.id, o.order_no as orderNo, o.salesman_id as salesmanId, o.sign_time as signTime,
	o.buyer_id as buyerId, u.nickname as buyName, o.total_amount as totalAmount,
	o.first_amount as firstAmount, o.settlement_amount as settlementAmount, o.refund_amount as refundAmount,
	o.order_status as orderStatus, o.project_status as projectStatus, b.bonus_subject as bonusSubject,
	o.proof_count as proofCount, b.grant_status as grantStatus, a.name as salesName
	,b.create_time as startTime1, b.grant_target as grantTarget
	from t_order_bonus b
	left join t_order_new o on o.order_no = b.order_no
	left join admin a on o.salesman_id = a.id
	left join `user` u on o.buyer_id = u.id
	where b.grant_type = 0
	<if test="grantBy != null">
	and b.grant_by = #{grantBy,jdbcType=VARCHAR}
	</if>
	<if test='salesName != null and salesName != ""'>
	<bind name="sn" value="'%' + salesName + '%'" />
	and a.name like #{sn,jdbcType=VARCHAR}
	</if>
	<if test='deps !=null'>
	and a.department_id in
        <if test='deps !=null '>
            and a.department_id in
            <foreach close=")" collection="deps" item="depId" open="(" separator=",">
                #{depId}
            </foreach>
        </if>
	</if>
	<if test="grantStatus != null">
	and b.grant_status = #{grantStatus,jdbcType=INTEGER}
	</if>
	<if test='startTime != null and startTime != "" and endTime != null and endTime != ""'>
	<bind name="a" value="startTime +'-01'"/>
	<bind name="e" value="endTime +'-31'"/>
	and o.sign_time between #{a,jdbcType=VARCHAR} and #{e,jdbcType=VARCHAR}
	</if>
	<if test='startTime1 != null and startTime1 != "" and endTime1 != null and endTime1 != ""'>
	<bind name="a1" value="startTime1 +' 00:00:00'"/>
	<bind name="e1" value="endTime1 +' 23:59:59'"/>
	and b.create_time between #{a1,jdbcType=VARCHAR} and #{e1,jdbcType=VARCHAR}
	</if>
  </select>
  <!-- 根据订单编号和科目和奖金发放人检查这个奖金是否存在 -->
  <select id="checkByOnoAndSub" parameterType="com.goafanti.common.model.TOrderBonus" resultType="java.lang.Integer">
  	select count(*) from t_order_bonus where order_no = #{orderNo,jdbcType=VARCHAR} and bonus_subject = #{bonusSubject,jdbcType=INTEGER}
  	and grant_target = #{grantTarget,jdbcType=VARCHAR} and grant_type = #{grantType,jdbcType=INTEGER}
  	<if test="taskId != null">
  	and task_id = #{taskId,jdbcType=INTEGER}
  	</if>
  </select>
  <!-- 判断当前订单中有没有未发放的数据 -->
  <select id="checkNoPay" parameterType="com.goafanti.order.bo.TOrderBonusBo" resultType="java.lang.Integer">
  	select bonus_subject from t_order_bonus
	where id = #{id,jdbcType=VARCHAR} and bonus_subject > (
	select min(bonus_subject) from t_order_bonus
	where order_no = #{orderNo,jdbcType=VARCHAR} and grant_status = 0 and grant_type = 0)
  </select>

</mapper>
