<?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.ruoyi.robot.mapper.LdTaskExecutionLogMapper">

    <resultMap type="com.ruoyi.robot.domain.LdTaskExecutionLog" id="LdTaskExecutionLogResult">
        <result property="id" column="id"/>
        <result property="taskId" column="task_id"/>
        <result property="taskName" column="task_name"/>
        <result property="mapName" column="map_name"/>
        <result property="executeTime" column="execute_time"/>
        <result property="endTime" column="end_time"/>
        <result property="status" column="status"/>
        <result property="startWaypoint" column="start_waypoint"/>
        <result property="endWaypoint" column="end_waypoint"/>
        <result property="completedWaypoints" column="completed_waypoints"/>
        <result property="totalWaypoints" column="total_waypoints"/>
        <result property="errorCode" column="error_code"/>
        <result property="errorMessage" column="error_message"/>
        <result property="executeResult" column="execute_result"/>
        <result property="deviceId" column="device_id"/>
        <result property="createTime" column="create_time"/>
    </resultMap>

    <sql id="selectLdTaskExecutionLogVo">
        select id, task_id, task_name, map_name, execute_time, end_time, status,
               start_waypoint, end_waypoint, completed_waypoints, total_waypoints,
               error_code, error_message, execute_result, device_id, create_time
        from ld_task_execution_log
    </sql>

    <select id="selectLdTaskExecutionLogList" parameterType="com.ruoyi.robot.domain.LdTaskExecutionLog" resultMap="LdTaskExecutionLogResult">
        <include refid="selectLdTaskExecutionLogVo"/>
        <where>
            <if test="taskId != null">
                AND task_id = #{taskId}
            </if>
            <if test="taskName != null and taskName != ''">
                AND task_name like concat('%', #{taskName}, '%')
            </if>
            <if test="mapName != null and mapName != ''">
                AND map_name = #{mapName}
            </if>
            <if test="status != null and status != ''">
                AND status = #{status}
            </if>
            <if test="deviceId != null and deviceId != ''">
                AND device_id = #{deviceId}
            </if>
        </where>
        order by execute_time desc
    </select>

    <select id="selectLdTaskExecutionLogById" parameterType="Long" resultMap="LdTaskExecutionLogResult">
        <include refid="selectLdTaskExecutionLogVo"/>
        where id = #{id}
    </select>

    <select id="selectLdTaskExecutionLogByTaskId" parameterType="Long" resultMap="LdTaskExecutionLogResult">
        <include refid="selectLdTaskExecutionLogVo"/>
        where task_id = #{taskId}
        order by execute_time desc
    </select>

    <select id="selectTodayExecutionLogsByDeviceId" resultMap="LdTaskExecutionLogResult">
        <include refid="selectLdTaskExecutionLogVo"/>
        where device_id = #{deviceId}
          and date(execute_time) = #{executeDate}
        order by execute_time desc
    </select>

    <insert id="insertLdTaskExecutionLog" parameterType="com.ruoyi.robot.domain.LdTaskExecutionLog" useGeneratedKeys="true" keyProperty="id">
        insert into ld_task_execution_log
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="taskId != null">task_id,</if>
            <if test="taskName != null and taskName != ''">task_name,</if>
            <if test="mapName != null and mapName != ''">map_name,</if>
            <if test="executeTime != null">execute_time,</if>
            <if test="endTime != null">end_time,</if>
            <if test="status != null and status != ''">status,</if>
            <if test="startWaypoint != null">start_waypoint,</if>
            <if test="endWaypoint != null">end_waypoint,</if>
            <if test="completedWaypoints != null">completed_waypoints,</if>
            <if test="totalWaypoints != null">total_waypoints,</if>
            <if test="errorCode != null">error_code,</if>
            <if test="errorMessage != null">error_message,</if>
            <if test="executeResult != null">execute_result,</if>
            <if test="deviceId != null">device_id,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="taskId != null">#{taskId},</if>
            <if test="taskName != null and taskName != ''">#{taskName},</if>
            <if test="mapName != null and mapName != ''">#{mapName},</if>
            <if test="executeTime != null">#{executeTime},</if>
            <if test="endTime != null">#{endTime},</if>
            <if test="status != null and status != ''">#{status},</if>
            <if test="startWaypoint != null">#{startWaypoint},</if>
            <if test="endWaypoint != null">#{endWaypoint},</if>
            <if test="completedWaypoints != null">#{completedWaypoints},</if>
            <if test="totalWaypoints != null">#{totalWaypoints},</if>
            <if test="errorCode != null">#{errorCode},</if>
            <if test="errorMessage != null">#{errorMessage},</if>
            <if test="executeResult != null">#{executeResult},</if>
            <if test="deviceId != null">#{deviceId},</if>
        </trim>
    </insert>

    <update id="updateLdTaskExecutionLog" parameterType="com.ruoyi.robot.domain.LdTaskExecutionLog">
        update ld_task_execution_log
        <trim prefix="SET" suffixOverrides=",">
            <if test="endTime != null">end_time = #{endTime},</if>
            <if test="status != null and status != ''">status = #{status},</if>
            <if test="endWaypoint != null">end_waypoint = #{endWaypoint},</if>
            <if test="completedWaypoints != null">completed_waypoints = #{completedWaypoints},</if>
            <if test="errorCode != null">error_code = #{errorCode},</if>
            <if test="errorMessage != null">error_message = #{errorMessage},</if>
            <if test="executeResult != null">execute_result = #{executeResult},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteLdTaskExecutionLogById" parameterType="Long">
        delete from ld_task_execution_log where id = #{id}
    </delete>

    <delete id="deleteLdTaskExecutionLogByIds" parameterType="Long">
        delete from ld_task_execution_log where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

    <delete id="deleteLdTaskExecutionLogByTaskId" parameterType="Long">
        delete from ld_task_execution_log where task_id = #{taskId}
    </delete>

</mapper>
