<?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.maintenance.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 userId,
			a.user_dept userDept,
			a.user_name userName,
			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.maintenance.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.maintenance.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.maintenance.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
                  *
           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.point_num as taskPlanNum,
					pt.finish_num finishNum,
		            (pt.point_num - pt.finish_num) as waitNum,
					pt.finish_status finishStatus,
					pt.id batchNo,
					pt.route_id,
					pt.user_name userName,
					r.owner_id,
			        R.owner_name as ownerName
				FROM
					p_plan_task pt
					INNER JOIN  p_plan p ON pt.plan_id = p.id
					INNER JOIN p_route r on r.id = pt.route_id
                    where pt.status = 0
			) a
		<include refid="mobile-plan-task-where" />
		<if test="orderBy != null and orderBy != ''"> order by ${orderBy}</if>
		limit #{offset},#{pageSize}
	</select>
	<sql id="mobile-plan-task-where">
		<where>
			<if test="userId != null and userId > 0 "> and find_in_set(#{userId},a.userId)>0</if>
			<if test="finishStatus != null and finishStatus!=''"> and a.finishStatus = #{finishStatus}</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>
			<choose>
				<when test="identityType==1">
					And (a.orgCode LIKE CONCAT( #{orgCode}, '-%' ) or a.orgCode= #{orgCode} )
					<if test="companyId != null and companyId != ''"> and a.owner_id = #{companyId}</if>
				</when>
				<when test="identityType==2">
					And a.owner_id = #{companyId}
				</when>
			</choose>
		</where>
	</sql>

	<select id="getPlanTasksCount" resultType="long">
		SELECT
			count(1) tasksCount
		FROM
			(
			SELECT
				pt.id,
				pt.user_id as userId,
				pt.finish_status as finishStatus,
				pt.org_code as orgCode,
				pt.begin_time as beginTime,
				pt.end_time as endTime,
				r.owner_id
			FROM
				p_plan_task pt
			INNER JOIN  p_plan p ON pt.plan_id = p.id
			INNER JOIN p_route r on r.id = pt.route_id
			    where pt.status = 0
			) a
		<include refid="mobile-plan-task-where" />
	</select>
	<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
		    ptd.id,
			p.name,
			p.point_no pointNo,
		    concat_ws(' ',p.building_name,p.address) as position,
			p.belong_system_name as belongSystemName,
			ptd.status,
			ptd.is_finish as isFinish,
			pt.route_id as routeId,
			c.id checkId,
			prp.order_no orderNo,
		    prp.id as routePointId
		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 and pt.id = #{planTaskId}
			<if test="buildingId != null and buildingId != ''">and find_in_set(p.building_id,#{buildingId})</if>
			<if test="isFinish != null and isFinish != ''">and ptd.is_finish = #{isFinish}</if>
			<if test="systemId != null and systemId != ''">and p.belong_system_id = #{systemId}</if>
			<if test="pointNo != null and pointNo != ''">and p.point_no = #{pointNo}</if>
			<if test="pointName != null and pointName != ''">and p.name like CONCAT ('%',#{pointName},'%')</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.maintenance.business.entity.mybatis.PlanTaskPointInputItemBo">
		SELECT
			ppi.input_item_id inputItemId,
			temp1.order_no orderNo,
			temp1.routePointItemId,
			temp1.classifyId,
			temp1.classifyName,
			temp1.routeName,
			temp1.pointName,
			temp1.ownerId,
			temp1.pointNo,
			temp1.buildingName,
			temp1.equipmentName,
			temp1.remark as riskAndManage,
			temp1.address,
			temp1.ownerName,
			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,
				    r.name as routeName,
					p.name as pointName,
				    p.owner_id as ownerId,
				    p.owner_name as ownerName,
				    p.point_no as pointNo,
				    p.building_name as buildingName,
					p.equipment_name as equipmentName,
					p.remark,
					p.address
				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
					LEFT JOIN p_route r ON r.id = prp.route_id
					LEFT JOIN p_point p ON p.id = prp.point_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.begin_time AS beginTime,
					ptd.end_time AS endTime,
					ptd.company_id as companyId,
					ptd.company_name as companyName,
					pptd.id as planTaskDetailId,
					(select p_plan.dept_id from  p_plan where p_plan.id=ptd.plan_id) depId,
					(select p_plan.name from  p_plan where p_plan.id=ptd.plan_id) planName,
					(select p_plan.plan_type from  p_plan where p_plan.id=ptd.plan_id) planType
				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.maintenance.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.maintenance.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.maintenance.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}
		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.maintenance.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.maintenance.business.vo.LeavePlanTaskPointVo"
			 select="getPlanTaskLeavePoints" column="{planTaskId=planTaskId}" >
		</collection>
	</resultMap>

	<resultMap type="com.yeejoin.amos.maintenance.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.maintenance.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.maintenance.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>

	<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.maintenance.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>
    <select id="statisticsTaskWithAuth" resultType="java.util.Map">
		SELECT
			count(1) as total,
			td.is_finish
		from p_plan_task_detail td ,
			 p_plan_task t,
			 p_route r
		where
		      td.task_no = t.id
		  and t.route_id = r.id
		<choose>
			<when test="identityType==1">
				and (t.org_code LIKE CONCAT( #{orgCode}, '-%' ) or t.org_code= #{orgCode} )
			</when>
			<when test="identityType==2">
				And r.owner_id = #{companyId}
			</when>
		</choose>
		GROUP BY td.is_finish
	</select>
</mapper>