<?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.yfd.platform.modules.patroltask.mapper.TaskResultMapper">

    <select id="getAbnormalDeviceList" resultType="java.util.Map">
        SELECT
        t.*,sd.device_class,sd.early_min,sd.early_max,sd.same_min,sd.same_max,sd.serious_min,sd.serious_max,sd.critical_min,sd.critical_max,tt.cexamine_user_name,tt.examine_date,
        CASE
        WHEN t.flag = '4' THEN '识别失败'
        WHEN t.flag = '6' THEN '采集失败'
        WHEN t.valid1 = '2' THEN '异常'
        -- 或者你可以设置一个默认值
        ELSE NULL
        END AS analysisResult
        FROM
        ( SELECT *, CASE WHEN revise_valid = '0' THEN '1' WHEN revise_valid = '1' THEN '2' ELSE valid END AS valid1 FROM
        iis_task_result ) t
        INNER JOIN iis_substation_device sd ON sd.device_id = t.device_id
        INNER JOIN iis_task_todo tt ON tt.task_todo_id = t.task_todo_id
        WHERE
        t.datastatus = '1'
        AND task_type != '4'
        AND (t.valid1= '2'
        OR ( t.flag = '4' OR t.flag = '6' ))
        <if test="stationCode != null and stationCode != ''">
            AND t.station_code=#{stationCode}
        </if>
        <if test="areaId != null and areaId != ''">
            AND t.area_id=#{areaId}
        </if>
        <if test="bayId != null and bayId != ''">
            AND t.bay_id=#{bayId}
        </if>
        <if test="taskName != null and taskName != ''">
            AND t.task_name LIKE concat('%',#{taskName},'%')
        </if>
        <if test="deviceName != null and deviceName != ''">
            AND t.device_name LIKE concat('%',#{deviceName},'%')
        </if>
        <if test="patroldeviceName != null and patroldeviceName != ''">
            AND t.patroldevice_name LIKE concat('%',#{patroldeviceName},'%')
        </if>
        <if test="componentName != null and componentName != ''">
            AND t.component_name LIKE concat('%',#{componentName},'%')
        </if>
        <if test="recognitionType != null and recognitionType != ''">
            AND FIND_IN_SET(#{recognitionType},t.recognition_type)
        </if>

       <!-- <choose>
            <when test="status == '4' or status == '6'">
                and t.flag = #{status}
            </when>
            <when test="status == '2'">
                and t.valid1 = #{status}
            </when>
        </choose>-->
        <if test="startDate != null and startDate != ''">
            and str_to_date(time, '%Y-%m-%d') &gt;= #{startDate}
        </if>
        <if test="endDate != null and endDate != ''">
            and str_to_date(time, '%Y-%m-%d') &lt;= #{endDate}
        </if>
        ORDER BY t.patroldevice_date
    </select>

    <select id="getTaskDeviceList" resultType="java.util.Map">
        SELECT
        t.*,sd.meter_type AS meterType,sd.patroldevice_json AS patroldeviceJson,sd.device_class AS deviceClass,atd.commonLower,atd.commonUpper,atd.seriousLower,atd.seriousUpper,atd.dangerLower,atd.dangerUpper
        FROM
        ( SELECT *, CASE WHEN revise_valid = '0' THEN '1' WHEN revise_valid = '1' THEN '2' ELSE valid END AS valid1 FROM
        iis_task_result ) t
        INNER JOIN iis_substation_device sd ON sd.device_id= t.device_id
        INNER JOIN iis_task_todo td ON td.task_todo_id=t.task_todo_id
        LEFT JOIN (SELECT
        device_id,
        MAX(CASE WHEN (alarm_level = '1' AND decide_rule ='6') THEN base_line_value ELSE NULL END) AS commonLower,
        MAX(CASE WHEN (alarm_level = '1' AND decide_rule ='7') THEN base_line_value ELSE NULL END) AS commonUpper,
        MAX(CASE WHEN (alarm_level = '2' AND decide_rule ='6') THEN base_line_value ELSE NULL END) AS seriousLower,
        MAX(CASE WHEN (alarm_level = '2' AND decide_rule ='7') THEN base_line_value ELSE NULL END) AS seriousUpper,
        MAX(CASE WHEN (alarm_level = '3' AND decide_rule ='6') THEN base_line_value ELSE NULL END) AS dangerLower,
        MAX(CASE WHEN (alarm_level = '3' AND decide_rule ='7') THEN base_line_value ELSE NULL END) AS dangerUpper
        FROM
        iis_alarm_threshold GROUP BY device_id) atd ON atd.device_id = t.device_id
        WHERE
        t.datastatus = '1'
        AND t.task_todo_id = #{taskTodoId}
        <if test="deviceName != null and deviceName != ''">
            AND sd.device_name LIKE concat('%',#{deviceName},'%')
        </if>
        <if test="valid == 1">
            AND (t.valid1= #{valid}
            AND ( t.flag != '4' AND t.flag != '6' ))
        </if>
        <if test="valid == 2">
            AND (t.valid1= #{valid}
            OR ( t.flag = '4' OR t.flag = '6' ))
        </if>
        ORDER BY
        sd.device_code ASC
    </select>
    <select id="getHistoricalCurve" resultType="java.util.Map">
        SELECT
        tr.*
        FROM
        iis_task_result tr
        INNER JOIN iis_task t ON tr.task_code = t.task_code
        WHERE
        t.task_todo_type = '3'
        AND tr.device_id = #{deviceId}
        AND tr.datastatus = '1'
        AND tr.time &lt; NOW()
        ORDER BY tr.time DESC
	    LIMIT 20
    </select>
    <select id="getTaskResultById" resultType="java.util.Map">
    SELECT
	t.*,
	sd.meter_type AS meterType,
	sd.patroldevice_json AS patroldeviceJson,
	sd.device_class AS deviceClass,
	REPLACE ( JSON_EXTRACT( sd.picture_analysis_type_list, '$.PictureAnalysisType' ), '"', '' ),
CASE

		WHEN (
			sd.picture_analysis_type_list = ''
			OR sd.picture_analysis_type_list IS NULL
			OR JSON_EXTRACT( sd.picture_analysis_type_list, '$.PictureAnalysisType' ) IS NULL
			OR (
				REPLACE ( JSON_EXTRACT( sd.picture_analysis_type_list, '$.PictureAnalysisType' ), '"', '' ) != 'meter'
				AND REPLACE ( JSON_EXTRACT( sd.picture_analysis_type_list, '$.PictureAnalysisType' ), '"', '' ) != 'infrared'
			)) THEN
			t.`desc` ELSE t.`value`
		END AS `result`
	FROM
		(
		SELECT
			patroldevice_code,
			patroldevice_name,
			patroldevice_channelcode,
			patroldevice_pos,
			patroldevice_baseimage,
			patroldevice_effectiveregion,
			task_code,
			task_name,
			area_id,
			bay_id,
			material_id,
			main_device_id,
			area_name,
			bay_name,
			main_device_name,
			component_id,
			component_name,
			device_id,
			device_name,
			data_type,
			TIME,
			`desc`,
			`value`,
			revise_value,
			file_path,
			defect_file_path,
			rectangle,
			datastatus,
			task_todo_id,
			flag,
			value_type,
			file_type,
			is_alarm,
			station_code,
			order_num,
			unit,
			custom1,
		CASE

				WHEN revise_valid = '0' THEN
				'1'
				WHEN revise_valid = '1' THEN
				'2' ELSE valid
			END AS valid1
		FROM
			iis_task_result
		) t
		LEFT JOIN iis_substation_device sd ON sd.device_id = t.device_id
	WHERE
		t.datastatus = '1'
	AND task_todo_id = #{taskTodoId}
        <if test="mainDeviceId != null and mainDeviceId != ''">
            AND t.main_device_id = #{mainDeviceId}
        </if>
        <if test="areaId != null and areaId != ''">
            AND t.area_id = #{areaId}
        </if>
        <if test="bayId != null and bayId != ''">
            AND t.bay_id = #{bayId}
        </if>
ORDER BY
	t.order_num
    </select>
    <select id="getAbnormalDevice" resultType="java.util.Map">
        SELECT
        t.*,sd.device_class,tt.cexamine_user_name,tt.examine_date
        FROM
        ( SELECT *, CASE WHEN revise_valid = '0' THEN '1' WHEN revise_valid = '1' THEN '2' ELSE valid END AS valid1 FROM
        iis_task_result ) t
        INNER JOIN iis_substation_device sd ON sd.device_id = t.device_id
        INNER JOIN iis_task_todo tt ON tt.task_todo_id = t.task_todo_id
        WHERE
        t.station_code=#{stationCode}
        AND
        t.datastatus = '1'
        AND task_type != '4'
        AND (t.valid1= '2'
        OR ( t.flag = '4' OR t.flag = '6' ))
        <if test="taskName != null and taskName != ''">
            AND t.task_name LIKE concat('%',#{taskName},'%')
        </if>
        <if test="deviceName != null and deviceName != ''">
            AND t.device_name LIKE concat('%',#{deviceName},'%')
        </if>
        <if test="patroldeviceName != null and patroldeviceName != ''">
            AND t.patroldevice_name LIKE concat('%',#{patroldeviceName},'%')
        </if>
        <if test="componentName != null and componentName != ''">
            AND t.component_name LIKE concat('%',#{componentName},'%')
        </if>
        <if test="recognitionType != null and recognitionType != ''">
            AND FIND_IN_SET(#{recognitionType},t.recognition_type)
        </if>

        <!-- <choose>
             <when test="status == '4' or status == '6'">
                 and t.flag = #{status}
             </when>
             <when test="status == '2'">
                 and t.valid1 = #{status}
             </when>
         </choose>-->
        <if test="startDate != null and startDate != ''">
            and str_to_date(time, '%Y-%m-%d %H:%i:%s') &gt;= #{startDate}
        </if>
        <if test="endDate != null and endDate != ''">
            and str_to_date(time, '%Y-%m-%d %H:%i:%s') &lt;= #{endDate}
        </if>
        ORDER BY t.patroldevice_date
    </select>
    <!--根据任务执行情况ID查询点位查询-->
    <select id="getTaskDeviceById" resultType="java.util.Map">
        SELECT
        t.*,sd.meter_type AS meterType,sd.patroldevice_json AS patroldeviceJson,sd.device_class AS deviceClass
        FROM
        ( SELECT *, CASE WHEN revise_valid = '0' THEN '1' WHEN revise_valid = '1' THEN '2' ELSE valid END AS valid1 FROM
        iis_task_result ) t
        INNER JOIN iis_substation_device sd
        ON sd.device_id= t.device_id
        WHERE
        t.datastatus = '1'
        AND task_todo_id = #{taskTodoId}
        <if test="deviceName != null and deviceName != ''">
            AND sd.device_name LIKE concat('%',#{deviceName},'%')
        </if>
        <if test="valid == 1">
            AND (t.valid1= #{valid}
            AND ( t.flag != '4' AND t.flag != '6' ))
        </if>
        <if test="valid == 2">
            AND (t.valid1= #{valid}
            OR ( t.flag = '4' OR t.flag = '6' ))
        </if>
        ORDER BY t.order_num
    </select>
    <!--巡视设备巡检统计-->
    <select id="getDeviceInspectionStat" resultType="java.util.Map">
        SELECT
        a.patroldevice_code,
        count( a.sdate ) count,
        TIMESTAMPDIFF(
        DAY,
        a.production_date,
        NOW()) production_count,
        a.production_date,
        a.patroldevice_name,
        a.type,
        a.`online`
        FROM
        (
        SELECT
        sp.patroldevice_code,
        str_to_date( tr.patroldevice_date, '%Y-%m-%d' ) sdate,
        sp.production_date production_date,
        sp.patroldevice_name patroldevice_name,
        sp.type type,
        sp.`online` `online`

        FROM
        iis_task_result tr
        LEFT JOIN iis_substation_patroldevice sp ON tr.patroldevice_code = sp.patroldevice_code
        WHERE
        tr.flag IN ( '2', '3', '4', '5' )
        AND tr.patroldevice_date &gt;= sp.production_date
        AND tr.patroldevice_date &lt;= NOW()
        <if test="patrolDeviceCode != null and patrolDeviceCode != ''">
            AND sp.patroldevice_code LIKE concat('%',#{patrolDeviceCode},'%')
        </if>
        <if test="patrolDeviceName != null and patrolDeviceName != ''">
            AND sp.patroldevice_name LIKE concat('%',#{patrolDeviceName},'%')
        </if>
        <if test="type != null and type != ''">
            AND sp.type = #{type}
        </if>
        GROUP BY
        sp.patroldevice_code,
        str_to_date( tr.patroldevice_date, '%Y-%m-%d' )
        ) a
        GROUP BY
        a.patroldevice_code
    </select>
    <!--巡视任务数据统计-->
    <select id="getTaskDataStat" resultType="java.util.Map">
        SELECT
        y.date,y.count1,y.sum1,y.count2,y.sum2,y.count3,y.sum3,y.count4,y.sum4,y.count5,y.sum5,z.num,z.duration,z.count,z.type
        FROM ((SELECT
        <if test="type ==1">
            date date,
        </if>
        <if test="type ==2">
            weekday date,
        </if>
        <if test="type ==3">
            moon date,
        </if>
        SUM( count1 ) AS count1,
        SUM( sum1 ) AS sum1,
        SUM( count2 ) AS count2,
        SUM( sum2 ) AS sum2,
        SUM( count3 ) AS count3,
        SUM( sum3 ) AS sum3,
        SUM( count4 ) AS count4,
        SUM( sum4 ) AS sum4,
        SUM( count5 ) AS count5,
        SUM( sum5 ) AS sum5
        FROM
        (
        SELECT
        CONCAT( DATE_FORMAT( date, '%Y-%m' ), '第', FLOOR(( DAY ( date ) - 1 ) / 7 ) + 1, '周' ) weekday,
        DATE_FORMAT( date, '%Y-%m月' ) moon,
        date,
        SUM(count1) AS count1,
        SUM(sum1) AS sum1,
        SUM(count2) AS count2,
        SUM(sum2) AS sum2,
        SUM(count3) AS count3,
        SUM(sum3) AS sum3,
        SUM(count4) AS count4,
        SUM(sum4) AS sum4,
        SUM(count5) AS count5,
        SUM(sum5) AS sum5
        FROM ((SELECT
        date,
        COUNT(type) count1,
        SUM(type) sum1,
        NULL count2,
        NULL sum2,
        NULL count3,
        NULL sum3,
        NULL count4,
        NULL sum4,
        NULL count5,
        NULL sum5

        FROM
        (
        SELECT
        str_to_date( tr.patroldevice_date, '%Y-%m-%d' ) date,
        CASE

        WHEN ( flag = '4' OR flag = '6' ) THEN
        1 ELSE 0
        END type
        FROM
        iis_task_result tr
        LEFT JOIN iis_task_todo tt ON tr.task_todo_id = tt.task_todo_id
        WHERE
        tt.task_state != '0' AND tr.patroldevice_date IS NOT NULL
        <if test="stationCode != null and stationCode != ''">
            AND tt.station_code=#{stationCode}
        </if>

        AND str_to_date(tr.patroldevice_date, '%Y-%m-%d %H:%i:%s') &gt;= #{startDate}
        AND str_to_date(tr.patroldevice_date, '%Y-%m-%d %H:%i:%s') &lt;= #{endDate}

        ) t
        GROUP BY
        date
        ORDER BY
        date)

        UNION

        (SELECT
        date,
        NULL count1,
        NULL sum1,
        SUM(device_sumnum) count2,
        SUM(type*device_sumnum) sum2,
        NULL count3,
        NULL sum3,
        NULL count4,
        NULL sum4,
        NULL count5,
        NULL sum5
        FROM
        (
        SELECT
        str_to_date( start_time, '%Y-%m-%d' ) date,
        device_sumnum,
        CASE

        WHEN examine_flag = '1' THEN
        1 ELSE 0
        END type
        FROM
        iis_task_todo
        WHERE
        task_state != '0' AND start_time IS NOT NULL
        <if test="stationCode != null and stationCode != ''">
            AND station_code=#{stationCode}
        </if>

        AND str_to_date(start_time, '%Y-%m-%d %H:%i:%s') &gt;= #{startDate}
        AND str_to_date(start_time, '%Y-%m-%d %H:%i:%s') &lt;= #{endDate}

        ) t
        GROUP BY
        date
        ORDER BY
        date)

        UNION

        (SELECT
        date,
        NULL count1,
        NULL sum1,
        NULL count2,
        NULL sum2,
        COUNT(type) count3,
        SUM(type) sum3,
        NULL count4,
        NULL sum4,
        NULL count5,
        NULL sum5
        FROM
        (
        SELECT
        str_to_date( al.alarm_date, '%Y-%m-%d' ) date,
        CASE

        WHEN tr.revise_value IS NULL THEN
        1 ELSE 0
        END type
        FROM
        iis_alarm_log al
        LEFT JOIN iis_task_result tr ON al.task_result_id = tr.result_id
        WHERE
        al.check_flag = '1'
        <if test="stationCode != null and stationCode != ''">
            AND al.station_code=#{stationCode}
        </if>

        AND str_to_date(al.alarm_date, '%Y-%m-%d %H:%i:%s') &gt;= #{startDate}
        AND str_to_date(al.alarm_date, '%Y-%m-%d %H:%i:%s') &lt;= #{endDate}

        ) a
        GROUP BY
        a.date
        ORDER BY
        date)

        UNION

        (SELECT
        date,
        NULL count1,
        NULL sum1,
        NULL count2,
        NULL sum2,
        NULL count3,
        NULL sum3,
        COUNT(type) count4,
        SUM(type) sum4,
        NULL count5,
        NULL sum5
        FROM
        (
        SELECT
        str_to_date( al.alarm_date, '%Y-%m-%d' ) date,
        CASE

        WHEN al.check_flag = '1' THEN
        1 ELSE 0
        END type
        FROM
        iis_alarm_log al
        WHERE
        1=1
        <if test="stationCode != null and stationCode != ''">AND al.station_code=#{stationCode}
        </if>


        AND str_to_date(al.alarm_date, '%Y-%m-%d %H:%i:%s') &gt;= #{startDate}
        AND str_to_date(al.alarm_date, '%Y-%m-%d %H:%i:%s') &lt;= #{endDate}

        ) a
        GROUP BY
        date
        ORDER BY
        date)

        UNION

        (SELECT
        date,
        NULL count1,
        NULL sum1,
        NULL count2,
        NULL sum2,
        NULL count3,
        NULL sum3,
        NULL count4,
        NULL sum4,
        COUNT(type) count5,
        SUM(type) sum5
        FROM
        (
        SELECT
        str_to_date( start_time, '%Y-%m-%d' ) date,
        CASE

        WHEN ( task_state = '1' OR task_state = '4' ) THEN
        1 ELSE 0
        END type
        FROM
        iis_task_todo
        WHERE
        task_state != '0' AND start_time IS NOT NULL
        <if test="stationCode != null and stationCode != ''">
            AND station_code=#{stationCode}
        </if>
        AND str_to_date(start_time, '%Y-%m-%d %H:%i:%s') &gt;= #{startDate}
        AND str_to_date(start_time, '%Y-%m-%d %H:%i:%s') &lt;= #{endDate}
        ) t
        GROUP BY
        date
        ORDER BY
        date)) stat
        GROUP BY stat.date
        ORDER BY stat.date) e
        GROUP BY
        <if test="type ==1">
            e.date
        </if>
        <if test="type ==2">
            e.weekday
        </if>
        <if test="type ==3">
            e.moon
        </if>
        ) y LEFT JOIN
        (SELECT
        t3.stattime,
        t3.num,
        t3.duration,
        t4.count,
        t4.type
        FROM
        ((
        SELECT
        count(*) num,
        SUM(
        TIMESTAMPDIFF( SECOND, start_time, end_time )) duration,
        <if test="type ==1">
            DATE_FORMAT( start_time, '%Y-%m-%d' ) stattime
        </if>
        <if test="type ==2">
            CONCAT( DATE_FORMAT( start_time, '%Y-%m' ), '第', FLOOR(( DAY ( start_time ) - 1 ) / 7 ) + 1, '周' ) stattime
        </if>
        <if test="type ==3">
            DATE_FORMAT( start_time, '%Y-%m月' ) stattime
        </if>

        FROM
        `iis_task_todo`
        WHERE
        task_state = '1'
        <if test="startDate != null and startDate != ''">
            AND start_time &gt;= #{startDate}
        </if>
        <if test="endDate != null and endDate != ''">
            AND end_time &lt;= #{endDate}
        </if>
        GROUP BY
        stattime ORDER BY stattime
        ) t3
        LEFT JOIN (
        SELECT
        SUM( count ) count,
        count(*) type,
        stattime
        FROM
        (
        SELECT
        count(*) count,
        <if test="type ==1">
            DATE_FORMAT( tt.start_time, '%Y-%m-%d' ) stattime,
        </if>
        <if test="type ==2">
            CONCAT( DATE_FORMAT( tt.start_time, '%Y-%m' ), '第', FLOOR(( DAY ( start_time ) - 1 ) / 7 ) + 1, '周' )
            stattime,
        </if>
        <if test="type ==3">
            DATE_FORMAT( tt.start_time, '%Y-%m月' ) stattime,
        </if>
        tr.recognition_type
        FROM
        iis_task_result tr
        INNER JOIN iis_task_todo tt ON tr.task_todo_id = tt.task_todo_id
        WHERE
        tr.valid = '2'
        AND tt.task_state = '1'
        <if test="startDate != null and startDate != ''">
            AND tt.start_time &gt;= #{startDate}
        </if>
        <if test="endDate != null and endDate != ''">
            AND tt.end_time &lt;= #{endDate}
        </if>
        GROUP BY
        tr.recognition_type,
        stattime
        ) t1
        GROUP BY
        t1.stattime
        ) t4 ON t3.stattime = t4.stattime)) z ON y.date=z.stattime)
    </select>

    <!--根据任务id查询未审核告警和数量-->
    <select id="getNotCheckAlarmInfo" resultType="java.util.Map">
        SELECT
        al.*
        FROM
        iis_task_result tr
        INNER JOIN iis_task_todo tt ON tr.task_todo_id = tt.task_todo_id
        INNER JOIN iis_alarm_log al ON tr.result_id = al.task_result_id
        WHERE al.check_flag = 0 AND tt.task_todo_id IN
        <foreach collection="taskTodoIds.split(',')" item="id" open="(" close=")" separator=",">
            #{id}
        </foreach>
        ORDER BY al.alarm_date
    </select>
    <select id="getPatrolDeviceInspectionStat" resultType="java.util.Map">
        SELECT
        a.patroldevice_code,
        count( a.sdate ) count,
        TIMESTAMPDIFF(
        DAY,
        a.production_date,
        NOW()) production_count,
        a.production_date,
        a.patroldevice_name,
        a.type,
        a.`online`
        FROM
        (
        SELECT
        sp.patroldevice_code,
        str_to_date( tr.patroldevice_date, '%Y-%m-%d' ) sdate,
        sp.production_date production_date,
        sp.patroldevice_name patroldevice_name,
        sp.type type,
        sp.`online` `online`

        FROM
        iis_task_result tr
        LEFT JOIN iis_substation_patroldevice sp ON tr.patroldevice_code = sp.patroldevice_code
        WHERE
        tr.flag IN ( '2', '3', '4', '5' )
        AND tr.patroldevice_date &gt;= sp.production_date
        AND tr.patroldevice_date &lt;= NOW()
        GROUP BY
        sp.patroldevice_code,
        str_to_date( tr.patroldevice_date, '%Y-%m-%d' )
        ) a
        GROUP BY
        a.patroldevice_code
    </select>
    <select id="getNonHomologousAnalysis" resultType="java.util.Map">
        SELECT
        *
        FROM
        iis_task_result
        ORDER BY
        task_code,
        device_name,
        order_num
        WHERE 1=1
        <if test="taskName != null and taskName != ''">
            AND task_name  #{taskName}
        </if>
        <if test="endDate != null and endDate != ''">
            AND tt.end_time &lt;= #{endDate}
        </if>
        <if test="startDate != null and startDate != ''">
            AND tt.start_time &gt;= #{startDate}
        </if>
        <if test="endDate != null and endDate != ''">
            AND tt.end_time &lt;= #{endDate}
        </if>
        <if test="startDate != null and startDate != ''">
            AND tt.start_time &gt;= #{startDate}
        </if>
        <if test="endDate != null and endDate != ''">
            AND tt.end_time &lt;= #{endDate}
        </if>
    </select>
    <!--     String recognitionType, String startFormat, String endFormat-->
    <select id="getNonCoherentAnalysis" resultType="java.util.Map">
        SELECT
        tr.*,
        IFNULL(sd.meter_type,'') meter_type,
        sd.phase,
        tt.start_time
        FROM
        iis_task_result tr
        INNER JOIN iis_substation_device sd ON tr.device_id = sd.device_id
        INNER JOIN iis_task_todo tt ON tt.task_todo_id= tr.task_todo_id
        WHERE
        tr.flag = '2'
        AND tr.recognition_type ='1'
        AND sd.phase IS NOT NULL
        AND sd.phase != ''
        AND tt.start_time IS NOT NULL
        AND tt.start_time != ''
        <if test="taskTodoId != null and taskTodoId != ''">
            AND tr.task_todo_id = #{taskTodoId}
        </if>
        <if test="stationCode != null and stationCode != ''">
            AND tr.station_code = #{stationCode}
        </if>
        <if test="taskName != null and taskName != ''">
            AND tr.task_name like CONCAT('%',#{taskName},'%')
        </if>
        <if test="mainDeviceName != null and mainDeviceName != ''">
            AND tr.main_device_name like CONCAT('%',#{mainDeviceName},'%')
        </if>
        <if test="componentName != null and componentName != ''">
            AND tr.component_name like CONCAT('%',#{componentName},'%')
        </if>
        <if test="recognitionType != null and recognitionType != ''">
            AND tr.recognition_type = #{recognitionType}
        </if>
        <if test="startDate != null and startDate != ''">
            AND tr.patroldevice_date &gt;= #{startDate}
        </if>
        <if test="endDate != null and endDate != ''">
            AND tr.patroldevice_date &lt;= #{endDate}
        </if>
    </select>
    <select id="getAbnormalResult" resultType="com.yfd.platform.modules.patroltask.domain.TaskResult">
         SELECT
        t.*
        FROM
        ( SELECT *, CASE WHEN revise_valid = '0' THEN '1' WHEN revise_valid = '1' THEN '2' ELSE valid END AS valid1 FROM
        iis_task_result ) t
        INNER JOIN iis_task_todo tt ON tt.task_todo_id = t.task_todo_id
        WHERE
        t.datastatus = '1'
        <if test="taskTodoId != null and taskTodoId != ''">
            AND t.task_todo_id=#{taskTodoId}
        </if>
        AND task_type != '4'
        AND (t.valid1= '2'
        OR ( t.flag = '4' OR t.flag = '6' ))
    </select>

    <select id="getResultByPatroldevice" resultType="java.util.Map">
        SELECT
        t.*,sd.meter_type AS meterType,sd.patroldevice_json AS patroldeviceJson,sd.label_attri,sd.point_type AS deviceClass,atd.commonLower,atd.commonUpper,atd.seriousLower,atd.seriousUpper,atd.dangerLower,atd.dangerUpper
        FROM
        ( SELECT *, CASE WHEN revise_valid = '0' THEN '1' WHEN revise_valid = '1' THEN '2' ELSE valid END AS valid1 FROM
        iis_task_result ) t
        INNER JOIN iis_substation_device sd ON sd.device_id= t.device_id
        INNER JOIN iis_task_todo td ON td.task_todo_id=t.task_todo_id
        LEFT JOIN (SELECT
        device_id,
        MAX(CASE WHEN (alarm_level = '1' AND decide_rule ='6') THEN base_line_value ELSE NULL END) AS commonLower,
        MAX(CASE WHEN (alarm_level = '1' AND decide_rule ='7') THEN base_line_value ELSE NULL END) AS commonUpper,
        MAX(CASE WHEN (alarm_level = '2' AND decide_rule ='6') THEN base_line_value ELSE NULL END) AS seriousLower,
        MAX(CASE WHEN (alarm_level = '2' AND decide_rule ='7') THEN base_line_value ELSE NULL END) AS seriousUpper,
        MAX(CASE WHEN (alarm_level = '3' AND decide_rule ='6') THEN base_line_value ELSE NULL END) AS dangerLower,
        MAX(CASE WHEN (alarm_level = '3' AND decide_rule ='7') THEN base_line_value ELSE NULL END) AS dangerUpper
        FROM
        iis_alarm_threshold GROUP BY device_id) atd ON atd.device_id = t.device_id
        WHERE
        t.datastatus = '1'
        <if test="taskName != null and taskName != ''">
            AND t.task_name LIKE concat('%',#{taskName},'%')
        </if>
        <if test="valid == 1">
            AND (t.valid1= #{valid}
            AND ( t.flag != '4' AND t.flag != '6' ))
        </if>
        <if test="valid == 2">
            AND (t.valid1= #{valid}
            OR ( t.flag = '4' OR t.flag = '6' ))
        </if>

        <if test="internationalId != null and internationalId != ''">
            AND t.patroldevice_code=#{internationalId}
        </if>
        <if test="mainDeviceId != null and mainDeviceId != ''">
            AND t.main_device_id = #{mainDeviceId}
        </if>
        <if test="recognitionType != null and recognitionType != ''">
            AND t.recognition_type=#{recognitionType}
        </if>
        <if test="isReport != null and isReport != ''">
            AND t.is_report=#{isReport}
        </if>
        <if test="startDate != null and startDate != ''">
            AND t.patroldevice_date &gt;= #{startDate}
        </if>
        <if test="endDate != null and endDate != ''">
            AND t.patroldevice_date &lt;= #{endDate}
        </if>
        ORDER BY
        t.order_num ASC
    </select>
    <select id="getNonCoherentResult" resultType="com.yfd.platform.modules.patroltask.domain.TaskResult">
        SELECT
        a.*
        FROM
        iis_task_result a,iis_substation_device sd,
        ( SELECT phase, max( patroldevice_date ) patroldevice_date FROM iis_task_result WHERE phase IS NOT NULL and phase != '' AND flag = '2' GROUP BY phase ) b

        WHERE
        a.device_id=sd.device_id
        AND a.flag = '2'
        AND a.value != ''
        AND a.value is not null
        AND a.phase = b.phase
        AND a.patroldevice_date = b.patroldevice_date
        <if test="areaId != null and areaId != ''">
            AND a.area_id = #{areaId}
        </if>
        <if test="bayId != null and bayId != ''">
            AND a.bay_id = #{bayId}
        </if>
        <if test="mainDeviceId != null and mainDeviceId != ''">
            AND a.main_device_id = #{mainDeviceId}
        </if>
        <if test="componentId != null and componentId != ''">
            AND a.component_id = #{componentId}
        </if>
        <if test="recognitionTypeList != null and recognitionTypeList != ''">
            AND a.recognition_type = #{recognitionTypeList}
        </if>
        <if test="meterType != null and meterType != ''">
            AND sd.meter_type = #{meterType}
        </if>
        <if test="resultId != null and resultId != ''">
            AND a.result_id != #{resultId}
        </if>
        <if test="taskTodoId != null and taskTodoId != ''">
            AND a.task_todo_id = #{taskTodoId}
        </if>
        <if test="phase != null and phase != ''">
            AND a.phase != #{phase}
        </if>
    </select>
    <select id="getValueByDeviceId" resultType="java.util.Map">
        SELECT * FROM iis_recognition WHERE device_id=#{deviceId}
    </select>
    <select id="getHistoryDevice" resultType="java.util.Map">
        SELECT
            t.*,
        CASE

                WHEN t.flag = '4' THEN
                '识别失败'
                WHEN t.flag = '6' THEN
                '采集失败'
                WHEN t.valid1 = '2' THEN
                '异常' -- 或者你可以设置一个默认值
                WHEN t.valid1 = '1' THEN
                '正常' -- 或者你可以设置一个默认值
            ELSE NULL
            END AS analysisResult
        FROM
            ( SELECT *, CASE WHEN revise_valid = '0' THEN '1' WHEN revise_valid = '1' THEN '2' ELSE valid END AS valid1 FROM iis_task_result ) t
        WHERE
            t.datastatus = '1'
            AND t.flag != '0'
        <if test="deviceId != null and deviceId != ''">
            AND t.device_id = #{deviceId}
        </if>
        <if test="startDate != null and startDate != ''">
            AND t.patroldevice_date &gt;= #{startDate}
        </if>
        <if test="endDate != null and endDate != ''">
            AND t.patroldevice_date &lt;= #{endDate}
        </if>
        ORDER BY t.time DESC
    </select>
    <select id="getResultCurveData" resultType="com.yfd.platform.modules.patroltask.domain.TaskResult">
        SELECT
        device_id,
        value,
        patroldevice_date
        FROM (
        SELECT
        *,
        ROW_NUMBER() OVER (
        PARTITION BY device_id
        ORDER BY patroldevice_date DESC
        ) AS row_num
        FROM iis_task_result
        WHERE flag='2' AND device_id  IN
        <foreach collection="deviceIdList" item="deviceId" open="(" separator=","
                 close=")">
            #{deviceId}
        </foreach>

        ) AS ranked
        WHERE row_num &lt;= 60
        ORDER BY device_id, patroldevice_date DESC;

    </select>
</mapper>
