<?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.tdengine.mapper.DeviceStatusMapper">

    <select id="getOnlineDevicesGroupByDay" parameterType="com.ruoyi.tdengine.api.domain.DeviceStatus" resultType="com.ruoyi.tdengine.api.domain.DeviceStatus" >
        select timetruncate(ts,1d) ts,devicetype,onlinestatus,deviceid from #{params.databaseName}.#{params.superTableName}
        where onlinestatus=1
        <if test="startTime!= null and endTime != null and startTime!= 0 and endTime != 0">
            and ts BETWEEN #{startTime} AND #{endTime}
        </if>
        <if test="sceneId != null "> and scene_id = #{sceneId}</if>
        group by timetruncate(ts,1d),devicetype,onlinestatus,deviceid
    </select>


    <select id="getLastOnlineDevices" parameterType="com.ruoyi.tdengine.api.domain.DeviceStatus" resultType="java.util.Map" >
        select  <foreach item="item" collection="schemaFields" separator=","
                         index="">last_row(${item.fieldName})</foreach>,last_row(deviceid) from #{params.databaseName}.#{params.superTableName}
        <where>
        <if test="endTime!= null and endTime != 0">
            and ts &lt;= #{endTime}
        </if>
        </where>
        group by deviceid
    </select>



</mapper>
