<?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.partrolresult.mapper.PatrolResultMapper">

    <resultMap type="PatrolResult" id="PatrolResultResult">
        <result property="lineId" column="line_id"/>
        <result property="patrolDeviceName" column="patrol_device_name"/>
        <result property="patrolDeviceCode" column="patrol_device_code"/>
        <result property="taskName" column="task_name"/>
        <result property="taskCode" column="task_code"/>
        <result property="deviceName" column="device_name"/>
        <result property="deviceId" column="device_id"/>
        <result property="valueType" column="value_type"/>
        <result property="value" column="value"/>
        <result property="valueUnit" column="value_unit"/>
        <result property="unit" column="unit"/>
        <result property="time" column="time"/>
        <result property="recognitionType" column="recognition_type"/>
        <result property="fileType" column="file_type"/>
        <result property="filePath" column="file_path"/>
        <result property="rectangle" column="rectangle"/>
        <result property="taskPatrolledId" column="task_patrolled_id"/>
        <result property="valid" column="valid"/>
        <result property="patrolStatus" column="patrol_status"/>
        <result property="checkPerson" column="check_person"/>
        <result property="checkTime" column="check_time"/>
        <result property="checkResult" column="check_result"/>
        <result property="materialId" column="material_id"/>
        <result property="dataType" column="data_type"/>
        <result property="threshold" column="threshold"/>
        <result property="taskId" column="task_id"/>
        <result property="mainId" column="main_id"/>
        <result property="createTime" column="create_time"/>
    </resultMap>

    <sql id="selectPatrolResultVo">
        select line_id,
               main_id,
               check_result,
               create_time,
               task_id,
               patrol_device_name,
               patrol_status,
               check_person,
               check_time,
               patrol_device_code,
               task_name,
               task_code,
               device_name,
               device_id,
               value_type,
               value,
               value_unit,
               unit, time, recognition_type, file_type, file_path, rectangle, task_patrolled_id, valid, material_id, data_type, threshold
        from patrol_result
    </sql>

    <select id="selectPatrolResultList" parameterType="PatrolResult" resultMap="PatrolResultResult">
        <include refid="selectPatrolResultVo"/>
        <where>
            <if test="patrolDeviceName != null  and patrolDeviceName != ''">and patrol_device_name like concat('%',
                #{patrolDeviceName}, '%')
            </if>
            <if test="patrolDeviceCode != null  and patrolDeviceCode != ''">and patrol_device_code =
                #{patrolDeviceCode}
            </if>
            <if test="taskName != null  and taskName != ''">and task_name like concat('%', #{taskName}, '%')</if>
            <if test="lineId != null  and lineId != ''">and line_id = #{lineId}</if>
            <if test="taskCode != null  and taskCode != ''">and task_code = #{taskCode}</if>
            <if test="deviceName != null  and deviceName != ''">and device_name like concat('%', #{deviceName}, '%')
            </if>
            <if test="deviceId != null  and deviceId != ''">and device_id = #{deviceId}</if>
            <if test="valueType != null  and valueType != ''">and value_type = #{valueType}</if>
            <if test="value != null  and value != ''">and value = #{value}</if>
            <if test="valueUnit != null  and valueUnit != ''">and value_unit = #{valueUnit}</if>
            <if test="unit != null  and unit != ''">and unit = #{unit}</if>
            <if test="time != null ">and time = #{time}</if>
            <if test="recognitionType != null  and recognitionType != ''">and recognition_type = #{recognitionType}</if>
            <if test="fileType != null  and fileType != ''">and file_type = #{fileType}</if>
            <if test="filePath != null  and filePath != ''">and file_path = #{filePath}</if>
            <if test="rectangle != null  and rectangle != ''">and rectangle = #{rectangle}</if>
            <if test="taskPatrolledId != null  and taskPatrolledId != ''">and task_patrolled_id = #{taskPatrolledId}
            </if>
            <if test="valid != null  and valid != ''">and valid = #{valid}</if>
            <if test="patrolStatus != null  and patrolStatus != ''">and patrol_status = #{patrolStatus}</if>
            <if test="checkPerson != null  and checkPerson != ''">and check_person = #{checkPerson}</if>
            <if test="checkTime != null  ">and check_time = #{checkTime}</if>
            <if test="checkResult != null  ">and check_result = #{checkResult}</if>
            <if test="materialId != null  and materialId != ''">and material_id = #{materialId}</if>
            <if test="dataType != null  and dataType != ''">and data_type = #{dataType}</if>
            <if test="threshold != null  and threshold != ''">and threshold = #{threshold}</if>
            <if test="taskId != null  and taskId != ''">and task_id = #{taskId}</if>
            <if test="mainId != null  and mainId != ''">and main_id = #{mainId}</if>
            ORDER BY line_id desc
        </where>
    </select>

    <select id="selectPatrolResultByDeviceName" parameterType="String" resultMap="PatrolResultResult">
        select
            line_id,
            device_name,
            device_id
        from patrol_result
        where
            device_name = #{deviceName} order by line_id desc limit 1
    </select>

    <select id="selectPatrolResultListOrderBy" parameterType="PatrolResult" resultMap="PatrolResultResult">
        <include refid="selectPatrolResultVo"/>
        <where>
            <if test="patrolDeviceName != null  and patrolDeviceName != ''">and patrol_device_name like concat('%',
                #{patrolDeviceName}, '%')
            </if>
            <if test="patrolDeviceCode != null  and patrolDeviceCode != ''">and patrol_device_code =
                #{patrolDeviceCode}
            </if>
            <if test="taskName != null  and taskName != ''">and task_name like concat('%', #{taskName}, '%')</if>
            <if test="lineId != null  and lineId != ''">and line_id = #{lineId}</if>
            <if test="taskCode != null  and taskCode != ''">and task_code = #{taskCode}</if>
            <if test="deviceName != null  and deviceName != ''">and device_name like concat('%', #{deviceName}, '%')
            </if>
            <if test="deviceId != null  and deviceId != ''">and device_id = #{deviceId}</if>
            <if test="valueType != null  and valueType != ''">and value_type = #{valueType}</if>
            <if test="value != null  and value != ''">and value = #{value}</if>
            <if test="valueUnit != null  and valueUnit != ''">and value_unit = #{valueUnit}</if>
            <if test="unit != null  and unit != ''">and unit = #{unit}</if>
            <if test="time != null ">and time = #{time}</if>
            <if test="recognitionType != null  and recognitionType != ''">and recognition_type = #{recognitionType}</if>
            <if test="fileType != null  and fileType != ''">and file_type = #{fileType}</if>
            <if test="filePath != null  and filePath != ''">and file_path = #{filePath}</if>
            <if test="rectangle != null  and rectangle != ''">and rectangle = #{rectangle}</if>
            <if test="taskPatrolledId != null  and taskPatrolledId != ''">and task_patrolled_id = #{taskPatrolledId}
            </if>
            <if test="valid != null  and valid != ''">and valid = #{valid}</if>
            <if test="patrolStatus != null  and patrolStatus != ''">and patrol_status = #{patrolStatus}</if>
            <if test="checkPerson != null  and checkPerson != ''">and check_person = #{checkPerson}</if>
            <if test="checkTime != null  ">and check_time = #{checkTime}</if>
            <if test="checkResult != null  ">and check_result = #{checkResult}</if>
            <if test="materialId != null  and materialId != ''">and material_id = #{materialId}</if>
            <if test="dataType != null  and dataType != ''">and data_type = #{dataType}</if>
            <if test="threshold != null  and threshold != ''">and threshold = #{threshold}</if>
            <if test="taskId != null  and taskId != ''">and task_id = #{taskId}</if>
            <if test="mainId != null  and mainId != ''">and main_id = #{mainId}</if>
        </where>
    </select>
    <!--    <resultMap type="PatrolData" id="PatrolDataResult">-->
    <!--        <result property="lineId" column="line_id"/>-->
    <!--        <result property="pointName" column="point_name"/>-->
    <!--        <result property="patrolTime" column="patrol_time"/>-->
    <!--        <result property="pointStatus" column="point_status"/>-->
    <!--        <result property="warnStatus" column="warn_status"/>-->
    <!--        <result property="desc" column="desc"/>-->
    <!--        <result property="img" column="img"/>-->
    <!--        <result property="imgType" column="img_type"/>-->
    <!--        <result property="dataType" column="data_type"/>-->
    <!--        <result property="imgAnalyse" column="img_analysis"/>-->
    <!--    </resultMap>-->
    <resultMap type="PatrolData" id="PatrolDataResult">
        <result property="lineId" column="line_id"/>
        <result property="pointName" column="point_name"/>
        <result property="pointId" column="point_id"/>
        <result property="patrolTime" column="patrol_time"/>
        <result property="pointStatus" column="point_status"/>
        <result property="warnStatus" column="warn_status"/>
        <result property="desc" column="desc"/>
        <result property="img" column="img"/>
        <result property="imgType" column="img_type"/>
        <result property="dataType" column="data_type"/>
        <result property="imgAnalyse" column="img_analysis"/>
        <result property="filter" column="filter"/>
        <result property="requestId" column="request_id"/>
        <result property="resStatus" column="res_status"/>
        <result property="deviceName" column="device_name"/>
        <result property="areaName" column="area_name"/>
        <result property="areaId" column="area_id"/>
        <result property="algType" column="alg_type"/>
        <result property="objectId" column="objectId"/>
        <result property="algName" column="alg_name"/>
        <result property="suggestion" column="suggestion"/>
        <result property="resValue" column="res_value"/>
        <result property="deviceSource" column="device_source"/>
        <result property="channelName" column="channel_name"/>
        <result property="imageNormalUrlPath" column="image_normal_url_path"/>
        <result property="filePath" column="file_path"/>
        <result property="originalValue" column="original_value"/>
        <result property="taskPatrolId" column="task_patrol_id"/>
    </resultMap>

    <select id="selectPatrolDataResultByTaskCode" parameterType="PatrolResult" resultMap="PatrolDataResult">
        select
        a.line_id ,
        a.device_name as point_name,
        a.create_time as patrol_time,
        b.point_status ,
        b.result_type as warn_status,
        b.description as 'desc',
        a.file_path as img,
        a.file_type as img_type,
        a.data_type,
        JSON_VALUE(b.result_content, '$**.resImageUrl') imgAnalyse
        from patrol_result a
        LEFT JOIN result_analysis b on a.line_id=b.business_id and a.task_patrolled_id = b.task_patrol_id
        <where>
            <if test="taskCode != null  and taskCode != ''">and a.task_code = #{taskCode}</if>
            <if test="taskPatrolledId != null  and taskPatrolledId != ''">and a.task_patrolled_id=#{taskPatrolledId}
            </if>
            <if test="dataType != null  and dataType != ''">and a.data_type = #{dataType}</if>
            <if test="recognitionType != null  and recognitionType != ''">and a.recognition_type = #{recognitionType}
            </if>
            <if test="valid != null  and valid != ''">and a.valid = #{valid}</if>
            <if test="patrolStatus != null  and patrolStatus != ''">and a.patrol_status = #{patrolStatus}</if>
        </where>
        ORDER BY line_id desc

    </select>

    <select id="selectPatrolDataResultByTaskCodeV2" parameterType="ResultAnalysis" resultMap="PatrolDataResult">
        select
        b.line_id ,
        a.device_name as point_name,
        a.device_id as point_id,
        a.create_time as patrol_time,
        b.point_status ,
        b.result_type as warn_status,
        b.description as 'desc',
        <if test="filter == 0 ">a.file_path as img,</if>
        <if test="filter == 1 ">b.res_img_url as img,</if>
        <if test="filter == null or filter ==''">
            case when b.filter = 0 then a.file_path else b.res_img_url end as img,
        </if>
        b.image_normal_url_path as image_normal_url_path,
        a.file_path as file_path,
        a.file_type as img_type,
        a.data_type,
        b.filter,
        b.request_id,
        b.res_img_url as img_analysis,
        b.original_value,
        b.res_status,
        d.device_name,
        e.area_name,
        b.alg_type,
        b.objectId,
        b.suggestion,
        b.res_value,
        b.task_patrol_id
        <if test="posType != null  and posType != '' and posType == '0'.toString">
            ,
            g.channel_name,
            g.device_source
        </if>
        <if test="posType != null  and posType != '' and posType != '0'.toString">
            ,
            h.patrol_device_name channel_name,
            h.device_source
        </if>
        from patrol_result a
        LEFT JOIN result_analysis b on a.line_id=b.business_id
        left join basedata_patrolpoint c on a.device_id = c.patrol_point_id
        left join basedata_device d on c.device_id = d.device_id
        left join basedata_area e on e.area_id = d.area_id
        <choose>
            <when test="posType != null  and posType != '' and posType != '0'.toString">
                left join basedata_patrolpoint_preset f on c.patrol_point_id = f.patrol_point_id
            </when>
            <otherwise>
                left join patrol_preset_pos f on c.patrol_point_id = f.patrol_point_id
            </otherwise>
        </choose>
        <if test="posType != null  and posType != '' and posType == '0'.toString">
            left join basedata_eqpbook_channel g on f.channel_id = g.channel_id
        </if>
        <if test="posType != null  and posType != '' and posType != '0'.toString">
            left join basedata_eqpbook h on f.eqp_book_id = h.eqp_book_id
        </if>
        <where>
            <if test="taskPatrolIds != null  and !taskPatrolIds.isEmpty()">
                and a.task_patrolled_id IN
                <foreach collection="taskPatrolIds" item="id" open="(" separator="," close=")">
                    #{id}
                </foreach>
                and b.task_patrol_id IN
                <foreach collection="taskPatrolIds" item="id" open="(" separator="," close=")">
                    #{id}
                </foreach>
            </if>
            <if test="objectId != null  and objectId != ''">and b.objectId=#{objectId}</if>
            <if test="result != null  and result != ''">and b.result = #{result}</if>
            <if test="resultType != null  and resultType != ''">and b.result_type =
                #{resultType}
            </if>
            <if test="filter != null  and filter != ''">
                and b.filter = #{filter}
            </if>
            <if test="resCode != null and resCode!= ''">
                and b.res_code=#{resCode}
            </if>
            <if test="pointStatus != null  and pointStatus != ''">
                and b.point_status = #{pointStatus}
            </if>
            <if test="pointStatusList != null  and pointStatusList != ''">and b.point_status in
                <foreach item="pointStatus" collection="pointStatusList" open="(" separator="," close=")">
                    #{pointStatus}
                </foreach>
            </if>
            <if test="pointName != null  and pointName != ''">and a.device_name like concat('%', #{pointName}, '%')</if>
            <if test="deviceName != null  and deviceName != ''">and d.device_name like concat('%', #{deviceName}, '%')
            </if>
            <if test="posType != null  and posType != '' and posType == '0'.toString">
                <if test="channelName != null  and channelName != ''">and g.channel_name like concat('%',
                    #{channelName}, '%')
                </if>
                <if test="deviceSource != null  and deviceSource != ''">and g.device_source like concat('%',
                    #{deviceSource}, '%')
                </if>
            </if>
            <if test="posType != null  and posType != '' and posType != '0'.toString">
                <if test="channelName != null  and channelName != ''">and h.patrol_device_name like concat('%',
                    #{channelName}, '%')
                </if>
                <if test="deviceSource != null  and deviceSource != ''">and h.device_source like concat('%',
                    #{deviceSource}, '%')
                </if>
            </if>
            <if test="desc != null  and desc != ''">and b.description like concat('%', #{desc}, '%')</if>
        </where>
        <if test="abnormal == 0">ORDER BY d.device_name,a.device_name</if>
        <if test="abnormal == 1">ORDER BY b.point_status</if>
    </select>

    <select id="selectPatrolDataResultByTaskCodeList" parameterType="PatrolResult" resultMap="PatrolDataResult">
        select
        a.line_id,
        a.device_name AS point_name,
        a.create_time AS patrol_time,
        a.valid AS 'desc',
        a.file_path AS img,
        a.file_type AS img_type
        from patrol_result a
        <where>
            <if test="taskCode != null  and taskCode != ''">and a.task_code = #{taskCode}</if>
            <if test="taskPatrolledId != null  and taskPatrolledId != ''">and a.task_patrolled_id=#{taskPatrolledId}
            </if>
        </where>
        ORDER BY line_id desc
    </select>
    <resultMap type="com.inspect.system.base.openDomain.BasedataEqpBookMoMain" id="BasedataEqpBookResult">
        <result property="eqpBookId" column="eqp_book_id"/>
        <result property="stationName" column="station_name"/>
        <result property="stationCode" column="station_code"/>
        <result property="areaId" column="area_id"/>
        <result property="areaName" column="area_name"/>
        <result property="patrolDeviceName" column="patrol_device_name"/>
        <result property="patrolDeviceCode" column="patrol_device_code"/>
        <result property="upSystemDeviceCode" column="up_system_device_code"/>
        <result property="deviceModel" column="device_model"/>
        <result property="manufacturer" column="manufacturer"/>
        <result property="useUnit" column="use_unit"/>
        <result property="deviceSource" column="device_source"/>
        <result property="productionDate" column="production_date"/>
        <result property="productionCode" column="production_code"/>
        <result property="isTransport" column="is_transport"/>
        <result property="useMode" column="use_mode"/>
        <result property="videoMode" column="video_mode"/>
        <result property="place" column="place"/>
        <result property="positionX" column="position_x"/>
        <result property="positionY" column="position_y"/>
        <result property="positionZ" column="position_z"/>
        <result property="type" column="type"/>
        <result property="patrolDeviceInfo" column="patrol_device_info"/>
        <result property="robotsCode" column="robots_code"/>
        <result property="ipAddr" column="ip_addr"/>
        <result property="port" column="port"/>
        <result property="user" column="user"/>
        <result property="password" column="password"/>
        <result property="delFlag" column="del_flag"/>
        <result property="createBy" column="create_by"/>
        <result property="createTime" column="create_time"/>
        <result property="updateBy" column="update_by"/>
        <result property="updateTime" column="update_time"/>
        <result property="mainSystemCode" column="main_system_code"/>
    </resultMap>
    <select id="selectBasedataEqpBookList" parameterType="com.inspect.system.base.openDomain.BasedataEqpBookMoMain"
            resultMap="BasedataEqpBookResult">
        select * from basedata_eqpbook

        <where>
            <if test="stationName != null  and stationName != ''">and station_name like concat('%', #{stationName},
                '%')
            </if>
            <if test="stationCode != null  and stationCode != ''">and station_code = #{stationCode}</if>
            <if test="areaId != null ">and area_id = #{areaId}</if>
            <if test="type != null ">and `type` = #{type}</if>
            <if test="areaName != null  and areaName != ''">and area_name like concat('%', #{areaName}, '%')</if>
            <if test="patrolDeviceName != null  and patrolDeviceName != ''">and patrol_device_name like concat('%',
                #{patrolDeviceName}, '%')
            </if>
            <if test="patrolDeviceCode != null  and patrolDeviceCode != ''">and patrol_device_code =
                #{patrolDeviceCode}
            </if>
            <if test="deviceModel != null  and deviceModel != ''">and device_model = #{deviceModel}</if>
            <if test="manufacturer != null  and manufacturer != ''">and manufacturer = #{manufacturer}</if>
            <if test="useUnit != null  and useUnit != ''">and use_unit = #{useUnit}</if>
            <if test="deviceSource != null  and deviceSource != ''">and device_source = #{deviceSource}</if>
            <if test="productionDate != null  and productionDate != ''">and production_date = #{productionDate}</if>
            <if test="productionCode != null  and productionCode != ''">and production_code = #{productionCode}</if>
            <if test="isTransport != null  and isTransport != ''">and is_transport = #{isTransport}</if>
            <if test="useMode != null  and useMode != ''">and use_mode = #{useMode}</if>
            <if test="videoMode != null  and videoMode != ''">and video_mode = #{videoMode}</if>
            <if test="place != null  and place != ''">and place = #{place}</if>
            <if test="positionX != null  and positionX != ''">and position_x = #{positionX}</if>
            <if test="positionY != null  and positionY != ''">and position_y = #{positionY}</if>
            <if test="positionZ != null  and positionZ != ''">and position_z = #{positionZ}</if>
            <!--             <if test="type != null  and type != ''"> and type in concat('(',#{type},')') </if>-->
            <if test="algId != null  and algId != ''">and type in
                <foreach item="algIda" collection="algId" open="(" separator="," close=")">
                    #{algIda}
                </foreach>

            </if>
            <if test="patrolDeviceInfo != null  and patrolDeviceInfo != ''">and patrol_device_info =
                #{patrolDeviceInfo}
            </if>
            <if test="robotsCode != null  and robotsCode != ''">and robots_code = #{robotsCode}</if>
            <if test="ipAddr != null  and ipAddr != ''">and ip_addr = #{ipAddr}</if>
            <if test="port != null  and port != ''">and port = #{port}</if>
            <if test="user != null  and user != ''">and user = #{user}</if>
            <if test="password != null  and password != ''">and password = #{password}</if>
        </where>
    </select>

    <select id="selectCamera" parameterType="com.inspect.system.base.openDomain.BasedataEqpBookMoMain"
            resultMap="BasedataEqpBookResult">
        SELECT
        a.eqp_book_id,
        a.station_name,
        a.station_code,
        a.area_id,
        a.area_name,
        b.channel_name AS patrol_device_name,
        b.patrol_device_code,
        b.up_system_device_code,
        a.device_model,
        a.manufacturer,
        a.use_unit,
        b.device_source,
        a.production_date,
        a.production_code,
        a.is_transport,
        ifnull(a.use_mode,b.use_mode) as use_mode,
        ifnull(a.video_mode,b.video_mode) as video_mode,
        a.place,
        a.position_x,
        a.position_y,
        a.position_z,
        a.type,
        a.patrol_device_info,
        a.robots_code,
        b.HOST AS ip_addr,
        b.PORT as port,
        b.username AS user,
        b.`password`,
        a.main_system_code
        from basedata_eqpbook a
        left join basedata_eqpbook_channel b on a.eqp_book_id = b.eqp_book_id
        where type in
            <foreach item="algIda" collection="algId" open="(" separator="," close=")">
                #{algIda}
            </foreach>
    </select>


    <select id="selectPatroByAllDev" parameterType="PatrolResult" resultMap="PatrolResultResult">
        select a.*,c.area_name from patrol_result a
        left JOIN basedata_patrolpoint b on a.device_id=b.patrol_point_id
        left join basedata_device c on b.main_device_id = c.device_id
        left JOIN basedata_area d on c.area_id =d.area_id
        left JOIN basedata_devicetype e on e.device_type_id =c.device_type_id
        <where>
            <if test="areaName != null  and areaName != ''">and d.area_name like concat('%',#{areaName},'%')</if>
            <if test="areaType != null  and areaType != ''">and d.area_name like concat('%',#{areaType},'%') and
                area_type ='bayunit'
            </if>
            <if test="deviceName != null  and deviceName != ''">and a.device_name like concat('%',#{deviceName},'%')
            </if>
            <if test="deviceTypeName != null  and deviceTypeName != ''">and e.device_type_name like
                concat('%',#{deviceTypeName},'%')
            </if>
            <if test="deviceId != null  and deviceId != ''">and a.device_id =#{deviceId}</if>
            <if test="mainId != null  and mainId != ''">and a.main_id =#{mainId}</if>
            <if test="taskName != null  and taskName != ''">and a.task_name like concat('%',#{taskName},'%')</if>
        </where>
        ORDER BY line_id desc
    </select>

    <select id="selectPatroByAllDevByMainIds" parameterType="PatrolResult" resultMap="PatrolResultResult">
        select a.*,c.area_name from patrol_result a
        left JOIN basedata_patrolpoint b on a.device_id=b.patrol_point_id
        left join basedata_device c on b.main_device_id = c.device_id
        left JOIN basedata_area d on c.area_id =d.area_id
        left JOIN basedata_devicetype e on e.device_type_id =c.device_type_id
        <where>
            <if test="areaName != null  and areaName != ''">and d.area_name like concat('%',#{areaName},'%')</if>
            <if test="areaType != null  and areaType != ''">and d.area_name like concat('%',#{areaType},'%') and
                area_type ='bayunit'
            </if>
            <if test="deviceName != null  and deviceName != ''">and a.device_name like concat('%',#{deviceName},'%')
            </if>
            <if test="deviceTypeName != null  and deviceTypeName != ''">and e.device_type_name like
                concat('%',#{deviceTypeName},'%')
            </if>
            <if test="deviceId != null  and deviceId != ''">and a.device_id =#{deviceId}</if>
            <if test="mainIds != null and mainIds.size() > 0">
                and a.main_id in
                <foreach collection="mainIds" item="mainId" open="(" separator="," close=")">
                    #{mainId}
                </foreach>
            </if>
            <if test="taskName != null  and taskName != ''">and a.task_name like concat('%',#{taskName},'%')</if>
        </where>
        ORDER BY line_id desc
    </select>


    <resultMap type="AlgInfo" id="AlgInfoResult">
        <result property="algSubtypeName" column="alg_subtype_name"/>
        <result property="algSubtypeCode" column="alg_subtype_code"/>
        <result property="algName" column="alg_name"/>
    </resultMap>
    <select id="selectAlgInfo" parameterType="PatrolResult" resultMap="AlgInfoResult">
        select a.alg_subtype_name,a.alg_subtype_code ,b.alg_name from basedata_alg_subtype a
        left join basedata_alg b on a.alg_id=b.alg_id
        <where>
            <if test="algId != null  and algId != ''">and a.alg_subtype_id in
                <foreach item="algIda" collection="algId" open="(" separator="," close=")">
                    #{algIda}
                </foreach>

            </if>

        </where>
        order by a.alg_subtype_id
    </select>


    <select id="queryPatrolResultByPointID" resultMap="PatrolResultResult">
        select *
        from patrol_result
        where device_id = #{deviceId}
          and time between #{startTime}
          and #{endTime}
    </select>


    <resultMap type="PatrolPresetPosModule" id="PatrolPresetPosResult">
        <result property="presetPosId" column="preset_pos_id"/>
        <result property="presetPosCode" column="preset_pos_code"/>
        <result property="presetPosName" column="preset_pos_name"/>
        <result property="eqpBookId" column="eqp_book_id"/>
        <result property="patrolDeviceCode" column="patrol_device_code"/>
        <result property="patrolPointId" column="patrol_point_id"/>
        <result property="channelId" column="channel_id"/>
        <result property="channelCode" column="channel_code"/>
        <result property="channelType" column="channel_type"/>
        <result property="coordinateType" column="coordinate_type"/>
        <result property="stopTime" column="stop_time"/>
        <result property="channelImg" column="channel_img"/>
        <result property="description" column="description"/>
        <result property="algSubtypeCode" column="alg_subtype_code"/>
        <result property="isEnable" column="is_enable"/>
        <result property="createTime" column="create_time"/>
        <result property="algName" column="alg_name"/>
    </resultMap>
    <select id="selectAlgorithmTypeList" parameterType="String" resultMap="PatrolPresetPosResult">
        select a.alg_subtype_code, c.alg_name
        from patrol_preset_pos a
                 left join basedata_alg_subtype b on a.alg_subtype_code = b.alg_subtype_code
                 INNER join basedata_alg c on b.alg_id = c.alg_id
        where a.patrol_point_id = #{lineId}
          and a.alg_subtype_code is not null
    </select>
    <select id="selectPatrolPresetParam" parameterType="String" resultType="java.util.Map">
        SELECT a.alarm_min_temp, a.alarm_max_temp
        FROM patrol_preset_param a, patrol_preset_pos b
        WHERE a.preset_pos_id = b.preset_pos_id AND b.patrol_point_id = #{lineId}
        limit 1
    </select>
    <select id="selectAlgorithmType" parameterType="String" resultType="java.util.Map">
        select b.alg_subtype_code, c.alg_name, a.alg_subtype_ids
        from basedata_patrolpoint a
                 left join basedata_alg_subtype b on a.alg_subtype_ids = b.alg_subtype_id
                 left join basedata_alg c on b.alg_id = c.alg_id
        where CONVERT(a.patrol_point_id, CHAR) = #{lineId} limit 1
    </select>
    <select id="selectAlgorithmInfo" parameterType="String" resultType="java.util.Map">
        select *
        from basedata_alg_subtype b
                 INNER join basedata_alg c on b.alg_id = c.alg_id

        where b.alg_subtype_code = #{code}
    </select>
    <select id="selectThresholdByPointId" parameterType="String" resultType="java.util.Map">
        select a.*, b.alarm_threshold
        from basedata_patrolpoint a
                 LEFT JOIN basedata_stdpatrol_point b on a.std_parol_point_id = b.std_patrol_point_id
        where a.patrol_point_id = #{lineId}
    </select>
    <select id="selectAlgTypeDataType" parameterType="String" resultType="java.util.Map">
        select *
        from sys_dict_data
        where dict_type = 'defect_type'
          and dict_value = #{type}
          and remark is not null
    </select>
    <select id="selectAlgTypeByCode" parameterType="String" resultType="java.util.Map">
        select a.*
        from basedata_alg a
                 INNER JOIN basedata_alg_subtype b on a.alg_id = b.alg_id
        where b.alg_subtype_code = #{algcode} limit 1
    </select>
    <select id="selectBasedataStation" parameterType="String" resultType="java.util.Map">
        select *
        from basedata_station a
    </select>
    <select id="selectEvnData" parameterType="String" resultType="java.util.Map">
        select *
        from basedata_mont_evndata a
                 INNER JOIN basedata_eqpbook b on a.eqp_book_id = b.eqp_book_id
        where b.patrol_device_code = #{code}
    </select>
    <select id="selectDevInfoById" parameterType="String" resultType="java.util.Map">
        select *
        from basedata_device a
        where a.device_id = #{deviceId}
    </select>
    <select id="selectAreaByAudit" parameterType="String" resultType="java.util.Map">
        select *
        from basedata_area a
        where a.area_id = #{areaId}
          and a.area_type = 'bayunit'
    </select>
    <select id="selectAreaInfo" parameterType="String" resultType="java.util.Map">
        select *
        from basedata_area a
        where a.area_id = #{areaId}
    </select>
    <select id="selectAreaInfoByParentId" parameterType="String" resultType="java.util.Map">
        select *
        from basedata_area a
        where a.parent_id = #{parentId}
    </select>
    <select id="selectPatrolResultByLineId" parameterType="Long" resultMap="PatrolResultResult">
        <include refid="selectPatrolResultVo"/>
        where line_id = #{lineId}
    </select>
    <resultMap type="PatrolResultRef" id="PatrolResultRef">
        <result property="lineId" column="line_id"/>
        <result property="mainId" column="main_id"/>
        <result property="deviceId" column="device_id"/>
        <result property="taskName" column="task_name"/>
        <result property="devType" column="dev_type"/>
        <result property="deviceSource" column="device_source"/>
        <result property="time" column="time"/>
        <result property="value" column="value"/>
        <result property="threshold" column="threshold"/>
        <result property="filePath" column="file_path"/>
        <result property="filter" column="filter"/>
        <result property="description" column="description"/>
        <result property="pointStatus" column="point_status"/>
        <result property="valueUnit" column="value_unit"/>
        <result property="resultContent" column="result_content"/>
        <result property="resultType" column="result_type"/>
        <result property="taskPatrolledId" column="task_patrol_id"/>
        <result property="algType" column="alg_type"/>
        <result property="algName" column="alg_name"/>
    </resultMap>
    <select id="selectResultRefByLineId" parameterType="String" resultMap="PatrolResultRef">
        select a.line_id,
               a.device_id,
               c.dev_type,
               a.time,
               a.value,
               a.threshold,
               a.file_path,
               b.description,
               b.point_status,
               a.value_unit,
               b.result_content
        from patrol_result a
                 left join result_analysis b on a.line_id = b.business_id
                 left join patrol_task_info c on a.task_code = c.task_major_id
            and a.device_id = c.device_id
            and a.patrol_device_code = c.dev_no
        where a.main_id = #{mainId}
    </select>

    <select id="selectResultRefByLineIds" resultMap="PatrolResultRef">
        select a.line_id,
               a.main_id,
               a.device_id,
               a.task_name,
               c.dev_type,
               COALESCE(e.device_source, h.device_source) as device_source,
               a.time,
               b.res_value value,
               a.threshold,
               a.file_path,
               b.filter,
               b.description,
               b.point_status,
               b.result_type,
               b.task_patrol_id,
               a.value_unit,
               a.unit,
               b.result_content,
               b.alg_type,
               d.alg_subtype_name as alg_name
        from patrol_result a
                 left join result_analysis b on a.line_id = b.business_id and a.task_patrolled_id = b.task_patrol_id and a.device_id = b.objectId
                 left join basedata_alg_subtype d on b.alg_type = d.alg_subtype_code
                 left join patrol_task_info c on a.task_code = c.task_major_id
            and a.device_id = c.device_id
            and a.patrol_device_code = c.dev_no
        left join patrol_preset_pos f1 on c.device_id = f1.patrol_point_id and c.dev_type not in ('0', '1')
        left join basedata_eqpbook_channel e on f1.channel_id = e.channel_id and c.dev_type not in ('0', '1')
        left join basedata_patrolpoint_preset f2 on c.device_id = f2.patrol_point_id and c.dev_type in ('0', '1')
        left join basedata_eqpbook h on f2.eqp_book_id = h.eqp_book_id and c.dev_type in ('0', '1')
        where
            a.main_id in
        <foreach collection="mainIds" item="item" index="index" open="(" separator="," close=")">
            #{item}
        </foreach>
            and b.point_status != '3'
        <if test="filter != null ">and b.filter = #{filter}</if>
    </select>
    <select id="selectBaseInfoByPointId" parameterType="String" resultType="java.util.Map">
        select a.patrol_point_name, b.device_name, c.device_name as main_name, d.area_name as jg_name, e.area_name
        from basedata_patrolpoint a
                 left JOIN basedata_device b on a.device_id = b.device_id and b.power_main_equ_flag = "no"
                 left JOIN basedata_device c on b.parent_id = c.device_id
                 LEFT JOIN basedata_area d on c.area_id = d.area_id and d.area_type = 'bayunit'
                 LEFT JOIN basedata_area e on c.area_id = e.area_id
        where a.patrol_point_id = #{lineId}
    </select>

    <resultMap type="BasePointAreaInfo" id="BasePointAreaInfo">
        <result property="deviceId" column="device_id"/>
        <result property="patrolPointId" column="patrol_point_id"/>
        <result property="patrolPointName" column="patrol_point_name"/>
        <result property="deviceName" column="device_name"/>
        <result property="mainName" column="main_name"/>
        <result property="jgName" column="jg_name"/>
        <result property="areaName" column="area_name"/>
    </resultMap>
    <select id="selectBaseInfoByMainId" parameterType="String" resultMap="BasePointAreaInfo">
        select f.device_id,
               a.patrol_point_id,
               a.patrol_point_name,
               b.device_name,
               c.device_name as main_name,
               d.area_name   as jg_name,
               e.area_name
        from patrol_result f
                 left join basedata_patrolpoint a
                           on f.device_id = a.patrol_point_id OR f.device_name = a.patrol_point_name
                 left join basedata_device b on a.device_id = b.device_id and b.power_main_equ_flag = "no"
                 left join basedata_device c on b.parent_id = c.device_id
                 left join basedata_area d on c.area_id = d.area_id and d.area_type = 'bayunit'
                 left join basedata_area e on c.area_id = e.area_id
        where f.main_id = #{mainId}
    </select>
    <select id="selectBaseInfoByMainIds" resultMap="BasePointAreaInfo">
        select
            f.device_id,
            a.patrol_point_id,
            a.patrol_point_name,
            b.device_name,
            c.device_name as main_name,
            d.area_name as jg_name,
            e.area_name
        from patrol_result f
            left join basedata_patrolpoint a on f.device_id = a.patrol_point_id OR f.device_name = a.patrol_point_name
            left join basedata_device b on a.device_id = b.device_id and b.power_main_equ_flag = "no"
            left join basedata_device c on a.main_device_id = c.device_id
            left join basedata_area d on a.area_id = d.area_id and d.area_type = 'bayunit'
            left join basedata_area e on d.parent_id = e.area_id
        where f.main_id in
        <foreach collection="mainIds" item="item" index="index" open="(" separator="," close=")">
            #{item}
        </foreach>
    </select>
    <select id="selectAlgTypeByAlaName" parameterType="String" resultType="java.util.Map">
        select a.*
        from basedata_alg a
                 INNER JOIN basedata_alg_subtype b on a.alg_id = b.alg_id
        where b.alg_subtype_code = #{type}
    </select>
    <select id="selectBaseByPointId" parameterType="String" resultType="java.util.Map">
        select a.patrol_point_name, b.device_id, c.device_id as bujianId
        from basedata_patrolpoint a
                 left JOIN basedata_device b on a.device_id = b.device_id and b.power_main_equ_flag = "no"
                 left JOIN basedata_device c on c.device_id = b.parent_id

        where a.patrol_point_id = #{lineId}
    </select>

    <insert id="insertPatrolResult" parameterType="PatrolResult" useGeneratedKeys="true" keyProperty="lineId">
        insert into patrol_result
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="patrolDeviceName != null">patrol_device_name,</if>
            <if test="patrolDeviceCode != null">patrol_device_code,</if>
            <if test="taskName != null">task_name,</if>
            <if test="taskCode != null">task_code,</if>
            <if test="deviceName != null">device_name,</if>
            <if test="deviceId != null">device_id,</if>
            <if test="valueType != null">value_type,</if>
            <if test="value != null">value,</if>
            <if test="valueUnit != null">value_unit,</if>
            <if test="unit != null">unit,</if>
            <if test="time != null">time,</if>
            <if test="recognitionType != null">recognition_type,</if>
            <if test="fileType != null">file_type,</if>
            <if test="filePath != null">file_path,</if>
            <if test="rectangle != null">rectangle,</if>
            <if test="taskPatrolledId != null">task_patrolled_id,</if>
            <if test="valid != null">valid,</if>
            <if test="patrolStatus != null">patrol_status,</if>
            <if test="checkPerson != null">check_person,</if>
            <if test="checkTime != null">check_time,</if>
            <if test="checkResult != null">check_result,</if>
            <if test="materialId != null">material_id,</if>
            <if test="dataType != null">data_type,</if>
            <if test="threshold != null">threshold,</if>
            <if test="taskId != null">task_id,</if>
            <if test="mainId != null">main_id,</if>
            <if test="createTime != null">create_time,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="patrolDeviceName != null">#{patrolDeviceName},</if>
            <if test="patrolDeviceCode != null">#{patrolDeviceCode},</if>
            <if test="taskName != null">#{taskName},</if>
            <if test="taskCode != null">#{taskCode},</if>
            <if test="deviceName != null">#{deviceName},</if>
            <if test="deviceId != null">#{deviceId},</if>
            <if test="valueType != null">#{valueType},</if>
            <if test="value != null">#{value},</if>
            <if test="valueUnit != null">#{valueUnit},</if>
            <if test="unit != null">#{unit},</if>
            <if test="time != null">#{time},</if>
            <if test="recognitionType != null">#{recognitionType},</if>
            <if test="fileType != null">#{fileType},</if>
            <if test="filePath != null">#{filePath},</if>
            <if test="rectangle != null">#{rectangle},</if>
            <if test="taskPatrolledId != null">#{taskPatrolledId},</if>
            <if test="valid != null">#{valid},</if>
            <if test="patrolStatus != null">#{patrolStatus},</if>
            <if test="checkPerson != null">#{checkPerson},</if>
            <if test="checkTime != null">#{checkTime},</if>
            <if test="checkResult != null">#{checkResult},</if>
            <if test="materialId != null">#{materialId},</if>
            <if test="dataType != null">#{dataType},</if>
            <if test="threshold != null">#{threshold},</if>
            <if test="taskId != null">#{taskId},</if>
            <if test="mainId != null">#{mainId},</if>
            <if test="createTime != null">#{createTime},</if>
        </trim>
    </insert>

    <update id="updatePatrolResult" parameterType="PatrolResult">
        update patrol_result
        <trim prefix="SET" suffixOverrides=",">
            <if test="patrolDeviceName != null">patrol_device_name = #{patrolDeviceName},</if>
            <if test="patrolDeviceCode != null">patrol_device_code = #{patrolDeviceCode},</if>
            <if test="taskName != null">task_name = #{taskName},</if>
            <if test="taskCode != null">task_code = #{taskCode},</if>
            <if test="deviceName != null">device_name = #{deviceName},</if>
            <if test="deviceId != null">device_id = #{deviceId},</if>
            <if test="valueType != null">value_type = #{valueType},</if>
            <if test="value != null">value = #{value},</if>
            <if test="valueUnit != null">value_unit = #{valueUnit},</if>
            <if test="unit != null">unit = #{unit},</if>
            <if test="time != null">time = #{time},</if>
            <if test="recognitionType != null">recognition_type = #{recognitionType},</if>
            <if test="fileType != null">file_type = #{fileType},</if>
            <if test="filePath != null">file_path = #{filePath},</if>
            <if test="rectangle != null">rectangle = #{rectangle},</if>
            <if test="taskPatrolledId != null">task_patrolled_id = #{taskPatrolledId},</if>
            <if test="valid != null">valid = #{valid},</if>
            <if test="patrolStatus != null">patrol_status = #{patrolStatus},</if>
            <if test="checkPerson != null">check_person = #{checkPerson},</if>
            <if test="checkTime != null">check_time = #{checkTime},</if>
            <if test="checkResult != null">check_result = #{checkResult},</if>
            <if test="materialId != null">material_id = #{materialId},</if>
            <if test="dataType != null">data_type = #{dataType},</if>
            <if test="threshold != null">threshold = #{threshold},</if>
            <if test="taskId != null">task_id = #{taskId},</if>
            <if test="mainId != null">main_id = #{mainId},</if>
        </trim>
        where line_id = #{lineId}
    </update>

    <update id="updatePatrolResultByMainId" parameterType="String">
        update patrol_result
        set patrol_status = '1'
        where main_id = #{mainId}
    </update>
    <delete id="deletePatrolResultByLineId" parameterType="Long">
        delete
        from patrol_result
        where line_id = #{lineId}
    </delete>

    <delete id="deletePatrolResultByLineIds" parameterType="String">
        delete from patrol_result where line_id in
        <foreach item="lineId" collection="array" open="(" separator="," close=")">
            #{lineId}
        </foreach>
    </delete>

    <update id="updatePatrolResultValue" parameterType="PatrolResult">
        update patrol_result
        <trim prefix="SET" suffixOverrides=",">
            <if test="value != null and value != ''">value = #{value},</if>
        </trim>
        where line_id = #{lineId}
    </update>
    <resultMap type="ResultValue" id="ResultValue">
        <result property="areaName" column="area_name"/>
        <result property="deviceName" column="device_name"/>
        <result property="patrolDeviceName" column="patrol_device_name"/>
        <result property="patrolPointName" column="patrol_point_name"/>
        <result property="value" column="value"/>
        <result property="lineId" column="line_id"/>
        <result property="createTime" column="create_time"/>
    </resultMap>

<!--    <select id="selectOilResultValue" parameterType="PatrolResult" resultMap="ResultValue">-->

<!--        SELECT-->
<!--        a.patrol_point_id,-->
<!--        d.area_name,-->
<!--        d.patrol_device_name,-->
<!--        f.patrol_point_name,-->
<!--        CASE-->
<!--        WHEN e.`value` = 'check image' THEN-->
<!--        '0' ELSE e.`value`-->
<!--        END `value`,-->
<!--        h.dict_value line_id,-->
<!--        e.create_time,-->
<!--        a.channel_code-->
<!--        FROM-->
<!--        patrol_preset_pos a-->
<!--        LEFT JOIN patrol_result e ON a.patrol_point_id = e.device_id-->
<!--        RIGHT JOIN (-->
<!--        SELECT-->
<!--        MAX( h.line_id ) line_id-->
<!--        FROM-->
<!--        patrol_result h-->
<!--        <where>-->
<!--            <if test="createTime != null ">-->
<!--                DATE_FORMAT( h.create_time, '%y%m%d' ) = DATE_FORMAT( #{createTime}, '%y%m%d' )-->
<!--            </if>-->
<!--        </where>-->
<!--        GROUP BY-->
<!--        h.device_id-->
<!--        ) i ON e.line_id = i.line_id-->
<!--        RIGHT JOIN ( SELECT g.dict_value, g.dict_label FROM sys_dict_data g WHERE g.dict_type = 'oil_api' ) h ON-->
<!--        h.dict_label = a.patrol_point_id-->
<!--        LEFT JOIN basedata_eqpbook d ON a.eqp_book_id = d.eqp_book_id-->
<!--        LEFT JOIN basedata_patrolpoint f ON a.patrol_point_id = f.patrol_point_id-->
<!--        ORDER BY-->
<!--        d.area_name,-->
<!--        d.patrol_device_name-->
<!--    </select>-->

    <select id="selectOilResultValue" parameterType="PatrolResult" resultMap="ResultValue">
        SELECT
        a.patrol_point_id,
        d.area_name,
        d.patrol_device_name,
        f.patrol_point_name,
        CASE
        WHEN e.value = 'check image' THEN '0'
        ELSE e.value
        END AS `value`,
        h.dict_value AS line_id,
        e.create_time,
        a.channel_code
        FROM patrol_preset_pos a

        INNER JOIN (
        SELECT dict_label, dict_value
        FROM sys_dict_data
        WHERE dict_type = 'oil_api'
        ) h ON h.dict_label = a.patrol_point_id

        LEFT JOIN basedata_eqpbook d
        ON a.eqp_book_id = d.eqp_book_id

        LEFT JOIN basedata_patrolpoint f
        ON a.patrol_point_id = f.patrol_point_id

        LEFT JOIN (
        SELECT *
        FROM (
        SELECT
        *,
        ROW_NUMBER() OVER (PARTITION BY device_id ORDER BY line_id DESC) AS rn
        FROM patrol_result
        <where>
            <if test="createTime != null">
                create_time <![CDATA[>=]]> DATE(#{createTime})
                AND create_time <![CDATA[<]]> DATE_ADD(DATE(#{createTime}), INTERVAL 1 DAY)
            </if>
        </where>
        ) t
        WHERE rn = 1
        ) e ON a.patrol_point_id = e.device_id

        ORDER BY
        d.area_name,
        d.patrol_device_name
    </select>


<!--    <select id="select6FsResultValue" parameterType="PatrolResult" resultMap="ResultValue">-->

<!--        SELECT-->
<!--        a.patrol_point_id,-->
<!--        d.area_name,-->
<!--        d.patrol_device_name,-->
<!--        f.patrol_point_name,-->
<!--        CASE-->
<!--        WHEN e.`value` = 'check image' THEN-->
<!--        '0' ELSE e.`value`-->
<!--        END `value`,-->
<!--        h.dict_value line_id,-->
<!--        e.create_time,-->
<!--        a.channel_code-->
<!--        FROM-->
<!--        patrol_preset_pos a-->
<!--        LEFT JOIN patrol_result e ON a.patrol_point_id = e.device_id-->
<!--        RIGHT JOIN (-->
<!--        SELECT-->
<!--        MAX( h.line_id ) line_id-->
<!--        FROM-->
<!--        patrol_result h-->
<!--        <where>-->
<!--            <if test="createTime != null ">-->
<!--                DATE_FORMAT( h.create_time, '%y%m%d' ) = DATE_FORMAT( #{createTime}, '%y%m%d' )-->
<!--            </if>-->
<!--        </where>-->
<!--        GROUP BY-->
<!--        h.device_id-->
<!--        ) i ON e.line_id = i.line_id-->
<!--        RIGHT JOIN ( SELECT g.dict_value, g.dict_label FROM sys_dict_data g WHERE g.dict_type = 'sf6_api' ) h ON-->
<!--        h.dict_label = a.patrol_point_id-->
<!--        LEFT JOIN basedata_eqpbook d ON a.eqp_book_id = d.eqp_book_id-->
<!--        LEFT JOIN basedata_patrolpoint f ON a.patrol_point_id = f.patrol_point_id-->
<!--        ORDER BY-->
<!--        d.area_name,-->
<!--        d.patrol_device_name-->

<!--    </select>-->
    <select id="select6FsResultValue" parameterType="PatrolResult" resultMap="ResultValue">
        SELECT
        a.patrol_point_id,
        d.area_name,
        d.patrol_device_name,
        f.patrol_point_name,
        CASE
        WHEN e.value = 'check image' THEN '0'
        ELSE e.value
        END AS `value`,
        h.dict_value AS line_id,
        e.create_time,
        a.channel_code
        FROM patrol_preset_pos a

        INNER JOIN (
        SELECT dict_label, dict_value
        FROM sys_dict_data
        WHERE dict_type = 'sf6_api'
        ) h ON h.dict_label = a.patrol_point_id

        LEFT JOIN basedata_eqpbook d
        ON a.eqp_book_id = d.eqp_book_id

        LEFT JOIN basedata_patrolpoint f
        ON a.patrol_point_id = f.patrol_point_id

        LEFT JOIN (
        SELECT *
        FROM (
        SELECT
        *,
        ROW_NUMBER() OVER (PARTITION BY device_id ORDER BY line_id DESC) AS rn
        FROM patrol_result
        <where>
            <if test="createTime != null">
                create_time <![CDATA[>=]]> DATE(#{createTime})
                AND create_time <![CDATA[<]]> DATE_ADD(DATE(#{createTime}), INTERVAL 1 DAY)
            </if>
        </where>
        ) t
        WHERE rn = 1
        ) e ON a.patrol_point_id = e.device_id

        ORDER BY
        d.area_name,
        d.patrol_device_name
    </select>


    <select id="queryNumTypeResult" resultMap="PatrolResultResult">
        select *
        from patrol_result
        where device_id = #{deviceId}
        order by create_time desc
            limit #{num}

    </select>
    <select id="queryDiffTimeResult" resultMap="PatrolResultResult">
        select *
        from patrol_result
        where create_time between date_sub(now(), interval #{time} minute) and now()
          and task_code = #{taskCode}
          and value is not null
          and value != ''
        order by value *1 desc
--         注释：这个*1的写法  是将字符串的数字转为数值排序
    </select>

    <sql id="selectStatisticsTaskConfigVo">
        select id, type, task_code, threshold, msg
        from statistics_task_config
    </sql>
    <resultMap type="com.inspect.statistics.domain.TaskStatisticsConfig" id="StatisticsTaskConfigResult">
        <result property="id" column="id"/>
        <result property="type" column="type"/>
        <result property="taskCode" column="task_code"/>
        <result property="threshold" column="threshold"/>
        <result property="msg" column="msg"/>
    </resultMap>
    <select id="selectStatisticsTaskConfigList" parameterType="java.lang.String" resultMap="StatisticsTaskConfigResult">
        <include refid="selectStatisticsTaskConfigVo"/>
        where task_code = #{taskCode,jdbcType=VARCHAR}
        limit 1
    </select>
    <select id="queryTaskDeviceNum" parameterType="java.lang.String" resultType="java.lang.Integer">
        select count(1) deviceNum
        from patrol_task_info info
                 left join patrol_task task
                           on task.task_id = info.task_major_id
        where task.task_code = #{taskCode}
    </select>
    <select id="queryOneTaskResult" parameterType="com.inspect.partrolresult.domain.PatrolResult"
            resultMap="PatrolResultResult">
        select *
        from patrol_result
        where task_code = #{taskCode}
          and to_days(create_time) = to_days(#{createTime})
    </select>
    <resultMap type="com.inspect.partrolresult.domain.BasedataEqpBookChannel" id="channelResultMap">
        <result property="channelName" column="channel_name"/>
    </resultMap>
    <select id="selectChannelName" parameterType="java.lang.String"
            resultMap="channelResultMap">
        select channel_name channel_name
        from basedata_eqpbook_channel
        where channel_code = #{channelCode}
    </select>

    <resultMap type="com.inspect.partrolresult.domain.InspectionPointResultVo" id="InspectionPointResultMap">
        <result property="id" column="id"/>
<!--        <result property="deviceName" column="device_name"/>-->
        <result property="patroldeviceCode" column="patroldevice_code"/>
        <result property="patroldeviceName" column="patroldevice_name"/>
        <result property="taskCode" column="task_code"/>
        <result property="taskName" column="task_name"/>
        <result property="taskPatrolledId" column="task_patrolled_id"/>
        <result property="deviceId" column="device_id"/>
        <result property="deviceName" column="device_name"/>
        <result property="mainDevId" column="main_dev_id"/>
        <result property="mainDevName" column="main_dev_name"/>
        <result property="componentId" column="component_id"/>
        <result property="componentName" column="component_name"/>
        <result property="value" column="value"/>
        <result property="unit" column="unit"/>
        <result property="recognitionType" column="recognition_type"/>
        <result property="time" column="time"/>
        <result property="fileType" column="file_type"/>
        <result property="filePath" column="file_path"/>
        <result property="baseFilePath" column="base_file_path"/>
        <result property="rectangle" column="rectangle"/>
        <result property="alarmLevel" column="alarm_level"/>
        <result property="alarmType" column="alarm_type"/>
        <result property="content" column="content"/>
        <result property="vaild" column="vaild"/>
        <result property="filter" column="filter"/>
        <result property="resultType" column="result_type"/>

    </resultMap>
    <select id="inspectResult" parameterType="com.inspect.partrolresult.domain.InspectionPointParam" resultMap="InspectionPointResultMap">
        select   pr.line_id  id,
                 bec.channel_code patroldevice_code,
                 bec.channel_name patroldevice_name,
                 pr.task_code,
                 pr.task_name,
                 pr.line_id task_patrolled_id,
                 bp.patrol_point_name device_name,
                 bp.patrol_point_id device_id,
                 bd.device_name main_dev_name,
                 bd.device_id main_dev_id ,
                 bdb.device_name component_name,
                 bdb.device_id component_id ,
                 ra.res_value value ,
                 bp.purpose unit,
                 bas.alg_subtype_id  recognition_type,
                 pr.time,
                 pr.file_type,
                 ra.res_img_url file_path,
                 CASE
                 WHEN ra.image_normal_url_path IS NOT NULL THEN CONCAT('/', ra.image_normal_url_path)
                 ELSE ra.image_normal_url_path
                 END base_file_path,
                 ra.res_pos rectangle,
                 pr.valid valid,
                 ra.filter filter,
                 ra.result_type result_type,
                 ra.description content
        FROM patrol_result pr
        LEFT JOIN basedata_patrolpoint bp on bp.patrol_point_id = pr.device_id
        LEFT JOIN patrol_preset_pos bpp ON bp.patrol_point_id  = bpp.patrol_point_id
        LEFT JOIN basedata_eqpbook_channel bec on bpp.channel_code = bec.channel_code
        LEFT JOIN basedata_device bd on bp.main_device_id = bd.device_id
        LEFT JOIN result_analysis ra on       ra.business_id  =  pr.line_id
        LEFT JOIN basedata_device bdb on bdb.device_id = bp.device_id
        LEFT JOIN basedata_alg_subtype bas  on ra.alg_type = bas.alg_subtype_code
        <where>
            <if test="startTime != null and startTime != ''">        and pr.time  &gt;= CONCAT(#{startTime}, ' 00:00:00')   </if>
            <if test="endTime != null and endTime != ''">          and pr.time  &lt;= CONCAT(#{endTime}, ' 23:59:59')        </if>
            <if test="patroldeviceCode != null and patroldeviceCode != ''"> and bec.channel_code = #{patroldeviceCode}  </if>
            <if test="deviceId != null and deviceId != ''">         and bp.patrol_point_id  = #{deviceId}                           </if>
            <if test="mainDevId != null and mainDevId != ''">         and bd.device_id  = #{mainDevId}                           </if>
            <if test="componentId != null and componentId != ''">      and bdb.device_id = #{componentId}                    </if>
            <if test="taskPatrolledId != null and taskPatrolledId != ''">  and pr.line_id   = #{taskPatrolledId}             </if>
            <if test="filter != null and filter != ''">  and  ra.filter  = #{filter}             </if>
            <if test="resultType != null and resultType != ''">       and ra.result_type  = #{resultType}  and filter = 0               </if>
        </where>
     order by pr.time desc   LIMIT #{pageSize} OFFSET #{offset}
    </select>

    <select id="selectPatrolResultListByMainIds" resultMap="PatrolResultResult">
        <include refid="selectPatrolResultVo"/>
        where main_id in
            <foreach collection="mainIds" item="item" index="index" open="(" separator="," close=")">
                #{item}
            </foreach>
        ORDER BY main_id,create_time
    </select>

    <select id="selectResultDoc" parameterType="PatrolResult" resultMap="PatrolResultRef">
        select
        b.line_id,
        a.device_id,
        c.dev_type,
        a.time,
        a.value,
        a.threshold,
        a.file_path,
        b.description,
        b.point_status,
        b.res_status,
        a.value_unit,
        b.result_content,
        b.res_img_url,
        b.res_value,
        b.res_desc,
        b.alg_type,
        a.device_name,
        a.patrol_device_name,
        a.task_name,
        a.data_type,
        a.patrol_device_code,
        a.task_code,
        a.task_patrolled_id,
        b.original_value
        from patrol_result a
        left join result_analysis b on a.line_id = b.business_id
        left join patrol_task_info c on a.task_code = c.task_major_id
        and a.device_id = c.device_id
        and a.patrol_device_code = c.dev_no
        where b.result = '1' and b.point_status != '1'
        <if test="mainId != null and mainId != ''"> and a.main_id = #{mainId}</if>
        <if test="lineId != null and lineId != ''"> and b.line_id = #{lineId}</if>
    </select>

    <select id="selectBaseInfoByMainIdByReport" parameterType="String" resultMap="BasePointAreaInfo">
        select a.patrol_point_id,a.patrol_point_name, b.device_name, b.device_name as main_name, e.area_name,c.channel_id,c.eqp_book_id
        from patrol_result f
                 left join basedata_patrolpoint a on f.device_id = a.patrol_point_id
                 left join basedata_device b on a.device_id = b.device_id
                 left join basedata_area e on b.area_id = e.area_id
                 LEFT JOIN basedata_patrolpoint_preset c ON a.patrol_point_id = c.patrol_point_id
        where f.main_id = #{mainId}
    </select>

    <select id="selectDeviceNameById" parameterType="BasePointAreaInfo" resultType="java.util.Map">
        SELECT
        <if test="eqpBookId != null  and eqpBookId != ''">a.patrol_device_name,a.device_source </if>
        <if test="channelId != null  and channelId != ''">b.channel_name,b.device_source </if>
        FROM
        <if test="eqpBookId != null  and eqpBookId != ''">basedata_eqpbook a</if>
        <if test="channelId != null  and channelId != ''">basedata_eqpbook_channel b</if>
        WHERE
        <if test="eqpBookId != null  and eqpBookId != ''">a.eqp_book_id = #{eqpbookId}</if>
        <if test="channelId != null  and channelId != ''">b.channel_id = #{channelId}</if>
    </select>

    <select id="selectAlgList" parameterType="String" resultType="String">
        SELECT p.alg_subtype_ids FROM basedata_patrolpoint p
        LEFT JOIN patrol_task_info i ON p.patrol_point_id = i.device_id
        LEFT JOIN basedata_eqpbook e ON i.ebook_id = e.eqp_book_id
        <where>
            <if test="taskCode != null  and taskCode != ''">and i.task_major_id = #{taskCode}</if>
            <if test="sendCode != null  and sendCode != ''">and e.main_system_code = #{sendCode}</if>
            <if test="devType != null  and devType != ''">and i.dev_type = #{devType}</if>
        </where>

    </select>

    <select id="selectPatrolDataResultByTaskCodeV3" parameterType="ResultAnalysis" resultMap="PatrolDataResult">
        SELECT
            b.line_id,
            a.device_name AS point_name,
            a.device_id AS point_id,
            a.create_time AS patrol_time,
            b.point_status,
            b.result_type AS warn_status,
            b.description AS `desc`,
            <if test="filter == 0 ">a.file_path as img,</if>
            <if test="filter == 1 ">b.res_img_url as img,</if>
            <if test="filter == null or filter ==''">
                case when b.filter = 0 then a.file_path else b.res_img_url end as img,
            </if>
            b.image_normal_url_path,
            a.file_path,
            a.file_type AS img_type,
            a.data_type,
            b.filter,
            b.request_id,
            b.res_img_url AS img_analysis,
            b.original_value,
            b.res_status,
            d.device_name,
            e.area_name,
            e.area_id,
            b.alg_type,
            b.objectId,
            b.suggestion,
            b.res_value,
            s.pos_type,
            b.task_patrol_id,
            CASE
                WHEN s.pos_type = '0' THEN g.channel_name
                ELSE h.patrol_device_name
                END AS channel_name,

            CASE
                WHEN s.pos_type = '0' THEN g.device_source
                ELSE h.device_source
                END AS device_source

        FROM patrol_result a
                 INNER JOIN result_analysis b
                            ON a.line_id = b.business_id

                 LEFT JOIN basedata_patrolpoint c
                           ON a.device_id = c.patrol_point_id

                 LEFT JOIN basedata_device d
                           ON c.device_id = d.device_id

                 LEFT JOIN basedata_area e
                           ON e.area_id = d.area_id

                 LEFT JOIN patrol_task_status s
                           ON s.task_patrolled_id = a.task_patrolled_id

                 LEFT JOIN patrol_preset_pos f1
                           ON s.pos_type = '0'
                               AND c.patrol_point_id = f1.patrol_point_id

                 LEFT JOIN basedata_eqpbook_channel g
                           ON f1.channel_id = g.channel_id
                            <if test="channelName != null  and channelName != ''">and g.channel_name like concat('%',
                                #{channelName}, '%')
                            </if>
                            <if test="deviceSource != null  and deviceSource != ''">and g.device_source like concat('%',
                                #{deviceSource}, '%')
                            </if>

                 LEFT JOIN basedata_patrolpoint_preset f2
                           ON s.pos_type != '0'
                               AND c.patrol_point_id = f2.patrol_point_id

                 LEFT JOIN basedata_eqpbook h
                           ON f2.eqp_book_id = h.eqp_book_id
                            <if test="channelName != null  and channelName != ''">and h.patrol_device_name like concat('%',
                                #{channelName}, '%')
                            </if>
                            <if test="deviceSource != null  and deviceSource != ''">and h.device_source like concat('%',
                                #{deviceSource}, '%')
                            </if>

        <where>
            (g.channel_id IS NOT NULL OR h.eqp_book_id IS NOT NULL)
            <if test="taskPatrolIds != null  and !taskPatrolIds.isEmpty()">
                and a.task_patrolled_id IN
                <foreach collection="taskPatrolIds" item="id" open="(" separator="," close=")">
                    #{id}
                </foreach>
                and b.task_patrol_id IN
                <foreach collection="taskPatrolIds" item="id" open="(" separator="," close=")">
                    #{id}
                </foreach>
            </if>
            <if test="pointStatusList != null  and !pointStatusList.isEmpty()">
                and b.point_status IN
                <foreach collection="pointStatusList" item="id" open="(" separator="," close=")">
                    #{id}
                </foreach>
            </if>
            <if test="objectId != null  and objectId != ''">and b.objectId=#{objectId}</if>
            <if test="result != null  and result != ''">and b.result = #{result}</if>
            <if test="resultType != null  and resultType != ''">and b.result_type =
                #{resultType}
            </if>
            <if test="filter != null  and filter != ''">
                and b.filter = #{filter}
            </if>
            <if test="resCode != null and resCode!= ''">
                and b.res_code=#{resCode}
            </if>
            <if test="pointStatus != null  and pointStatus != ''">
                and b.point_status = #{pointStatus}
            </if>
            <if test="pointName != null  and pointName != ''">and a.device_name like concat('%', #{pointName}, '%')</if>
            <if test="deviceName != null  and deviceName != ''">and d.device_name like concat('%', #{deviceName}, '%')
            </if>
        </where>
        <if test="abnormal == 0">ORDER BY d.device_name,a.device_name</if>
        <if test="abnormal == 1">ORDER BY b.point_status</if>
    </select>

    <select id="selectPointAlgInfo" resultMap="AlgInfoResult">
        SELECT a.alg_name, s.alg_subtype_code, s.alg_subtype_name
        FROM basedata_patrolpoint p
                 LEFT JOIN basedata_alg_subtype s ON FIND_IN_SET(s.alg_subtype_id, p.alg_subtype_ids)
                 LEFT JOIN basedata_alg a ON s.alg_id = a.alg_id
        WHERE p.patrol_point_id = #{patrolPointId}
    </select>

    <select id="selectAlgInfoByCode" resultMap="AlgInfoResult">
        SELECT a.alg_name, s.alg_subtype_code, s.alg_subtype_name
        FROM basedata_alg_subtype s
                 LEFT JOIN basedata_alg a ON s.alg_id = a.alg_id
        WHERE FIND_IN_SET(s.alg_subtype_code, #{codes})
    </select>
</mapper>
