Files
fishery-back/intc-modules/intc-tdengine/src/main/resources/mapper/tdengine/DeviceSensorDataMapper.xml

128 lines
4.9 KiB
XML

<?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.intc.tdengine.mapper.DeviceSensorDataMapper">
<update id="dropDB">
drop database if exists fishery
</update>
<update id="createDB">
create database if not exists fishery
</update>
<update id="createSuperTable">
create table if not exists
fishery.device_sensor_data(time timestamp,createTime timestamp, dissolvedOxygen double, temperature double , saturability double , ph double , salinity double , treference double , tfluorescence double , phaseDifference double , battery double)
tags(
tenant_id nchar(50),
serialNum nchar(100),
deviceId BIGINT,
userId BIGINT,
userName nchar(100),
mobilePhone nchar(20),
deviceName nchar(100),
deviceType int
)
</update>
<update id="createTable" parameterType="com.intc.tdengine.domain.DeviceSensorData">
create table if not exists
`fishery`.`t_${serialNum}`
using fishery.device_sensor_data
tags(#{tenantId},#{serialNum},#{deviceId},#{userId},#{userName},#{mobilePhone},#{deviceName},#{deviceType})
</update>
<select id="checkTableExists" resultType="java.lang.Integer">
SELECT COUNT(*) FROM information_schema.ins_tables
WHERE db_name = 'fishery' AND stable_name = 'device_sensor_data' AND table_name = 't_${serialNum}'
</select>
<insert id="batchInsertDeviceSensorData">
insert into
<foreach collection="dataList" item="data" open="" close="" separator=" ">
`fishery`.`t_${data.serialNum}`
using fishery.device_sensor_data
tags(#{data.tenantId},#{data.serialNum},#{data.deviceId},#{data.userId},#{data.userName},#{data.mobilePhone},#{data.deviceName},#{data.deviceType})
(time, createTime, dissolvedOxygen, temperature, saturability, ph, salinity, treference, tfluorescence, phaseDifference, battery)
values (#{data.time}, #{data.createTime}, #{data.dissolvedOxygen}, #{data.temperature}, #{data.saturability}, #{data.ph}, #{data.salinity}, #{data.treference}, #{data.tfluorescence}, #{data.phaseDifference}, #{data.battery})
</foreach>
</insert>
<select id="getHistoryDataList" resultType="com.intc.tdengine.domain.DeviceSensorData">
<if test="intervalType == null or intervalType == 1">
<!-- 原始数据,不聚合 -->
SELECT
`time`,
createTime,
dissolvedOxygen,
temperature,
saturability,
ph,
salinity,
treference,
tfluorescence,
phaseDifference,
battery,
serialNum,
deviceId,
mobilePhone,
deviceType
FROM `fishery`.`t_${serialNum}`
<where>
<if test="startTime != null and startTime != ''">AND `time` >= #{startTime}</if>
<if test="endTime != null and endTime != ''">AND `time` &lt;= #{endTime}</if>
</where>
</if>
<if test="intervalType != null and intervalType != 1">
<!-- 按时间间隔聚合 -->
SELECT
_wstart as `time`,
_wstart as createTime,
AVG(dissolvedOxygen) as dissolvedOxygen,
AVG(temperature) as temperature,
AVG(saturability) as saturability,
AVG(ph) as ph,
AVG(salinity) as salinity,
AVG(treference) as treference,
AVG(tfluorescence) as tfluorescence,
AVG(phaseDifference) as phaseDifference,
AVG(battery) as battery
FROM `fishery`.`t_${serialNum}`
<where>
<if test="startTime != null and startTime != ''">AND `time` >= #{startTime}</if>
<if test="endTime != null and endTime != ''">AND `time` &lt;= #{endTime}</if>
</where>
<choose>
<when test="intervalType == 2">INTERVAL(10m)</when>
<when test="intervalType == 3">INTERVAL(30m)</when>
<when test="intervalType == 4">INTERVAL(1h)</when>
<when test="intervalType == 5">INTERVAL(3h)</when>
<when test="intervalType == 6">INTERVAL(6h)</when>
</choose>
</if>
</select>
<select id="getLatestReportTime" resultType="com.intc.tdengine.domain.DeviceSensorData">
SELECT
`time`,
createTime
FROM `fishery`.`t_${serialNum}`
ORDER BY `time` DESC
LIMIT 1
</select>
<update id="dropSubTable">
DROP TABLE IF EXISTS `fishery`.`t_${serialNum}`
</update>
</mapper>