<?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.limap.core.screen.mapper.DeviceSensorDataMapper">

    <select id="getHistoryDataList" resultType="com.limap.core.screen.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>

</mapper>
