Files
MoviconHub/MoviconWebApi/API/DeviceAlarmApi/Data.cs
2025-09-17 17:22:09 +08:00

181 lines
7.1 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using MoviconWebApi.Entities;
namespace MoviconWebApi.API.DeviceAlarmApi
{
public static class DeviceAlarmData
{
/// <summary>
/// 获取设备报警列表
/// </summary>
public static async Task<(List<DeviceAlarmResponse> Items, long Total)> GetDeviceAlarmList(
IFreeSql freeSql,
DeviceAlarmRequest request)
{
try
{
var query = freeSql.Select<DeviceAlarm>()
.WhereIf(!string.IsNullOrWhiteSpace(request.DeviceCode),
a => a.DeviceCode == request.DeviceCode)
.WhereIf(request.DeviceState > 0,
a => a.DeviceState == request.DeviceState);
// 处理时间范围查询
if (!string.IsNullOrWhiteSpace(request.StartTime) && DateTime.TryParse(request.StartTime, out var startTime))
{
query = query.Where(a => a.StartTime >= startTime);
}
if (!string.IsNullOrWhiteSpace(request.EndTime) && DateTime.TryParse(request.EndTime, out var endTime))
{
query = query.Where(a => a.EndTime <= endTime);
}
// 统计总数
var total = await query.CountAsync();
// 参数兜底与限制
var pageNo = request.PageNo <= 0 ? 1 : request.PageNo;
var pageSize = request.PageSize <= 0 ? 100 : request.PageSize;
if (pageSize > 100) pageSize = 100;
var data = await query
.OrderByDescending(a => a.StartTime)
.Page(pageNo, pageSize)
.ToListAsync(a => new DeviceAlarmResponse
{
DeviceCode = a.DeviceCode,
DeviceName = a.DeviceName,
DeviceState = a.DeviceState,
AlarmMessage = a.AlarmMessage,
StartTime = a.StartTime.ToString("yyyy-MM-dd HH:mm:ss"),
EndTime = a.EndTime.ToString("yyyy-MM-dd HH:mm:ss")
});
return (data ?? new List<DeviceAlarmResponse>(), total);
}
catch (Exception ex)
{
throw new Exception($"获取设备报警数据失败: {ex.Message}", ex);
}
}
/// <summary>
/// 获取设备报警分页数据
/// </summary>
public static async Task<(List<DeviceAlarmResponse> Items, long Total)> GetDeviceAlarmPagedList(
IFreeSql freeSql,
DeviceAlarmPagedRequest request)
{
try
{
var query = freeSql.Select<DeviceAlarm>()
.WhereIf(!string.IsNullOrWhiteSpace(request.DeviceCode),
a => a.DeviceCode == request.DeviceCode)
.WhereIf(request.DeviceState > 0,
a => a.DeviceState == request.DeviceState);
// 处理时间范围查询
if (!string.IsNullOrWhiteSpace(request.StartTime) && DateTime.TryParse(request.StartTime, out var startTime))
{
query = query.Where(a => a.StartTime >= startTime);
}
if (!string.IsNullOrWhiteSpace(request.EndTime) && DateTime.TryParse(request.EndTime, out var endTime))
{
query = query.Where(a => a.EndTime <= endTime);
}
var total = await query.CountAsync();
var data = await query
.OrderByDescending(a => a.StartTime)
.Page(request.PageNumber, request.PageSize)
.ToListAsync(a => new DeviceAlarmResponse
{
DeviceCode = a.DeviceCode,
DeviceName = a.DeviceName,
DeviceState = a.DeviceState,
AlarmMessage = a.AlarmMessage,
StartTime = a.StartTime.ToString("yyyy-MM-dd HH:mm:ss"),
EndTime = a.EndTime.ToString("yyyy-MM-dd HH:mm:ss")
});
return (data ?? new List<DeviceAlarmResponse>(), total);
}
catch (Exception ex)
{
throw new Exception($"获取设备报警分页数据失败: {ex.Message}", ex);
}
}
/// <summary>
/// 获取最新的设备报警记录
/// </summary>
public static async Task<DeviceAlarmResponse?> GetLatestDeviceAlarm(
IFreeSql freeSql,
DeviceAlarmRequest request)
{
try
{
var query = freeSql.Select<DeviceAlarm>()
.WhereIf(!string.IsNullOrWhiteSpace(request.DeviceCode),
a => a.DeviceCode == request.DeviceCode)
.WhereIf(request.DeviceState > 0,
a => a.DeviceState == request.DeviceState);
var data = await query
.OrderByDescending(a => a.StartTime)
.FirstAsync(a => new DeviceAlarmResponse
{
DeviceCode = a.DeviceCode,
DeviceName = a.DeviceName,
DeviceState = a.DeviceState,
AlarmMessage = a.AlarmMessage,
StartTime = a.StartTime.ToString("yyyy-MM-dd HH:mm:ss"),
EndTime = a.EndTime.ToString("yyyy-MM-dd HH:mm:ss")
});
return data;
}
catch (Exception ex)
{
throw new Exception($"获取最新设备报警数据失败: {ex.Message}", ex);
}
}
/// <summary>
/// 获取指定设备的活动报警EndTime为最大值表示未结束
/// </summary>
public static async Task<List<DeviceAlarmResponse>> GetActiveAlarms(
IFreeSql freeSql,
string? deviceCode)
{
try
{
var query = freeSql.Select<DeviceAlarm>()
.WhereIf(!string.IsNullOrWhiteSpace(deviceCode),
a => a.DeviceCode == deviceCode)
.Where(a => a.EndTime == DateTime.MaxValue || a.EndTime > DateTime.Now);
var data = await query
.OrderByDescending(a => a.StartTime)
.ToListAsync(a => new DeviceAlarmResponse
{
DeviceCode = a.DeviceCode,
DeviceName = a.DeviceName,
DeviceState = a.DeviceState,
AlarmMessage = a.AlarmMessage,
StartTime = a.StartTime.ToString("yyyy-MM-dd HH:mm:ss"),
EndTime = a.EndTime.ToString("yyyy-MM-dd HH:mm:ss")
});
return data ?? new List<DeviceAlarmResponse>();
}
catch (Exception ex)
{
throw new Exception($"获取活动报警数据失败: {ex.Message}", ex);
}
}
}
}