根据新需求更改
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
using MoviconWebApi.Entities;
|
||||
using MoviconWebApi.Entities;
|
||||
|
||||
namespace MoviconWebApi.API.DeviceAlarmApi
|
||||
{
|
||||
@@ -7,7 +7,7 @@ namespace MoviconWebApi.API.DeviceAlarmApi
|
||||
/// <summary>
|
||||
/// 获取设备报警列表
|
||||
/// </summary>
|
||||
public static async Task<List<DeviceAlarmResponse>> GetDeviceAlarmList(
|
||||
public static async Task<(List<DeviceAlarmResponse> Items, long Total)> GetDeviceAlarmList(
|
||||
IFreeSql freeSql,
|
||||
DeviceAlarmRequest request)
|
||||
{
|
||||
@@ -30,8 +30,17 @@ namespace MoviconWebApi.API.DeviceAlarmApi
|
||||
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,
|
||||
@@ -42,7 +51,7 @@ namespace MoviconWebApi.API.DeviceAlarmApi
|
||||
EndTime = a.EndTime.ToString("yyyy-MM-dd HH:mm:ss")
|
||||
});
|
||||
|
||||
return data ?? new List<DeviceAlarmResponse>();
|
||||
return (data ?? new List<DeviceAlarmResponse>(), total);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
using Azure;
|
||||
using Azure;
|
||||
using FastEndpoints;
|
||||
using MoviconWebApi.Common;
|
||||
using MoviconWebApi.Entities;
|
||||
@@ -35,12 +35,33 @@ namespace MoviconWebApi.API.DeviceAlarmApi
|
||||
{
|
||||
try
|
||||
{
|
||||
var data = await DeviceAlarmData.GetDeviceAlarmList(_freeSql, req);
|
||||
Response = ApiResponse<List<DeviceAlarmResponse>>.Success(data, "success");
|
||||
// 参数兜底
|
||||
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}");
|
||||
Response = ApiResponse<List<DeviceAlarmResponse>>.Error("500", $"获取数据失败: {ex.Message}", new List<DeviceAlarmResponse>());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -73,7 +94,9 @@ namespace MoviconWebApi.API.DeviceAlarmApi
|
||||
{
|
||||
try
|
||||
{
|
||||
var (items, total) = await DeviceAlarmData.GetDeviceAlarmPagedList(_freeSql, req);
|
||||
var result = await DeviceAlarmData.GetDeviceAlarmPagedList(_freeSql, req);
|
||||
var items = result.Items;
|
||||
var total = result.Total;
|
||||
var pagedResponse = new DeviceAlarmPagedResponse
|
||||
{
|
||||
Items = items,
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
namespace MoviconWebApi.API.DeviceAlarmApi
|
||||
namespace MoviconWebApi.API.DeviceAlarmApi
|
||||
{
|
||||
/// <summary>
|
||||
/// 设备报警查询请求
|
||||
@@ -24,6 +24,16 @@
|
||||
/// 设备状态(0表示返回所有状态记录)
|
||||
/// </summary>
|
||||
public int DeviceState { get; set; } = 0;
|
||||
|
||||
/// <summary>
|
||||
/// 当前页码(从1开始)
|
||||
/// </summary>
|
||||
public int PageNo { get; set; } = 1;
|
||||
|
||||
/// <summary>
|
||||
/// 分页大小
|
||||
/// </summary>
|
||||
public int PageSize { get; set; } = 100;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
Reference in New Issue
Block a user