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

    <resultMap type="DroneMissions" id="DroneMissionsResult">
        <result property="id" column="id"/>
        <result property="missionName" column="mission_name"/>
        <result property="flightPathUrl" column="flight_path_url"/>
        <result property="flightPathMd5" column="flight_path_md5"/>
        <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"/>
    </resultMap>

    <sql id="selectDroneMissionsVo">
        select id,
               mission_name,
               flight_path_url,
               flight_path_md5,
               return_altitude,
               return_altitude_mode,
               out_of_control_action,
               flight_control_action,
               wayline_precision_type,
               created_at
        from drone_missions
    </sql>

    <select id="selectDroneMissionsList" parameterType="DroneMissions" resultMap="DroneMissionsResult">
        <include refid="selectDroneMissionsVo"/>
        <where>
            <if test="missionName != null  and missionName != ''">and mission_name like concat('%', #{missionName},
                '%')
            </if>
            <if test="flightPathUrl != null  and flightPathUrl != ''">and flight_path_url = #{flightPathUrl}</if>
            <if test="flightPathMd5 != null  and flightPathMd5 != ''">and flight_path_md5 = #{flightPathMd5}</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>
        </where>
    </select>

    <select id="selectDroneMissionsById" parameterType="Long" resultMap="DroneMissionsResult">
        <include refid="selectDroneMissionsVo"/>
        where id = #{id}
    </select>

    <!-- 查询条件 -->
    <select id="selectDroneMissionsByCondition" resultMap="DroneMissionsResult">
        select drone_missions.*
        from drone_missions
                 join flight_paths on drone_missions.flight_path_md5 = flight_paths.file_md5
                 join flight_tasks on drone_missions.id = flight_tasks.messionId
        where flight_tasks.flightId = #{flightId}
    </select>

    <select id="selectDroneMissionsVoList" resultType="com.ruoyi.system.dto.DroneMissionsVo">
        select
        drone_missions.id,
        drone_missions.mission_name as missionName,
        drone_missions.flight_path_url as flightPathUrl,
        drone_missions.flight_path_md5 as flightPathUrl,
        drone_missions.return_altitude as returnAltitude,
        drone_missions.return_altitude_mode as returnAltitudeMode,
        drone_missions.out_of_control_action as outOfControlAction,
        drone_missions.flight_control_action as flightControlAction,
        drone_missions.wayline_precision_type as waylinePrecisionType,
        drone_missions.created_at as createdAt, flight_paths.file_name as waylineName
        from drone_missions
        join flight_paths on drone_missions.flight_path_md5 = flight_paths.file_md5
        <where>
            <if test="missionName != null  and missionName != ''">and drone_missions.mission_name like concat('%',
                #{missionName},
                '%')
            </if>
            <if test="flightPathUrl != null  and flightPathUrl != ''">and drone_missions.flight_path_url =
                #{flightPathUrl}
            </if>
            <if test="flightPathMd5 != null  and flightPathMd5 != ''">and drone_missions.flight_path_md5 =
                #{flightPathMd5}
            </if>
            <if test="returnAltitude != null ">and drone_missions.return_altitude = #{returnAltitude}</if>
            <if test="returnAltitudeMode != null ">and drone_missions.return_altitude_mode = #{returnAltitudeMode}</if>
            <if test="outOfControlAction != null ">and drone_missions.out_of_control_action = #{outOfControlAction}</if>
            <if test="flightControlAction != null ">and drone_missions.flight_control_action = #{flightControlAction}
            </if>
            <if test="waylinePrecisionType != null ">and drone_missions.wayline_precision_type =
                #{waylinePrecisionType}
            </if>
            <if test="createdAt != null ">and drone_missions.created_at = #{createdAt}</if>
        </where>
        order by drone_missions.id desc
    </select>

    <insert id="insertDroneMissions" parameterType="DroneMissions" useGeneratedKeys="true" keyProperty="id">
        insert into drone_missions
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="missionName != null and missionName != ''">mission_name,</if>
            <if test="flightPathUrl != null and flightPathUrl != ''">flight_path_url,</if>
            <if test="flightPathMd5 != null and flightPathMd5 != ''">flight_path_md5,</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>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="missionName != null and missionName != ''">#{missionName},</if>
            <if test="flightPathUrl != null and flightPathUrl != ''">#{flightPathUrl},</if>
            <if test="flightPathMd5 != null and flightPathMd5 != ''">#{flightPathMd5},</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>
        </trim>
    </insert>

    <update id="updateDroneMissions" parameterType="DroneMissions">
        update drone_missions
        <trim prefix="SET" suffixOverrides=",">
            <if test="missionName != null and missionName != ''">mission_name = #{missionName},</if>
            <if test="flightPathUrl != null and flightPathUrl != ''">flight_path_url = #{flightPathUrl},</if>
            <if test="flightPathMd5 != null and flightPathMd5 != ''">flight_path_md5 = #{flightPathMd5},</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>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteDroneMissionsById" parameterType="Long">
        delete
        from drone_missions
        where id = #{id}
    </delete>

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