<?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.ruixuan.salesLeads.mapper.SaleClueRecordInfoMapper">


	<select id="getSaleClueRecordNumber" resultType="com.ruixuan.salesLeads.pojo.SaleClueRecordInfoPojo">
		SELECT
		r.id,
		r.seller_id,
		r.record_times,
		r.record_script,
		r.continue_flag,
		r.intended_cooperation,
		r.record_info,
		r.remarks,
		r.create_user_id,
		r.create_time
		FROM
		sale_clue_record_info r
		<where>
			<if test="sellerId !=null and sellerId != ''">
				and r.seller_id = #{sellerId}
			</if>
			<if test="number !=null and number != ''">
				and r.record_times = #{number}
			</if>
		</where>
		LIMIT 1
	</select>




	<!-- 根据销售 查询 本周有意向的数量 -->
	<select id="selectSaleClueIntention" resultType="java.lang.Integer">
		SELECT
			COUNT( a.id ) num
		FROM
		(
		SELECT
			r.id,
			r.seller_id,
			r.record_times,
			r.continue_flag,
			r.intended_cooperation,
			r.create_user_id,
			r.create_time
		FROM
			sale_clue_record_info r
		) a,
		(
			SELECT MAX(r1.record_times) record_times,r1.seller_id FROM sale_clue_record_info r1
			GROUP BY r1.seller_id
		)b
		WHERE a.seller_id = b.seller_id and a.record_times = b.record_times
		and a.create_user_id = #{saleId}
		AND a.create_time &gt;= #{startTime}
		AND a.create_time &lt;= #{endTime}
		AND (a.continue_flag = 3 or a.intended_cooperation IN ( 2, 3, 4, 5, 6))

	</select>




	<!-- 卖家id 查询销售记录  -->
	<select id="selectDistributionList" resultType="com.ruixuan.salesLeads.pojo.SaleClueRecordInfoPojo">
		SELECT
		r.id,
		r.seller_id,
		r.record_times,
		r.record_script,
		r.continue_flag,
		r.intended_cooperation,
		r.record_info,
		r.remarks,
		r.create_user_id,
		r.create_time,
		(SELECT MIN(create_time) FROM sale_clue_record_info WHERE seller_id = #{sellerId}) as taskTime
		FROM
		sale_clue_record_info r
		<where>
			<if test="sellerId !=null and sellerId != ''">
				and r.seller_id = #{sellerId}
			</if>
		</where>
		ORDER BY r.record_times

	</select>





	<!-- 添加销售记录  -->
	<insert id="insertSaleRedord">
		insert into sale_clue_record_info (
		seller_id,
		record_times,
		record_script,
		continue_flag,
		intended_cooperation,
		record_info,
		remarks,
		create_user_id,
		create_time)
		values
		(
		 #{sellerId},
		 #{recordTimes},
		 #{recordScript},
		 #{continueFlag},
		 #{intendedCooperation},
		 #{recordInfo},
		 #{remarks},
		 #{createUserId},
		 NOW()
		)


	</insert>







</mapper> 