Files
2025-09-15 17:59:48 +08:00

81 lines
2.6 KiB
C#
Raw Permalink 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 Azure;
using FastEndpoints;
using Microsoft.Extensions.Logging;
using MoviconWebApi.Common;
namespace MoviconWebApi.API.ClearStaticApi
{
/// <summary>
/// 获取清洗统计数据端点
/// </summary>
public class Endpoint : Endpoint<ClearStaticRequest, ApiResponse<ClearStaticResponse>>
{
private readonly IFreeSql _freeSql;
/// <summary>
/// 构造函数注入FreeSql实例
/// </summary>
/// <param name="freeSql">FreeSql实例</param>
public Endpoint(IFreeSql freeSql)
{
_freeSql = freeSql;
}
public override void Configure()
{
// 配置路由支持GET方式
Get("/clearstatic/summary");
// 允许匿名访问(根据实际需求可以改为需要认证)
AllowAnonymous();
// 配置摘要信息用于Swagger文档
Summary(s =>
{
s.Summary = "获取清洗统计数据";
s.Description = "根据设备编号获取清洗统计数据,包括作业数量、作业时长等";
s.Response<ApiResponse<ClearStaticResponse>>(200, "成功返回清洗统计数据");
s.Response(404, "未找到数据");
s.Response(500, "服务器内部错误");
});
}
public override async Task HandleAsync(ClearStaticRequest request, CancellationToken ct)
{
try
{
// 调用Data层方法获取数据
var staticData = await Data.GetClearStatic(request, _freeSql);
if (staticData == null)
{
// 未找到数据返回空数据但状态码200
Response = ApiResponse<ClearStaticResponse>.Success(
new ClearStaticResponse(),
"暂无数据"
);
}
else
{
// 返回统计数据
Response = ApiResponse<ClearStaticResponse>.Success(
staticData,
"查询成功"
);
}
}
catch (Exception ex)
{
// 记录错误日志
Logger.LogError(ex, "获取清洗统计数据失败");
// 返回错误响应
Response = ApiResponse<ClearStaticResponse>.Error(
"500",
"服务器内部错误"
);
}
}
}
}