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