<?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.LdTaskMapper">

    <resultMap type="com.ruoyi.robot.domain.LdTask" id="LdTaskResult">
        <result property="id" column="id"/>
        <result property="mapName" column="map_name"/>
        <result property="taskName" column="task_name"/>
        <result property="taskDesc" column="task_desc"/>
        <result property="cronExpression" column="cron_expression"/>
        <result property="executeTime" column="execute_time"/>
        <result property="executeWeekdays" column="execute_weekdays"/>
        <result property="repeatCount" column="repeat_count"/>
        <result property="coordType" column="coord_type"/>
        <result property="waypointIds" column="waypoint_ids"/>
        <result property="waypointCoords" column="waypoint_coords"/>
        <result property="waypointTypes" column="waypoint_types"/>
        <result property="waypointActions" column="waypoint_actions"/>
        <result property="status" column="status"/>
        <result property="lastExecuteTime" column="last_execute_time"/>
        <result property="lastExecuteStatus" column="last_execute_status"/>
        <result property="nextExecuteTime" column="next_execute_time"/>
        <result property="totalExecuteCount" column="total_execute_count"/>
        <result property="deviceId" column="device_id"/>
        <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="remark" column="remark"/>
    </resultMap>

    <sql id="selectLdTaskVo">
        select id, map_name, task_name, task_desc, cron_expression, execute_time, execute_weekdays, 
               repeat_count, coord_type, waypoint_ids, waypoint_coords, waypoint_types, waypoint_actions,
               status, last_execute_time, last_execute_status, next_execute_time, total_execute_count,
               device_id, create_by, create_time, update_by, update_time, remark
        from ld_task
    </sql>

    <select id="selectLdTaskList" parameterType="com.ruoyi.robot.domain.LdTask" resultMap="LdTaskResult">
        <include refid="selectLdTaskVo"/>
        <where>
            <if test="mapName != null and mapName != ''">
                AND map_name = #{mapName}
            </if>
            <if test="taskName != null and taskName != ''">
                AND task_name like concat('%', #{taskName}, '%')
            </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 create_time desc
    </select>

    <select id="selectLdTaskById" parameterType="Long" resultMap="LdTaskResult">
        <include refid="selectLdTaskVo"/>
        where id = #{id}
    </select>

    <select id="selectLdTaskListByMapName" parameterType="String" resultMap="LdTaskResult">
        <include refid="selectLdTaskVo"/>
        where map_name = #{mapName}
        order by create_time desc
    </select>

    <select id="selectLdTaskByMapAndName" resultMap="LdTaskResult">
        <include refid="selectLdTaskVo"/>
        where map_name = #{mapName} and task_name = #{taskName}
    </select>

    <select id="selectPendingScheduledTasks" resultMap="LdTaskResult">
        <include refid="selectLdTaskVo"/>
        where cron_expression is not null 
          and cron_expression != ''
          and status in ('idle', 'paused')
        order by create_time asc
    </select>

    <insert id="insertLdTask" parameterType="com.ruoyi.robot.domain.LdTask" useGeneratedKeys="true" keyProperty="id">
        insert into ld_task
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="mapName != null and mapName != ''">map_name,</if>
            <if test="taskName != null and taskName != ''">task_name,</if>
            <if test="taskDesc != null">task_desc,</if>
            <if test="cronExpression != null">cron_expression,</if>
            <if test="executeTime != null">execute_time,</if>
            <if test="executeWeekdays != null">execute_weekdays,</if>
            <if test="repeatCount != null">repeat_count,</if>
            <if test="coordType != null">coord_type,</if>
            <if test="waypointIds != null">waypoint_ids,</if>
            <if test="waypointCoords != null">waypoint_coords,</if>
            <if test="waypointTypes != null">waypoint_types,</if>
            <if test="waypointActions != null">waypoint_actions,</if>
            <if test="status != null">status,</if>
            <if test="deviceId != null">device_id,</if>
            <if test="createBy != null and createBy != ''">create_by,</if>
            <if test="remark != null">remark,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="mapName != null and mapName != ''">#{mapName},</if>
            <if test="taskName != null and taskName != ''">#{taskName},</if>
            <if test="taskDesc != null">#{taskDesc},</if>
            <if test="cronExpression != null">#{cronExpression},</if>
            <if test="executeTime != null">#{executeTime},</if>
            <if test="executeWeekdays != null">#{executeWeekdays},</if>
            <if test="repeatCount != null">#{repeatCount},</if>
            <if test="coordType != null">#{coordType},</if>
            <if test="waypointIds != null">#{waypointIds},</if>
            <if test="waypointCoords != null">#{waypointCoords},</if>
            <if test="waypointTypes != null">#{waypointTypes},</if>
            <if test="waypointActions != null">#{waypointActions},</if>
            <if test="status != null">#{status},</if>
            <if test="deviceId != null">#{deviceId},</if>
            <if test="createBy != null and createBy != ''">#{createBy},</if>
            <if test="remark != null">#{remark},</if>
        </trim>
    </insert>

    <update id="updateLdTask" parameterType="com.ruoyi.robot.domain.LdTask">
        update ld_task
        <trim prefix="SET" suffixOverrides=",">
            <if test="taskDesc != null">task_desc = #{taskDesc},</if>
            <if test="cronExpression != null">cron_expression = #{cronExpression},</if>
            <if test="executeTime != null">execute_time = #{executeTime},</if>
            <if test="executeWeekdays != null">execute_weekdays = #{executeWeekdays},</if>
            <if test="repeatCount != null">repeat_count = #{repeatCount},</if>
            <if test="coordType != null">coord_type = #{coordType},</if>
            <if test="waypointIds != null">waypoint_ids = #{waypointIds},</if>
            <if test="waypointCoords != null">waypoint_coords = #{waypointCoords},</if>
            <if test="waypointTypes != null">waypoint_types = #{waypointTypes},</if>
            <if test="waypointActions != null">waypoint_actions = #{waypointActions},</if>
            <if test="status != null">status = #{status},</if>
            <if test="lastExecuteTime != null">last_execute_time = #{lastExecuteTime},</if>
            <if test="lastExecuteStatus != null">last_execute_status = #{lastExecuteStatus},</if>
            <if test="nextExecuteTime != null">next_execute_time = #{nextExecuteTime},</if>
            <if test="totalExecuteCount != null">total_execute_count = #{totalExecuteCount},</if>
            <if test="deviceId != null">device_id = #{deviceId},</if>
            <if test="updateBy != null and updateBy != ''">update_by = #{updateBy},</if>
            <if test="remark != null">remark = #{remark},</if>
        </trim>
        where id = #{id}
    </update>

    <update id="updateTaskStatus">
        update ld_task set status = #{status}, update_time = sysdate()
        where id = #{id}
    </update>

    <delete id="deleteLdTaskById" parameterType="Long">
        delete from ld_task where id = #{id}
    </delete>

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

    <delete id="deleteLdTaskByMapName" parameterType="String">
        delete from ld_task where map_name = #{mapName}
    </delete>

    <!-- 根据设备ID查询正在运行的任务（状态为running或paused） -->
    <select id="selectRunningTasksByDeviceId" parameterType="String" resultMap="LdTaskResult">
        <include refid="selectLdTaskVo"/>
        where device_id = #{deviceId}
          and status in ('running', 'paused')
        order by update_time desc
    </select>

    <!-- 根据设备ID批量更新任务状态 -->
    <update id="updateTaskStatusByDeviceId">
        update ld_task set status = #{status}, update_time = sysdate()
        where device_id = #{deviceId} and status in ('running', 'paused')
    </update>

</mapper>
