<?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.inspect.client.mapper.ClientStatisticsMapper">

    <resultMap type="com.inspect.client.domain.ResDomain" id="statisticsResult">
        <result property="totalNum"    column="totalNum"    />
        <result property="validNum"    column="validNum"    />
    </resultMap>

    <!--  查询任务完成率-->
    <select id="queryTaskDone" parameterType="com.inspect.client.domain.ReqDomain" resultMap="statisticsResult">
        select count(1) totalNum,sum(case when task_state = 4 then 1 else 0 end) validNum from patrol_task_status
        <if test="beginTime != null and endTime != null">
            where start_time between #{beginTime} and #{endTime}
        </if>
    </select>

    <!--  巡视告警人工审核完成率 -->
    <select id="queryPersonExamine" parameterType="com.inspect.client.domain.ReqDomain" resultMap="statisticsResult">
        select count(1) totalNum,sum(case when res_status = 1 then 1 else 0 end) validNum from result_analysis
        where result_type = 0
        <if test="beginTime != null and endTime != null">
          and create_time between #{beginTime} and #{endTime}
        </if>
    </select>

    <!--  巡视告警准确率 -->
    <select id="queryWarning" parameterType="com.inspect.client.domain.ReqDomain" resultMap="statisticsResult">
        select count(result.line_id) totalNum,sum(case when aux.line_id is null then 1 else 0 end) validNum
        from patrol_result result
        left join patrol_result_aux aux
        on aux.result_id = result.line_id
        <if test="beginTime != null and endTime != null">
        where result.time between #{beginTime} and #{endTime}
        </if>
    </select>

    <!--  巡视结果人工审核完成率 -->
    <select id="queryResultPersonExamine" parameterType="com.inspect.client.domain.ReqDomain" resultMap="statisticsResult">
        select count(1) totalNum,sum(case when patrol_status = 1 then 1 else 0 end) validNum from patrol_result
        <if test="beginTime != null and endTime != null">
        where time between #{beginTime} and #{endTime}
        </if>

    </select>

    <!--  巡检点位漏检率 -->
    <select id="queryPointNum" parameterType="com.inspect.client.domain.ReqDomain" resultMap="statisticsResult">
        select count(DISTINCT point.patrol_point_id) totalNum,count(DISTINCT result.device_id) validNum from basedata_patrolpoint point
        left join patrol_result result
        on point.patrol_point_id = result.device_id
        <if test="beginTime != null and endTime != null">
               and result.time between #{beginTime} and #{endTime}
        </if>
    </select>

    <!--  查询巡视主机唯一标识 -->
    <select id="queryIICSystemCode"  resultType="java.lang.String">
        select patrol_device_code from basedata_eqpbook where type = 20
    </select>
    <!--  根据巡视设备编码查询巡视系统唯一标识 -->
    <select id="queryPatrolSystemCodeByDeviceCode"  resultType="java.lang.String" parameterType="java.lang.String">
        select main_system_code from basedata_eqpbook where patrol_device_code = #{code}
    </select>

    <!--    查询机器人巡视系统编码-->
    <select id="queryRobotSystemCode" resultType="java.lang.String">
        select main_system_code from basedata_eqpbook
        where FIND_IN_SET(type,'1,2,3')
        and main_system_code is not null and main_system_code != ''
        limit 1
    </select>

    <!--    查询无人机巡视系统编码-->
    <select id="queryNestSystemCode" resultType="java.lang.String">
        select main_system_code from basedata_eqpbook
        where FIND_IN_SET(type,'13')
        and main_system_code is not null and main_system_code != ''
        limit 1
    </select>

    <select id="selectStationNo" resultType="java.lang.String">
        select station_no stationNo from basedata_station limit 1
    </select>
</mapper>