添加项目文件。
This commit is contained in:
171
MoviconWebApi/API/DeviceAlarmApi/Data.cs
Normal file
171
MoviconWebApi/API/DeviceAlarmApi/Data.cs
Normal file
@@ -0,0 +1,171 @@
|
||||
using MoviconWebApi.Entities;
|
||||
|
||||
namespace MoviconWebApi.API.DeviceAlarmApi
|
||||
{
|
||||
public static class DeviceAlarmData
|
||||
{
|
||||
/// <summary>
|
||||
/// 获取设备报警列表
|
||||
/// </summary>
|
||||
public static async Task<List<DeviceAlarmResponse>> 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 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);
|
||||
}
|
||||
}
|
||||
|
||||
/// <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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
174
MoviconWebApi/API/DeviceAlarmApi/Endpoint.cs
Normal file
174
MoviconWebApi/API/DeviceAlarmApi/Endpoint.cs
Normal file
@@ -0,0 +1,174 @@
|
||||
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
|
||||
{
|
||||
var data = await DeviceAlarmData.GetDeviceAlarmList(_freeSql, req);
|
||||
Response = ApiResponse<List<DeviceAlarmResponse>>.Success(data, "success");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Response = ApiResponse<List<DeviceAlarmResponse>>.Error("500", $"获取数据失败: {ex.Message}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <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 (items, total) = await DeviceAlarmData.GetDeviceAlarmPagedList(_freeSql, req);
|
||||
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}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
42
MoviconWebApi/API/DeviceAlarmApi/Mapper.cs
Normal file
42
MoviconWebApi/API/DeviceAlarmApi/Mapper.cs
Normal file
@@ -0,0 +1,42 @@
|
||||
using MoviconWebApi.Entities;
|
||||
|
||||
namespace MoviconWebApi.API.DeviceAlarmApi
|
||||
{
|
||||
/// <summary>
|
||||
/// 设备报警数据映射器
|
||||
/// </summary>
|
||||
public static class DeviceAlarmMapper
|
||||
{
|
||||
/// <summary>
|
||||
/// 将实体映射到响应模型
|
||||
/// </summary>
|
||||
public static DeviceAlarmResponse ToResponse(this DeviceAlarm entity)
|
||||
{
|
||||
return new DeviceAlarmResponse
|
||||
{
|
||||
DeviceCode = entity.DeviceCode,
|
||||
DeviceName = entity.DeviceName,
|
||||
DeviceState = entity.DeviceState,
|
||||
AlarmMessage = entity.AlarmMessage,
|
||||
StartTime = entity.StartTime.ToString("yyyy-MM-dd HH:mm:ss"),
|
||||
EndTime = entity.EndTime.ToString("yyyy-MM-dd HH:mm:ss")
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 批量映射
|
||||
/// </summary>
|
||||
public static List<DeviceAlarmResponse> ToResponseList(this IEnumerable<DeviceAlarm> entities)
|
||||
{
|
||||
return entities.Select(e => e.ToResponse()).ToList();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 判断报警是否活动
|
||||
/// </summary>
|
||||
public static bool IsActive(this DeviceAlarm entity)
|
||||
{
|
||||
return entity.EndTime == DateTime.MaxValue || entity.EndTime > DateTime.Now;
|
||||
}
|
||||
}
|
||||
}
|
||||
96
MoviconWebApi/API/DeviceAlarmApi/Models.cs
Normal file
96
MoviconWebApi/API/DeviceAlarmApi/Models.cs
Normal file
@@ -0,0 +1,96 @@
|
||||
namespace MoviconWebApi.API.DeviceAlarmApi
|
||||
{
|
||||
/// <summary>
|
||||
/// 设备报警查询请求
|
||||
/// </summary>
|
||||
public class DeviceAlarmRequest
|
||||
{
|
||||
/// <summary>
|
||||
/// 设备编号
|
||||
/// </summary>
|
||||
public string? DeviceCode { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 开始时间
|
||||
/// </summary>
|
||||
public string? StartTime { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 结束时间
|
||||
/// </summary>
|
||||
public string? EndTime { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 设备状态(0表示返回所有状态记录)
|
||||
/// </summary>
|
||||
public int DeviceState { get; set; } = 0;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设备报警分页查询请求
|
||||
/// </summary>
|
||||
public class DeviceAlarmPagedRequest : DeviceAlarmRequest
|
||||
{
|
||||
/// <summary>
|
||||
/// 页码
|
||||
/// </summary>
|
||||
public int PageNumber { get; set; } = 1;
|
||||
|
||||
/// <summary>
|
||||
/// 每页大小
|
||||
/// </summary>
|
||||
public int PageSize { get; set; } = 10;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设备报警响应
|
||||
/// </summary>
|
||||
public class DeviceAlarmResponse
|
||||
{
|
||||
/// <summary>
|
||||
/// 设备编号
|
||||
/// </summary>
|
||||
public string? DeviceCode { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 设备名称
|
||||
/// </summary>
|
||||
public string? DeviceName { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 设备状态
|
||||
/// </summary>
|
||||
public int? DeviceState { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 报警信息
|
||||
/// </summary>
|
||||
public string? AlarmMessage { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 开始时间
|
||||
/// </summary>
|
||||
public string? StartTime { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 结束时间
|
||||
/// </summary>
|
||||
public string? EndTime { get; set; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设备报警分页响应
|
||||
/// </summary>
|
||||
public class DeviceAlarmPagedResponse
|
||||
{
|
||||
/// <summary>
|
||||
/// 数据列表
|
||||
/// </summary>
|
||||
public List<DeviceAlarmResponse> Items { get; set; } = new List<DeviceAlarmResponse>();
|
||||
|
||||
/// <summary>
|
||||
/// 总数
|
||||
/// </summary>
|
||||
public long Total { get; set; }
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user