<?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.IotDeviceStatusMapper">
    
    <resultMap type="IotDeviceStatus" id="IotDeviceStatusResult">
        <result property="deviceStatusId"    column="device_status_id"    />
        <result property="deviceId"    column="device_id"    />
        <result property="deviceNum"    column="device_num"    />
        <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"    />
        <result property="airTemperature"    column="air_temperature"    />
        <result property="airHumidity"    column="air_humidity"    />
        <result property="triggerSource"    column="trigger_source"    />
        <result property="brightness"    column="brightness"    />
        <result property="lightInterval"    column="light_interval"    />
        <result property="lightMode"    column="light_mode"    />
        <result property="fadeTime"    column="fade_time"    />
        <result property="red"    column="red"    />
        <result property="green"    column="green"    />
        <result property="blue"    column="blue"    />
        <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>

    <sql id="selectIotDeviceStatusVo">
        select device_status_id, device_id, device_num, relay_status, light_status, is_online, device_temperature, rssi, air_temperature, air_humidity, trigger_source, brightness, light_interval, light_mode,fade_time, red, green, blue, create_by, create_time, update_by, update_time, remark from iot_device_status
    </sql>

    <select id="selectIotDeviceStatusList" parameterType="IotDeviceStatus" resultMap="IotDeviceStatusResult">
        <include refid="selectIotDeviceStatusVo"/>
        <where>
            del_flag= '0'
            <if test="deviceId != null "> and device_id = #{deviceId}</if>
            <if test="deviceNum != null  and deviceNum != ''"> and device_num = #{deviceNum}</if>
            <if test="relayStatus != null "> and relay_status = #{relayStatus}</if>
            <if test="lightStatus != null "> and light_status = #{lightStatus}</if>
            <if test="isOnline != null "> and is_online = #{isOnline}</if>
            <if test="triggerSource != null "> and trigger_source = #{triggerSource}</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>
        order by create_time desc
    </select>
    
    <select id="selectIotDeviceStatusById" parameterType="Long" resultMap="IotDeviceStatusResult">
        <include refid="selectIotDeviceStatusVo"/>
        where device_status_id = #{deviceStatusId}
    </select>

    <select id="selectIotDeviceStatusByDeviceId" parameterType="Long" resultMap="IotDeviceStatusResult">
        <include refid="selectIotDeviceStatusVo"/>
        where device_id = #{deviceId}
        order by create_time desc
        limit 1
    </select>

    <select id="selectIotDeviceStatusByDeviceNum" parameterType="String" resultMap="IotDeviceStatusResult">
        <include refid="selectIotDeviceStatusVo"/>
        where device_num = #{deviceNum}
        order by create_time desc
        limit 1
    </select>
        
    <insert id="insertIotDeviceStatus" parameterType="IotDeviceStatus" useGeneratedKeys="true" keyProperty="deviceStatusId">
        insert into iot_device_status
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="deviceId != null">device_id,</if>
            <if test="deviceNum != null and deviceNum != ''">device_num,</if>
            <if test="relayStatus != null">relay_status,</if>
            <if test="lightStatus != null">light_status,</if>
            <if test="isOnline != null">is_online,</if>
            <if test="deviceTemperature != null">device_temperature,</if>
            <if test="rssi != null">rssi,</if>
            <if test="airTemperature != null">air_temperature,</if>
            <if test="airHumidity != null">air_humidity,</if>
            <if test="triggerSource != null">trigger_source,</if>
            <if test="brightness != null">brightness,</if>
            <if test="lightInterval != null">light_interval,</if>
            <if test="lightMode != null">light_mode,</if>
            <if test="fadeTime != null">fade_time,</if>
            <if test="red != null">red,</if>
            <if test="green != null">green,</if>
            <if test="blue != null">blue,</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="deviceId != null">#{deviceId},</if>
            <if test="deviceNum != null and deviceNum != ''">#{deviceNum},</if>
            <if test="relayStatus != null">#{relayStatus},</if>
            <if test="lightStatus != null">#{lightStatus},</if>
            <if test="isOnline != null">#{isOnline},</if>
            <if test="deviceTemperature != null">#{deviceTemperature},</if>
            <if test="rssi != null">#{rssi},</if>
            <if test="airTemperature != null">#{airTemperature},</if>
            <if test="airHumidity != null">#{airHumidity},</if>
            <if test="triggerSource != null">#{triggerSource},</if>
            <if test="brightness != null">#{brightness},</if>
            <if test="lightInterval != null">#{lightInterval},</if>
            <if test="lightMode != null">#{lightMode},</if>
            <if test="fadeTime != null">#{fadeTime},</if>
            <if test="red != null">#{red},</if>
            <if test="green != null">#{green},</if>
            <if test="blue != null">#{blue},</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="updateIotDeviceStatus" parameterType="IotDeviceStatus">
        update iot_device_status
        <trim prefix="SET" suffixOverrides=",">
            <if test="deviceId != null">device_id = #{deviceId},</if>
            <if test="deviceNum != null and deviceNum != ''">device_num = #{deviceNum},</if>
            <if test="relayStatus != null">relay_status = #{relayStatus},</if>
            <if test="lightStatus != null">light_status = #{lightStatus},</if>
            <if test="isOnline != null">is_online = #{isOnline},</if>
            <if test="deviceTemperature != null">device_temperature = #{deviceTemperature},</if>
            <if test="rssi != null">rssi = #{rssi},</if>
            <if test="airTemperature != null">air_temperature = #{airTemperature},</if>
            <if test="airHumidity != null">air_humidity = #{airHumidity},</if>
            <if test="triggerSource != null">trigger_source = #{triggerSource},</if>
            <if test="brightness != null">brightness = #{brightness},</if>
            <if test="lightInterval != null">light_interval = #{lightInterval},</if>
            <if test="lightMode != null">light_mode = #{lightMode},</if>
            <if test="fadeTime != null">fade_time = #{fadeTime},</if>
            <if test="red != null">red = #{red},</if>
            <if test="green != null">green = #{green},</if>
            <if test="blue != null">blue = #{blue},</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_status_id = #{deviceStatusId}
    </update>

    <delete id="deleteIotDeviceStatusById" parameterType="Long">
        update iot_device_status set del_flag= '2' where device_status_id = #{deviceStatusId}
    </delete>

    <delete id="deleteIotDeviceStatusByIds" parameterType="String">
        update iot_device_status set del_flag= '2' where device_status_id in
        <foreach item="deviceStatusId" collection="array" open="(" separator="," close=")">
            #{deviceStatusId}
        </foreach>
    </delete>
</mapper>