<?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="org.linlinjava.jz.db.dao.JzReportMapper">

	<select id="selectBuiltlistReport"
		resultType="org.linlinjava.jz.db.dto.BuiltlistReportDto">
		SELECT
		jb.user_id AS userId,
		su.user_name AS userName,
		jb.campaign_id AS campaignId,
		jcp.NAME AS taskname,
		count(*) as tasksum,
		count(jb.call_result>0 or null) as dialed,
		count(jb.call_result=0 or
		null) as nodialed,
		count(jb.call_result in(1,6,11,16) or null) as
		dialedsuccess,
		count(jb.call_result in(2,3,4,5,7,8,12,13,14,15,17,18) or null) as
		noconnect,
		count(jb.call_result in (9,19) or null) as refuse

		From jz_built_list
		jb
		left join sys_user su on jb.user_id=su.id
		left join jz_campaign jcp
		on jb.campaign_id=jcp.id
		where 1=1 
		<if test="null !=campaignId and campaignId !='' ">
		and jb.campaign_id=#{campaignId}
		</if>
		 and jb.create_time BETWEEN #{startTime}
		and #{endTime}
		group by
		jb.user_id,jb.campaign_id
	</select>

	<select id="selectTicketReport"
		resultType="org.linlinjava.jz.db.dto.TicketReportDto">
		SELECT
		( CASE jt.ticket_type WHEN 0 THEN '咨询' WHEN 1 THEN
		'投诉' END )
		ticketType,
		jt.skill_group_id AS skillGroupId,
		jsg.NAME AS
		skillGroupName,
		( CASE jt.STATUS WHEN 0 THEN '创建' WHEN 1 THEN '处理中'
		WHEN 2 THEN '已完成'
		WHEN 3 THEN '作废' END ) statusName,
		( CASE jt.substatus 
			WHEN 101 THEN
			'待处理' 
			WHEN 102 THEN
			'流转' 
			WHEN 103 THEN
			'驳回' 
			WHEN 104 THEN
			'重新打开' 
			WHEN 201 THEN
			'完成' 
			WHEN 301 THEN
			'作废'  END
		)
		subStatusName,
		count( * ) counts
	FROM
		jz_ticket jt
		LEFT JOIN jz_skill_group jsg ON jt.skill_group_id = jsg.id
	WHERE
		jt.create_time BETWEEN #{startTime} AND #{endTime}
		GROUP BY
		jt.ticket_type,
		jt.skill_group_id,
		jt.STATUS,
		jt.substatus
	</select>
	
	<select id="ticketConsultTypeReport"
		resultType="org.linlinjava.jz.db.dto.ConsultTypeReport">
		SELECT
			( CASE jt.ticket_type WHEN 0 THEN '咨询' WHEN 1 THEN '投诉' END ) ticketType,
			( CASE jt.STATUS WHEN 0 THEN '创建' WHEN 1 THEN '处理中' WHEN 2 THEN '已完成' WHEN 3 THEN '作废' END ) statusName,
			 jtc.consult_id consultId,
			 count(*) counts
		FROM
			jz_ticket jt
			left join jz_ticket_consult jtc on jt.id=jtc.ticket_id
			left join jz_ticket_relation jtr on jt.id=jtr.ticket_id
			left join jz_built_list jbl on jtr.f_id=jbl.id
		WHERE
			1=1
			 	<if test="null !=campaignId and campaignId !='' ">
					and jbl.campaign_id=#{campaignId}
				</if>
			and jt.create_time  BETWEEN #{startTime} AND #{endTime}
			GROUP BY
				jt.ticket_type,
				jt.STATUS,
				jtc.consult_id
	</select>

</mapper>