<?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.yeejoin.amos.patrol.business.dao.mapper.PlanTaskMapper">
   <!--统计-计划执行 -->
	<select id="countPlanTask" resultType="long">
	    SELECT COUNT(1) as total_num FROM (
		SELECT
			b.id
		FROM p_plan_task_detail b
		LEFT JOIN	p_plan_task a ON a.id = b.task_no
		LEFT JOIN	p_point d ON d.id = b.point_id
		<where>
		<if test="pointNo!=null"> and d.point_no like concat('%',#{pointNo},'%')</if>
		<if test="pointName!=null"> and d.name like concat('%',#{pointName},'%')</if>
		AND
	        ((<if test="beginDate!=null and endDate!=null">a.begin_Time <![CDATA[>=]]> #{beginDate} AND a.end_Time <![CDATA[<=]]> #{endDate}</if>)
	          OR
	        (<if test="beginDate!=null and endDate!=null">a.begin_Time <![CDATA[<=]]> #{beginDate} AND a.end_Time <![CDATA[>=]]> #{beginDate}</if>)
	       	  OR
	       	(<if test="beginDate!=null and endDate!=null">a.begin_Time <![CDATA[<=]]> #{endDate} AND a.end_Time <![CDATA[>=]]> #{endDate}</if>)
	          OR
	       	(<if test="beginDate!=null and endDate!=null">a.begin_Time <![CDATA[<=]]> #{beginDate} AND a.end_Time <![CDATA[>=]]> #{endDate}</if>)
	        )
		<if test="status!=null"> and b.is_finish  = #{status}</if>
		<if test="planId!=null"> and a.plan_id = #{planId}</if>
		<if test="routeId!=null"> and a.route_id = #{routeId}</if>
		<if test="orgCode!=null">
		 and (a.org_Code like concat (#{orgCode},"-%")or a.org_Code= #{orgCode})
		 </if>
		</where>
		order by b.id) t
	</select>
	<sql id="planTaskInfoSql">
		SELECT
			b.id,
			a.id batchNo,
			a.plan_id planId,
			c. NAME planName,
			e. NAME routeName,
			d.point_no pointNo,
			d. NAME pointName,
			a.user_id userName,
			a.user_dept userDept,
			date_format(
				a.begin_time,
				'%Y-%m-%d %H:%i:%s'
			) beginTime,
			date_format(
				a.end_time,
				'%Y-%m-%d %H:%i:%s'
			) endTime,
			b.is_finish status,
			c.dept_id deptId,
			c.remark1 as remark
		FROM p_plan_task_detail b
		LEFT JOIN	p_plan_task a ON a.id = b.task_no
		LEFT JOIN	p_plan c ON c.id = a.plan_id
		LEFT JOIN	p_point d ON d.id = b.point_id
		LEFT JOIN	p_route e ON e.id = a.route_id
	</sql>
	<!--计划执行查询 -->
	<select id="getPlanTaskInfo" resultType="java.util.HashMap">
		<include refid="planTaskInfoSql" />

		<where>
			<if test="pointNo!=null"> and d.point_no like concat('%',#{pointNo},'%')</if>
			<if test="pointName!=null"> and d.name like concat('%',#{pointName},'%')</if>
			AND
	        ((<if test="beginDate!=null and endDate!=null">a.begin_Time <![CDATA[>=]]> #{beginDate} AND a.end_Time <![CDATA[<=]]> #{endDate}</if>)
	          OR
	        (<if test="beginDate!=null and endDate!=null">a.begin_Time <![CDATA[<=]]> #{beginDate} AND a.end_Time <![CDATA[>=]]> #{beginDate}</if>)
	       	  OR
	       	(<if test="beginDate!=null and endDate!=null">a.begin_Time <![CDATA[<=]]> #{endDate} AND a.end_Time <![CDATA[>=]]> #{endDate}</if>)
	          OR
	       	(<if test="beginDate!=null and endDate!=null">a.begin_Time <![CDATA[<=]]> #{beginDate} AND a.end_Time <![CDATA[>=]]> #{endDate}</if>)
	        )
			<if test="status!=null"> and b.is_finish = #{status}</if>
			<if test="routeId!=null"> and a.route_id = #{routeId}</if>
			<if test="planId!=null"> and a.plan_id = #{planId}</if>
			<if test="orgCode!=null">
				and (a.org_Code like concat (#{orgCode},"-%")or a.org_Code= #{orgCode})
			</if>
		</where>
		order by b.id
		<choose>
	     <when test="pageSize==-1"></when>
	     <when test="pageSize!=-1">limit #{offset},#{pageSize}</when>
	    </choose>
	</select>

	<select id="getPlanTaskInfoList" resultType="com.yeejoin.amos.patrol.business.vo.PlanTaskVo">
		<include refid="planTaskInfoSql" />
		<where>
		<if test="pointNo!=null"> and d.point_no = #{pointNo}</if>
		<if test="pointName!=null"> and c.name like concat(#{pointName},'%')</if>
		<if test="beginDate!=null"> and a.begin_Time >= #{beginDate}</if>
		<if test="endDate!=null"> and a.end_time <![CDATA[<=]]> #{endDate}</if>
		<!-- <if test="status!=null"> and a.finish_status = #{status}</if> -->
		<if test="status!=null"> and b.is_finish = #{status}</if>
		<if test="planId!=null"> and a.plan_id = #{planId}</if>
		<if test="routeId!=null"> and a.route_id = #{routeId}</if>
		<if test="orgCode!=null">
		 and a.org_code like #{orgCode}
		 </if>
		</where>
		order by b.id

	</select>

	<select id="getPlanTaskListByIds" resultType="com.yeejoin.amos.patrol.business.vo.PlanTaskVo">
		<include refid="planTaskInfoSql" />
		where  b.id in
		<foreach item="Id" collection="ids" index="index" open="(" separator="," close=")">
			#{Id}
		</foreach>
		order by b.id
	</select>

	<delete id ="planTaskDet">
	 delete from p_plan_task_detail where id in
	<foreach item="Id" collection="array" index="index" open="(" separator="," close=")">
		#{Id}
	</foreach>
	</delete>
	<update id="updatePlanTaskPtInfo"  >
	  update p_plan_task a
	  set a.point_num = (select count(1) from p_plan_task_detail b where a.id = b.task_no),
	  a.finish_num = (select count(1) from p_plan_task_detail c where a.id = c.task_no and c.is_finish = #{FINISH_YES})
	  where a.id  in
	  <foreach item="Id" collection="IDS" index="index" open="(" separator="," close=")">
	    #{Id}
	  </foreach>
	</update>

	<select id="getGenPlanTask" resultType="long">
		select a.id from p_plan_task  a
		<trim prefix="where" prefixOverrides="AND |OR">
		<if test="beginDate!=null"> and a.begin_Time >= #{beginDate}</if>
		<if test="endDate!=null"> and a.begin_Time <![CDATA[<=]]> #{endDate}</if>
		<if test="planId!=null"> and a.plan_id = #{planId}</if>
		</trim>
	</select>

	<select id="getPlanTaskByRouteId" resultType="com.yeejoin.amos.patrol.dao.entity.PlanTask">
		SELECT * FROM `p_plan_task`
		<where>
			<if test="routeId != null">route_id = #{routeId}</if>
		</where>
	</select>

	<select id="getPlanTaskByPointId" resultType="Map">
		SELECT ptd.point_id pointId, pt.id planTaskId,
			pt.org_code OrgCode, p.name taskName, pt.status , pt.user_id userId,
			date_format(pt.begin_time, '%Y-%m-%d %H:%i:%s') beginTime, date_format(pt.end_time, '%Y-%m-%d %H:%i:%s') endTime, pt.check_date checkDate,
		    pt.finish_num finishNum, pt.finish_status finishStatus,pt.batch_no batchNo
		FROM
			p_plan p
		LEFT JOIN p_plan_task pt ON pt.plan_id = p.id
		LEFT JOIN p_plan_task_detail ptd ON ptd.task_no = pt.id
		where
		pt.finish_status = 1
		<if test="pointId!=null"> and ptd.point_id = #{pointId} </if>
		<if test="orgCode!=null">and p.org_code = #{orgCode}</if>
		<if test="adminOrgCode!=null">and p.org_code like concat(#{adminOrgCode},'%')</if>
		<if test="userId!=null">and find_in_set(#{userId},pt.user_id)>0 </if>
		and pt.status = 0
		and SYSDATE() <![CDATA[>=]]> pt.begin_time
		and SYSDATE() <![CDATA[<]]> pt.end_time
	</select>
	<select id="getPlanTasks" resultType="Map">
			SELECT
			a.planTaskId,
			a.OrgCode,
			a.taskName,
			a.userId,
			a.beginTime,
			a.endTime,
			a.checkDate,
			a.finshNum,
			a.taskPlanNum,
			a.finishStatus,
			a.batchNo,
			a.userId  executiveName,
			a.userName,
			a.userDept
		FROM
			(
				SELECT
					pt.id planTaskId,
					pt.org_code OrgCode,
					p. NAME taskName,
					pt. STATUS,
					pt.user_id userId,
					date_format(
						pt.begin_time,
						'%Y-%m-%d %H:%i:%s'
					) beginTime,
					date_format(
						pt.end_time,
						'%Y-%m-%d %H:%i:%s'
					) endTime,
					date_format(
						pt.check_date,
						'%Y-%m-%d %H:%i:%s'
					)checkDate,
					pt.finish_num finishNum,
					pt.finish_status finishStatus,
					pt.id batchNo,
					pt.route_id,
					pt.point_num taskPlanNum,
					pt.finish_num finshNum,
					pt.user_name userName,
					pt.user_dept userDept
				FROM
				p_plan_task pt
				INNER JOIN  p_plan p ON pt.plan_id = p.id
			) a
		<include refid="plan-task-app-where"/>
		<if test="orderBy != null and orderBy != ''"> order by ${orderBy} </if>
		limit #{offset},#{pageSize}
	</select>
	<select id="getPlanTasksCount" resultType="long">
		SELECT
			count(1) tasksCount
		FROM
			(
				SELECT
					pt.id planTaskId,
					pt.org_code OrgCode,
					p. NAME taskName,
					pt. STATUS,
					pt.user_id userId,
					date_format(
						pt.begin_time,
						'%Y-%m-%d %H:%i:%s'
					) beginTime,
					date_format(
						pt.end_time,
						'%Y-%m-%d %H:%i:%s'
					) endTime,
					date_format( pt.check_date, '%Y-%m-%d %H:%i:%s' ) checkDate,
					pt.finish_num finishNum,
					pt.finish_status finishStatus,
					pt.id batchNo,
					pt.route_id,
					pt.point_num taskPlanNum,
					pt.finish_num finshNum,
					pt.user_name userName,
					pt.user_dept userDept
				FROM
				p_plan_task pt
				INNER JOIN  p_plan p ON pt.plan_id = p.id
			) a
		<include refid="plan-task-app-where"/>
	</select>
    <sql id="plan-task-app-where">
		<where>
			<if test="userId != null and userId > 0 "> and find_in_set(#{userId},a.userId)>0</if>
			<if test="routeId != null and routeId > 0 "> and a.route_id = #{routeId} </if>
			<if test="checkDate != null and checkDate != '' "> and a.beginTime <![CDATA[<=]]> #{checkDate} and a.endTime <![CDATA[>=]]> #{checkDate} </if>
			<if test="finishStatus != null"> and a.finishStatus = #{finishStatus}</if>
			<if test="orgCode != null and orgCode !=''" >
				and (a.OrgCode LIKE CONCAT( #{orgCode}, '-%' ) or a.OrgCode= #{orgCode} )
			</if>
			<if test="departmentId != null and departmentId != 0 ">and a.userDept like  concat('%', #{departmentId}, '%')</if>
			<if test="startTime != null and startTime != '' and endTime != null and endTime != ''  ">
				AND (
				(
				a.beginTime <![CDATA[>=]]>  #{startTime}
				AND a.endTime <![CDATA[<=]]> #{endTime}
				)
				OR (
				a.beginTime <![CDATA[<=]]> #{endTime}
				AND a.endTime <![CDATA[>=]]> #{endTime}
				)
				OR (
				a.beginTime <![CDATA[<=]]> #{startTime}
				AND a.endTime <![CDATA[>]]> #{startTime}
				)
				OR (
				a.beginTime <![CDATA[<=]]> #{startTime}
				AND a.endTime <![CDATA[>=]]> #{endTime}
				)
				)
			</if>
			<if test="query != null and query != '' ">
				<if test="queryType != null and queryType != 'executiveName' and queryType != 'plan' ">
					AND EXISTS (
					SELECT
					1
					FROM
					p_point pp
					LEFT JOIN p_route_point rp ON rp.point_id = pp.id
					LEFT JOIN p_route r ON r.id = rp.route_id
					where a.route_id = r.id
					<if test="queryType == null or queryType == '' or queryType == 'all' ">
						AND CONCAT(pp.name, ',',r.name, ',', pp.point_no,a.taskName, ',', a.batchNo) LIKE concat('%', #{query}, '%')
					</if>
					<if test="queryType != null and queryType == 'route' ">
						AND r.name LIKE concat('%', #{query}, '%')
					</if>
					<if test="queryType != null and queryType == 'point' ">
						AND CONCAT(pp.name, ',', pp.point_no) LIKE concat('%', #{query}, '%')
					</if>
					)
				</if>
				<if test="queryType != null and queryType == 'executiveName' ">
					AND a.userName  LIKE concat('%',  #{query}, '%')
				</if>
				<if test="queryType != null and queryType == 'plan' ">
					AND CONCAT(a.taskName, ',', a.batchNo) LIKE concat('%', #{query}, '%')
				</if>
			</if>
		</where>
	</sql>


	<select id="queryPlanTaskById" resultType="Map">
	  SELECT
			a.planTaskId,
			a.OrgCode,
			a.taskName,
			a.userId as userId,
			a.beginTime,
			a.endTime,
			a.checkDate,
			sum(a.finish) finshNum,
			sum(a.omission) omission,
			sum(a.unqualified) unqualified,
			sum(a.unplan) unplan,
			count(a.finish) taskPlanNum,
			a.finishStatus,
			a.batchNo,
			a.inOrder
		FROM
			(
				SELECT
					pt.id planTaskId,
					pt.org_code OrgCode,
					p. NAME taskName,
					pt.in_order inOrder,
					pt. STATUS,
					pt.user_id userId,
					date_format(
						pt.begin_time,
						'%Y-%m-%d %H:%i:%s'
					) beginTime,
					date_format(
						pt.end_time,
						'%Y-%m-%d %H:%i:%s'
					) endTime,
					pt.check_date checkDate,
					pt.finish_num finishNum,
					pt.finish_status finishStatus,
					pt.batch_no batchNo,
					CASE ptd.`status`
				WHEN 1 THEN
					1
				ELSE
					0
				END finish,
				CASE ptd.`status`
				WHEN 3 THEN
					1
				ELSE
					0
				END omission,
				CASE ptd.`status`
				WHEN 2 THEN
					1
				ELSE
					0
				END unqualified,
				CASE ptd.`status`
				WHEN 0 THEN
					1
				ELSE
					0
				END unplan
				FROM
					p_plan_task_detail ptd
					LEFT JOIN p_plan_task pt ON ptd.task_no = pt.id
					LEFT JOIN p_plan p  ON pt.plan_id = p.id
			) a
		where 1 = 1
		<if test="planTaskId != null and planTaskId >0  "> and a.planTaskId = #{planTaskId} </if>
	</select>

	<select id="getPlanTaskPoints" resultType="Map">
		SELECT
			p.name,
			p.id pointId,
			p.point_no pointNO,
			p.offline,
			ptd.status,
			ptd.is_finish finish,
			p.is_fixed isFixed,
			pt.route_id routeId,
			c.id checkId,
			prp.order_no orderNo
		FROM
			p_plan_task_detail ptd
		LEFT JOIN p_plan_task pt  ON pt.id = ptd.task_no
		LEFT JOIN p_point p ON p.id = ptd.point_id
		LEFT JOIN p_check c ON c.plan_task_detail_id = ptd.id
		LEFT JOIN p_route_point prp ON prp.point_id = ptd.point_id AND prp.route_id = pt.route_id
		WHERE
			p.is_delete = 0
		<if test="planTaskId != null and planTaskId > 0  ">and pt.id = #{planTaskId}</if>
		ORDER BY orderNo
	</select>

	<select id="getCurrentPlanTaskCount" resultType="int" parameterType="long">
		SELECT
			count(1) num
		FROM
			p_plan_task pt
		where
			DATE_FORMAT(pt.begin_time, '%y-%M-%d') <![CDATA[<=]]> DATE_FORMAT(SYSDATE(),'%y-%M-%d')
			and DATE_FORMAT(pt.end_time, '%y-%M-%d') <![CDATA[>=]]> DATE_FORMAT(SYSDATE(),'%y-%M-%d')
		    and pt.finish_status = 1
		<if test="userId != null and userId > 0">and find_in_set( #{userId},pt.user_id)>0</if>
	</select>

	<select id="getPlanTaskPointInputItemByPlanTaskId" resultType="com.yeejoin.amos.patrol.business.entity.mybatis.PlanTaskPointInputItemBo">

		SELECT
			ppi.input_item_id inputItemId,
			temp1.order_no orderNo,
			temp1.routePointItemId,
			temp1.classifyId,
			temp1.classifyName,
			pii.`name` inputName,
			temp2.*
		FROM
			p_point_inputitem ppi
			LEFT JOIN p_input_item pii ON pii.id = ppi.input_item_id
			RIGHT JOIN
			(
				SELECT
					prpi.order_no,
					prpi.point_input_item_id,
					prp.point_id,
					prp.route_id,
					prpi.id routePointItemId,
					ppc.id classifyId,
					ppc.`name` classifyName
				FROM
					p_route_point_item prpi
					LEFT JOIN p_route_point prp ON prp.id = prpi.route_point_id
					LEFT JOIN p_point_classify ppc ON prpi.point_classify_id = ppc.id
			) temp1 ON(ppi.id = temp1.point_input_item_id)
		RIGHT JOIN

			(
				SELECT
					pptd.point_id pointId,
					ptd.id AS planTaskId,
					ptd.route_id routeId,
					ptd.plan_id planId,
					ptd.user_id AS userId,
					ptd.org_code AS orgCode,
					ptd.end_time AS endTime,
					pptd.id as planTaskDetailId,
					(select p_plan.dept_id from  p_plan where p_plan.id=ptd.plan_id) depId
				FROM
					p_plan_task_detail pptd,
					p_plan_task ptd
				WHERE
					pptd.task_no = ptd.id
				AND ptd.id = #{planTaskId} and pptd.`status` = #{planTaskDetailStatus}
			) temp2 on(temp1.point_id = temp2.pointId
		AND temp1.route_id = temp2.routeId)

	</select>
	<!-- 巡检执行情况统计分页用 -->
	<select id="countChkExListData" resultType="long">
		select
		count(A.ID) total_num
		from
		p_plan_task a,
		p_plan b
		<!-- s_user c -->
		WHERE <!-- A.USER_ID = C.ID AND --> A.PLAN_ID = B.ID
		<if test="checkDate!=null"> and date_format(a.begin_time, '%Y%m%d') <![CDATA[<=]]> #{checkDate} and date_format(a.end_time, '%Y%m%d') >= #{checkDate}</if>
		<if test="orgCode != null and orgCode !=''" >
			And (a.org_code LIKE CONCAT( #{orgCode}, '-%' ) or a.org_code= #{orgCode} )
		</if>
	</select>
	<resultMap id="ChkExResultMap"
		type="com.yeejoin.amos.patrol.business.entity.mybatis.CheckChkExListBo">
	</resultMap>
	<!-- 巡检执行情况查询 -->
	<select id="getChkExList" resultMap="ChkExResultMap">
		select
		a.id,
		b.name,
		date_format(a.begin_time, '%Y-%m-%d %H:%i:%s') lastTime,
		date_format(a.end_time, '%Y-%m-%d %H:%i:%s') as endTime,
		a.finish_status finishStatus,
		a.USER_ID  realName <!-- , -->
		<!-- IFNULL(case when c.mobile = '' or c.mobile is null then null else c.mobile END,c.telephone) as LoginName -->
		from
		p_plan_task a,
		p_plan b
		<!-- s_user c -->
		WHERE <!-- A.USER_ID = C.ID AND --> A.PLAN_ID = B.ID
		<if test="checkDate!=null"> and date_format(a.begin_time, '%Y%m%d') <![CDATA[<=]]> #{checkDate} and date_format(a.end_time, '%Y%m%d') >= #{checkDate}</if>
		<if test="orgCode != null and orgCode !=''" >
			And (a.org_code LIKE CONCAT( #{orgCode}, '-%' ) or a.org_code= #{orgCode} )
		</if>
		order by a.id
		<choose>
	     <when test="pageSize==-1"></when>
	     <when test="pageSize!=-1">limit #{offset},#{pageSize}</when>
	    </choose>
	</select>

	<select id="getPlanTaskStatisticsForApp" resultType="Map">
		SELECT
			count(1) total,
			CASE
		WHEN sum(a.unfinished) IS NULL THEN
			0
		ELSE
			sum(a.unfinished)
		END unfinished,
		 CASE
		WHEN sum(a.finished) IS NULL THEN
			0
		ELSE
			sum(a.finished)
		END finished,
		 CASE
		WHEN sum(a.missed) IS NULL THEN
			0
		ELSE
			sum(a.missed)
		END missed
		FROM
			(
				SELECT
					ptd.id,
					ptd.is_finish,
					CASE ptd.is_finish
				WHEN 0 THEN
					1
				ELSE
					0
				END unfinished,
				CASE ptd.is_finish
			WHEN 1 THEN
				1
			ELSE
				0
			END finished,
			CASE ptd.is_finish
		WHEN 2 THEN
			1
		ELSE
			0
		END missed,
		 pt.begin_time,
		 pt.end_time,
		 pt.user_id,
		 pt.org_code,
		 pt.user_dept
		FROM
			p_plan_task_detail ptd
		LEFT JOIN p_plan_task pt ON pt.id = ptd.task_no
		<where>
			<if test="userId != null and userId > 0">and find_in_set(#{userId},pt.user_id)>0</if>
			<if test="orgCode != null and orgCode !='' ">and pt.org_code like CONCAT(#{orgCode}, '%')</if>
			<if test="userDept != null and userDept !='' ">and pt.user_dept like CONCAT('%', #{userDept}, '%')</if>
			<if test="startTime != null and startTime != ''  "> and pt.end_time  <![CDATA[>=]]>  #{startTime} </if>
			<if test="endTime != null and endTime != ''  "> and pt.begin_time  <![CDATA[<=]]>  #{endTime} </if>
		</where>
			) a

	</select>

	<select id="getPointPlanTaskInfo" resultType="com.yeejoin.amos.patrol.business.entity.mybatis.PointCheckDetailBo" parameterType="long">
		SELECT
			pp.id pointId,
			pp.`name` pointName,
			pp.point_no pointNo,
			ppt.route_id routeId,
			ppl.`name` planName,
			ppt.begin_time checkTime,
			ppl.dept_id checkDepartmentId,
			ppt.user_id userId,
			pptd.`status` pointStatus
		FROM
			p_plan_task_detail pptd
		LEFT JOIN p_plan_task ppt ON pptd.task_no = ppt.id
		LEFT JOIN p_plan ppl ON ppl.id = ppt.plan_id
		LEFT JOIN p_point pp ON pp.id = pptd.point_id
		<where>
			<if test="planTaskId != null and planTaskId !=''">
				AND ppt.id = #{planTaskId}
			</if>
			<if test="pointId != null and pointId !=''">
				AND pptd.point_id = #{pointId}
			</if>
		</where>
	</select>

	<select id="getPointInputByRouteIdAndPointId" resultType="com.yeejoin.amos.patrol.business.entity.mybatis.PointCheckDetailBo" parameterType="long">
		SELECT
			pii.`name` inputName,
			pii.id checkInputId,
			ppc.id classifyId,
			ppc.name classifyName,
			pii.item_type itemType,
			pii.risk_desc riskDesc,
			pii.data_json dataJson,
			pii.is_must isMust,
			pii.picture_json pictureJson,
			pii.is_multiline isMultiline,
			pii.order_no orderNo
		FROM
			p_route_point_item prpi
			LEFT JOIN p_point_classify ppc ON ppc.id = prpi.point_classify_id
			LEFT JOIN p_route_point prp ON prp.id = prpi.route_point_id
			LEFT JOIN p_point_inputitem ppii ON ppii.id = prpi.point_input_item_id
			LEFT JOIN p_input_item pii ON pii.id = ppii.input_item_id
		WHERE prp.point_id = #{pointId}
		AND prp.route_id =#{routeId}
		AND NOT FIND_IN_SET(prpi.point_input_item_id,prp.exclude_items)
		ORDER BY
			prpi.order_no

		<!--
		SELECT
			pii.`name` inputName,
			pii.id checkInputId
		FROM
			p_route_point_item prpi,
			p_route_point prp,
			p_point_inputitem ppii,
			p_input_item pii
		WHERE
			prpi.route_point_id = prp.id
		AND prpi.point_input_item_id = ppii.id
		AND pii.id = ppii.input_item_id
		AND prp.point_id = #{pointId}
		AND prp.route_id = #{routeId}
		ORDER BY
			prpi.order_no -->
	</select>

	<select id="getCumulativePlanCountByOrgCode" resultType="String">
		SELECT
			concat(
				coalesce(sum(a.finish), 0),
				'/',
				coalesce(sum(a.total), 0)
			) finish
		FROM
			(
				SELECT
					CASE
				WHEN t.is_finish = 1 THEN
					1
				ELSE
					0
				END finish,
				1 total
			FROM
				p_plan_task_detail t
				LEFT JOIN p_plan_task p on p.id = t.task_no
			WHERE 1=1
		<if test="orgCode != null and orgCode !=''" >
			And (p.org_code LIKE CONCAT( #{orgCode}, '-%' ) or p.org_code= #{orgCode} )
		</if>
			AND p.check_date = DATE_FORMAT(SYSDATE(),'%Y-%m-%d')
			) a
	</select>

	<resultMap type="com.yeejoin.amos.patrol.business.vo.LeavePlanTaskVo" id="LeavePlanTaskMap">
		<id column="planTaskId" property="planTaskId"/>
		<result column="taskName" property="taskName"/>
		<result column="beginTime" property="beginTime"/>
		<result column="endTime" property="endTime"/>
		<result column="batchNo" property="batchNo"/>
		<result column="OrgCode" property="OrgCode"/>
		<result column="finishStatus" property="finishStatus"/>
		<result column="inOrder" property="inOrder"/>
		<result column="remark" property="remark"/>
		<collection property="points" ofType="com.yeejoin.amos.patrol.business.vo.LeavePlanTaskPointVo"
			 select="getPlanTaskLeavePoints" column="{planTaskId=planTaskId}" >
		</collection>
	</resultMap>

	<resultMap type="com.yeejoin.amos.patrol.business.vo.LeavePlanTaskPointVo" id="LeavePlanTaskPointMap">
		<result column="pointId" property="pointId"/>
		<result column="routeId" property="routeId"/>
		<result column="planTaskId" property="planTaskId"/>
		<result column="name" property="name"/>
		<result column="pointNO" property="pointNO"/>
		<result column="offline" property="offline"/>
		<result column="status" property="status"/>
		<result column="isFixed" property="isFixed"/>
		<result column="orderNo" property="orderNo"/>
		<result column="remark" property="remark"/>
		<collection property="classify"  ofType="Map" select="queryPointClassifyByRouteIdAndPointId"
			column="{routeId=routeId,pointId=pointId}"  >
		</collection>
		<collection property="inputItems"  ofType="Map" select="getRoutePointInputItem"
			column="{routeId=routeId,pointId=pointId}"  >
		</collection>
	</resultMap>

	<select id="getPlanTaskLeavePoints" resultMap="LeavePlanTaskPointMap">
		SELECT
			p.name,
			p.id pointId,
			p.point_no pointNO,
			p.offline,
			ptd.is_finish status,
			p.is_fixed isFixed,
			pt.route_id routeId,
			c.id checkId,
			pt.id planTaskId,
			pp.name taskName,
			rp.order_no orderNo,
			p.remark remark
		FROM
			p_plan_task pt
		LEFT JOIN p_plan_task_detail ptd ON pt.id = ptd.task_no
		LEFT JOIN p_plan pp on pp.id = plan_id
		LEFT JOIN p_point p ON p.id = ptd.point_id
		LEFT JOIN p_check c ON c.plan_task_detail_id = ptd.id
		LEFT JOIN p_route_point rp on rp.route_id = pt.route_id and rp.point_id = ptd.point_id
		WHERE
			p.is_delete = 0
		<if test="planTaskId != null and planTaskId > 0  ">and pt.id = #{planTaskId}</if>
		order by orderNo
	</select>


    <resultMap id="routePointInputItemMap" type="com.yeejoin.amos.patrol.business.vo.PointInputItemVo">
       <result property="id" column="id" />
       <result property="pointId" column="point_id" />
       <result property="inputItemId" column="input_item_id" />
       <result property="classifyIds" column="classify_ids" />
       <result property="createDate" column="create_date" />
       <result property="isDelete" column="is_delete" />
       <result property="orderNo" column="order_no" />
       <result property="riskDesc" column="risk_desc" />
    </resultMap>
    <select id="getRoutePointInputItem" resultType="com.yeejoin.amos.patrol.business.vo.PointInputItemVo">
    	SELECT
			pii.*, ppc.id classifyIds, ppc.`name`  classifyNames
		FROM
			p_route_point_item prpi
		LEFT JOIN p_route_point prp ON prp.id = prpi.route_point_id
		LEFT JOIN p_point_classify ppc on ppc.id = prpi.point_classify_id
		LEFT JOIN p_point_inputitem ppi ON ppi.id = prpi.point_input_item_id
		LEFT JOIN p_input_item pii ON pii.id = ppi.input_item_id
		 <where>
            <if test="routeId != null">prp.route_id = #{routeId}</if>
            <if test="pointId != null">and prp.point_id = #{pointId}</if>
        </where>
    	<!--
    	SELECT
			ii.*, pii.classify_ids, group_concat(ppc.name)  classifyNames
		FROM
			p_input_item ii
		LEFT JOIN p_point_inputitem pii ON pii.input_item_id = ii.id
		LEFT JOIN p_point_classify ppc on pii.classify_ids REGEXP CONCAT(
			'^',
			ppc.id,
			',|,',
			ppc.id,
			',|,',
			ppc.id,
			'$',
			'|^',
			ppc.id,
			'$'
			)
		LEFT JOIN p_route_point_item prpi ON prpi.point_input_item_id = pii.id,
		 (
			SELECT
				id AS route_point_id
			FROM
				p_route_point rp
		        <where>
		            <if test="routeId != null">route_id = #{routeId}</if>
		            <if test="pointId != null">and point_id = #{pointId}</if>
		        </where>
		) prp
		WHERE
			prp.route_point_id = prpi.route_point_id
			GROUP BY ii.id,
			ii.org_code,
			ii.name,
			ii.order_no,
			ii.item_type,
			ii.is_must,
			ii.is_score,
			ii.default_value,
			ii.is_multiline,
			ii.catalog_id,
			ii.input_json,
			ii.data_json,
			ii.remark,
			ii.picture_json,
			ii.create_by,
			ii.create_date,
			ii.is_delete,
			ii.level
			, pii.classify_ids

	    SELECT
			*
		FROM
			p_point_inputitem
		WHERE
			id IN (
				SELECT
                    point_input_item_id
                FROM
                    p_route_point_item prpi,
                    (
                        SELECT
                            id AS route_point_id
                        FROM
                            p_route_point
                        <where>
                            <if test="routeId != null">route_id = #{routeId}</if>
                            <if test="pointId != null">and point_id = #{pointId}</if>
                        </where>
                    ) prp
                WHERE
                    prp.route_point_id = prpi.route_point_id
			) -->
    </select>

     <select id="queryPointClassifyByRouteIdAndPointId" resultType="Map">
		SELECT
			ppc.id,
			ppc.point_id pointId,
			ppc.`name`,
			ppc.order_no orderNo
		FROM
			`p_route_point_item` prpi
		LEFT JOIN p_route_point prp ON prp.id = prpi.route_point_id
		LEFT JOIN p_point_classify ppc ON ppc.id = prpi.point_classify_id
			WHERE
			prp.route_id = #{routeId}
		AND prp.point_id = #{pointId}
		GROUP BY
			ppc.id,
			ppc.point_id,
			ppc.`name`,
			ppc.order_no
    </select>

	<select id="queryLeavePlanTask" resultMap="LeavePlanTaskMap">
		SELECT
			pt.id planTaskId,
			p. NAME taskName,
			pt.batch_no batchNo,
			pt.finish_status finishStatus,
			pt.user_id userId,
			pt.org_code OrgCode,
			pt.finish_num finshNum,
			pt.point_num taskPlanNum,
			pt.begin_time beginTime,
			pt.end_time endTime,
			pt.route_id routeId,
			p.in_order inOrder,
			p.remark remark
		FROM
			p_plan_task pt
		LEFT JOIN p_plan p ON pt.plan_id = p.id
		WHERE
			1 = 1
		   and pt.finish_status in (0,1)
			<if test="userId != null and userId > 0">and find_in_set(#{userId},pt.user_id)>0 </if>
			<if test="orgCode != null and orgCode !='' ">and pt.org_code like CONCAT(#{orgCode}, '%')</if>
		ORDER BY begin_time
	</select>
	 <resultMap id="codeOrderVoMap" type="com.yeejoin.amos.patrol.business.vo.CodeOrderVo">
       <result property="inOrder" column="in_order" />
       <result property="orderNo" column="order_no" />
       <result property="pointId" column="point_id" />
       <result property="playTaskId" column="playTaskId" />
       <result property="pointNo" column="pointNo" />
       <result property="status" column="finish_status" />
       <result property="isFinish" column="is_finish" />
    </resultMap>
	<select id="queryCodeOrderVo" resultMap="codeOrderVoMap">
		SELECT
			ptd.point_id ,
			pt.finish_status,
			pt.in_order ,
			rp.order_no,
			ptd.is_finish
		FROM p_plan_task_detail ptd ,p_plan_task pt ,p_route_point rp
		where pt.id = ptd.task_no and pt.route_id = rp.route_id and rp.point_id = ptd.point_id and ptd.task_no = #{planTaskId}
		order by rp.order_no
	</select>

	<update id="reformStatistics" >
		call planTaskStatistics(#{userId}, #{refDate}, #{orgCode});
	</update>

	<select id="countFinishByPlanIds" resultType="java.util.Map">
		select
			plan_id as planId,
			sum(if(finish_status=2,1,0)) as finishTimes
		from
			p_plan_task
		where
			plan_id in
			<foreach collection="list" index="index" item="item" open="(" separator="," close=")">
				#{item}
			</foreach>
		group by plan_id
	</select>

	<select id="queryPlanTaskTimeAxis" resultType="java.util.Map" parameterType="java.util.HashMap">
	SELECT
		ppk.id,
		pp.NAME AS planName,
		ppk.begin_time AS beginTime,
		ppk.end_time AS endTime,
		ppk.user_id AS userId,
		( CASE pptd.STATUS WHEN 0 THEN '未执行' WHEN 1 THEN '正常' WHEN 2 THEN '异常' WHEN 3 THEN '漏检' END ) AS STATUS
	FROM
		`p_plan_task` ppk
		LEFT JOIN p_plan pp ON pp.id = ppk.plan_id
		LEFT JOIN p_plan_task_detail pptd ON pptd.task_no = ppk.id
		<where>
			<if test="userId != null ">and find_in_set(#{userId},ppk.user_id) </if>
			<if test="beginTime != null and beginTime != '' "> and ppk.begin_time <![CDATA[>=]]>  #{beginTime} </if>
			<if test="endTime != null and endTime != ''"> and ppk.end_time  <![CDATA[<=]]>  #{endTime} </if>
		</where>
	GROUP BY
		ppk.begin_time,
		ppk.id
	ORDER BY
		ppk.begin_time DESC
	</select>


	<select id="queryTimeAxis" resultType="java.util.Map" parameterType="java.util.HashMap">
		SELECT
		ppk.id,
		pp.NAME AS planName,
		ppk.begin_time AS beginTime,
		ppk.end_time AS endTime,
		ppk.user_id AS userId,
		point.name,
		point.risk_source_id,
		( CASE pptd.STATUS WHEN 0 THEN '未执行' WHEN 1 THEN '正常' WHEN 2 THEN '异常' WHEN 3 THEN '漏检' END ) AS STATUS
		FROM
		`p_plan_task` ppk
		LEFT JOIN p_plan pp ON pp.id = ppk.plan_id
		LEFT JOIN p_plan_task_detail pptd ON pptd.task_no = ppk.id
		LEFT JOIN p_point point ON point.id = pptd.point_id
		<where>
			<if test="userId != null ">and find_in_set(#{userId},ppk.user_id) </if>
			<if test="beginTime != null and beginTime != '' "> and ppk.begin_time <![CDATA[>=]]>  #{beginTime} </if>
			<if test="endTime != null and endTime != ''"> and ppk.end_time  <![CDATA[<=]]>  #{endTime} </if>
		</where>
		GROUP BY
		ppk.begin_time,
		ppk.id
		ORDER BY
		ppk.begin_time DESC  LIMIT 60
	</select>
</mapper>