<?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.system.mapper.MissionsMapper">

    <resultMap type="Missions" id="MissionsResult">
        <result property="id" column="id"/>
        <result property="missionName" column="mission_name"/>
        <result property="fileId" column="file_id"/>
        <result property="returnAltitude" column="return_altitude"/>
        <result property="returnAltitudeMode" column="return_altitude_mode"/>
        <result property="outOfControlAction" column="out_of_control_action"/>
        <result property="flightControlAction" column="flight_control_action"/>
        <result property="waylinePrecisionType" column="wayline_precision_type"/>
        <result property="createdAt" column="created_at"/>
        <result property="type" column="type"/>
        <result property="execTime" column="exec_time"/>
        <result property="flightId" column="flightId"/>
        <result property="state" column="state"/>
        <result property="gateway" column="gateway"/>
        <result property="timer" column="timer"/>
        <result property="flag" column="flag"/>
        <result property="lastTime" column="last_time"/>
    </resultMap>

    <sql id="selectMissionsVo">
        select id,
               mission_name,
               file_id,
               return_altitude,
               return_altitude_mode,
               out_of_control_action,
               flight_control_action,
               wayline_precision_type,
               created_at,
               type,
               exec_time,
               flightId,
               state,
               gateway,
               timer,
               flag,
               last_time
        from missions
    </sql>

    <select id="selectMissionsList" parameterType="Missions" resultMap="MissionsResult">
        <include refid="selectMissionsVo"/>
        <where>
            <if test="missionName != null  and missionName != ''">and mission_name like concat('%', #{missionName},
                '%')
            </if>
            <if test="fileId != null  and fileId != ''">and file_id = #{fileId}</if>
            <if test="returnAltitude != null ">and return_altitude = #{returnAltitude}</if>
            <if test="returnAltitudeMode != null ">and return_altitude_mode = #{returnAltitudeMode}</if>
            <if test="outOfControlAction != null ">and out_of_control_action = #{outOfControlAction}</if>
            <if test="flightControlAction != null ">and flight_control_action = #{flightControlAction}</if>
            <if test="waylinePrecisionType != null ">and wayline_precision_type = #{waylinePrecisionType}</if>
            <if test="createdAt != null ">and created_at = #{createdAt}</if>
            <if test="type != null ">and type = #{type}</if>
            <if test="execTime != null ">and exec_time = #{execTime}</if>
            <if test="flightId != null ">and flightId = #{flightId}</if>
            <if test="state != null  and state != ''">and state = #{state}</if>
            <if test="gateway != null  and gateway != ''">and gateway = #{gateway}</if>
            <if test="timer != null ">and timer = #{timer}</if>
            <if test="flag != null ">and flag = #{flag}</if>
            <if test="lastTime != null ">and last_time = #{lastTime}</if>
        </where>
    </select>

    <select id="selectMissionsById" parameterType="Long" resultMap="MissionsResult">
        <include refid="selectMissionsVo"/>
        where id = #{id}
    </select>

    <!-- 查询所有任务 -->
    <select id="list" resultType="com.ruoyi.system.dto.MissionsDto">
        select missions.id,
        missions.mission_name as missionName,
        missions.file_id as fileId,
        missions.return_altitude as returnAltitude,
        missions.return_altitude_mode as returnAltitudeMode,
        missions.out_of_control_action as outOfControlAction,
        missions.flight_control_action as flightControlAction,
        missions.wayline_precision_type as waylinePrecisionType,
        missions.created_at as createdAt,
        missions.type,
        missions.exec_time as execTime,
        missions.flightId,
        missions.state,
        missions.gateway,
        missions.timer,
        missions.flag,
        missions.last_time as lastTime,
        video_info.business_name as deviceName,
        flight_paths.file_name as fileName,
        flight_paths.points as points
        from missions
        join video_info on video_info.sn = #{gateway}
        join flight_paths on flight_paths.id = missions.file_id
        <!-- 条件查询：任务类型 -->
        <if test="type != null and type != ''">
            AND missions.type = #{type}
        </if>
        <!-- 网关筛选 -->
        <if test="gateway != null and gateway != ''">
            AND missions.gateway = #{gateway}
        </if>
        <!-- 条件查询：状态 -->
        <if test="state != null and state != '' and state != '全部'">
            AND missions.state = #{state}
        </if>
        <!-- 时间范围查询：created_at -->
        <if test="startTime != null">
            AND missions.created_at &gt; #{startTime}
        </if>
        <if test="endTime != null">
            AND missions.created_at &lt; #{endTime}
        </if>
        <!-- 模糊查询：设备名称（deviceName）或文件名（fileName） -->
        <if test="name != null and name != ''">
            AND (missions.mission_name LIKE CONCAT('%', #{name}, '%')
            OR flight_paths.file_name LIKE CONCAT('%', #{name}, '%'))
        </if>
        order by missions.created_at desc
    </select>

    <select id="getLatest" resultType="com.ruoyi.system.dto.MissionsDto">
        select missions.id,
               missions.mission_name           as missionName,
               missions.file_id                as fileId,
               missions.return_altitude        as returnAltitude,
               missions.return_altitude_mode   as returnAltitudeMode,
               missions.out_of_control_action  as outOfControlAction,
               missions.flight_control_action  as flightControlAction,
               missions.wayline_precision_type as waylinePrecisionType,
               missions.created_at             as createdAt,
               missions.type,
               missions.exec_time              as execTime,
               missions.flightId,
               missions.state,
               missions.gateway,
               missions.timer,
               missions.flag,
               missions.last_time              as lastTime,
               video_info.business_name        as deviceName,
               flight_paths.file_name          as fileName,
               flight_paths.points             as points
        from missions
                 join video_info on video_info.sn = #{gateway}
                 join flight_paths on flight_paths.id = missions.file_id
        WHERE missions.type = 1
          AND missions.timer > NOW()
          AND missions.gateway = #{gateway}
        ORDER BY missions.timer asc LIMIT 1
    </select>



    <insert id="insertMissions" parameterType="Missions" useGeneratedKeys="true" keyProperty="id">
        insert into missions
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="missionName != null and missionName != ''">mission_name,</if>
            <if test="fileId != null and fileId != ''">file_id,</if>
            <if test="returnAltitude != null">return_altitude,</if>
            <if test="returnAltitudeMode != null">return_altitude_mode,</if>
            <if test="outOfControlAction != null">out_of_control_action,</if>
            <if test="flightControlAction != null">flight_control_action,</if>
            <if test="waylinePrecisionType != null">wayline_precision_type,</if>
            <if test="createdAt != null">created_at,</if>
            <if test="type != null">type,</if>
            <if test="execTime != null">exec_time,</if>
            <if test="flightId != null">flightId,</if>
            <if test="state != null">state,</if>
            <if test="gateway != null">gateway,</if>
            <if test="timer != null">timer,</if>
            <if test="flag != null">flag,</if>
            <if test="lastTime != null">last_time,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="missionName != null and missionName != ''">#{missionName},</if>
            <if test="fileId != null and fileId != ''">#{fileId},</if>
            <if test="returnAltitude != null">#{returnAltitude},</if>
            <if test="returnAltitudeMode != null">#{returnAltitudeMode},</if>
            <if test="outOfControlAction != null">#{outOfControlAction},</if>
            <if test="flightControlAction != null">#{flightControlAction},</if>
            <if test="waylinePrecisionType != null">#{waylinePrecisionType},</if>
            <if test="createdAt != null">#{createdAt},</if>
            <if test="type != null">#{type},</if>
            <if test="execTime != null">#{execTime},</if>
            <if test="flightId != null">#{flightId},</if>
            <if test="state != null">#{state},</if>
            <if test="gateway != null">#{gateway},</if>
            <if test="timer != null">#{timer},</if>
            <if test="flag != null">#{flag},</if>
            <if test="lastTime != null">#{lastTime},</if>
        </trim>
    </insert>

    <update id="updateMissions" parameterType="Missions">
        update missions
        <trim prefix="SET" suffixOverrides=",">
            <if test="missionName != null and missionName != ''">mission_name = #{missionName},</if>
            <if test="fileId != null and fileId != ''">file_id = #{fileId},</if>
            <if test="returnAltitude != null">return_altitude = #{returnAltitude},</if>
            <if test="returnAltitudeMode != null">return_altitude_mode = #{returnAltitudeMode},</if>
            <if test="outOfControlAction != null">out_of_control_action = #{outOfControlAction},</if>
            <if test="flightControlAction != null">flight_control_action = #{flightControlAction},</if>
            <if test="waylinePrecisionType != null">wayline_precision_type = #{waylinePrecisionType},</if>
            <if test="createdAt != null">created_at = #{createdAt},</if>
            <if test="type != null">type = #{type},</if>
            <if test="execTime != null">exec_time = #{execTime},</if>
            <if test="flightId != null">flightId = #{flightId},</if>
            <if test="state != null">state = #{state},</if>
            <if test="gateway != null">gateway = #{gateway},</if>
            <if test="timer != null">timer = #{timer},</if>
            <if test="flag != null">flag = #{flag},</if>
            <if test="lastTime != null">last_time = #{lastTime},</if>
        </trim>
        where id = #{id}
    </update>

    <!-- 设置 state 和 last_date -->
    <update id="updateMissionsByFlightId">
        update missions
        set state     = #{state},
            last_time = #{lastTime}
        where flightId = #{flightId}
    </update>

    <delete id="deleteMissionsById" parameterType="Long">
        delete
        from missions
        where id = #{id}
    </delete>

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