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

    <resultMap type="FlightTasks" id="FlightTasksResult">
        <result property="id"    column="id"    />
        <result property="sn"    column="sn"    />
        <result property="isExecuted"    column="is_executed"    />
        <result property="status"    column="status"    />
        <result property="messionId"    column="messionId"    />
        <result property="flightId"    column="flightId"    />
        <result property="realPhotoNum"    column="real_photo_num"    />
        <result property="createdAt"    column="created_at"    />
        <result property="completeAt"    column="complete_at"    />
    </resultMap>

    <sql id="selectFlightTasksVo">
        select id, sn, is_executed, status, messionId, flightId, real_photo_num, created_at, complete_at from flight_tasks
    </sql>

    <select id="selectFlightTasksList" parameterType="FlightTasks" resultMap="FlightTasksResult">
        <include refid="selectFlightTasksVo"/>
        <where>
            <if test="sn != null  and sn != ''"> and sn = #{sn}</if>
            <if test="isExecuted != null "> and is_executed = #{isExecuted}</if>
            <if test="status != null  and status != ''"> and status = #{status}</if>
            <if test="messionId != null "> and messionId = #{messionId}</if>
            <if test="flightId != null  and flightId != ''"> and flightId = #{flightId}</if>
            <if test="realPhotoNum != null "> and real_photo_num = #{realPhotoNum}</if>
            <if test="createdAt != null "> and created_at = #{createdAt}</if>
            <if test="completeAt != null "> and complete_at = #{completeAt}</if>
        </where>
    </select>

    <select id="selectFlightTasksById" parameterType="Long" resultMap="FlightTasksResult">
        <include refid="selectFlightTasksVo"/>
        where id = #{id}
    </select>

    <!-- 查询任务列表 -->
    <select id="selectFlightTasksVoList" resultType="com.ruoyi.system.dto.FlightTasksVo">
        select flight_tasks.id,
        flight_tasks.sn,
        flight_tasks.is_executed as isExecuted,
        flight_tasks.status,
        flight_tasks.messionId,
        flight_tasks.flightId,
        flight_tasks.real_photo_num as realPhotoNum,
        flight_tasks.created_at as createdAt,
        flight_tasks.complete_at as completeAt,
        drone_missions.mission_name as missionName,
        flight_paths.photo_num as photoNum,
        flight_paths.wayline_len as waylineLen,
        flight_paths.type as waylineType,
        flight_paths.file_name as waylineName
        from flight_tasks
        join drone_missions on flight_tasks.messionId = drone_missions.id
        join flight_paths on drone_missions.flight_path_md5 = flight_paths.file_md5
        <where>
            <if test="sn != null  and sn != ''">and flight_tasks.sn = #{sn}</if>
            <if test="isExecuted != null ">and flight_tasks.is_executed = #{isExecuted}</if>
            <if test="status != null  and status != ''">and flight_tasks.status = #{status}</if>
            <if test="messionId != null ">and flight_tasks.messionId = #{messionId}</if>
            <if test="flightId != null  and flightId != ''">and flight_tasks.flightId = #{flightId}</if>
            <if test="realPhotoNum != null ">and flight_tasks.real_photo_num = #{realPhotoNum}</if>
            <if test="createdAt != null ">and flight_tasks.created_at = #{createdAt}</if>
            <if test="completeAt != null ">and flight_tasks.complete_at = #{completeAt}</if>
        </where>
        order by flight_tasks.id desc
    </select>

    <insert id="insertFlightTasks" parameterType="FlightTasks" useGeneratedKeys="true" keyProperty="id">
        insert into flight_tasks
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="sn != null and sn != ''">sn,</if>
            <if test="isExecuted != null">is_executed,</if>
            <if test="status != null and status != ''">status,</if>
            <if test="messionId != null">messionId,</if>
            <if test="flightId != null">flightId,</if>
            <if test="realPhotoNum != null">real_photo_num,</if>
            <if test="createdAt != null">created_at,</if>
            <if test="completeAt != null">complete_at,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="sn != null and sn != ''">#{sn},</if>
            <if test="isExecuted != null">#{isExecuted},</if>
            <if test="status != null and status != ''">#{status},</if>
            <if test="messionId != null">#{messionId},</if>
            <if test="flightId != null">#{flightId},</if>
            <if test="realPhotoNum != null">#{realPhotoNum},</if>
            <if test="createdAt != null">#{createdAt},</if>
            <if test="completeAt != null">#{completeAt},</if>
         </trim>
    </insert>

    <update id="updateFlightTasks" parameterType="FlightTasks">
        update flight_tasks
        <trim prefix="SET" suffixOverrides=",">
            <if test="sn != null and sn != ''">sn = #{sn},</if>
            <if test="isExecuted != null">is_executed = #{isExecuted},</if>
            <if test="status != null and status != ''">status = #{status},</if>
            <if test="messionId != null">messionId = #{messionId},</if>
            <if test="flightId != null">flightId = #{flightId},</if>
            <if test="realPhotoNum != null">real_photo_num = #{realPhotoNum},</if>
            <if test="createdAt != null">created_at = #{createdAt},</if>
            <if test="completeAt != null">complete_at = #{completeAt},</if>
        </trim>
        where id = #{id}
    </update>

    <update id="updateFlightTasksStatus">
        update flight_tasks
        <trim prefix="SET" suffixOverrides=",">
            <if test="status != null and status != ''">status = #{status},</if>
            <if test="updateTime != null">complete_at = #{updateTime},</if>
        </trim>
        where flightId = #{flightId}
    </update>

    <delete id="deleteFlightTasksById" parameterType="Long">
        delete from flight_tasks where id = #{id}
    </delete>

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