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

    <resultMap type="Drone" id="DroneResult">
        <result property="id"    column="id"    />
        <result property="gateway"    column="gateway"    />
        <result property="sn"    column="sn"    />
        <result property="isOnline"    column="is_online"    />
    </resultMap>

    <sql id="selectDroneVo">
        select id, gateway, sn, is_online from drone
    </sql>

    <select id="selectDroneList" parameterType="Drone" resultMap="DroneResult">
        <include refid="selectDroneVo"/>
        <where>
            <if test="gateway != null  and gateway != ''"> and gateway = #{gateway}</if>
            <if test="sn != null  and sn != ''"> and sn = #{sn}</if>
            <if test="isOnline != null  and isOnline != ''"> and is_online = #{isOnline}</if>
        </where>
    </select>

    <select id="selectDroneById" parameterType="Long" resultMap="DroneResult">
        <include refid="selectDroneVo"/>
        where id = #{id}
    </select>

    <!-- 根据设备sn查询设备信息 -->
    <select id="selectDroneBySn" resultType="com.ruoyi.system.domain.Drone">
        select id, gateway, sn, is_online from drone where sn = #{sn}
    </select>

    <insert id="insertDrone" parameterType="Drone" useGeneratedKeys="true" keyProperty="id">
        insert into drone
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="gateway != null">gateway,</if>
            <if test="sn != null and sn != ''">sn,</if>
            <if test="isOnline != null">is_online,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="gateway != null">#{gateway},</if>
            <if test="sn != null and sn != ''">#{sn},</if>
            <if test="isOnline != null">#{isOnline},</if>
         </trim>
    </insert>

    <update id="updateDrone" parameterType="Drone">
        update drone
        <trim prefix="SET" suffixOverrides=",">
            <if test="gateway != null">gateway = #{gateway},</if>
            <if test="sn != null and sn != ''">sn = #{sn},</if>
            <if test="isOnline != null">is_online = #{isOnline},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteDroneById" parameterType="Long">
        delete from drone where id = #{id}
    </delete>

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