<?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.IotDeviceMapper">
    
    <resultMap type="IotDevice" id="IotDeviceResult">
        <result property="deviceId"    column="device_id"    />
        <result property="deviceNum"    column="device_num"    />
        <result property="categoryId"    column="category_id"    />
        <result property="categoryName"    column="category_Name"    />
        <result property="deviceTemp"    column="device_temp"    />
        <result property="deviceName"    column="device_name"    />
        <result property="firmwareVersion"    column="firmware_version"    />
        <result property="ownerId"    column="owner_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>

    <resultMap type="IotDeviceListDto" id="IotDeviceListResult">
        <result property="deviceId"    column="device_id"    />
        <result property="deviceNum"    column="device_num"    />
        <result property="categoryId"    column="category_id"    />
        <result property="categoryName"    column="category_Name"    />
        <result property="deviceName"    column="device_name"    />
        <result property="firmwareVersion"    column="firmware_version"    />
        <result property="ownerId"    column="owner_id"    />
        <result property="nickName"    column="nick_name"    />
        <result property="createTime"    column="create_time"    />
        <result property="isAlarm"    column="is_alarm"    />
        <result property="isRadar"    column="is_radar"    />
        <result property="isRfControl"    column="is_rf_control"    />
        <result property="networkAddress"    column="network_address"    />
        <result property="networkIp"    column="network_ip"    />
        <result property="relayStatus"    column="relay_status"    />
        <result property="lightStatus"    column="light_status"    />
        <result property="isOnline"    column="is_online"    />
        <result property="deviceTemperature"    column="device_temperature"    />
        <result property="rssi"    column="rssi"    />
    </resultMap>

    <sql id="selectIotDeviceVo">
        select d.device_id, d.device_num, d.category_id, d.device_name, d.firmware_version, d.owner_id, d.create_by, d.create_time, d.update_by, d.update_time, d.remark,
               c.category_name,
               s.device_temperature as device_temp
        from iot_device d
                 left join iot_category c on d.category_id=c.category_id
                 left join (select device_id,device_temperature from iot_device_status
                                    right join (select max(device_status_id) as status_id from iot_device_status group by device_id) mt
                                    on mt.status_id=device_status_id ) as s
                 on d.device_id=s.device_id
    </sql>

    <sql id="selectIotDeviceListVo">
        select d.device_id, d.device_num, d.category_id, d.device_name, d.firmware_version, d.owner_id, d.create_time,
               s.relay_status,s.light_status,s.is_online,s.rssi,s.device_temperature,
               t.network_address, t.network_ip,t.is_alarm, t.is_radar,t.is_rf_control,
               u.nick_name,
               c.category_name
               <if test="groupId != null and groupId!=0">,g.group_id </if>
        from iot_device d
                 left join (select * from iot_device_set
                                right join (select max(device_set_id) as set_id from iot_device_set group by device_id) ms on ms.set_id=device_set_id ) as t
                      on d.device_id = t.device_id
                 left join (select * from iot_device_status
                                right join (select max(device_status_id) as status_id from iot_device_status group by device_id) mt on mt.status_id=device_status_id ) as s
                       on d.device_id=s.device_id
                 left join sys_user u on d.owner_id=u.user_id
                 left join iot_category c on d.category_id=c.category_id
                 <if test="groupId != null and groupId!=0"> left join iot_device_group g on d.device_id=g.device_id </if>

    </sql>

    <select id="selectIotDeviceList" parameterType="IotDevice" resultMap="IotDeviceListResult">
        <include refid="selectIotDeviceListVo"/>
        <where>
            d.del_flag= '0'
            <if test="deviceNum != null  and deviceNum != ''"> and d.device_num like concat('%', #{deviceNum}, '%')</if>
            <if test="categoryId != null "> and d.category_id = #{categoryId}</if>
            <if test="groupId != null and groupId!=0 "> and g.group_id = #{groupId}</if>
            <if test="deviceName != null  and deviceName != ''"> and d.device_name like concat('%', #{deviceName}, '%')</if>
            <if test="firmwareVersion != null  and firmwareVersion != ''"> and d.firmware_version like concat('%', #{firmwareVersion}, '%')</if>
            <if test="ownerId != null  and ownerId != ''"> and d.owner_id like concat('%', #{ownerId}, '%')</if>
            <if test="params.beginCreateTime != null and params.beginCreateTime != '' and params.endCreateTime != null and params.endCreateTime != ''"> and create_time between #{params.beginCreateTime} and #{params.endCreateTime}</if>
        </where>
    </select>
    
    <select id="selectIotDeviceById" parameterType="Long" resultMap="IotDeviceResult">
        <include refid="selectIotDeviceVo"/>
        where d.device_id = #{deviceId}
    </select>

    <select id="selectIotDeviceByNum" parameterType="String" resultMap="IotDeviceResult">
        <include refid="selectIotDeviceVo"/>
        where d.device_num = #{deviceNum}
    </select>
        
    <insert id="insertIotDevice" parameterType="IotDevice" useGeneratedKeys="true" keyProperty="deviceId">
        insert into iot_device
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="deviceNum != null and deviceNum != ''">device_num,</if>
            <if test="categoryId != null">category_id,</if>
            <if test="deviceName != null and deviceName != ''">device_name,</if>
            <if test="firmwareVersion != null">firmware_version,</if>
            <if test="ownerId != null">owner_id,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="remark != null">remark,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="deviceNum != null and deviceNum != ''">#{deviceNum},</if>
            <if test="categoryId != null">#{categoryId},</if>
            <if test="deviceName != null and deviceName != ''">#{deviceName},</if>
            <if test="firmwareVersion != null">#{firmwareVersion},</if>
            <if test="ownerId != null">#{ownerId},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="remark != null">#{remark},</if>
         </trim>
    </insert>

    <update id="updateIotDevice" parameterType="IotDevice">
        update iot_device
        <trim prefix="SET" suffixOverrides=",">
            <if test="deviceNum != null and deviceNum != ''">device_num = #{deviceNum},</if>
            <if test="categoryId != null">category_id = #{categoryId},</if>
            <if test="deviceName != null and deviceName != ''">device_name = #{deviceName},</if>
            <if test="firmwareVersion != null">firmware_version = #{firmwareVersion},</if>
            <if test="ownerId != null">owner_id = #{ownerId},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="remark != null">remark = #{remark},</if>
        </trim>
        where device_id = #{deviceId}
    </update>

    <delete id="deleteIotDeviceById" parameterType="Long">
        update iot_device set del_flag= '2' where device_id = #{deviceId}
    </delete>

    <delete id="deleteIotDeviceByIds" parameterType="String">
        update iot_device set del_flag= '2' where device_id in
        <foreach item="deviceId" collection="array" open="(" separator="," close=")">
            #{deviceId}
        </foreach>
    </delete>
</mapper>