<?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.exec.mapper.PatrolTaskExecRecordMapper">

    <resultMap type="PatrolTaskExecRecord" id="PatrolTaskExecRecordResult">
        <result property="taskPatrolId" column="task_patrolled_id"/>
        <result property="taskName" column="task_name"/>
        <result property="taskCode" column="task_code"/>
        <result property="taskState" column="task_state"/>
        <result property="planStartTime" column="plan_start_time"/>
        <result property="startTime" column="start_time"/>
        <result property="taskProgress" column="task_progress"/>
        <result property="taskEstimatedTime" column="task_estimated_time"/>
        <result property="description" column="description"/>
        <result property="endTime" column="end_time"/>
        <result property="devNo" column="dev_no"/>
        <result property="execPointId" column="exec_point_id"/>
        <result property="deviceId" column="device_id"/>
        <result property="taskId" column="task_id"/>
        <result property="taskType" column="task_type"/>
        <result property="taskPriority" column="task_priority"/>
        <result property="stationName" column="station_name"/>
        <result property="stationCode" column="station_code"/>
        <result property="manualPauseState" column="manual_pause_state"/>
        <result property="oldTaskPatrolId" column="old_task_patrolled_id"/>
        <result property="totalNumber" column="total_number"/>
        <result property="finishNumber" column="finish_number"/>
        <result property="cursorNumber" column="cursor_number"/>
        <result property="rebootTimes" column="reboot_times"/>
    </resultMap>

    <sql id="selectPatrolTaskExecRecordVo">
        select task_patrolled_id,
               task_name,
               task_code,
               task_state,
               plan_start_time,
               start_time,
               task_progress,
               task_estimated_time,
               description,
               end_time,
               dev_no,
               exec_point_id,
               device_id,
               task_id,
               task_type,
               task_priority,
               station_name,
               station_code,
               manual_pause_state,
               old_task_patrolled_id,
               total_number,
               finish_number,
               cursor_number,
               reboot_times
        from patrol_task_exec_record
    </sql>

    <select id="selectPatrolTaskExecRecordList" parameterType="PatrolTaskExecRecord"
            resultMap="PatrolTaskExecRecordResult">
        <include refid="selectPatrolTaskExecRecordVo"/>
        <where>
            <if test="taskPatrolId != null  and taskPatrolId != ''">and task_patrolled_id = #{taskPatrolId}</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="taskState != null  and taskState != ''">and task_state = #{taskState}</if>
            <if test="planStartTime != null ">and plan_start_time = #{planStartTime}</if>
            <if test="startTime != null ">and start_time = #{startTime}</if>
            <if test="taskProgress != null  and taskProgress != ''">and task_progress = #{taskProgress}</if>
            <if test="taskEstimatedTime != null  and taskEstimatedTime != ''">and task_estimated_time =
                #{taskEstimatedTime}
            </if>
            <if test="description != null  and description != ''">and description = #{description}</if>
            <if test="endTime != null ">and end_time = #{endTime}</if>
            <if test="devNo != null ">and dev_no = #{devNo}</if>
            <if test="execPointId != null ">and exec_point_id = #{execPointId}</if>
            <if test="deviceId != null ">and device_id = #{deviceId}</if>
            <if test="taskId != null ">and task_id = #{taskId}</if>
            <if test="taskType != null ">and task_type = #{taskType}</if>
            <if test="taskPriority != null ">and task_priority = #{taskPriority}</if>
            <if test="stationName != null ">and station_name = #{stationName}</if>
            <if test="stationCode != null ">and station_code = #{stationCode}</if>
            <if test="manualPauseState != null ">and manual_pause_state = #{manualPauseState}</if>
            <if test="oldTaskPatrolId != null ">and old_task_patrolled_id = #{oldTaskPatrolId}</if>
            <if test="totalNumber != null ">and total_number = #{totalNumber}</if>
            <if test="finishNumber != null ">and finish_number = #{finishNumber}</if>
            <if test="cursorNumber != null ">and cursor_number = #{cursorNumber}</if>
            <if test="rebootTimes != null ">and reboot_times = #{rebootTimes}</if>
        </where>
    </select>

    <select id="selectCameraPatrolTaskExecRecordList" resultMap="PatrolTaskExecRecordResult">
        <include refid="selectPatrolTaskExecRecordVo"/>
        <where>
            <foreach collection="taskStates" item="taskState" index="index" separator="OR">
                `task_state` LIKE CONCAT('%',#{taskState},'%')
            </foreach>
            <if test="taskCode != null ">and task_code = #{taskCode}</if>
            <if test="devNo != null ">and dev_no = #{devNo}</if>
        </where>
        ORDER BY start_time asc
    </select>

    <select id="selectPatrolTaskExecRecordByTaskPatrolId" parameterType="String" resultMap="PatrolTaskExecRecordResult">
        <include refid="selectPatrolTaskExecRecordVo"/>
        where task_patrolled_id = #{taskPatrolId}
    </select>

    <select id="selectAbeyancePatrolTaskExecRecordList" resultMap="PatrolTaskExecRecordResult">
        <include refid="selectPatrolTaskExecRecordVo"/>
        <where>
            exec_point_id is not null and task_state='2' and device_id is not null and device_id != '-1' and
            exec_point_id != -1
        </where>
    </select>


    <insert id="insertPatrolTaskExecRecord" parameterType="PatrolTaskExecRecord">
        insert into patrol_task_exec_record
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="taskPatrolId != null">task_patrolled_id,</if>
            <if test="taskName != null">task_name,</if>
            <if test="taskCode != null">task_code,</if>
            <if test="taskState != null">task_state,</if>
            <if test="planStartTime != null">plan_start_time,</if>
            <if test="startTime != null">start_time,</if>
            <if test="taskProgress != null">task_progress,</if>
            <if test="taskEstimatedTime != null">task_estimated_time,</if>
            <if test="description != null">description,</if>
            <if test="endTime != null">end_time,</if>
            <if test="devNo != null">dev_no,</if>
            <if test="execPointId != null">exec_point_id,</if>
            <if test="deviceId != null">device_id,</if>
            <if test="taskId != null">task_id,</if>
            <if test="taskType != null">task_type,</if>
            <if test="taskPriority != null">task_priority,</if>
            <if test="stationName != null">station_name,</if>
            <if test="stationCode != null">station_code,</if>
            <if test="manualPauseState != null">manual_pause_state,</if>
            <if test="oldTaskPatrolId != null">old_task_patrolled_id,</if>
            <if test="totalNumber != null">total_number,</if>
            <if test="finishNumber != null">finish_number,</if>
            <if test="cursorNumber != null">cursor_number,</if>
            <if test="rebootTimes != null">reboot_times,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="taskPatrolId != null">#{taskPatrolId},</if>
            <if test="taskName != null">#{taskName},</if>
            <if test="taskCode != null">#{taskCode},</if>
            <if test="taskState != null">#{taskState},</if>
            <if test="planStartTime != null">#{planStartTime},</if>
            <if test="startTime != null">#{startTime},</if>
            <if test="taskProgress != null">#{taskProgress},</if>
            <if test="taskEstimatedTime != null">#{taskEstimatedTime},</if>
            <if test="description != null">#{description},</if>
            <if test="endTime != null">#{endTime},</if>
            <if test="devNo != null">#{devNo},</if>
            <if test="execPointId != null">#{execPointId},</if>
            <if test="deviceId != null">#{deviceId},</if>
            <if test="taskId != null">#{taskId},</if>
            <if test="taskType != null">#{taskType},</if>
            <if test="taskPriority != null">#{taskPriority},</if>
            <if test="stationName != null">#{stationName},</if>
            <if test="stationCode != null">#{stationCode},</if>
            <if test="manualPauseState != null">#{manualPauseState},</if>
            <if test="oldTaskPatrolId != null">#{oldTaskPatrolId},</if>
            <if test="totalNumber != null">#{totalNumber},</if>
            <if test="finishNumber != null">#{finishNumber},</if>
            <if test="cursorNumber != null">#{cursorNumber},</if>
            <if test="rebootTimes != null">#{rebootTimes},</if>
        </trim>
    </insert>

    <update id="updatePatrolTaskExecRecord" parameterType="PatrolTaskExecRecord">
        update patrol_task_exec_record
        <trim prefix="SET" suffixOverrides=",">
            <if test="taskName != null">task_name = #{taskName},</if>
            <if test="taskCode != null">task_code = #{taskCode},</if>
            <if test="taskState != null">task_state = #{taskState},</if>
            <if test="planStartTime != null">plan_start_time = #{planStartTime},</if>
            <if test="startTime != null">start_time = #{startTime},</if>
            <if test="taskProgress != null">task_progress = #{taskProgress},</if>
            <if test="taskEstimatedTime != null">task_estimated_time = #{taskEstimatedTime},</if>
            <if test="description != null">description = #{description},</if>
            <if test="endTime != null">end_time = #{endTime},</if>
            <if test="devNo != null">dev_no = #{devNo},</if>
            <if test="execPointId != null">exec_point_id = #{execPointId},</if>
            <if test="deviceId != null">device_id = #{deviceId},</if>
            <if test="taskId != null">task_id = #{taskId},</if>
            <if test="taskType != null">task_type = #{taskType},</if>
            <if test="taskPriority != null">task_priority = #{taskPriority},</if>
            <if test="stationName != null">station_name = #{stationName},</if>
            <if test="stationCode != null">station_code = #{stationCode},</if>
            <if test="manualPauseState != null">manual_pause_state = #{manualPauseState},</if>
            <if test="oldTaskPatrolId != null">old_task_patrolled_id = #{oldTaskPatrolId},</if>
            <if test="totalNumber != null">total_number = #{totalNumber},</if>
            <if test="finishNumber != null">finish_number = #{finishNumber},</if>
            <if test="cursorNumber != null">cursor_number = #{cursorNumber},</if>
            <if test="rebootTimes != null">reboot_times = #{rebootTimes},</if>
        </trim>
        where task_patrolled_id = #{taskPatrolId}
    </update>

    <delete id="deletePatrolTaskExecRecordByTaskPatrolId" parameterType="String">
        delete
        from patrol_task_exec_record
        where task_patrolled_id = #{taskPatrolId}
    </delete>

    <delete id="deletePatrolTaskExecRecordByTaskPatrolIds" parameterType="String">
        delete from patrol_task_exec_record where task_patrolled_id in
        <foreach item="taskPatrolId" collection="array" open="(" separator="," close=")">
            #{taskPatrolId}
        </foreach>
    </delete>

    <delete id="deletePatrolTaskExecRecordByTaskState" parameterType="String">
        delete
        from patrol_task_exec_record
        where task_state = #{taskState}
    </delete>
</mapper>