<?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.resultmain.mapper.PatrolTaskResultMainMapper">

    <resultMap type="PatrolTaskResultMain" id="PatrolTaskResultMainResult">
        <result property="lineId" column="line_id"/>
        <result property="taskId" column="task_id"/>
        <result property="taskName" column="task_name"/>
        <result property="taskCode" column="task_code"/>
        <result property="createTime" column="create_time"/>
        <result property="createBy" column="create_by"/>
        <result property="fileStatus" column="file_status"/>
        <result property="areaName" column="area_name"/>
        <result property="taskType" column="task_type"/>
        <result property="taskTime" column="task_time"/>
        <result property="intervalName" column="interval_name"/>
        <result property="devType" column="dev_type"/>
        <result property="taskResult" column="task_result"/>
        <result property="requestId" column="request_id"/>
        <result property="checkPerson" column="check_person"/>
        <result property="checkTime" column="check_time"/>
        <result property="taskPatrolledId" column="task_patrolled_id"/>
        <result property="unifiedTaskPatrolledId" column="unified_task_patrolled_id"/>
    </resultMap>

    <sql id="selectPatrolTaskResultMainVo">
        select line_id,
               task_id,
               task_patrolled_id,
               unified_task_patrolled_id,
               task_name,
               task_code,
               create_time,
               create_by,
               file_status,
               area_name,
               task_type,
               task_time,
               interval_name,
               dev_type,
               task_result,
               request_id,
               check_person,
               check_time
        from patrol_task_result_main
    </sql>

    <select id="selectPatrolTaskResultMainList" parameterType="PatrolTaskResultMain" resultMap="PatrolTaskResultMainResult">
        <include refid="selectPatrolTaskResultMainVo"/>
        <where>
            <if test="taskId != null  and taskId != ''">and task_id = #{taskId}</if>
            <if test="taskName != null  and taskName != ''">and task_name like concat('%', #{taskName}, '%')</if>
            <if test="taskCode != null  and taskCode != ''">and task_code = #{taskCode}</if>
            <if test="createBy != null  and createBy != ''">and create_by = #{createBy}</if>
            <if test="fileStatus != null  and fileStatus != ''">and file_status = #{fileStatus}</if>
            <if test="areaName != null  and areaName != ''">and area_name like concat('%', #{areaName}, '%')</if>
            <if test="taskType != null  and taskType != ''">and task_type = #{taskType}</if>
            <if test="taskTime != null ">and task_time = #{taskTime}</if>
            <if test="intervalName != null  and intervalName != ''">and interval_name like concat('%', #{intervalName},
                '%')
            </if>
            <if test="devType != null  and devType != ''">and dev_type = #{devType}</if>
            <if test="taskResult != null  and taskResult != ''">and task_result = #{taskResult}</if>
            <if test="requestId != null  and requestId != ''">and request_id = #{requestId}</if>
            <if test="checkPerson != null  and checkPerson != ''">and check_person = #{checkPerson}</if>
            <if test="checkTime != null ">and check_time = #{checkTime}</if>
            <if test="taskPatrolledId != null ">and task_patrolled_id = #{taskPatrolledId}</if>
        </where>
        ORDER BY line_id desc limit 1
    </select>

    <select id="selectPatrolTaskResultMain" parameterType="PatrolTaskResultMain" resultMap="PatrolTaskResultMainResult">
        SELECT
        max(a.line_id) as line_id,
        max(a.task_id) as task_id,
        group_concat(DISTINCT a.task_patrolled_id) as task_patrolled_id,
        a.task_name as task_name,
        max(a.task_code) as task_code,
        max(a.create_time) as create_time,
        a.create_by as create_by,
        max(a.file_status) as file_status,
        group_concat(DISTINCT a.area_name) as area_name,
        max(a.task_type) as task_type,
        max(a.task_time) as task_time,
        max(a.interval_name) as interval_name,
        group_concat(DISTINCT a.dev_type) as dev_type,
        group_concat(DISTINCT a.task_result) as task_result,
        max(a.request_id) as request_id,
        max(a.check_person) as check_person,
        max(a.check_time) as check_time,
        a.unified_task_patrolled_id as unified_task_patrolled_id
        FROM
        patrol_task_result_main a,
        patrol_task_status b
        WHERE
        a.task_patrolled_id = b.task_patrolled_id
        AND b.task_state = '1'
        <if test="taskId != null  and taskId != ''">and a.task_id = #{taskId}</if>
        <if test="taskName != null  and taskName != ''">and a.task_name like concat('%', #{taskName}, '%')</if>
        <if test="taskCode != null  and taskCode != ''">and a.task_code = #{taskCode}</if>
        <if test="createBy != null  and createBy != ''">and a.create_by = #{createBy}</if>
        <if test="fileStatus != null  and fileStatus != ''">and a.file_status = #{fileStatus}</if>
        <if test="areaName != null  and areaName != ''">and a.area_name like concat('%', #{areaName}, '%')</if>
        <if test="taskType != null  and taskType != ''">and a.task_type = #{taskType}</if>
        <if test="taskTime != null ">and a.task_time = #{taskTime}</if>
        <if test="intervalName != null  and intervalName != ''">and a.interval_name like concat('%', #{intervalName},
            '%')
        </if>
        <if test="devType != null  and devType != ''">and a.dev_type = #{devType}</if>
        <if test="taskResult != null  and taskResult != ''">and a.task_result = #{taskResult}</if>
        <if test="requestId != null  and requestId != ''">and a.request_id = #{requestId}</if>
        <if test="checkPerson != null  and checkPerson != ''">and a.check_person = #{checkPerson}</if>
        <if test="checkTime != null ">and a.check_time = #{checkTime}</if>
        <if test="taskPatrolledId != null ">and a.task_patrolled_id = #{taskPatrolledId}</if>
        group by a.unified_task_patrolled_id,a.task_id,a.task_name,a.create_by
        ORDER BY line_id desc
    </select>

    <select id="selectUnifiedTaskPatrolledId" parameterType="PatrolTaskResultMain" resultType="java.lang.String">
        select unified_task_patrolled_id from patrol_task_result_main where task_id = #{taskId} and file_status = 0 and (now() - create_time) &lt;= 120 ORDER BY line_id desc limit 1
    </select>

    <select id="selectPatrolTaskResultMainByLineId" parameterType="Long" resultMap="PatrolTaskResultMainResult">
        <include refid="selectPatrolTaskResultMainVo"/>
        where line_id = #{lineId}
    </select>

    <select id="selectMainFileStatus" resultMap="PatrolTaskResultMainResult">
        SELECT
            line_id,
            task_patrolled_id,
            file_status
        FROM
            patrol_task_result_main
        WHERE
            task_patrolled_id in
        <foreach collection="list" item="taskPatrolledId" open="(" separator="," close=")">
            #{taskPatrolledId}
        </foreach>
    </select>

    <select id="selectLineIds" resultType="java.lang.Long">
        SELECT DISTINCT d.line_id
        FROM patrol_task_result_main a
                 JOIN patrol_task_status b ON a.task_patrolled_id = b.task_patrolled_id
                 JOIN patrol_task_status c ON c.parent_id = b.line_id
                 JOIN patrol_task_result_main d ON c.task_patrolled_id = d.task_patrolled_id
        WHERE a.line_id = #{lineId};
    </select>

    <select id="selectLineIdsByList" resultType="java.lang.Long">
        select
        line_id
        from
        patrol_task_result_main
        where
        unified_task_patrolled_id in (select unified_task_patrolled_id from patrol_task_result_main where line_id in
        <foreach collection="lineIds" item="lineId" open="(" separator="," close=")">
            #{lineId}
        </foreach>)
    </select>

    <select id="selectPatrolTaskResultMainByTaskPatrolledId" resultMap="PatrolTaskResultMainResult">
        <include refid="selectPatrolTaskResultMainVo"/>
        where task_patrolled_id = #{taskPatrolledId}
        ORDER BY line_id desc limit 1
    </select>

    <select id="selectPatrolTaskResultMainByTaskPatrolledIds" resultMap="PatrolTaskResultMainResult">
        <include refid="selectPatrolTaskResultMainVo"/>
        where task_patrolled_id in
            <foreach collection="taskPatrolledIds" item="taskPatrolledId" open="(" separator="," close=")">
                #{taskPatrolledId}
            </foreach>
    </select>

    <select id="selectPatrolTaskResultMainOne" resultMap="PatrolTaskResultMainResult" parameterType="String">
        select c.* from patrol_task_result_main c
        where c.task_patrolled_id = (
                select
                    CASE
                        WHEN a.main_id is null THEN a.task_patrolled_id
                        ELSE a.main_id
                        END
                FROM patrol_task_status a
                where a.task_patrolled_id = #{patrolTaskId}
            )
    </select>

    <insert id="insertPatrolTaskResultMain" parameterType="PatrolTaskResultMain" useGeneratedKeys="true"
            keyProperty="lineId">
        insert into patrol_task_result_main
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="taskId != null">task_id,</if>
            <if test="taskName != null">task_name,</if>
            <if test="taskCode != null">task_code,</if>
            <if test="createTime != null">create_time,</if>
            <if test="createBy != null">create_by,</if>
            <if test="fileStatus != null">file_status,</if>
            <if test="areaName != null">area_name,</if>
            <if test="taskType != null">task_type,</if>
            <if test="taskTime != null">task_time,</if>
            <if test="intervalName != null">interval_name,</if>
            <if test="devType != null">dev_type,</if>
            <if test="taskResult != null">task_result,</if>
            <if test="requestId != null">request_id,</if>
            <if test="checkPerson != null">check_person,</if>
            <if test="checkTime != null">check_time,</if>
            <if test="taskPatrolledId != null">task_patrolled_id,</if>
            <if test="unifiedTaskPatrolledId != null">unified_task_patrolled_id,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="taskId != null">#{taskId},</if>
            <if test="taskName != null">#{taskName},</if>
            <if test="taskCode != null">#{taskCode},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="fileStatus != null">#{fileStatus},</if>
            <if test="areaName != null">#{areaName},</if>
            <if test="taskType != null">#{taskType},</if>
            <if test="taskTime != null">#{taskTime},</if>
            <if test="intervalName != null">#{intervalName},</if>
            <if test="devType != null">#{devType},</if>
            <if test="taskResult != null">#{taskResult},</if>
            <if test="requestId != null">#{requestId},</if>
            <if test="checkPerson != null">#{checkPerson},</if>
            <if test="checkTime != null">#{checkTime},</if>
            <if test="taskPatrolledId != null">#{taskPatrolledId},</if>
            <if test="unifiedTaskPatrolledId != null">#{unifiedTaskPatrolledId},</if>
        </trim>
    </insert>

    <update id="updatePatrolTaskResultMain" parameterType="PatrolTaskResultMain">
        update patrol_task_result_main
        <trim prefix="SET" suffixOverrides=",">
            <if test="taskId != null">task_id = #{taskId},</if>
            <if test="taskName != null">task_name = #{taskName},</if>
            <if test="taskCode != null">task_code = #{taskCode},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="fileStatus != null">file_status = #{fileStatus},</if>
            <if test="areaName != null">area_name = #{areaName},</if>
            <if test="taskType != null">task_type = #{taskType},</if>
            <if test="taskTime != null">task_time = #{taskTime},</if>
            <if test="intervalName != null">interval_name = #{intervalName},</if>
            <if test="devType != null">dev_type = #{devType},</if>
            <if test="taskResult != null">task_result = #{taskResult},</if>
            <if test="requestId != null">request_id = #{requestId},</if>
            <if test="checkPerson != null">check_person = #{checkPerson},</if>
            <if test="checkTime != null">check_time = #{checkTime},</if>
            <if test="taskPatrolledId != null">task_patrolled_id = #{taskPatrolledId},</if>
            <if test="unifiedTaskPatrolledId != null">unified_task_patrolled_id = #{unifiedTaskPatrolledId},</if>
        </trim>
        where line_id = #{lineId}
    </update>

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

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