<?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.shzg.project.worn.mapper.MqttSensorDeviceMapper">

    <!-- ================= resultMap ================= -->
    <resultMap type="MqttSensorDevice" id="MqttSensorDeviceResult">
        <result property="id" column="id"/>
        <result property="devEui" column="dev_eui"/>
        <result property="deviceName" column="device_name"/>
        <result property="deviceType" column="device_type"/>
        <result property="deptId" column="dept_id"/>
        <result property="deptName" column="dept_name"/>
        <result property="reportIntervalMinute" column="report_interval_minute"/>
        <result property="status" column="status"/>
        <result property="remark" column="remark"/>
        <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="isDelete" column="is_delete"/>
    </resultMap>

    <!-- ================= 公共查询SQL ================= -->
    <sql id="selectMqttSensorDeviceVo">
        select
            d.id,
            d.dev_eui,
            d.device_name,
            d.device_type,
            d.dept_id,
            dept.dept_name,
            d.report_interval_minute,
            d.status,
            d.remark,
            d.create_by,
            d.create_time,
            d.update_by,
            d.update_time,
            d.is_delete
        from mqtt_sensor_device d
                 left join sys_dept dept on d.dept_id = dept.dept_id
    </sql>

    <!-- ================= 查询列表 ================= -->
    <select id="selectMqttSensorDeviceList" parameterType="MqttSensorDevice" resultMap="MqttSensorDeviceResult">
        <include refid="selectMqttSensorDeviceVo"/>
        <where>
            <if test="devEui != null and devEui != ''">
                and d.dev_eui = #{devEui}
            </if>
            <if test="deviceName != null and deviceName != ''">
                and d.device_name like concat('%', #{deviceName}, '%')
            </if>
            <if test="deviceType != null and deviceType != ''">
                and d.device_type = #{deviceType}
            </if>
            <if test="deptId != null">
                and d.dept_id = #{deptId}
            </if>
            <if test="reportIntervalMinute != null">
                and d.report_interval_minute = #{reportIntervalMinute}
            </if>
            <if test="status != null and status != ''">
                and d.status = #{status}
            </if>
            <if test="isDelete != null and isDelete != ''">
                and d.is_delete = #{isDelete}
            </if>
        </where>
    </select>

    <select id="selectAllForMqttCache" resultMap="MqttSensorDeviceResult">
        <include refid="selectMqttSensorDeviceVo"/>
        where d.is_delete = '0'
    </select>

    <!-- ================= 根据ID查询 ================= -->
    <select id="selectMqttSensorDeviceById" parameterType="Long" resultMap="MqttSensorDeviceResult">
        <include refid="selectMqttSensorDeviceVo"/>
        where d.id = #{id}
    </select>

    <!-- ================= 新增 ================= -->
    <insert id="insertMqttSensorDevice" parameterType="MqttSensorDevice" useGeneratedKeys="true" keyProperty="id">
        insert into mqtt_sensor_device
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="devEui != null and devEui != ''">dev_eui,</if>
            <if test="deviceName != null">device_name,</if>
            <if test="deviceType != null">device_type,</if>
            <if test="deptId != null">dept_id,</if>
            <if test="reportIntervalMinute != null">report_interval_minute,</if>
            <if test="status != null">status,</if>
            <if test="remark != null">remark,</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="isDelete != null">is_delete,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="devEui != null and devEui != ''">#{devEui},</if>
            <if test="deviceName != null">#{deviceName},</if>
            <if test="deviceType != null">#{deviceType},</if>
            <if test="deptId != null">#{deptId},</if>
            <if test="reportIntervalMinute != null">#{reportIntervalMinute},</if>
            <if test="status != null">#{status},</if>
            <if test="remark != null">#{remark},</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="isDelete != null">#{isDelete},</if>
        </trim>
    </insert>

    <!-- ================= 修改 ================= -->
    <update id="updateMqttSensorDevice" parameterType="MqttSensorDevice">
        update mqtt_sensor_device
        <trim prefix="SET" suffixOverrides=",">
            <if test="devEui != null and devEui != ''">dev_eui = #{devEui},</if>
            <if test="deviceName != null">device_name = #{deviceName},</if>
            <if test="deviceType != null">device_type = #{deviceType},</if>
            <if test="deptId != null">dept_id = #{deptId},</if>
            <if test="reportIntervalMinute != null">report_interval_minute = #{reportIntervalMinute},</if>
            <if test="status != null">status = #{status},</if>
            <if test="remark != null">remark = #{remark},</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="isDelete != null">is_delete = #{isDelete},</if>
        </trim>
        where id = #{id}
    </update>

    <update id="updateRuntimeStatus" parameterType="MqttSensorDevice">
        update mqtt_sensor_device
        set status = #{status},
            update_time = #{updateTime}
        where id = #{id}
        and status != #{status}
    </update>

    <!-- ================= 删除 ================= -->
    <delete id="deleteMqttSensorDeviceById" parameterType="Long">
        delete from mqtt_sensor_device where id = #{id}
    </delete>

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

    <!-- ================= 根据DevEUI查询 ================= -->
    <select id="selectByDevEui" parameterType="String" resultMap="MqttSensorDeviceResult">
        <include refid="selectMqttSensorDeviceVo"/>
        where d.dev_eui = #{devEui}
        and d.is_delete = '0'
        limit 1
    </select>

    <select id="countDeviceStat" resultType="map">
        SELECT
        COUNT(1) AS total,
        COALESCE(SUM(CASE WHEN status = 0 THEN 1 ELSE 0 END),0) AS online,
        COALESCE(SUM(CASE WHEN status = 1 THEN 1 ELSE 0 END),0) AS offline,
        CASE
        WHEN COUNT(1) = 0 THEN 0
        ELSE ROUND(
        SUM(CASE WHEN status = 0 THEN 1 ELSE 0 END) / COUNT(1) * 100,
        1
        )
        END AS onlineRate
        FROM mqtt_sensor_device
        WHERE is_delete = 0
        AND dept_id IN
        <foreach collection="deptIds" item="id" open="(" separator="," close=")">
            #{id}
        </foreach>
    </select>

    <select id="selectOfflineDeviceList" resultMap="MqttSensorDeviceResult">
        <include refid="selectMqttSensorDeviceVo"/>
        where d.is_delete = '0'
        and d.status = '0'
        and (
        exists (
        select 1
        from mqtt_sensor_data sd
        where sd.device_id = d.id
        and sd.is_delete = '0'
        )
        and (
        select max(sd.create_time)
        from mqtt_sensor_data sd
        where sd.device_id = d.id
        and sd.is_delete = '0'
        ) &lt; DATE_SUB(
        NOW(),
        INTERVAL (IFNULL(d.report_interval_minute, 10) * 3) MINUTE
        )
        )
    </select>

</mapper>
