<?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.CheckMapper">

    <resultMap id="checkInfoMap" type="com.yeejoin.amos.patrol.business.vo.CheckInfoVo">
        <result property="orgCode" column="org_code"/>
        <result property="pointId" column="point_Id"/>
        <result property="pointName" column="point_Name"/>
        <result property="pointNo" column="pointNo"/>
        <result property="userId" column="user_Id"/>
        <result property="userName" column="user_Name"/>
        <result property="departmentName" column="department_name"/>
        <result property="checkTime" column="checkDate"/>
        <result property="uploadTime" column="upload_Time"/>
        <result property="isOk" column="is_ok"/>
        <result property="routeName" column="route_name"/>
        <result property="checkMode" column="check_mode"/>
        <result property="planName" column="plan_name"/>
        <result property="planTaskId" column="plan_Task_Id"/>
        <result property="catalogId" column="Catalog_Id"/>
        <result property="depId" column="departmentId"/>

    </resultMap>
    <!--统计-记录统计 -->
    <select id="countCheckInfoData" resultType="long">
        SELECT
        count(1) AS total_num
        FROM
        `p_check` `a`
        <choose>
            <when test="pointName!=null or pointNo!=null or isFixed!=null or catalogId!=null">
                LEFT JOIN `p_point` `b` ON `a`.`point_id` = `b`.`id`
            </when>
            <when test="departmentId!=null ">
                LEFT JOIN `p_plan` `e` ON `a`.`plan_id` = `e`.`id`
            </when>
        </choose>
        <trim prefix="WHERE" prefixOverrides="AND ">
            <if test="beginDate!=null and endDate!=null">and a.check_time BETWEEN #{beginDate} and #{endDate}</if>
            <!-- <if test="endDate!=null"> and #{endDate} <![CDATA[>=]]> a.check_time  </if> -->
            <if test="userName!=null">and a.user_name like concat(concat("%",#{userName}),"%")</if>
            <if test="pointName!=null">and b.name like concat(concat("%",#{pointName}),"%")</if>
            <if test="pointNo!=null">and b.point_no like concat('%',#{pointNo},'%')</if>
            <if test="isFixed!=null">and b.is_fixed = #{isFixed}</if>
            <if test="isOK!=null">and a.is_OK = #{isOK}</if>
            <if test="planId!=null">and a.plan_Id = #{planId}</if>
            <if test="planTaskId!=null">and a.plan_task_detail_id = #{planTaskId}</if>
            <if test="userId!=null">and find_in_set(#{userId}, a.user_id) > 0</if>
            <if test="routeId!=null">and a.route_Id = #{routeId}</if>
            <if test="catalogId!=null">and b.Catalog_Id = #{catalogId}</if>
            <if test="orgCode!=null">and (a.org_Code like concat (#{orgCode},"-%")or a.org_Code= #{orgCode})</if>
            <if test="pointId!=null">and a.point_id = #{pointId}</if>
            <if test="departmentId!=null and departmentId!='-1'"> and find_in_set(#{departmentId}, a.dep_id) > 0</if>
            <choose>
                <when test="finishStatus ==  0">and (`a`.`plan_task_detail_id` = 0 OR `a`.`plan_task_detail_id` is
                    null)
                </when>
                <when test="finishStatus ==  1">and (`a`.`plan_task_detail_id` != 0 AND `a`.`is_ok` != 3)</when>
                <when test="finishStatus ==  2">and `a`.`is_ok` = 3</when>
            </choose>
        </trim>
    </select>
    <!--记录查询 -->
    <select id="getCheckInfo" resultMap="checkInfoMap">
        SELECT
        a.id,
        a.org_code,
        a.point_id pointId,
        b.name point_name,
        b.is_fixed,
        b.point_no pointNo,
        a.user_id,
        a.user_name,
        a.dep_name department_name,
        a.dep_id departmentId,
        date_format(
        `a`.`check_time`,
        '%Y-%m-%d %H:%i:%s'
        ) AS `checkDate`,
        date_format(
        `a`.`upload_time`,
        '%Y-%m-%d %H:%i:%s'
        ) AS `upload_time`,
        (
        CASE
        WHEN `a`.`plan_task_detail_id` = 0 OR `a`.`plan_task_detail_id` is null
        THEN '0'
        WHEN `a`.`plan_task_detail_id` != 0 AND `a`.`is_ok` != 3
        THEN '1'
        WHEN `a`.`is_ok` = 3
        THEN '2'
        END
        ) AS `Finish_Status`,
      	a.is_ok is_ok,
        a.score,
        d.`name` AS `route_name`,
        a.check_mode,
        e.`name` AS `plan_name`,
        a.plan_task_id,
        a.plan_id,
        a.`route_id`,
        b.`catalog_id`,
        a.error,
        a.remark
        FROM
        `p_check` `a`
        LEFT JOIN `p_point` `b` ON `a`.`point_id` = `b`.`id`
        LEFT JOIN `p_route` `d` ON `a`.`route_id` = `d`.`id`
        LEFT JOIN `p_plan` `e` ON `a`.`plan_id` = `e`.`id`
        <if test="dangerId != null and dangerId != ''">
            LEFT JOIN ( SELECT check_id, GROUP_CONCAT( latent_danger_id ) dangerIds FROM p_latent_danger_patrol GROUP BY check_id ) t ON t.check_id = a.id
        </if>
        <trim prefix="WHERE" prefixOverrides="AND ">
            <if test="beginDate!=null and endDate!=null">and a.check_time BETWEEN #{beginDate} and #{endDate}</if>
            <!-- <if test="endDate!=null"> and #{endDate} <![CDATA[>=]]> a.check_time  </if> -->
            <if test="userName!=null">and a.user_name like concat(concat("%",#{userName}),"%")</if>
            <if test="pointName!=null">and b.name like concat(concat("%",#{pointName}),"%")</if>
            <if test="pointNo!=null">and b.point_no like concat('%',#{pointNo},'%')</if>
            <if test="isFixed!=null">and b.is_fixed = #{isFixed}</if>
            <if test="isOK!=null">and a.is_OK = #{isOK}</if>
            <if test="planId!=null">and a.plan_Id = #{planId}</if>
            <if test="planTaskId!=null">and a.plan_task_detail_id = #{planTaskId}</if>
            <if test="userId!=null">and find_in_set(#{userId}, a.user_id) > 0</if>
            <if test="routeId!=null">and a.route_Id = #{routeId}</if>
            <if test="catalogId!=null">and b.Catalog_Id = #{catalogId}</if>
            <if test="orgCode!=null">and (a.org_Code like concat (#{orgCode},"-%")or a.org_Code= #{orgCode})</if>
            <if test="pointId!=null">and a.point_id = #{pointId}</if>
            <if test="checkTime!=null">and TO_DAYS(a.check_time) = TO_DAYS('${checkTime}')</if>
            <if test="departmentId!=null and departmentId!='-1'"> and find_in_set(#{departmentId}, a.dep_id) > 0</if>
            <if test="checkType == '计划检查'">and a.plan_task_id &gt; 0</if>
            <if test="checkType == '无计划检查'">and a.plan_task_id &lt;= 0</if>
            <choose>
                <when test="finishStatus ==  0">and (`a`.`plan_task_detail_id` = 0 OR `a`.`plan_task_detail_id` is
                    null)
                </when>
                <when test="finishStatus ==  1">and (`a`.`plan_task_detail_id` != 0 AND `a`.`is_ok` != 3)</when>
                <when test="finishStatus ==  2">and `a`.`is_ok` = 3</when>
            </choose>
            <if test="dangerId != null and dangerId != ''">
                and FIND_IN_SET(#{dangerId}, t.dangerIds)
            </if>
        </trim>
        order by ${orderBy}
        <choose>
            <when test="pageSize==-1"></when>
            <when test="pageSize!=-1">limit #{offset},#{pageSize}</when>
        </choose>
    </select>

    <select id="queryUnqualifiedInputItem" resultType="Map" parameterType="int">
		SELECT
			ii.name,
			c.id checkId,
			i.input_value inputValue,
			c.point_id pointId,
			c.route_id routeId,
			i.input_id itemId
		FROM
			p_check c,
			p_check_input i,
			p_input_item ii
		WHERE
			i.check_id = c.id
		AND i.input_id = ii.id
		and i.is_ok = '2'
		and c.id = #{checkId}
	</select>

    <select id="queryCheckPointImgs" resultType="Map" parameterType="int">
        <!--
        SELECT
            s.photo_data photoData,
            s.point_name pointName
        FROM
            p_check c,
            p_check_shot s
        WHERE
            s.check_id = c.id
        AND c.plan_task_id = #{planTaskId}
        AND c.point_id = #{pointId}		-->

        SELECT
        pcs.check_input_id checkInputId,
        pcs.photo_data photoDate,
        pcs.point_name pointName
        FROM
        `p_check_shot` pcs
        LEFT JOIN p_check pc ON pc.id = pcs.check_id
        WHERE
        pc.plan_task_id = #{planTaskId}
        AND pc.point_id =#{pointId};
    </select>

    <select id="queryCheckPointInputItem" resultType="Map" parameterType="int">
		SELECT
			c.plan_task_id,
			c.point_id pointId,
			ci.input_id inputId,
			ci.input_value inputValue,
			ci.is_ok isOk
		FROM
			p_check c,
			p_check_input ci
		LEFT JOIN p_input_item i ON ci.input_id = i.id
		WHERE
			1 = 1
		AND ci.check_id = c.id
			and c.point_id = #{pointId}
			and c.plan_task_id = #{planTaskId}
		order by ci.order_no
	</select>

    <select id="findCheckPointInputItem" resultType="com.yeejoin.amos.patrol.business.entity.mybatis.PointCheckDetailBo"
            parameterType="long">
		SELECT
			pp.id pointId,
			pp. NAME pointName,
			pp.point_no pointNo,
			pc.is_ok pointStatus,
			ppl. NAME planName,
			pc.dep_name departmentName,
			pc.user_name username,
			pc.check_time checkTime,
			pc.remark checkRemark,
			pci.check_id checkId,
			pci.remark,
		  	pii.name inputName,
		  	pii.risk_desc riskDesc,
		  	pii.item_type itemType,
		  	pii.item_type itemType,
			pii.data_json dataJson,
			pii.is_must isMust,
			pii.picture_json pictureJson,
			pii.is_multiline isMultiline,
		  	pci.input_value inputValue,
		  	pci.is_ok inputStatus,
			pci.input_id checkInputId,
			pci.order_no orderNo,
			pci.point_classify_name classifyName,
			pci.point_classify_id classifyId
		FROM
			p_check_input pci
		INNER JOIN p_input_item pii ON  pii.id = pci.input_id
		LEFT JOIN p_check pc ON pc.id = pci.check_id
		LEFT JOIN p_point pp ON pp.id = pc.point_id
		LEFT JOIN p_plan ppl ON ppl.id = pc.plan_id
		WHERE
		pci.check_id = #{checkId}
		order by pci.order_no
	</select>

    <select id="checkHasRecord" resultType="int">
		SELECT
			count(1) num
		FROM
			p_check c
		WHERE
			is_ok in (1,2)
			and c.point_id = #{pointId}
			and c.plan_task_id = #{planTaskId}
	</select>

    <select id="pieChartData" resultType="Map">
        SELECT
        COALESCE(SUM(
        CASE
        WHEN is_ok = '1' THEN
        1
        ELSE
        0
        END
        ),0) AS "qualified",
        COALESCE(SUM(
        CASE
        WHEN is_ok = '2' THEN
        1
        ELSE
        0
        END
        ),0) AS "unqualified",
        COALESCE(SUM(
        CASE
        WHEN is_ok = '3' THEN
        1
        ELSE
        0
        END
        ),0) AS "omission",
        COUNT(id) count
        FROM
        p_check
        WHERE
        DATE_FORMAT(check_time, '%Y-%m') = #{checkTime}
        <if test="userId!=null and userId!=0">AND FIND_IN_SET(#{userId}, user_id)>0</if>
        <if test="routeId!=null and routeId!=0">AND route_id = #{routeId}</if>
        <if test="orgCode!=null">AND org_code LIKE #{orgCode}</if>
    </select>

    <select id="calendarData" resultType="Map">
        SELECT
        sum(is_ok) AS count,
        DATE_FORMAT(check_time, '%Y-%m-%d') time,
        is_ok status
        FROM
        p_check
        WHERE
        DATE_FORMAT(check_time, '%Y-%m') = #{checkTime}
        <if test="userId!=null and userId!=0">AND FIND_IN_SET(#{userId}, user_id)>0</if>
        <if test="routeId!=null and routeId!=0">AND route_id = #{routeId}</if>
        <if test="orgCode!=null">AND org_code LIKE #{orgCode}</if>
        GROUP BY is_ok,time
        ORDER BY time
    </select>


    <select id="queryRecordByPointId" resultType="Map" parameterType="Map">
        SELECT
        c.id,
        c.is_ok,
        CASE
        when c.plan_id > 0 THEN
        p.NAME
        ELSE
        '计划外'
        END planName,
        c.user_name userName,
        DATE_FORMAT(c.check_time, '%Y-%m-%d') checkDate,
        DATE_FORMAT(c.check_time, '%k:%i:%s') checkTime
        FROM
        p_check c
        LEFT JOIN p_plan p ON p.id = c.plan_id
        WHERE 1 = 1
        <if test="orgCode!=null">AND (c.org_code LIKE concat(#{orgCode},"-%") or c.org_code = #{orgCode})</if>
        <if test="departmentId!=null">AND FIND_IN_SET(#{departmentId}, c.dep_id) > 0</if>
        <if test="userId != null and userId != '' ">
        	AND FIND_IN_SET(#{userId}, c.user_id) > 0
        </if>
        <if test="planId != null and planId != '' ">
            AND c.plan_id = #{planId}
        </if>
        <if test="routeId != null and routeId != '' ">
            AND c.route_id = #{routeId}
        </if>
        <if test="isOk != null and isOk != '' ">
            AND c.is_ok = #{isOk}
        </if>

        <if test="pointId != null and pointId != '' ">
            AND c.point_id = #{pointId}
        </if>
        <if test="startTime != null and startTime != '' ">AND c.check_time <![CDATA[>=]]> STR_TO_DATE(#{startTime},
            '%Y-%m-%d %H:%i:%s')
        </if>
        <if test="endTime != null and endTime != '' ">and c.check_time <![CDATA[<=]]> STR_TO_DATE(#{endTime}, '%Y-%m-%d
            %H:%i:%s')
        </if>
        ORDER BY checkTime desc, checkDate desc
    </select>

    <select id="queryCheckById" resultType="Map">
		SELECT
			c.id,
			CASE
		WHEN c.is_ok = '1' THEN
			'合格'
		WHEN c.is_ok = '2' THEN
			'不合格'
		ELSE
			'漏检'
		END AS isOk,
		 c.check_mode checkMode,
		 c.score,
		 c.remark,
		 CASE
		WHEN (c.plan_task_id = 0) THEN
			'计划外完成'
		WHEN (c.plan_task_id > 0 AND c.is_ok <![CDATA[<]]> 3 ) THEN
            '按时完成'
        ELSE '未执行'
		END AS finishStatus,
		 pt.point_no pointNo,
		 pt. NAME pointName,
		 CASE pt.is_fixed
		WHEN 1 THEN
			'固定点'
		ELSE
			'移动点'
		END isFixed,
		 CASE
		WHEN c.plan_id > 0 THEN
			p. NAME
		ELSE
			'计划外'
		END planName,
		 p.id planId,
		 c.user_name userName,
		 c.dep_name department,
		 DATE_FORMAT(c.check_time, '%Y-%m-%d') checkDate,
		 DATE_FORMAT(c.check_time, '%k:%i:%s') checkTime,
		 r. NAME routeName
		FROM
			p_check c
		LEFT JOIN p_plan p ON p.id = c.plan_id
		LEFT JOIN p_point pt ON pt.id = c.point_id
		LEFT JOIN p_route r ON r.id = c.route_id
		where c.id = #{checkId}
	</select>

    <select id="queryCheckInputItems" resultType="Map">
		SELECT
			c.id,
			ii. NAME inputItem,
			ci.input_value,
			CASE ci.is_ok
		WHEN 1 THEN
		    '合格'
		WHEN 2 THEN
		    '不合格'
		ELSE
		    '漏检'
		END AS is_ok,
			ci.score
		FROM
			p_check c,
			p_check_input ci,
			p_input_item ii
		WHERE
			c.id = ci.check_id
		AND ii.id = ci.input_id
		AND c.id = #{checkId}
		ORDER BY
			ci.order_no
	</select>

    <select id="queryCheckImgs" resultType="Map">
		SELECT
			shot_type shotType,
			photo_data photoData
		FROM
			p_check_shot
		WHERE
			check_id = #{checkId}
	</select>

    <select id="getCheckInfoImgsList" resultType="Map">
        SELECT
        s.photo_data photoData, a.id
        FROM
        p_check a
        JOIN p_check_shot s ON s.check_id = a.id
        <choose>
            <when test="pointName!=null or pointNo!=null or isFixed!=null or catalogId!=null">
                LEFT JOIN `p_point` `b` ON `a`.`point_id` = `b`.`id`
            </when>
        </choose>
        <trim prefix="WHERE" prefixOverrides="AND ">
            <if test="beginDate!=null and endDate!=null">and a.check_time BETWEEN #{beginDate} and #{endDate}</if>
            <!-- <if test="endDate!=null"> and #{endDate} <![CDATA[>=]]> a.check_time  </if> -->
            <if test="userName!=null">and a.user_name like concat(concat("%",#{userName}),"%")</if>
            <if test="pointName!=null">and a.point_name like concat(concat("%",#{pointName}),"%")</if>
            <if test="pointNo!=null">and b.point_no = #{pointNo}</if>
            <if test="isFixed!=null">and b.is_fixed = #{isFixed}</if>
            <if test="isOK!=null">and a.is_OK = #{isOK}</if>
            <if test="planId!=null">and a.plan_Id = #{planId}</if>
            <if test="userId!=null">and find_in_set(#{userId},a.user_id)>0</if>
            <if test="routeId!=null">and a.route_Id = #{routeId}</if>
            <if test="catalogId!=null">and b.catalog_Id = #{catalogId}</if>
            <if test="orgCode!=null">AND (a.org_code LIKE concat(#{orgCode},"-%") or a.org_code = #{orgCode})</if>
            <if test="pointId!=null">and a.point_id = #{pointId}</if>
            <choose>
                <when test="finishStatus ==  0">and (`a`.`plan_task_detail_id` = 0 OR `a`.`plan_task_detail_id` is
                    null)
                </when>
                <when test="finishStatus ==  1">and (`a`.`plan_task_detail_id` != 0 AND `a`.`is_ok` != 3)</when>
                <when test="finishStatus ==  2">and `a`.`is_ok` = 3</when>
            </choose>
            <if test="departmentId!=null"> and find_in_set(#{departmentId},a.dep_id)>0 </if>
        </trim>
    </select>

    <select id="getCheckInfoImgsByCheckIdsList" resultType="Map">
        SELECT
        s.photo_data photoData, a.id
        FROM
        p_check a,
        p_check_shot s
        WHERE
        s.check_id = a.id

        and a.id in
        <foreach collection="checkIds" item="id" index="index" open="(" close=")" separator=",">#{id}</foreach>
    </select>


    <select id="getSelectedCheckInfoList" resultMap="checkInfoMap">

        SELECT
        a.id,
        a.org_code,
        a.point_id pointId,
        b.name point_name,
        b.is_fixed,
        b.point_no pointNO,
        a.user_id,
        a.user_name user_name,
        a.dep_name department_name,
        date_format(
        `a`.`check_time`,
        '%Y-%m-%d %H:%i:%s'
        ) AS `checkDate`,
        date_format(
        `a`.`upload_time`,
        '%Y-%m-%d %H:%i:%s'
        ) AS `upload_time`,
        (
        CASE
        WHEN `a`.`plan_task_detail_id` = 0 OR `a`.`plan_task_detail_id` is null
        THEN '0'
        WHEN `a`.`plan_task_detail_id` != 0 AND `a`.`is_ok` != 3
        THEN '1'
        WHEN `a`.`is_ok` = 3
        THEN '2'
        END
        ) AS `Finish_Status`,
        CASE
        WHEN a.is_ok = '1' THEN
        '合格'
        WHEN a.is_ok = '2' THEN
        '不合格'
        ELSE
        '漏检'
        END AS is_ok,
        a.score,
        d.`name` AS `route_name`,
        a.check_mode,
        e.`name` AS `plan_name`,
        a.plan_task_id,
        a.error,
        a.remark
        from
        `p_check` `a`
        LEFT JOIN `p_point` `b` ON `a`.`point_id` = `b`.`id`
        LEFT JOIN `p_route` `d` ON `a`.`route_id` = `d`.`id`
        LEFT JOIN `p_plan` `e` ON `a`.`plan_id` = `e`.`id`
        WHERE
        a.id in
        <foreach collection="checkIds" item="id" index="index" open="(" close=")" separator=",">#{id}</foreach>
        order by a.id

    </select>
    <!-- 月巡检统计 -->
    <select id="getCountMonthChk" parameterType="java.util.HashMap" resultType="java.util.HashMap">


        <!-- SELECT
            ma.checkDate,
            IfNULL(sa.proNum, '0') proNum,
            IfNULL(sa.actualNum, '0') actualNum,
            IfNULL(sa.forNum, '0') forNum
        FROM
            (
                SELECT
                    DATE_ADD(
                        DATE_FORMAT(#{startDate}, '%Y-%m-%d'),
                        INTERVAL b.help_topic_id DAY
                    ) AS checkDate
                FROM
                    (SELECT 1) a
                JOIN mysql.help_topic b ON b.help_topic_id <![CDATA[<]]> (
                    DATEDIFF(
                        DATE_FORMAT(#{endDate}, '%Y-%m-%d'),
                        DATE_FORMAT(#{startDate}, '%Y-%m-%d')
                    ) + 1
                )
            ) ma
        LEFT JOIN (
            SELECT
                fa.checkDate,
                sum(fa.proNum)  proNum,
                sum(fa.forNum) forNum,
                sum(fa.actualNum) actualNum
            FROM
                (
                    SELECT
                        CASE
                        WHEN DATE_FORMAT(end_time, '%Y-%m-%d') <![CDATA[<=]]>  #{endDate} THEN
                            DATE_FORMAT(end_time, '%Y-%m-%d')
                        ELSE
                        #{endDate}
                        end checkDate,
                        CASE finish_status
                        WHEN 3 THEN
                            1
                        ELSE
                        0
                        end forNum,
                        CASE finish_status
                        WHEN 2 THEN
                            1
                        ELSE
                        0
                        end actualNum
                    FROM
                        p_plan_task a
                    JOIN mysql.help_topic b ON b.help_topic_id  <![CDATA[<]]>(
                        DATEDIFF(
                            DATE_FORMAT(end_time, '%Y-%m-%d'),
                            DATE_FORMAT(begin_time, '%Y-%m-%d')
                        ) + 1
                    )
                    WHERE
                        1 = 1
                    <if test="orgCode!=null"> AND LOCATE(#{orgCode}, a.org_Code) <![CDATA[>]]>0 </if>
                    <if test="userId !=null"> and user_id = #{userId}</if>
                    AND end_time <![CDATA[>=]]> str_to_date(concat(#{startDate} ' 00:00:00'), '%Y-%m-%d %H:%i:%s')
                    AND begin_time <![CDATA[<=]]> str_to_date(concat(#{endDate}, ' 23:59:59'), '%Y-%m-%d %H:%i:%s')

                ) fa
            GROUP BY
                checkDate
        ) sa ON ma.checkDate = sa.checkDate -->


        SELECT
        ma.checkDate,
        IfNULL(sa.proNum, '0') proNum,
        IfNULL(sa.actualNum, '0') actualNum,
        IfNULL(sa.forNum, '0') forNum
        FROM
        (
        SELECT
        DATE_ADD(
        DATE_FORMAT(#{startDate}, '%Y-%m-%d'),
        INTERVAL b.help_topic_id DAY
        ) AS checkDate
        FROM
        (SELECT 1) a
        JOIN mysql.help_topic b ON b.help_topic_id  <![CDATA[<]]> (
        DATEDIFF(
        DATE_FORMAT(#{endDate}, '%Y-%m-%d'),
        DATE_FORMAT(#{startDate}, '%Y-%m-%d')
        ) + 1
        )
        ) ma
        LEFT JOIN (
        SELECT
        fa.checkDate,
        sum(fa.proNum) proNum,
        sum(fa.forNum) forNum,
        sum(fa.actualNum) actualNum
        FROM
        (
        SELECT
        check_time checkDate,
        sum(plan_task_num) proNum,
        sum(exec_plan_task_num) actualNum,
        sum(missed_check_num) forNum
        FROM
        p_plan_exec_statistics
      	<where>
    		<if test="orgCode!=null">AND LOCATE(#{orgCode}, org_Code) <![CDATA[>]]>0</if>
	        <if test="userId !=null">and user_id = #{userId}</if>
	        AND check_time <![CDATA[>=]]> #{startDate}
	        AND check_time <![CDATA[<=]]> #{endDate}
      	</where>

        GROUP BY check_time
        ) fa
        GROUP BY
        checkDate
        ) sa ON ma.checkDate = sa.checkDate
        ORDER BY checkDate

    </select>

    <select id="getCountMonthChkNew" resultType="java.util.HashMap">
      SELECT
      T_DATE.checkDate
      , (
      SELECT count(1) FROM P_PLAN_TASK ppt RIGHT JOIN P_PLAN_TASK_DETAIL pptd ON ppt.ID = pptd.TASK_NO WHERE  t_date.checkDate BETWEEN DATE_FORMAT(ppt .BEGIN_TIME, '%Y-%m-%d') AND DATE_FORMAT(ppt .END_TIME , '%Y-%m-%d')
      ) AS proNum
      , (
      SELECT
      count(1)
      FROM
      P_CHECK pc
      WHERE
      DATE_FORMAT(pc.CHECK_TIME, '%Y-%m-%d') = t_date.checkDate
      AND pc.IS_OK = 3
      ) AS forNum
      , (
      SELECT
      count(1)
      FROM
      P_CHECK pc
      WHERE
      DATE_FORMAT(pc.CHECK_TIME, '%Y-%m-%d') = t_date.checkDate
      AND pc.IS_OK != 3
      ) AS actualNum
      FROM
      (
      SELECT
      DATE_FORMAT(DATE_ADD(DATE_ADD(CURDATE(), INTERVAL - DAY(CURDATE()) + 1 DAY), INTERVAL b.help_topic_id DAY ), '%Y-%m-%d') AS checkDate
      FROM
      (
      SELECT
      1
      ) a
      JOIN mysql.help_topic b ON
      b.help_topic_id <![CDATA[<]]> (
      DATEDIFF(LAST_DAY(CURDATE()), DATE_ADD(CURDATE(), INTERVAL - DAY(CURDATE()) + 1 DAY)) + 1
      )
      ) AS t_date
    </select>
    <!-- 巡检记录日历统计 -->
    <select id="getCalenderCountInfoList" parameterType="java.util.HashMap" resultType="java.util.HashMap">
        SELECT
        DATE_FORMAT(c.check_time, '%Y%m%d') as checkDate, count(*) num
        FROM
        p_check c
        <where>
            <if test="pointId != null">c.point_id = #{pointId}</if>
            <if test="orgCode != null">AND c.org_code LIKE #{orgCode}</if>
            <if test="checkMonth != null">AND DATE_FORMAT(c.check_time, '%Y%m') = DATE_FORMAT(CONCAT(#{checkMonth},
                '01'), '%Y%m')
            </if>
        </where>
        GROUP BY DATE_FORMAT(c.check_time, '%Y%m%d');
    </select>

    <!-- 获取巡检人员信息 -->
    <select id="getCheckUserInfoList" parameterType="java.util.HashMap" resultType="java.util.HashMap">
        SELECT su.name AS realName, su.id AS UserID FROM s_user su
        <where>
            <if test="org_code != null">c.org_code LIKE #{orgCode}</if>
        </where>
    </select>

    <!-- 巡检记录统计 -->
    <select id="countVCheckInfoData" resultType="long">
        SELECT count(1) total_num FROM v_check_Info a
        <where>
            <if test="pointID != null">a.PointID = #{pointID}</if>
            <if test="checkTime != null">AND CONVERT (VARCHAR (20),a.CheckTime,112) =#{checkTime}</if>
            <if test="userID != null and userID!=-1 ">AND a.userID = #{userID}</if>
            <if test="status != null">AND a.IsOK = #{status}</if>
            <if test="userName != null">AND a.RealName LIKE '%'+#{userName}+'%'</if>
            <if test="orgCode != null">AND a.org_code LIKE #{orgCode}</if>
        </where>
    </select>

    <!-- 根据巡检点获取最新一条巡检记录 -->
    <select id="getLastsetCheckByPointID" resultType="long">
        SELECT
        id
        FROM
        p_check
        <where>
            <if test="pointID != null">point_id = #{pointID}</if>
        </where>
        ORDER BY
        check_time DESC
        LIMIT 1;
    </select>

    <resultMap id="checkInputResultMap"
               type="com.yeejoin.amos.patrol.business.entity.mybatis.CheckInputBo">
        <result property="inputItemName" column="Name"/>
        <result property="inputValue" column="InputValue"/>
        <result property="isOK" column="IsOK"/>
        <result property="uploadTime" column="UploadTime"/>
        <result property="executor" column="RealName"/>
        <result property="inputItemScore" column="Score"/>
        <result property="classifyId" column="classifyId"/>
        <result property="classifyName" column="classifyName"/>
        <result property="checkInputId" column="checkInputId"/>
        <result property="inputId" column="inputId"/>
        <result property="remark" column="remark"/>
    </resultMap>

    <select id="countCheckInputByMybatis" resultType="long">
        SELECT
        COUNT(*) AS total_num
        FROM
        p_check_input
        <where>
            <if test="checkID != null">check_id = #{checkID}</if>
        </where>
    </select>

    <select id="countClassifyByMybatis" resultType="long">
        SELECT
        COUNT( * ) AS total_num
        FROM
        p_point_classify classify
        <where>
            classify.id IN ( SELECT pci.point_classify_id FROM p_check_input pci WHERE <if test="checkID != null">pci.check_id = #{checkID}</if> )
        </where>
    </select>



    <!-- 根据巡检记录ID和巡检点ID获取巡检项信息 -->
    <select id="getCheckInputByCheckId" resultMap="checkInputResultMap">
        SELECT
        ci.id checkInputId,
        ci.input_id inputId,
        ci.input_value AS InputValue,
        (
        CASE ci.is_ok
        WHEN 1 THEN
        '合格'
        WHEN 2 THEN
        '不合格'
        WHEN 3 THEN
        '漏检'
        END
        ) AS IsOK,
        ii.`name` AS NAME,
        c.upload_time AS UploadTime,
        c.user_name AS RealName,
        ci.score AS Score,
        ci.point_classify_id classifyId,
        ci.point_classify_name classifyName,
        ci.remark,
        (SELECT count(id) FROM p_check_shot pcs WHERE pcs.check_input_id = ci.input_id AND pcs.check_id = ci.check_id)
        imgCount
        FROM
        p_check_input ci
        LEFT JOIN p_input_item ii ON ci.input_id = ii.id
        LEFT JOIN p_check c ON ci.check_id = c.id
        <where>
        <if test="checkID != null">
            AND ci.check_id = #{checkID}
        </if>
        <if test="pointID != null">
            AND c.point_id = #{pointID}
        </if>
        </where>
    </select>

    <!-- 根据巡检记录ID和巡检点ID获取巡检项信息 -->
    <select id="getCheckEquipByCheckID" resultType="java.util.HashMap" parameterType="java.lang.Long">
        SELECT
        classify.id  as classifyId,
        classify.name as equipmentName,
        classify.equipment_id as equipmentId,
        (SELECT create_date FROM p_check WHERE id = #{checkID}) as createDate
        FROM
        p_point_classify classify
        WHERE
            classify.id IN (
             SELECT
             pci.point_classify_id
             FROM p_check_input pci WHERE
            <if test="pointID != null">
                pci.check_id = #{checkID}
            </if> )
    </select>
    <!-- 根据巡检记录ID和巡检点ID获取巡检项信息 -->
    <select id="getEquipInputByCheckId" resultType="java.util.HashMap">
        SELECT
        ci.id checkInputId,
        ci.input_id inputId,
        ci.input_value AS InputValue,
        (
        CASE ci.is_ok
        WHEN 1 THEN
        '合格'
        WHEN 2 THEN
        '不合格'
        WHEN 3 THEN
        '漏检'
        END
        ) AS IsOK,
        ii.`name` AS NAME,
        c.upload_time AS UploadTime,
        c.user_name AS RealName,
        ci.score AS Score,
        ci.point_classify_id classifyId,
        ci.point_classify_name classifyName,
        ci.remark,
        (SELECT count(id) FROM p_check_shot pcs WHERE pcs.check_input_id = ci.input_id AND pcs.check_id = ci.check_id)
        imgCount,
        ii.input_classify as inputClassify,
        ii.basis_json as basisJson,
        ii.check_method as checkMethod,
        ii.level
        FROM
        p_check_input ci
        LEFT JOIN p_input_item ii ON ci.input_id = ii.id
        LEFT JOIN p_check c ON ci.check_id = c.id
        <where>
            <if test="checkID != null">
                AND ci.check_id = #{checkID}
            </if>
            <if test="pointID != null">
                AND c.point_id = #{pointID}
            </if>
            <if test="classifyId != null">
                AND ci.point_classify_id  = #{classifyId}
            </if>
        </where>
    </select>

    <select id="getEquipInfoImgsByCheckIdsList" resultType="java.util.HashMap" parameterType="java.lang.Long">
        SELECT
        s.photo_data photoData,
        a.id,
        s.check_input_id as checkInputId,
        s.classify_id as classifyId
        FROM
        p_check a,
        p_check_shot s
        WHERE
        s.check_id = a.id
        and a.id = #{id}
    </select>

    <resultMap id="checkDetailResultMap"
               type="com.yeejoin.amos.patrol.business.entity.mybatis.CheckDetailBo">
        <result property="checkTime" column="CheckTime"/>
        <result property="pointName" column="PointName"/>
        <result property="Level" column="Level"/>
        <result property="pointNo" column="PointNo"/>
        <result property="checkUserName" column="RealName"/>
        <result property="group" column="groupName"/>
        <result property="isFixed" column="IsFixed"/>
        <result property="routeName" column="RouteName"/>
        <result property="isOK" column="IsOK"/>
        <result property="planName" column="planName"/>
        <result property="planRemark" column="Remark"/>
        <result property="checkScore" column="Score"/>
        <result property="contact" column="contact"/>
    </resultMap>
    <!-- 根据巡检记录ID和巡检点ID获取巡检详情 -->
    <select id="getCheckDetailByID" resultMap="checkDetailResultMap">

         SELECT
        c.user_id AS userId,
        c.dep_id AS depId,
        c.check_time AS CheckTime,
        p.point_no AS PointNo,
        p.`name` AS PointName,
        c.user_id AS RealName,
        c.dep_name AS groupName,
        (
        CASE p.is_fixed
        WHEN 0 THEN
        '移动点'
        ELSE
        '固定点'
        END
        ) AS IsFixed,
        r.`name` AS RouteName,
        p.`level` AS Level,
        (
        CASE c.is_ok
        WHEN 1 THEN
        '合格'
        WHEN 2 THEN
        '不合格'
        ELSE
        '漏检'
        END
        ) AS IsOK,
        pl.`name` AS planName,
        pl.remark AS Remark,
        if(c.plan_id =0 AND p.is_score=0,0,c.score) as score,
        c.remark checkRemark
        FROM
        p_check c
        LEFT JOIN p_point p ON c.point_id = p.id
        LEFT JOIN p_plan pl ON c.plan_id = pl.id
        LEFT JOIN p_route r ON r.id = c.route_id
        <where>
	        <if test="checkID != null and checkID!= -1">
	            AND c.id = #{checkID}
	        </if>
	        <if test="pointID != null">
	            AND c.point_id = #{pointID}
	        </if>
        </where>
        ORDER BY
        c.check_time DESC
    </select>

    <!-- 根据巡检记录ID查询检查项图片/视频 -->
    <select id="getCheckInputPhotoByID" resultType="String">
        SELECT
        cs.photo_data
        FROM
        p_check_shot cs
        <where>
            <if test="checkId != null">cs.check_id = #{checkId}</if>
        </where>
    </select>
    <select id="getLivePhotos" resultType="String">
        SELECT
        cs.photo_data
        FROM
        p_check_shot cs
        <where>
            <if test="checkId != null">cs.check_id = #{checkId}</if>
            and check_input_id = 0
        </where>

    </select>
    <select id="getPhotosByCheckIDAndInputId" resultType="String">
        SELECT
        cs.photo_data
        FROM
        p_check_shot cs
        <where>
            <if test="checkId != null">cs.check_id = #{checkId}</if>
            <if test="checkInputId != null">and check_input_id = #{checkInputId}</if>
            <if test="classifyId != null">and classify_id = #{classifyId}</if>
        </where>

    </select>
    <!-- 风险点最新巡检信息查询，3d屏使用 -->
    <select id="countLastPointCheckData" resultType="long">
        select count(1) from (
        SELECT
        a.route_id,
        a.plan_id,
        a.user_id,
        a.point_id,
        max(id) id
        FROM
        `p_check` a
        <trim prefix="WHERE" prefixOverrides="AND ">
            <if test="orgCode!=null">and a.org_code like #{orgCode}</if>
            <if test="pointId!=null">and a.point_id = #{pointId}</if>
        </trim>
        GROUP BY
        a.route_id,
        a.plan_id,
        a.user_id,
        a.point_id ) sa
    </select>


    <resultMap id="checkInfoResultMap" type="com.yeejoin.amos.patrol.core.common.response.PointCheckInfoRespone">
        <result property="checkID" column="checkId"/>
        <result property="pointID" column="pointId"/>
        <result property="pointName" column="pointName"/>
        <result property="pointNo" column="pointNo"/>
        <result property="pointName" column="pointName"/>
        <result property="planId" column="planId"/>
        <result property="userID" column="UserID"/>
        <result property="userName" column="userName"/>
        <result property="checkTime" column="checkTime"/>
        <result property="address" column="address"/>
        <result property="status" column="status"/>
        <result property="inputStatus" column="inputStatus"/>
        <result property="telephone" column="telephone"/>
        <result property="department" column="department"/>
        <result property="inputName" column="inputItemName"/>
        <result property="inputValue" column="inputValue"/>
        <result property="inputLevel" column="level"/>
    </resultMap>
    <select id="getCheckInfoById" parameterType="java.lang.Long" resultMap="checkInfoResultMap">
		SELECT
			pc.id checkId,
			pc.point_id pointId,
			pp.point_no pointNo,
			pp.`name` pointName,
			ppl.`name` planName,
			ppl.id planId,
			pc.user_id userId,
			pc.`user_name` userName,
			pc.check_time checkTime,
			pp.address,
			pc.is_ok status,
			pci.is_ok inputStatus,
			pii.`name` inputItemName,
			pci.input_value inputValue,
			pc.dep_name department,
			pii.level
		FROM
			`p_check_input` pci
		LEFT JOIN p_check pc ON  pc.id = pci.check_id
		LEFT JOIN p_route pr ON pr.id = pc.route_id
		LEFT JOIN p_plan ppl ON ppl.id = pc.plan_id
		LEFT JOIN p_point pp ON pp.id = pc.point_id
		LEFT JOIN p_input_item pii ON pii.id = pci.input_id
		WHERE
			pci.check_id = #{checkId}
	</select>


    <!-- 风险点最新巡检信息查询，3d屏使用 -->
    <resultMap id="planRoutePointResultMap" type="com.yeejoin.amos.patrol.business.entity.mybatis.PlanRoutePointBo">
        <result property="routeName" column="route_name"/>
        <result property="planName" column="plan_name"/>
        <result property="deptName" column="department_name"/>
        <result property="userName" column="user_name"/>
        <result property="checkTime" column="check_time"/>
        <result property="status" column="is_ok"/>
        <result property="classifyName" column="classifyName"/>
        <result property="error" column="error"/>
    </resultMap>
    <select id="getLastPointCheckList" resultMap="planRoutePointResultMap">
        SELECT
        ma.id,
        ma.error,
        IFNULL(ma.route_name,'计划外巡检') route_name,
        IFNULL(ma.plan_name,'计划外巡检') plan_name,
        ma.dep_name department_name,
        ma.user_name,
        date_format(ma.check_time, '%Y-%m-%d %H:%i:%s') check_time,
        ma.is_ok,
        ma.error_classify classifyName
        FROM
        (
        SELECT
        sa.*,
        b.check_time,
        b.error_classify,
        b.error,
        CASE b.is_ok
        WHEN 1 THEN
        '合格'
        WHEN 2 THEN
        '不合格'
        WHEN 3 THEN
        '漏检'
        END
        AS is_ok,
        c. NAME route_name,
        d.`name` plan_name,
        b.dep_name dep_name,
        b.user_name user_name
        FROM
        (
        SELECT
        a.route_id,
        a.plan_id,
        a.user_id,
        a.point_id,
        max(id) id
        FROM
        `p_check` a
        <trim prefix="WHERE" prefixOverrides="AND ">
            <if test="orgCode!=null">and a.org_code like #{orgCode}</if>
            <if test="pointId!=null">and a.point_id = #{pointId}</if>
        </trim>
        GROUP BY
        a.route_id,
        a.plan_id,
        a.user_id,
        a.point_id
        ) sa
        LEFT JOIN `p_check` b ON sa.id = b.id
        LEFT JOIN p_route c ON sa.route_id = c.id
        LEFT JOIN p_plan d ON d.id = sa.plan_id
        ) ma
        ORDER BY check_time desc
    </select>
    <resultMap id="checkTraListBoResultMap"
               type="com.yeejoin.amos.patrol.business.entity.mybatis.CheckTraListBo">
        <result property="userId" column="userID"/>
        <result property="routeId" column="routeID"/>
        <result property="traName" column="name"/>
        <result property="userName" column="realName"/>
        <result property="planId" column="planID"/>
        <result property="checkTime" column="checkTime"/>
        <result property="checkId" column="checkID"/>
    </resultMap>
    <!--巡检轨迹统计 -->
    <select id="countCheckTraListData" resultType="long">
        SELECT
        count(1) AS total_num
        FROM
        p_check c,
        (
        SELECT
        p.user_id,
        p.id
        FROM
        p_plan p,
        p_route r
        <where>
            p.route_id = r.id
            <if test="orgCode != null">AND p.org_code LIKE #{orgCode}</if>
        </where>
        ) sa
        <where>
            c.plan_id = sa.id
            <if test="userId != null">AND find_in_set(#{userId}, sa.user_id) >0 </if>
            <if test="checkDate != null">AND DATE_FORMAT(c.check_time, '%Y%m%d') = DATE_FORMAT(#{checkDate}, '%Y%m%d')
            </if>
        </where>
    </select>
    <!--巡检轨迹查询 -->
    <select id="getCheckTraList" resultMap="checkTraListBoResultMap">
        SELECT
        *
        FROM
        (
        SELECT
        sa.userID,
        sa.routeID,
        sa.name,
        sa.realName,
        sa.planID,
        DATE_FORMAT(d.check_time, '%Y-%m-%d %T') AS checkTime,
        d.id AS checkID,
        (@rowNum :=@rowNum + 1) AS rownumber
        FROM
        p_check d,
        (SELECT(@rowNum := 0)) b,
        (
        SELECT
        a.user_id AS userID,
        a.route_id AS routeID,
        b.name AS name,
        (SELECT
		GROUP_CONCAT(
		`name`
		)
		FROM
			s_user u
		WHERE
			find_in_set(u.id,a.user_id)>0 ) AS realName,
        a.id AS planID
        FROM
        p_plan a,
        p_route b
        WHERE
        a.route_id = b.id
        <if test="orgCode != null">AND a.org_code LIKE #{orgCode}</if>
        ) sa
        WHERE
        d.plan_id = sa.planID
        <if test="userId != null">AND find_in_set(#{userId},  sa.userID)>0 </if>
        <if test="checkDate != null">AND DATE_FORMAT(d.check_time, '%y%m%d') = #{checkDate}</if>
        ORDER BY d.id DESC
        ) temp_row
    </select>

    <!-- 巡检记录映射Map -->
    <resultMap id="CheckInfoResultMap" type="com.yeejoin.amos.patrol.business.entity.mybatis.CheckInfoBo">
        <result property="id" column="id"/>
    </resultMap>
    <!-- 巡检记录统计 -->
    <select id="countCheckInfoListData" resultType="long">
        SELECT count(1) total_num FROM v_check_info a
        <trim prefix="WHERE" prefixOverrides="AND ">
            <if test="companyID!=null">AND a.CompanyID = #{companyID}</if>
            <if test="pointID!=null">AND a.PointID =#{pointID}</if>
            <if test="checkTime!=null">AND DATE_FORMAT(a.CheckTime, '%Y%m%d') = #{checkTime}</if>
            <if test="userID!=null and userID!=-1 ">AND a.userID = #{userID}</if>
            <if test="status!=null">AND a.status = #{status}</if>
            <if test="userName!=null">AND a.RealName LIKE '%'+#{userName}+'%'</if>
            <if test="groupId!=null">AND a.GroupID = #{groupId}</if>
            <if test="orgCode!=null">AND a.orgCode LIKE #{orgCode}</if>
        </trim>
    </select>
    <!-- 巡检记录查询-->
    <select id="getCheckInfoList" resultMap="CheckInfoResultMap">
        SELECT
        *
        FROM
        (
        SELECT
        DATE_FORMAT(a.CheckTime, '%Y-%m-%d %T') AS CheckTime,
        a.RealName,
        a.GroupID,
        a.UserID,
        a.PointID,
        a.CheckID,
        a.CompanyID,
        a.status AS IsOK,
        a.LoginName,
        a. NAME,
        a.ErrorMsg,
        a.TaskID AS ErrorID,
        a.pointName
        FROM
        v_check_info a
        <trim prefix="WHERE" prefixOverrides="AND ">
            <if test="companyID!=null">AND a.CompanyID = #{companyID}</if>
            <if test="pointID!=null">AND a.PointID = #{pointID}</if>
            <if test="checkTime!=null">AND DATE_FORMAT(a.CheckTime, '%Y%m%d') = #{checkTime}</if>
            <if test="userID!=null and userID!=-1 ">AND a.userID = #{userID}</if>
            <if test="status!=null">AND a.STATUS = #{status}</if>
            <if test="userName!=null">AND a.RealName LIKE '%'+#{userName}+'%'</if>
            <if test="groupId!=null">AND a.GroupID = #{groupId}</if>
            <if test="pointName!=null">AND a.pointName LIKE '%' + #{pointName} + '%'</if>
            <if test="orgCode!=null">and (a.org_Code like concat (#{orgCode},"-%")or a.org_Code= #{orgCode})</if>
        </trim>
        ) temp_row
        <choose>
            <when test="pageSize==-1"></when>
            <when test="pageSize!=-1">limit #{offset},#{pageSize}</when>
        </choose>
    </select>

    <select id="getRoutesAndPointsInfo" resultType="java.util.HashMap">
		SELECT
			p. NAME pName,
			p.floor,
			p.is_indoor isIndoor,
			p.coordinates,
			p.Address,
			p.id PointID,
			p. STATUS RealTimeStatus,
			p.`level`,
			p.org_code compCode,
			ptd.task_id taskId,
			temp.*
		FROM
			p_point p
		LEFT JOIN (
			SELECT
				B.*, A.point_id
			FROM
				p_route_point A
			LEFT JOIN (
				SELECT
					r.id RouteID,
					r. NAME rName,
					su. NAME boss,
					r.tel,
					u.id OpenID,
					(SELECT
					GROUP_CONCAT(
					`name`
					)
					FROM
						s_user u
					WHERE
						find_in_set(u.id, p.user_id)>0) RealName,
					u.org_code orgCode,
					g.department_name GroupName,
					u.org_code gOrgCode
				FROM
					p_route r
				LEFT JOIN p_plan p ON p.route_id = r.id
				LEFT JOIN s_department g ON g.id = p.dept_id
				LEFT JOIN s_user su ON su.id = r.boss
			) B ON A.route_id = B.RouteID
			UNION
				SELECT
					B.*, A.point_id
				FROM
					p_route_point A
				RIGHT JOIN (
					SELECT
						r.id RouteID,
						r. NAME rName,
						su.`name` boss,
						r.tel,
						u.id OpenID,
						(SELECT
						GROUP_CONCAT(
						`name`
						)
						FROM
							s_user u
						WHERE
							find_in_set(u.id, p.user_id)>0) NAME,
						u.org_code,
						g.department_name GroupName,
						u.org_code gOrgCode
					FROM
						p_route r
					LEFT JOIN p_plan p ON p.route_id = r.id
					LEFT JOIN s_department g ON g.id = p.dept_id
					LEFT JOIN s_user su ON su.id = r.boss
				) B ON A.route_id = B.RouteID
		) temp ON temp.point_id = p.id
		LEFT JOIN p_task_detail ptd ON ptd.point_id = p.id
		AND ptd.`status` = 2
	</select>

    <select id="getCheckStatisticalAnalysis" resultType="com.yeejoin.amos.patrol.business.vo.CheckAnalysisVo">
        <!-- select
        <choose>
            <when test=" statisticsTyle == 0 ">
                a.planName name,
            </when>
            <when test=" statisticsTyle == 1 ">
                a.routeName name,
            </when>
            <when test=" statisticsTyle == 2 ">
                a.pointName name,
            </when>
            <when test=" statisticsTyle == 3 ">
                a.deptName name,
            </when>
            <when test=" statisticsTyle == 4 ">
                a.userName name,
            </when>
            <when test=" statisticsTyle == 5 ">
                a.checkTime name,
            </when>
            <when test=" statisticsTyle == 6 ">
                a.checkMonth name,
            </when>
        </choose>
        count(a.checkId) planTaskCount,
        sum(a.finish ) checkCount,
        sum(a.ok) ok,
        sum(a.faild) faild,
        sum(a.missed) missed,
        sum(a.score) score,
        sum(a.missed) / count(a.checkId) * 100 missedRate,
        sum(a.faild) / count(a.checkId) * 100 faildRate
        FROM
        (
        SELECT
        CASE
        WHEN c.plan_id = 0 THEN
        '计划外'
        ELSE
        p. NAME
        END planName,
        c.plan_id planId,
        r. NAME routeName,
        r.id routeId,
        pt. NAME pointName,
        pt.id pointId,
        u.name userName,
        u.id userId,
        c.score,
        CASE
        WHEN u.org_code LIKE CONCAT(#{orgCode}, '%') THEN
        d.department_name
        ELSE
        ''
        END deptName,
        d.id departmentId,
        c.id checkId,
        c.plan_task_detail_id taskDetailId,
        c.is_ok isOk,
        c.plan_task_id,
        DATE_FORMAT(c.check_time,'%Y-%m-%d') checkTime,
        DATE_FORMAT(c.check_time,'%Y-%m') checkMonth,
        CASE
        WHEN c.is_ok = 1 THEN
        1
        ELSE
        0
        END ok,
        CASE
        WHEN c.is_ok = 2 THEN
        1
        ELSE
        0
        END faild,
        CASE
        WHEN c.is_ok = 3 THEN
        1
        ELSE
        0
        END missed,
        CASE
        WHEN c.is_ok = 3 THEN
        0
        ELSE
        1
        END finish
        FROM
        p_check c
        LEFT JOIN p_plan p ON c.plan_id = p.id
        LEFT JOIN p_plan_task pnt ON c.plan_task_id = pnt.id
        LEFT JOIN p_route r ON c.route_id = r.id
        LEFT JOIN p_point pt ON c.point_id = pt.id
        LEFT JOIN s_user u ON c.user_id = u.id
        LEFT JOIN s_department d ON d.id = u.department_id
        WHERE c.org_code LIKE CONCAT(#{orgCode}, '%')
        <choose>
            <when test=" statisticsTyle == 1 ">
                and r.id is not NULL
            </when>
            <when test=" statisticsTyle == 2 ">
            	and pt.id is not null
            </when>
        </choose>
        <if test="userName !=null and userName != '' ">and c.user_name like CONCAT('%', #{userName}, '%')</if>
        <if test="pointNo !=null and pointNo != '' ">and pt.point_no like CONCAT('%', #{pointNo}, '%')</if>
        <if test="pointName !=null and pointName != '' ">and pt.name like CONCAT('%', #{pointName}, '%')</if>
        <if test="isFixed !=null and isFixed != '' ">and pt.is_fixed = #{isFixed}</if>
        <choose>
            <when test=" processType > 0 and processType == 4 ">
                and <![CDATA[ c.plan_id  = 0 ]]>
            </when>
            <when test="processType > 0 and processType != 4  ">
                and pnt.status = #{processType}
            </when>
        </choose>
        <if test="isOk !=null and isOk != '' ">and c.isOk = #{isOk}</if>
        <if test="planId !=null and planId != '' ">and c.plan_id = #{planId}</if>
        <if test="routeId !=null and routeId != '' ">and c.route_id = #{routeId}</if>
        <if test="startTime !=null and startTime!= '' "> <![CDATA[ AND c.check_time >= #{startTime} ]]> </if>
        <if test="endTime !=null and endTime!='' "><![CDATA[AND c.check_time <= #{endTime}  ]]></if>
        ) a
        GROUP BY
        <choose>
            <when test=" statisticsTyle == 0 ">
                a.planId
            </when>
            <when test=" statisticsTyle == 1 ">
                a.routeId
            </when>
            <when test=" statisticsTyle == 2 ">
                a.pointId
            </when>
            <when test=" statisticsTyle == 3 ">
                a.departmentId
            </when>
            <when test=" statisticsTyle == 4 ">
                a.userId
            </when>
            <when test=" statisticsTyle == 5 ">
                a.checkTime
            </when>
            <when test=" statisticsTyle == 6 ">
                a.checkMonth
            </when>
        </choose> -->
         select
        <choose>
            <when test=" statisticsTyle == 0 ">
                a.planName name,
            </when>
            <when test=" statisticsTyle == 1 ">
                a.routeName name,
            </when>
            <when test=" statisticsTyle == 2 ">
                a.pointName name,
            </when>
            <when test=" statisticsTyle == 3 ">
                a.deptName name,
            </when>
            <when test=" statisticsTyle == 4 ">
                a.userName name,
            </when>
            <when test=" statisticsTyle == 5 ">
                a.checkTime name,
            </when>
            <when test=" statisticsTyle == 6 ">
                a.checkMonth name,
            </when>
        </choose>
        count(a.checkId) planTaskCount,
        sum(a.finish ) checkCount,
        sum(a.ok) ok,
        sum(a.faild) faild,
        sum(a.missed) missed,
        sum(a.score) score,
        sum(a.missed) / count(a.checkId) * 100 missedRate,
        sum(a.faild) / count(a.checkId) * 100 faildRate
        FROM
        (
        SELECT
        CASE
        WHEN c.plan_id = 0 THEN
        '计划外'
        ELSE
        p. NAME
        END planName,
        c.plan_id planId,
        r. NAME routeName,
        r.id routeId,
        pt. NAME pointName,
        pt.id pointId,
        c.user_id userName,
        c.dep_id deptName,
         c.user_id userId,
        c.dep_id departmentId,
        c.score,
        c.id checkId,
        c.plan_task_detail_id taskDetailId,
        c.is_ok isOk,
        c.plan_task_id,
        DATE_FORMAT(c.check_time,'%Y-%m-%d') checkTime,
        DATE_FORMAT(c.check_time,'%Y-%m') checkMonth,
        CASE
        WHEN c.is_ok = 1 THEN
        1
        ELSE
        0
        END ok,
        CASE
        WHEN c.is_ok = 2 THEN
        1
        ELSE
        0
        END faild,
        CASE
        WHEN c.is_ok = 3 THEN
        1
        ELSE
        0
        END missed,
        CASE
        WHEN c.is_ok = 3 THEN
        0
        ELSE
        1
        END finish
        FROM
        p_check c
        LEFT JOIN p_plan p ON c.plan_id = p.id
        LEFT JOIN p_plan_task pnt ON c.plan_task_id = pnt.id
        LEFT JOIN p_route r ON c.route_id = r.id
        LEFT JOIN p_point pt ON c.point_id = pt.id
        WHERE 1=1
        <if test="orgCode!=null">
          and (c.org_code LIKE CONCAT( #{orgCode}, '-%' ) or c.org_code= #{orgCode} )
        </if>
        <choose>
            <when test=" statisticsTyle == 1 ">
                and r.id is not NULL
            </when>
            <when test=" statisticsTyle == 2 ">
                and pt.id is not null
            </when>
        </choose>
        <if test="userName !=null and userName != '' ">and c.user_name like CONCAT('%', #{userName}, '%')</if>
        <if test="pointNo !=null and pointNo != '' ">and pt.point_no like CONCAT('%', #{pointNo}, '%')</if>
        <if test="pointName !=null and pointName != '' ">and pt.name like CONCAT('%', #{pointName}, '%')</if>
        <if test="isFixed !=null and isFixed != '' ">and pt.is_fixed = #{isFixed}</if>
        <choose>
            <when test=" processType > 0 and processType == 4 ">
                and <![CDATA[ c.plan_id  = 0 ]]>
            </when>
            <when test="processType > 0 and processType != 4  ">
                and pnt.status = #{processType}
            </when>
        </choose>
        <if test="isOk !=null and isOk != '' ">and c.isOk = #{isOk}</if>
        <if test="planId !=null and planId != '' ">and c.plan_id = #{planId}</if>
        <if test="routeId !=null and routeId != '' ">and c.route_id = #{routeId}</if>
        <if test="startTime !=null and startTime!= '' "> <![CDATA[ AND c.check_time >= #{startTime} ]]> </if>
        <if test="endTime !=null and endTime!='' "><![CDATA[AND c.check_time <= #{endTime}  ]]></if>
        ) a
        GROUP BY
        <choose>
            <when test=" statisticsTyle == 0 ">
                a.planId
            </when>
            <when test=" statisticsTyle == 1 ">
                a.routeId
            </when>
            <when test=" statisticsTyle == 2 ">
                a.pointId
            </when>
            <when test=" statisticsTyle == 3 ">
                a.departmentId
            </when>
            <when test=" statisticsTyle == 4 ">
                a.userId
            </when>
            <when test=" statisticsTyle == 5 ">
                a.checkTime
            </when>
            <when test=" statisticsTyle == 6 ">
                a.checkMonth
            </when>
        </choose>
    </select>


    <select id="getCumulativeCheckCountByOrgCode" resultType="Long">
		select count(1) total from p_check c where position( #{orgCode} in c.org_code ) and c.is_ok != '3'
	</select>

    <select id="getCheckDataCount" resultType="Long">
        SELECT coalesce(count(c.id),0) total FROM (
        SELECT
        DATE_FORMAT(
        ADDDATE(#{startTime} , INTERVAL @d DAY),
        '%Y-%m-%d'
        ) AS date ,@d :=@d + 1 DAY
        FROM
        p_check,
        (SELECT @d := 0) temp
        WHERE
        ADDDATE(#{startTime} , INTERVAL @d DAY)<![CDATA[  < ]]>   DATE_FORMAT(#{endTime}, '%Y-%m-%d')
        ) d LEFT JOIN p_check c ON DATE_FORMAT(c.check_time, '%Y-%m-%d') = d.date
        <if test=" orgCode != null and orgCode != '' ">and position( #{orgCode} in c.org_code )</if>
        <if test=" userId != null and userId  != '' ">and find_in_set(#{userId}, c.user_id)>0 </if>
        <if test="type == 1">and c.is_ok != #{param}</if>
        <if test="type == 2">and c.plan_id != #{param}</if>
        <if test="type == 3">and c.is_ok = #{param}</if>
        <where>
        <if test="startTime !=null and startTime!= '' "> <![CDATA[ AND d.date >= #{startTime} ]]> </if>
        <if test="endTime !=null and endTime!='' "><![CDATA[AND d.date < #{endTime}  ]]></if>
        </where>
        group by d.date
        order BY d.date
    </select>

    <!-- 巡检记录查询 -->
<!--    <select id="getCheckInfoList1" resultMap="CheckInfoResultMap">-->
<!--        select * from (-->
<!--        SELECT-->
<!--        DATE_FORMAT(c.check_time, '%Y-%m-%d %T') as checkTime,-->
<!--        c.user_id as UserID,-->
<!--        c.point_id as PointID,-->
<!--        c.id as CheckID,-->
<!--        c.is_ok as IsOK,-->
<!--        c.error as ErrorMsg,-->
<!--        c.plan_task_id as ErrorID,-->
<!--        p.name as pointName,-->
<!--        c.dep_name as NAME,-->
<!--        c.dep_id as GroupID,-->
<!--        c.user_name as RealName,-->
<!--        (SELECT-->
<!--		GROUP_CONCAT(-->
<!--		IFNULL(case when u.mobile = '' or u.mobile is null then null else u.mobile END,u.telephone)-->
<!--		)-->
<!--		FROM-->
<!--			s_user u-->
<!--		WHERE-->
<!--			find_in_set(u.id,c.user_id)>0) as LoginName,-->
<!--        c.org_code as orgCode-->
<!--        FROM-->
<!--        p_check c,-->
<!--        p_point p-->
<!--        where-->
<!--        p.id =c.point_id ) as a-->
<!--        <trim prefix="WHERE" prefixOverrides="AND ">-->
<!--            <if test="pointID!=null">AND a.PointID = #{pointID}</if>-->
<!--            <if test="checkTime!=null">AND DATE_FORMAT(a.CheckTime, '%Y%m%d') = #{checkTime}</if>-->
<!--            <if test="userID!=null and userID!=-1 ">AND find_in_set(#{userID}, a.userID)>0 </if>-->
<!--            <if test="status!=null">AND a.IsOK = #{status}</if>-->
<!--            <if test="userName!=null">AND a.RealName LIKE concat(concat("%",#{userName}),"%")</if>-->
<!--            <if test="groupId!=null">AND find_in_set(#{groupId}, a.GroupID)>0</if>-->
<!--            <if test="pointName!=null">AND a.pointName LIKE concat(concat("%",#{pointName}),"%")</if>-->
<!--            <if test="orgCode!=null">AND a.orgCode LIKE #{orgCode}</if>-->
<!--        </trim>-->

<!--        <choose>-->
<!--            <when test="pageSize==-1"></when>-->
<!--            <when test="pageSize!=-1">limit #{offset},#{pageSize}</when>-->
<!--        </choose>-->
<!--    </select>!&ndash;&gt;-->

    <select id="getCheckInfoList1" resultType="Map">
        select
            c.id as checkID,
            c.dep_name as name,
            c.dep_id as groupID,
            c.user_name as realName,
            c.org_code as orgCode,
            c.user_id as userID,
            c.point_id as pointID,
            c.is_ok as isOK,
            c.error as errorMsg,
            c.plan_task_id as errorID,
            date_format(c.check_time, '%Y-%m-%d %T') as checkTime,
            p.name as pointName
        from
            p_check c,
            p_point p
        where c.point_id = p.id
            <if test="pointID!=null">AND c.point_id = #{pointID}</if>
            <if test="checkTime!=null">AND DATE_FORMAT(c.check_time, '%Y%m%d') = #{checkTime}</if>
            <if test="userID!=null and userID!=-1 ">AND find_in_set(#{userID}, c.user_id)>0 </if>
            <if test="status!=null">AND c.is_ok = #{status}</if>
            <if test="userName!=null">AND c.error LIKE concat(concat("%",#{userName}),"%")</if>
            <if test="groupId!=null">AND find_in_set(#{groupId}, c.dep_id)>0</if>
            <if test="pointName!=null">AND p.name LIKE concat(concat("%",#{pointName}),"%")</if>
            <if test="orgCode!=null">AND c.org_code LIKE #{orgCode}</if>
        order by c.create_date DESC
        <choose>
            <when test="pageSize==-1"></when>
            <when test="pageSize!=-1">limit #{offset},#{pageSize}</when>
        </choose>
        
    </select>


    <!-- 巡检记录统计 -->
    <select id="countCheckInfoListData1" resultType="long">
        select
            count(1)
        from
            p_check c,
            p_point p
        where c.point_id = p.id
        <if test="pointID!=null">AND c.point_id = #{pointID}</if>
        <if test="checkTime!=null">AND DATE_FORMAT(c.check_time, '%Y%m%d') = #{checkTime}</if>
        <if test="userID!=null and userID!=-1 ">AND find_in_set(#{userID}, c.user_id)>0 </if>
        <if test="status!=null">AND c.is_ok = #{status}</if>
        <if test="userName!=null">AND c.user_name LIKE concat(concat("%",#{userName}),"%")</if>
        <if test="groupId!=null">AND find_in_set(#{groupId}, c.dep_id)>0</if>
        <if test="pointName!=null">AND p.name LIKE concat(concat("%",#{pointName}),"%")</if>
        <if test="orgCode!=null">AND c.org_code LIKE #{orgCode}</if>
    </select>


    <resultMap id="checkInfoResulBusinesstMap"
               type="com.yeejoin.amos.patrol.core.common.response.PointCheckInfoBusinessRespone">
        <result column="checkId" property="checkId"/>
        <result property="id" column="pointId"/>
        <result property="name" column="pointName"/>
        <result property="nodeState" column="status"/>
        <result property="pointNo" column="pointNo"/>
        <result property="level" column="level"/>
        <collection property="pointInputitems"
                    ofType="com.yeejoin.amos.patrol.core.common.response.CheckInputBusinessResponse"
                    select="queryPointInputBu" column="{checkId=checkId}">
        </collection>
    </resultMap>
    <select id="getCheckInfoBusinessById" resultMap="checkInfoResulBusinesstMap">
		 SELECT
			pc.point_id pointId,
			pp.`name` pointName,
			pc.is_ok status,
			pc.id checkId,
			pc.user_name as checkUser,
			pp.point_no pointNo,
			pp.level
		FROM
		p_check pc
		LEFT JOIN p_point pp ON pp.id = pc.point_id
		WHERE pc.id = #{checkId}
	</select>

    <resultMap id="CheckInputBuMap" type="com.yeejoin.amos.patrol.core.common.response.CheckInputBusinessResponse">
        <result property="status" column="inputStatus"/>
        <result property="pointInputitemName" column="pointInputitemName"/>
        <result property="pointInputitemId" column="pointInputitemId"/>
        <result property="checkInputitemId" column="checkInputitemId"/>
    </resultMap>
    <select id="queryPointInputBu" resultMap="CheckInputBuMap">
                    SELECT
            pci.is_ok inputStatus,
            pii.`name` pointInputitemName,
            a.id pointInputitemId,
            pci.id checkInputitemId
        FROM
            `p_check_input` pci
            LEFT JOIN p_input_item pii ON pii.id = pci.input_id
            LEFT JOIN (
        SELECT
            ppi.id,
            ppi.input_item_id
        FROM
            p_check pc
            LEFT JOIN p_point_inputitem ppi ON ppi.point_id = pc.point_id
        WHERE
            pc.id =  #{checkId}
            ) a ON a.input_item_id = pii.id
        WHERE
            pci.check_id =  #{checkId}
	</select>

    <select id="queryUserInfoByIds" resultType="Map">
		SELECT
			GROUP_CONCAT(u.`name`) userName,
			GROUP_CONCAT( distinct d.department_name) depName,
			GROUP_CONCAT( distinct d.id) depId
		FROM
			s_user u
		LEFT JOIN s_department d ON u.department_id = d.id
		WHERE
			FIND_IN_SET(u.id, #{userIds})>0
	</select>


    <select id="getPlanCheckDataCount" resultType="Long">
        SELECT COALESCE
        ( SUM( c.point_num ), 0 ) total
        FROM
        (
        SELECT
        DATE_FORMAT( ADDDATE( #{startTime}, INTERVAL @d DAY ), '%Y-%m-%d' ) AS date,
        @d := @d + 1 DAY
        FROM
        p_plan_task,
        ( SELECT @d := 0 ) temp
        WHERE
        ADDDATE( #{startTime} , INTERVAL @d DAY ) <![CDATA[<]]>  DATE_FORMAT( #{endTime} , '%Y-%m-%d' )
        ) d
        LEFT JOIN p_plan_task c ON DATE_FORMAT(c.check_date, '%Y-%m-%d') = d.date
        <if test="orgCode !=null and orgCode!='' ">
          AND (c.org_code=#{orgCode} or c.org_code like CONCAT(#{orgCode},'-%'))
        </if>
          WHERE 1=1
          <if test="startTime !=null and startTime!= '' ">  AND d.date <![CDATA[ >= ]]>  #{startTime}</if>
          <if test="endTime !=null and endTime!='' "> AND d.date <![CDATA[ < ]]>  #{endTime} </if>
        GROUP BY
        d.date
        ORDER BY
        d.date
    </select>

</mapper>