<?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.analysis.mapper.ResultAnalysisMapper">

<!--    <resultMap type="ResultAnalysis" id="ResultAnalysisResult">-->
<!--        <result property="lineId" column="line_id"/>-->
<!--        <result property="resultType" column="result_type"/>-->
<!--        <result property="businessId" column="business_id"/>-->
<!--        <result property="resultContent" column="result_content"/>-->
<!--        <result property="resStatus" column="res_status"/>-->
<!--        <result property="pointStatus" column="point_status"/>-->
<!--        <result property="suggestion" column="suggestion"/>-->
<!--        <result property="createTime" column="create_time"/>-->
<!--        <result property="objectId" column="objectId"/>-->
<!--        <result property="description" column="description"/>-->
<!--        <result property="requestId" column="request_id"/>-->
<!--        <result property="createTime" column="create_time"/>-->
<!--    </resultMap>-->

    <resultMap type="ResultAnalysis" id="ResultAnalysisResult">
        <result property="lineId" column="line_id"/>
        <result property="resultType" column="result_type"/>
        <result property="businessId" column="business_id"/>
        <result property="resultContent" column="result_content"/>
        <result property="resStatus" column="res_status"/>
        <result property="pointStatus" column="point_status"/>
        <result property="suggestion" column="suggestion"/>
        <result property="createTime" column="create_time"/>
        <result property="objectId" column="objectId"/>
        <result property="description" column="description"/>
        <result property="requestId" column="request_id"/>
        <result property="createTime" column="create_time"/>
        <result property="resValue" column="res_value"/>
        <result property="algType" column="alg_type"/>
        <result property="filter" column="filter"/>
        <result property="taskPatrolId" column="task_patrol_id"/>
        <result property="resCode" column="res_code"/>
        <result property="resImgUrl" column="res_img_url"/>
        <result property="resConf" column="res_conf"/>
        <result property="resDesc" column="res_desc"/>
        <result property="resPos" column="res_pos"/>
        <result property="imageNormalUrlPath" column="image_normal_url_path"/>
        <result property="csQxNum" column="csQxNum"/>
        <result property="aiQxNum" column="aiQxNum"/>
        <result property="csYcNum" column="csYcNum"/>
        <result property="aiYcNum" column="aiYcNum"/>
    </resultMap>
    <resultMap id="AlgPatrolPointResult" type="com.inspect.analysis.domain.AlgPatrolPoint">
        <result property="patrolPointId" column="patrol_point_id"/>
        <result property="patrolPointName" column="patrol_point_name"/>
    </resultMap>


    <resultMap type="BasedataAlgSubtype" id="BasedataAlgSubtypeResult">
        <result property="algSubtypeId" column="alg_subtype_id"/>
        <result property="algId" column="alg_id"/>
        <result property="algSubtypeCode" column="alg_subtype_code"/>
        <result property="algSubtypeName" column="alg_subtype_name"/>
        <result property="lowerValue" column="lower_value"/>
        <result property="upperValue" column="upper_value"/>
        <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="lowerFlag" column="lower_flag"/>
        <result property="upperFlag" column="upper_flag"/>
    </resultMap>

    <sql id="selectResultAnalysisVo">
        select line_id,
            objectId,
            description,
            create_time,
            request_id,
            result_type,
            point_status,
            suggestion,
            business_id,
            res_status,
            result_content,
            res_value,
            alg_type,
            filter,
            task_patrol_id,
            res_code,
            res_img_url,
            res_conf,
            res_desc,
            res_pos,
            result,
            image_normal_url_path
        from result_analysis
    </sql>

    <select id="selectResultAnalysisList" parameterType="ResultAnalysis" resultMap="ResultAnalysisResult">
        <include refid="selectResultAnalysisVo"/>
        <where>
            <if test="taskPatrolId != null  and taskPatrolId != ''">and task_patrol_id = #{taskPatrolId}</if>
            <if test="resultType != null  and resultType != ''">and result_type = #{resultType}</if>
            <if test="requestId != null  and requestId != ''">and request_id = #{requestId}</if>
            <if test="createTime != null  ">and create_time = #{createTime}</if>
            <if test="businessId != null  and businessId != ''">and business_id = #{businessId}</if>
            <if test="resultContent != null  and resultContent != ''">and result_content like concat
                ('%',#{resultContent},'%')
            </if>
            <if test="resStatus != null  and resStatus != ''">and res_status = #{resStatus}</if>
            <if test="algType != null  and algType != ''">and alg_type = #{algType}</if>
            <if test="filter != null  and filter != ''">and filter = #{filter}</if>
            <if test="pointStatus != null  and pointStatus != ''">and point_status = #{pointStatus}</if>
            <if test="objectId != null  and objectId != ''">and objectId = #{objectId}</if>
            <if test="description != null  and description != ''">and description = #{description}</if>
            <if test="beginTime != null and beginTime != ''">
                and date_format(create_time,'%y%m%d') &gt;= date_format(#{beginTime},'%y%m%d')
            </if>
            <if test="endTime != null and endTime != ''">
                and date_format(create_time,'%y%m%d') &lt;= date_format(#{endTime},'%y%m%d')
            </if>
            <if test="lineId != null  and lineId != ''">
                and line_id = #{lineId}
            </if>
            <if test="taskPatrolIds != null and !taskPatrolIds.isEmpty()">
                and task_patrol_id in
                <foreach item="item" index="index" collection="taskPatrolIds" separator="," open="(" close=")">
                    #{item}
                </foreach>
            </if>
        </where>
        ORDER BY line_id desc
    </select>

    <select id="selectResultAnalysisListByMainId" parameterType="ResultAnalysis" resultMap="ResultAnalysisResult">
        select b.business_id, b.point_status
        from patrol_result a
                 left join result_analysis b on a.line_id = b.business_id
        where a.main_id = #{businessId}
          and b.business_id is not null
    </select>

    <select id="selectResultAnalysisListByMainIdNew" parameterType="ResultAnalysis" resultMap="ResultAnalysisResult">
        select b.business_id,b.point_status,b.res_status,b.alg_type
        from patrol_result a
        left join result_analysis b on a.line_id = b.business_id
        where a.main_id = #{businessId} and b.business_id is not null and b.point_status = '0' and b.res_code = '2000'
        <if test="filter != null ">and b.filter = #{filter}</if>
    </select>

    <select id="selectResultAnalysisListByMainIdsNew" resultMap="ResultAnalysisResult">
        select b.business_id, b.point_status, b.res_status, b.alg_type,b.result_type,b.objectId,b.task_patrol_id
        from patrol_result a
        left join result_analysis b on a.line_id = b.business_id and a.device_id = b.objectId and a.task_patrolled_id = b.task_patrol_id
        where a.main_id in
        <foreach item="item" index="index" collection="lineIds" open="(" separator="," close=")">
            #{item}
        </foreach>
        and b.business_id is not null
        <if test="filter != null ">and b.filter = #{filter}</if>
    </select>

    <select id="selectResultAnalysisListByMainIds" resultMap="ResultAnalysisResult">
        select b.business_id, b.point_status
        from patrol_result a
                 left join result_analysis b on a.line_id = b.business_id
        where a.main_id in
        <foreach item="item" index="index" collection="lineIds" open="(" separator="," close=")">
            #{item}
        </foreach>
          and b.business_id is not null
    </select>

    <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="resultContent" column="result_content"/>
        <result property="devNo" column="dev_no"/>
        <result property="componentName" column="component_name"/>
        <result property="componentId" column="component_id"/>
        <result property="bayName" column="bay_name"/>
        <result property="mainDevId" column="main_dev_id"/>
        <result property="deviceCode" column="device_code"/>
        <result property="areaName" column="area_name"/>
        <result property="phase" column="phase"/>
        <result property="devType" column="devType"/>
        <result property="createTime" column="create_time"/>
        <result property="resStatus" column="res_status"/>
        <result property="pointStatus" column="point_status"/>
        <result property="suggestion" column="suggestion"/>
        <result property="areaId" column="area_id"/>
        <result property="presetPosCode" column="preset_pos_code"/>
        <result property="description" column="description"/>
    </resultMap>
    <select id="selectResultAnd" parameterType="ResultAnalysis" resultMap="PatrolResultResult">
        SELECT
        b.result_content,
        b.create_time,
        b.res_status,
        b.suggestion,
        b.point_status,
        b.description,
        g.area_id,
        g.area_name,
        z.dev_no,
        f.device_name AS device_name,
        e.device_code AS device_code,
        f.device_id AS main_dev_id,
        e.device_name AS component_name,
        e.device_id AS component_id,
        g.area_name AS bay_name,
        f.phy_asset_id AS material_id,
        a.file_type,
        a.file_path,
        a.line_id,
        h.device_type_name AS devType,
        a.device_id,
        h.phase AS phase,
        k.preset_pos_code,
        k.patrol_device_code
        FROM
        patrol_result a
        INNER JOIN result_analysis b ON a.line_id = b.business_id and b.result_type != '1'
        <if test="resStatus != null ">and b.res_status = #{resStatus}</if>
        INNER JOIN patrol_task c ON a.task_code = c.task_code
        LEFT JOIN patrol_task_info z ON a.device_id = z.device_id
        AND c.task_id = z.task_major_id
        LEFT JOIN basedata_patrolpoint d ON a.device_id = d.patrol_point_id
        LEFT JOIN patrol_preset_pos k on d.patrol_point_id = k.patrol_point_id
        LEFT JOIN basedata_device e ON d.device_id = e.device_id
        AND e.power_main_equ_flag = "No"
        LEFT JOIN basedata_device f ON d.device_id = f.device_id
        AND f.power_main_equ_flag = "Yes"
        LEFT JOIN basedata_area g ON e.area_id = g.area_id
        LEFT JOIN basedata_devicetype h ON e.device_type_id = h.device_type_id
        <where>
            <if test="resultType != null  and resultType != ''">and b.result_type = #{resultType}</if>
            <if test="requestId != null  and requestId != ''">and b.request_id = #{requestId}</if>
            <if test="createTime != null  ">and b.create_time = #{createTime}</if>
            <if test="businessId != null  and businessId != ''">and b.business_id = #{businessId}</if>
            <if test="resultContent != null  and resultContent != ''">and b.result_content like concat
                ('%',#{resultContent},'%')
            </if>
            <if test="pointStatus != null  and pointStatus != ''">and b.point_status = #{pointStatus}</if>
            <if test="objectId != null  and objectId != ''">and b.objectId = #{objectId}</if>
            <if test="areaId != null  and areaId != ''">and (g.parent_id = #{areaId} or g.area_id = #{areaId})</if>
            <if test="mainDevId != null  and mainDevId != ''">and e.parent_id = #{mainDevId}</if>
            <if test="lineId != null  and lineId != ''">and a.line_id = #{lineId}</if>
            <if test="beginTime != null and beginTime != ''">
                and date_format(b.create_time,'%y%m%d') &gt;= date_format(#{beginTime},'%y%m%d')
            </if>
            <if test="endTime != null and endTime != ''">
                and date_format(b.create_time,'%y%m%d') &lt;= date_format(#{endTime},'%y%m%d')
            </if>
        </where>
        order by a.line_id desc
    </select>

    <select id="selectAlgorithmInfo" parameterType="String" resultType="java.util.Map">
        select *
        from basedata_alg_subtype
        where alg_subtype_code = #{algSubtypeCode}
    </select>
    <select id="selectDeviceInfo" parameterType="String" resultType="java.util.Map">
        select a.phy_asset_id
        from basedata_device a
                 INNER JOIN basedata_patrolpoint b on a.device_id = b.main_device_id
        where b.patrol_point_id = #{pointiId}
    </select>
    <select id="selectDeviceInfoByBj" parameterType="String" resultType="java.util.Map">
        select a.phy_asset_id
        from basedata_device a
                 INNER JOIN basedata_patrolpoint b on a.device_id = b.device_id
        where b.patrol_point_id = #{pointiId}
    </select>

    <select id="selectPointInfoById" parameterType="String" resultType="java.util.Map">
        select *
        from basedata_patrolpoint
        where CONVERT(patrol_point_id, CHAR) = #{deviceId} limit 1
    </select>
    <select id="selectChannelByEqId" parameterType="String" resultType="java.util.Map">
        select *
        from basedata_eqpbook_channel
        where eqp_book_id = #{eqId}
    </select>
    <select id="selectPointInfoByCode" parameterType="String" resultType="java.util.Map">
        select *
        from basedata_patrolpoint
        where patrol_point_code = #{deviceCode}
    </select>
    <select id="selectEqInfoByCode" parameterType="String" resultType="java.util.Map">
        select *
        from basedata_eqpbook
        where patrol_device_code = #{eqCode}
    </select>
    <select id="selectEqInfoBySendCode" parameterType="String" resultType="java.util.Map">
        select *
        from basedata_eqpbook
        where main_system_code = #{sendCode}
    </select>
    <select id="selectStadPointInfoById" parameterType="String" resultType="java.util.Map">
        select *
        from basedata_stdpatrol_point
        where std_patrol_point_id = #{deviceId}
    </select>

    <select id="selectResultAnalysisByLineId" parameterType="Long" resultMap="ResultAnalysisResult">
        <include refid="selectResultAnalysisVo"/>
        where line_id = #{lineId}
    </select>

    <insert id="insertResultAnalysis" parameterType="ResultAnalysis" useGeneratedKeys="true" keyProperty="lineId">
        insert into result_analysis
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="lineId != null">line_id,</if>
            <if test="resultType != null">result_type,</if>
            <if test="businessId != null">business_id,</if>
            <if test="resultContent != null">result_content,</if>
            <if test="resStatus != null">res_status,</if>
            <if test="pointStatus != null">point_status,</if>
            <if test="createTime != null">create_time,</if>
            <if test="objectId != null">objectId,</if>
            <if test="suggestion != null">suggestion,</if>
            <if test="description != null">description,</if>
            <if test="requestId != null">request_id,</if>
            <if test="taskPatrolId != null">task_patrol_id,</if>
            <if test="algType != null">alg_type,</if>
            <if test="resValue != null">res_value,</if>
            <if test="resCode != null">res_code,</if>
            <if test="resConf != null">res_conf,</if>
            <if test="resDesc != null">res_desc,</if>
            <if test="result != null">result,</if>
            <if test="resImgUrl != null">res_img_url,</if>
            <if test="imageNormalUrlPath != null">image_normal_url_path,</if>
            <if test="resPos != null">res_pos,</if>
            <if test="filter != null">filter,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="lineId != null">#{lineId},</if>
            <if test="resultType != null">#{resultType},</if>
            <if test="businessId != null">#{businessId},</if>
            <if test="resultContent != null">#{resultContent},</if>
            <if test="resStatus != null">#{resStatus},</if>
            <if test="pointStatus != null">#{pointStatus},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="objectId != null">#{objectId},</if>
            <if test="suggestion != null">#{suggestion},</if>
            <if test="description != null">#{description},</if>
            <if test="requestId != null">#{requestId},</if>
            <if test="taskPatrolId != null">#{taskPatrolId},</if>
            <if test="algType != null">#{algType},</if>
            <if test="resValue != null">#{resValue},</if>
            <if test="resCode != null">#{resCode},</if>
            <if test="resConf != null">#{resConf},</if>
            <if test="resDesc != null">#{resDesc},</if>
            <if test="result != null">#{result},</if>
            <if test="resImgUrl != null">#{resImgUrl},</if>
            <if test="imageNormalUrlPath != null">#{imageNormalUrlPath},</if>
            <if test="resPos != null">#{resPos},</if>
            <if test="filter != null">#{filter},</if>
        </trim>
    </insert>

    <update id="updateResultAnalysis" parameterType="ResultAnalysis">
        update result_analysis
        <trim prefix="SET" suffixOverrides=",">
            <if test="resultType != null">result_type = #{resultType},</if>
            <if test="businessId != null">business_id = #{businessId},</if>
            <if test="resultContent != null">result_content = #{resultContent},</if>
            <if test="resStatus != null">res_status = #{resStatus},</if>
            <if test="pointStatus != null">point_status = #{pointStatus},</if>
            <if test="objectId != null">objectId = #{objectId},</if>
            <if test="suggestion != null">suggestion = #{suggestion},</if>
            <if test="description != null">description = #{description},</if>
            <if test="requestId != null">request_id = #{requestId},</if>
            <if test="taskPatrolId != null">task_patrol_id  = #{taskPatrolId},</if>
            <if test="algType != null">alg_type = #{algType},</if>
            <if test="resValue != null">res_value = #{resValue},</if>
            <if test="resCode != null">res_code = #{resCode},</if>
            <if test="resConf != null">res_conf = #{resConf},</if>
            <if test="resDesc != null">res_desc = #{resDesc},</if>
            <if test="result != null">result = #{result},</if>
            <if test="resImgUrl != null">res_img_url = #{resImgUrl},</if>
            <if test="imageNormalUrlPath != null">image_normal_url_path = #{imageNormalUrlPath},</if>
            <if test="resPos != null">res_pos = #{resPos},</if>
            <if test="filter != null">filter = #{filter},</if>
            <if test="originalValue != null">original_value = #{originalValue},</if>
        </trim>
        where line_id = #{lineId}
    </update>
    <update id="updateResultAnalysisByCondition">
        update result_analysis
        <trim prefix="SET" suffixOverrides=",">
            <if test="resultType != null">result_type = #{resultType},</if>
            <if test="pointStatus != null">point_status = #{pointStatus},</if>
            <if test="suggestion != null">suggestion = #{suggestion},</if>
            <if test="description != null">description = #{description},</if>
        </trim>
            <where>
                objectId = #{objectId} and alg_type = #{algType} and task_patrol_id  = #{taskPatrolId}
                <if test="lineId != null  and lineId != ''">
                    and line_id = #{lineId}
                </if>
            </where>
    </update>

    <delete id="deleteResultAnalysisByLineId" parameterType="Long">
        delete
        from result_analysis
        where line_id = #{lineId}
    </delete>

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

    <select id="defectList1" resultType="java.util.Map">
        SELECT COUNT(1) num,
               ba.area_name,
               '异常'   type
        FROM (SELECT result ->> '$.type' type, ra5.*
              FROM
                  (
                  SELECT
                  results ->> '$[0]' result, ra4.*
                  FROM
                  (
                  SELECT
                  resultsArray ->> '$.results' results, ra3.*
                  FROM
                  (
                  SELECT
                  ra2.resultList ->> '$[0]' resultsArray, ra2.*
                  FROM
                  ( SELECT ra.result_content ->> '$.resultList' resultList, ra.* FROM result_analysis ra ) ra2
                  ) ra3
                  ) ra4
                  ) ra5) ra6,
             basedata_patrolpoint bp,
             basedata_area ba
        WHERE 1 = 1
          AND ra6.res_status = '1'
          AND ra6.point_status = '0'
          AND ra6.type = 'meter'
          AND ra6.objectId = bp.patrol_point_id
          AND bp.area_id = ba.area_id
        GROUP BY ba.area_id
    </select>

    <select id="defectList2" resultType="java.util.Map">
        SELECT COUNT(1) num,
               ba.area_name,
               '正常'   type
        FROM (SELECT result ->> '$.type' type, ra5.*
              FROM
                  (
                  SELECT
                  results ->> '$[0]' result, ra4.*
                  FROM
                  (
                  SELECT
                  resultsArray ->> '$.results' results, ra3.*
                  FROM
                  (
                  SELECT
                  ra2.resultList ->> '$[0]' resultsArray, ra2.*
                  FROM
                  ( SELECT ra.result_content ->> '$.resultList' resultList, ra.* FROM result_analysis ra ) ra2
                  ) ra3
                  ) ra4
                  ) ra5) ra6,
             basedata_patrolpoint bp,
             basedata_area ba
        WHERE 1 = 1
          AND ra6.res_status = '1'
          AND ra6.point_status = '1'
          AND ra6.type = 'meter'
          AND ra6.objectId = bp.patrol_point_id
          AND bp.area_id = ba.area_id
        GROUP BY ba.area_id
    </select>

    <select id="defectList3" resultType="java.util.Map">
        SELECT COUNT(1) num,
               ba.area_name,
               '确认'   type
        FROM (SELECT result ->> '$.type' type, ra5.*
              FROM
                  (
                  SELECT
                  results ->> '$[0]' result, ra4.*
                  FROM
                  (
                  SELECT
                  resultsArray ->> '$.results' results, ra3.*
                  FROM
                  (
                  SELECT
                  ra2.resultList ->> '$[0]' resultsArray, ra2.*
                  FROM
                  ( SELECT ra.result_content ->> '$.resultList' resultList, ra.* FROM result_analysis ra ) ra2
                  ) ra3
                  ) ra4
                  ) ra5) ra6,
             basedata_patrolpoint bp,
             basedata_area ba
        WHERE 1 = 1
          AND ra6.res_status = '1'
          AND ra6.point_status = '2'
          AND ra6.type = 'meter'
          AND ra6.objectId = bp.patrol_point_id
          AND bp.area_id = ba.area_id
        GROUP BY ba.area_id
    </select>

    <select id="pointStatistics" resultType="java.util.Map">
        --         SELECT
--             a.parent_id,
--             e.area_name,
--             COUNT( 1 ) num
--         FROM
--             basedata_area a
--                 INNER JOIN basedata_patrolpoint b ON b.area_id = a.area_id
--                 INNER JOIN patrol_preset_pos c ON b.patrol_point_id = c.patrol_point_id
--                 INNER JOIN (SELECT d.area_id,d.area_name FROM basedata_area d WHERE d.parent_id = '1' AND d.del_flag = '0') e ON a.parent_id = e.area_id
--         GROUP BY
--             a.parent_id
        SELECT a.area_id,
               a.area_name,
               COUNT(1) num
        FROM basedata_area a
                 INNER JOIN basedata_patrolpoint b ON b.area_id = a.area_id
                 INNER JOIN patrol_preset_pos c ON b.patrol_point_id = c.patrol_point_id
        GROUP BY a.area_id
    </select>


    <select id="selectResultAnalysisByMainId" parameterType="Long" resultType="int">
        SELECT COUNT(b.line_id)
        FROM patrol_result a
                 left join result_analysis b on a.line_id = b.business_id
        WHERE a.main_id = #{mainId}
    </select>

    <select id="selectResultAnalysisByMainIds" resultType="java.lang.Integer">
        SELECT COUNT(b.line_id)
        FROM patrol_result a
                 left join result_analysis b on a.line_id = b.business_id
        WHERE a.main_id in
        <foreach collection="lineIds" item="mainId" open="(" separator="," close=")">
            #{mainId}
        </foreach>
    </select>


<!--    <insert id="insertAnalyseLog" parameterType="com.inspect.analysis.domain.AnalyseLog">-->
<!--        insert into patrol_analyse_log (content,type,create_time,task_patrol_id,filter,request_id)-->
<!--        values(#{content},#{type},now(),#{taskPatrolId},#{filter},#{requestId})-->
<!--    </insert>-->

    <insert id="insertAnalyseLog" parameterType="com.inspect.analysis.domain.AnalyseLog">
        insert into patrol_analyse_log (task_patrol_id,object_id,request_id,request_url,filter,type,content,create_time)
        values(#{taskPatrolId},#{objectId},#{requestId},#{requestUrl},#{filter},#{type},#{content},now())
    </insert>

    <select id="selectTaskAbnormalData" parameterType="ResultAnalysis" resultMap="ResultAnalysisResult">
        SELECT
            (
                SELECT
                    COUNT( 1 ) count
                FROM
                    result_analysis a
                WHERE
                  a.result_type = '0'
                  AND a.filter = '1'
                  AND a.task_patrol_id = #{taskPatrolledId}
            ) csQxNum,
            (
                SELECT
                    COUNT( 1 ) count
                FROM
                    result_analysis a
                WHERE
                  a.result_type = '0'
                  AND a.filter = '0'
                  AND a.task_patrol_id = #{taskPatrolledId}
            ) aiQxNum,
            (
                SELECT
                    COUNT( 1 ) count
                FROM
                    result_analysis a
                WHERE
                  a.result_type = '2'
                  AND a.filter = '1'
                  AND a.task_patrol_id = #{taskPatrolledId}
            ) csYcNum,
            (
                SELECT
                    COUNT( 1 ) count
                FROM
                    result_analysis a
                WHERE
                  a.result_type = '2'
                  AND a.filter = '0'
                  AND a.task_patrol_id = #{taskPatrolledId}
            ) aiYcNum
    </select>

    <select id="countResultAnalysisSummary" parameterType="java.util.List" resultType="com.inspect.analysis.domain.ResultAnalysisSummaryDTO">
        SELECT
        task_patrol_id as taskPatrolledId,
        SUM(CASE WHEN result_type = '0' AND filter = '1' THEN 1 ELSE 0 END) as csQxNum,
        SUM(CASE WHEN result_type = '0' AND filter = '0' THEN 1 ELSE 0 END) as aiQxNum,
        SUM(CASE WHEN result_type = '2' AND filter = '1' THEN 1 ELSE 0 END) as csYcNum,
        SUM(CASE WHEN result_type = '2' AND filter = '0' THEN 1 ELSE 0 END) as aiYcNum,
        COUNT(DISTINCT CASE WHEN result_type = '0' THEN CONCAT(objectId, '_', alg_type) END) as qxNum
        FROM result_analysis
        WHERE task_patrol_id IN
        <foreach collection="list" item="id" open="(" close=")" separator=",">
            #{id}
        </foreach>
        AND result_type IN ('0', '2')
        GROUP BY task_patrol_id
    </select>

    <select id="selectTaskAbnormalDataBatch" parameterType="java.util.List" resultType="map">
        SELECT
        task_patrol_id as taskPatrolledId,
        SUM(CASE WHEN result_type = '0' AND filter = '1' THEN 1 ELSE 0 END) as csQxNum,
        SUM(CASE WHEN result_type = '0' AND filter = '0' THEN 1 ELSE 0 END) as aiQxNum,
        SUM(CASE WHEN result_type = '2' AND filter = '1' THEN 1 ELSE 0 END) as csYcNum,
        SUM(CASE WHEN result_type = '2' AND filter = '0' THEN 1 ELSE 0 END) as aiYcNum
        FROM result_analysis
        WHERE task_patrol_id IN
        <foreach collection="list" item="id" open="(" close=")" separator=",">
            #{id}
        </foreach>
        AND result_type IN ('0', '2')
        GROUP BY task_patrol_id
    </select>

    <select id="selectResultAnalyseForAlarm" parameterType="com.inspect.analysis.domain.AnalyseSummaryAlarmModel"
            resultMap="AlgPatrolPointResult">
        SELECT
        b.patrol_point_id,b.patrol_point_name
        FROM
        result_analysis a
        left join basedata_patrolpoint b on a.objectId = b.patrol_point_id
        <where>
            <if test="isToday != null and isToday != '' and isToday == 1">and to_days(a.create_time) = to_days(now())</if>
            <if test="pointName != null  and pointName != ''">and locate(#{pointName},b.patrol_point_name)</if>
            and a.result=1 and a.point_status=0 and b.patrol_point_id is not null
        </where>
        group by b.patrol_point_id,b.patrol_point_name
        order by b.patrol_point_id
    </select>

    <resultMap type="com.inspect.task.domain.PatrolData" id="PatrolDataResult">
        <result property="lineId" column="line_id"/>
        <result property="objectId" column="objectId"/>
        <result property="algType" column="alg_type"/>
        <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="imgAnalyse"/>
        <result property="filter" column="filter"/>
        <result property="requestId" column="request_id"/>
        <result property="resStatus" column="res_status"/>
        <result property="imageNormalUrlPath" column="image_normal_url_path"/>
        <result property="suggestion" column="suggestion"/>
    </resultMap>

    <select id="selectResultAnalysisListForAlarm" parameterType="com.inspect.analysis.domain.AnalyseSummaryAlarmModel"
            resultMap="PatrolDataResult">
        select
        b.line_id ,
        b.objectId as objectId,
        b.alg_type as alg_type,
        a.device_name as point_name,
        a.create_time as patrol_time,
        b.point_status ,
        b.result_type as warn_status,
        case when (b.description is null or b.description = '') then b.alg_type else b.description end as 'desc',
        b.res_img_url as img,
        a.file_type as img_type,
        a.data_type,
        b.filter,
        b.request_id,
        b.res_img_url imgAnalyse,
        b.res_status,
        b.image_normal_url_path,
        b.suggestion
        from patrol_result a
        LEFT JOIN result_analysis b on a.line_id=b.business_id
        <where>
            b.result_type != '2'
            <if test="isToday != null and isToday != '' and isToday == 1">and to_days(a.create_time) = to_days(now())</if>
            <if test="objectId != null  and objectId != ''">and b.objectId = #{objectId}</if>
            <if test="filter != null  and filter != ''">and b.filter = #{filter}</if>
            <if test="type != null  and type != '' and type == 0">and b.result_type = '0' </if>
        </where>
        ORDER BY b.line_id desc
    </select>

    <select id="selectResultAnalysisListBatch" resultMap="ResultAnalysisResult">
        <include refid="selectResultAnalysisVo"/>
        WHERE task_patrol_id IN
            <foreach collection="list" item="taskPatrolId" open="(" close=")" separator=",">
                #{taskPatrolId}
                </foreach>
        AND result_type = #{resultType}
    </select>
    <select id="selectResultAnalysisListDefectInfo" resultType="java.lang.Long">
        select
        count(*) as count
        from
        result_analysis b
        <where>
            b.result_type != '2'
            <if test="isToday != null and isToday != '' and isToday == 1">AND b.create_time &gt;= CURDATE()
                AND b.create_time &lt; CURDATE() + INTERVAL 1 DAY</if>
            <if test="filter != null  and filter != ''">and b.filter = #{filter}</if>
            <if test="type != null  and type != '' and type == 0">and b.result_type = '0' </if>
        </where>
    </select>

</mapper>
