<?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.statistics.mapper.StatisticsPatrolResultMapper">

    <resultMap type="com.inspect.statistics.domain.StatisticsPatrolResult" id="PatrolResult">
        <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="checkResult"    column="check_result"    />
        <result property="patrolStatus"    column="patrol_status"    />
        <result property="dataType"    column="data_type"    />
        <result property="materialId"    column="material_id"    />
        <result property="productionId"    column="productionId"    />
        <result property="patrolStatusName"    column="patrolStatusName"    />
        <result property="purpose"    column="purpose"    />
    </resultMap>

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

    <sql id="selectPatrolResultJoin">
        select result.*,point.device_id productionId,point.purpose purpose from basedata_patrolpoint point
        left join patrol_result result
        on point.patrol_point_id  = result.device_id

    </sql>

    <sql id="selectPatrolResultJoinExport1">
        select result.line_id,result.patrol_device_name,result.patrol_device_code,result.task_name,result.task_code,result.device_name,result.device_id,result.value_type,result.material_id,
        case result.data_type when 1 then '摄像机' when 2 then '机器人' when 3 then '无人机' when 4 then '声纹' when 5 then '在线监测'else data_type end data_type,
        result.value,result.value_unit,result.unit,result.time,result.check_result,
        case result.recognition_type when 1 then '表计读取' when 2 then '位置状态识别' when 4 then '红外测温' end recognition_type,
        case result.file_type when 1 then '红外图谱' when 2 then ' 可见光照片' when 3 then '音频' when 4 then '视频' else file_type end file_type,
        result.file_path,result.rectangle,result.task_patrolled_id,
        case result.valid when 0 then '失败' when 1 then '成功' when 2 then '判别异常' end valid,
        case result.patrol_status when 0 then '未审核' when 1 then '已审核' end patrolStatusName,
        result.patrol_status patrol_status
        from basedata_patrolpoint point
        left join patrol_result result
        on point.patrol_point_id  = result.device_id

    </sql>

    <sql id="selectPatrolResultJoinExport">
        select result.line_id,result.patrol_device_name,result.patrol_device_code,result.task_name,result.task_code,result.device_name,result.device_id,result.value_type,result.material_id,
               result.data_type data_type,
               result.value,result.value_unit,result.unit,result.time,result.check_result,
               result.recognition_type recognition_type,
               result.file_type file_type,
               result.file_path,result.rectangle,result.task_patrolled_id,
               result.valid valid,
               result.patrol_status patrolStatusName,
               result.patrol_status patrol_status
        from basedata_patrolpoint point
                 left join patrol_result result
                           on point.patrol_point_id  = result.device_id

    </sql>

    <select id="queryPatrolResult"  resultMap="PatrolResult">
        <include refid="selectPatrolResultJoin"/>
        <where>
            <if test="deviceId != null "> and point.device_id = #{deviceId}</if>
            <if test="recognitionType != null  and recognitionType != ''"> and result.recognition_type = #{recognitionType}</if>
            <if test="startTime != null and endTime != null "> and result.time between #{startTime} and #{endTime}</if>

        </where>
        order by time desc
    </select>

    <select id="queryPatrolResultMulti"  resultMap="PatrolResult">
        <include refid="selectPatrolResultJoin"/>
        <where>
            <if test="deviceIds != null "> and find_in_set(point.device_id, #{deviceIds})</if>
            <if test="recognitionTypes != null and recognitionTypes.size() > 0">
                and result.recognition_type in
                <foreach collection="recognitionTypes" item="item" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
            <if test="startTime != null and endTime != null "> and result.time between #{startTime} and #{endTime}</if>
            <if test="deviceName != null  and deviceName != ''"> and device_name like concat('%', #{deviceName}, '%')</if>
        </where>
        order by time asc
    </select>


    <select id="queryPatrolResultMulti2"  resultMap="PatrolResult">
        select
            result.line_id,
            result.patrol_device_name,
            result.patrol_device_code,
            result.task_name,
            result.task_code,
            result.device_name,
            result.device_id,
            result.value_type,
            case when (analysis.res_value is null or analysis.res_value = '') then 0 else analysis.res_value end as value,
            result.value_unit,
            result.unit,
            result.time,
            analysis.alg_type as recognition_type,
            result.file_type,
            result.file_path,
            result.rectangle,
            result.task_patrolled_id,
            result.valid,
            result.patrol_status,
            result.check_person,
            result.check_time,
            result.check_result,
            result.material_id,
            result.data_type,
            result.threshold,
            result.task_id,
            result.main_id,
            result.create_time,
            point.device_id productionId,
            point.purpose purpose
        from basedata_patrolpoint point
        left join result_analysis analysis on point.patrol_point_id  = analysis.objectId
        left join patrol_result result on result.line_id = analysis.business_id and analysis.objectId = result.device_id and analysis.task_patrol_id = result.task_patrolled_id
        <where>
            analysis.filter = '0' and analysis.res_code = '2000' and analysis.result_type = '1'
            <if test="deviceIds != null "> and find_in_set(point.device_id, #{deviceIds})</if>
            <if test="recognitionTypes != null and recognitionTypes.size() > 0">
                and analysis.alg_type in
                <foreach collection="recognitionTypes" item="item" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
            <if test="startTime != null and endTime != null "> and analysis.create_time between #{startTime} and #{endTime}</if>
            <if test="deviceName != null  and deviceName != ''"> and device_name like concat('%', #{deviceName}, '%')</if>
        </where>
        order by analysis.create_time asc
    </select>

    <select id="queryPatrolResultMultiExport"  resultMap="PatrolResult">
        <include refid="selectPatrolResultJoinExport"/>
        <where>
            <if test="deviceIds != null "> and find_in_set(point.device_id, #{deviceIds})</if>
            <if test="recognitionType != null  and recognitionType != ''"> and result.recognition_type = #{recognitionType}</if>
            <if test="startTime != null and endTime != null "> and result.time between #{startTime} and #{endTime}</if>
            <if test="deviceName != null  and deviceName != ''"> and device_name like concat('%', #{deviceName}, '%')</if>
        </where>
        order by time desc
    </select>

    <select id="queryPatrolResultCharts"  resultMap="PatrolResult">
        <include refid="selectPatrolResultJoin"/>
        <where>
            <if test="deviceId != null "> and point.device_id = #{deviceId}</if>
            <if test="recognitionType != null  and recognitionType != ''"> and result.recognition_type = #{recognitionType}</if>
            <if test="startTime != null and endTime != null "> and result.time between #{startTime} and #{endTime}</if>
            <if test="deviceName != null  and deviceName != ''"> and device_name like concat('%', #{deviceName}, '%')</if>
        </where>
        order by time
    </select>

    <select id="queryNumTypeInfo" resultMap="PatrolResult">
        <include refid="selectPatrolResultJoin"/>
        where
        recognition_type = 1
        <if test="deviceId != null "> and point.device_id = #{deviceId}</if>
        <if test="patrolDeviceCode != null "> and result.patrol_device_code = #{patrolDeviceCode}</if>
        order by time desc
        limit #{num}
    </select>

    <select id="queryResultByIds" resultMap="PatrolResult">
        <include refid="selectPatrolResultParam"/>
        where
        recognition_type = #{type}
        and  FIND_IN_SET(line_id,#{lineIds})

    </select>


</mapper>