温度看板搭建示例
This commit is contained in:
@@ -1,12 +1,70 @@
|
||||
using Model;
|
||||
using Model.Entity;
|
||||
using ORM;
|
||||
using Service.Interface;
|
||||
using SqlSugar;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Service.Implement
|
||||
{
|
||||
/// <summary>
|
||||
/// 设备看板 服务实现
|
||||
/// 设备看板服务实现(温湿度)
|
||||
/// </summary>
|
||||
public class DeviceDashboardService : IDeviceDashboardService
|
||||
{
|
||||
// TODO: 实现 设备看板 相关方法
|
||||
/// <summary>
|
||||
/// 获取每台设备最新一条温湿度数据(看板卡片用)
|
||||
/// </summary>
|
||||
public async Task<Result<List<TemperatureBoxDataEntity>>> GetRealtimeAsync()
|
||||
{
|
||||
try
|
||||
{
|
||||
var db = SqlSugarContext.DbContext;
|
||||
|
||||
// 雪花 Id 单调递增,每台设备取最大 Id 即最新一条
|
||||
var latestIds = await db.Queryable<TemperatureBoxDataEntity>()
|
||||
.Where(x => x.IsDel == 0)
|
||||
.GroupBy(x => x.DeviceCode)
|
||||
.Select(x => SqlFunc.AggregateMax(x.Id))
|
||||
.ToListAsync();
|
||||
|
||||
var list = await db.Queryable<TemperatureBoxDataEntity>()
|
||||
.Where(x => latestIds.Contains(x.Id))
|
||||
.OrderBy(x => x.DeviceCode)
|
||||
.ToListAsync();
|
||||
|
||||
return Result<List<TemperatureBoxDataEntity>>.Success(list);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return Result<List<TemperatureBoxDataEntity>>.Error("查询设备实时数据失败", ex);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取指定设备最近 N 分钟的温湿度曲线数据(按时间升序)
|
||||
/// </summary>
|
||||
public async Task<Result<List<TemperatureBoxDataEntity>>> GetCurveAsync(string deviceCode, int minutes)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(deviceCode))
|
||||
return Result<List<TemperatureBoxDataEntity>>.Error("设备标识不能为空");
|
||||
|
||||
try
|
||||
{
|
||||
var start = DateTime.Now.AddMinutes(-minutes);
|
||||
var list = await SqlSugarContext.DbContext.Queryable<TemperatureBoxDataEntity>()
|
||||
.Where(x => x.DeviceCode == deviceCode && x.IsDel == 0 && x.CreateTime >= start)
|
||||
.OrderBy(x => x.CreateTime)
|
||||
.ToListAsync();
|
||||
|
||||
return Result<List<TemperatureBoxDataEntity>>.Success(list);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return Result<List<TemperatureBoxDataEntity>>.Error("查询温湿度曲线失败", ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user