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

198 lines
6.7 KiB
C#

using Azure;
using FastEndpoints;
using MoviconWebApi.Common;
using MoviconWebApi.Entities;
using System;
namespace MoviconWebApi.API.DeviceAlarmApi
{
/// <summary>
/// 设备报警列表查询端点
/// </summary>
public class GetDeviceAlarmListEndpoint : Endpoint<DeviceAlarmRequest, ApiResponse<List<DeviceAlarmResponse>>>
{
private readonly IFreeSql _freeSql;
public GetDeviceAlarmListEndpoint(IFreeSql freeSql)
{
_freeSql = freeSql;
}
public override void Configure()
{
Get("/devicealarm/list");
AllowAnonymous();
Summary(s =>
{
s.Summary = "获取设备报警列表";
s.Description = "根据设备编号、时间范围和设备状态查询设备报警";
s.Response<ApiResponse<List<DeviceAlarmResponse>>>(200, "成功返回设备报警列表");
});
}
public override async Task HandleAsync(DeviceAlarmRequest req, CancellationToken ct)
{
try
{
// 参数兜底
if (req.PageNo <= 0) req.PageNo = 1;
if (req.PageSize <= 0) req.PageSize = 100;
if (req.PageSize > 100) req.PageSize = 100;
var listResult = await DeviceAlarmData.GetDeviceAlarmList(_freeSql, req);
var items = listResult.Item1;
var total = listResult.Item2;
var resp = ApiResponse<List<DeviceAlarmResponse>>.Success(
items ?? new List<DeviceAlarmResponse>(),
(items != null && items.Count > 0) ? "请求成功" : "暂无数据");
resp.pagination = new MoviconWebApi.Common.Pagination
{
total = total,
count = items?.Count ?? 0,
pageNo = req.PageNo,
totalPage = req.PageSize > 0 ? (int)Math.Ceiling((double)total / req.PageSize) : 0,
pageSize = req.PageSize
};
Response = resp;
}
catch (Exception ex)
{
Response = ApiResponse<List<DeviceAlarmResponse>>.Error("500", $"获取数据失败: {ex.Message}", new List<DeviceAlarmResponse>());
}
}
}
/// <summary>
/// 设备报警分页查询端点
/// </summary>
public class GetDeviceAlarmPagedEndpoint : Endpoint<DeviceAlarmPagedRequest, ApiResponse<DeviceAlarmPagedResponse>>
{
private readonly IFreeSql _freeSql;
public GetDeviceAlarmPagedEndpoint(IFreeSql freeSql)
{
_freeSql = freeSql;
}
public override void Configure()
{
Post("/devicealarm/paged");
AllowAnonymous();
Summary(s =>
{
s.Summary = "分页获取设备报警";
s.Description = "根据设备编号、时间范围和设备状态分页查询设备报警";
s.Response<ApiResponse<DeviceAlarmPagedResponse>>(200, "成功返回设备报警分页数据");
});
}
public override async Task HandleAsync(DeviceAlarmPagedRequest req, CancellationToken ct)
{
try
{
var result = await DeviceAlarmData.GetDeviceAlarmPagedList(_freeSql, req);
var items = result.Items;
var total = result.Total;
var pagedResponse = new DeviceAlarmPagedResponse
{
Items = items,
Total = total
};
Response = ApiResponse<DeviceAlarmPagedResponse>.Success(pagedResponse, "success");
}
catch (Exception ex)
{
Response = ApiResponse<DeviceAlarmPagedResponse>.Error("500", $"获取数据失败: {ex.Message}");
}
}
}
/// <summary>
/// 获取最新设备报警端点
/// </summary>
public class GetLatestDeviceAlarmEndpoint : Endpoint<DeviceAlarmRequest, ApiResponse<DeviceAlarmResponse>>
{
private readonly IFreeSql _freeSql;
public GetLatestDeviceAlarmEndpoint(IFreeSql freeSql)
{
_freeSql = freeSql;
}
public override void Configure()
{
Get("/devicealarm/latest");
AllowAnonymous();
Summary(s =>
{
s.Summary = "获取最新设备报警";
s.Description = "根据设备编号获取最新的设备报警记录";
s.Response<ApiResponse<DeviceAlarmResponse>>(200, "成功返回最新设备报警");
});
}
public override async Task HandleAsync(DeviceAlarmRequest req, CancellationToken ct)
{
try
{
var data = await DeviceAlarmData.GetLatestDeviceAlarm(_freeSql, req);
if (data == null)
{
Response = ApiResponse<DeviceAlarmResponse>.Error("404", "未找到设备报警记录");
}
else
{
Response = ApiResponse<DeviceAlarmResponse>.Success(data, "success");
}
}
catch (Exception ex)
{
Response = ApiResponse<DeviceAlarmResponse>.Error("500", $"获取数据失败: {ex.Message}");
}
}
}
/// <summary>
/// 获取活动报警端点
/// </summary>
public class GetActiveAlarmsEndpoint : Endpoint<DeviceAlarmRequest, ApiResponse<List<DeviceAlarmResponse>>>
{
private readonly IFreeSql _freeSql;
public GetActiveAlarmsEndpoint(IFreeSql freeSql)
{
_freeSql = freeSql;
}
public override void Configure()
{
Get("/devicealarm/active");
AllowAnonymous();
Summary(s =>
{
s.Summary = "获取活动报警";
s.Description = "获取指定设备的所有活动(未结束)报警";
s.Response<ApiResponse<List<DeviceAlarmResponse>>>(200, "成功返回活动报警列表");
});
}
public override async Task HandleAsync(DeviceAlarmRequest req, CancellationToken ct)
{
try
{
var data = await DeviceAlarmData.GetActiveAlarms(_freeSql, req.DeviceCode);
Response = ApiResponse<List<DeviceAlarmResponse>>.Success(data, "success");
}
catch (Exception ex)
{
Response = ApiResponse<List<DeviceAlarmResponse>>.Error("500", $"获取数据失败: {ex.Message}");
}
}
}
}