添加项目文件。
This commit is contained in:
257
MoviconWebApi/API/ClearDataQrApi/Endpoint.cs
Normal file
257
MoviconWebApi/API/ClearDataQrApi/Endpoint.cs
Normal file
@@ -0,0 +1,257 @@
|
||||
using Azure;
|
||||
using FastEndpoints;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using MoviconWebApi.Common;
|
||||
|
||||
namespace MoviconWebApi.API.ClearDataQrApi
|
||||
{
|
||||
/// <summary>
|
||||
/// 根据设备编号和部件二维码查询清洗数据端点
|
||||
/// </summary>
|
||||
public class GetByQrEndpoint : Endpoint<ClearDataQrRequest, ApiResponse<List<ClearDataQrResponse>>>
|
||||
{
|
||||
private readonly IFreeSql _freeSql;
|
||||
|
||||
public GetByQrEndpoint(IFreeSql freeSql)
|
||||
{
|
||||
_freeSql = freeSql;
|
||||
}
|
||||
|
||||
public override void Configure()
|
||||
{
|
||||
Get("/cleardata-qr/list");
|
||||
AllowAnonymous();
|
||||
|
||||
Summary(s =>
|
||||
{
|
||||
s.Summary = "根据设备编号和部件二维码查询清洗数据";
|
||||
s.Description = "可以通过设备编号、部件二维码或两者组合查询清洗数据";
|
||||
s.Response<ApiResponse<List<ClearDataQrResponse>>>(200, "成功返回清洗数据列表");
|
||||
s.Response(404, "未找到数据");
|
||||
s.Response(500, "服务器内部错误");
|
||||
});
|
||||
}
|
||||
|
||||
public override async Task HandleAsync(ClearDataQrRequest request, CancellationToken ct)
|
||||
{
|
||||
try
|
||||
{
|
||||
var dataList = await Data.GetClearDataByQr(request, _freeSql);
|
||||
|
||||
if (dataList == null || dataList.Count == 0)
|
||||
{
|
||||
Response = ApiResponse<List<ClearDataQrResponse>>.Success(
|
||||
new List<ClearDataQrResponse>(), "暂无数据");// "暂无数据"
|
||||
}
|
||||
else
|
||||
{
|
||||
Response = ApiResponse<List<ClearDataQrResponse>>.Success(
|
||||
dataList, "查询成功");// "查询成功"
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
//Logger.LogError(ex, "根据二维码查询清洗数据失败");
|
||||
//await Send.ErrorsAsync(500, ct);
|
||||
|
||||
Logger.LogError(ex, "根据二维码查询清洗数据失败");
|
||||
Response = ApiResponse<List<ClearDataQrResponse>>.Error(
|
||||
"500",
|
||||
"服务器内部错误"
|
||||
);
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 分页查询清洗数据端点
|
||||
/// </summary>
|
||||
public class GetPagedByQrEndpoint : Endpoint<ClearDataQrPagedRequest, ClearDataQrPagedResponse>
|
||||
{
|
||||
private readonly IFreeSql _freeSql;
|
||||
|
||||
public GetPagedByQrEndpoint(IFreeSql freeSql)
|
||||
{
|
||||
_freeSql = freeSql;
|
||||
}
|
||||
|
||||
public override void Configure()
|
||||
{
|
||||
Post("/cleardata-qr/paged");
|
||||
AllowAnonymous();
|
||||
|
||||
Summary(s =>
|
||||
{
|
||||
s.Summary = "分页查询清洗数据";
|
||||
s.Description = "根据设备编号和部件二维码分页查询清洗数据";
|
||||
s.Response<ClearDataQrPagedResponse>(200, "成功返回分页数据");
|
||||
s.Response(500, "服务器内部错误");
|
||||
});
|
||||
}
|
||||
|
||||
public override async Task HandleAsync(ClearDataQrPagedRequest request, CancellationToken ct)
|
||||
{
|
||||
try
|
||||
{
|
||||
// 验证分页参数
|
||||
if (request.PageIndex < 1)
|
||||
request.PageIndex = 1;
|
||||
if (request.PageSize < 1)
|
||||
request.PageSize = 20;
|
||||
if (request.PageSize > 100)
|
||||
request.PageSize = 100;
|
||||
|
||||
// 调用Data层方法,获取元组返回值
|
||||
var (data, total) = await Data.GetClearDataPagedByQr(request, _freeSql);
|
||||
|
||||
// 构建响应对象
|
||||
Response = new ClearDataQrPagedResponse
|
||||
{
|
||||
Items = data,
|
||||
TotalCount = total,
|
||||
PageIndex = request.PageIndex,
|
||||
PageSize = request.PageSize
|
||||
};
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logger.LogError(ex, "分页查询清洗数据失败");
|
||||
await Send.ErrorsAsync(500, ct);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取最新清洗数据端点
|
||||
/// </summary>
|
||||
public class GetLatestByQrEndpoint : Endpoint<ClearDataQrRequest, ClearDataQrResponse>
|
||||
{
|
||||
private readonly IFreeSql _freeSql;
|
||||
|
||||
public GetLatestByQrEndpoint(IFreeSql freeSql)
|
||||
{
|
||||
_freeSql = freeSql;
|
||||
}
|
||||
|
||||
public override void Configure()
|
||||
{
|
||||
Get("/cleardata-qr/latest");
|
||||
AllowAnonymous();
|
||||
|
||||
Summary(s =>
|
||||
{
|
||||
s.Summary = "获取最新的清洗数据";
|
||||
s.Description = "根据设备编号和部件二维码获取最新的一条清洗数据";
|
||||
s.Response<ClearDataQrResponse>(200, "成功返回最新清洗数据");
|
||||
s.Response(404, "未找到数据");
|
||||
s.Response(500, "服务器内部错误");
|
||||
});
|
||||
}
|
||||
|
||||
public override async Task HandleAsync(ClearDataQrRequest request, CancellationToken ct)
|
||||
{
|
||||
try
|
||||
{
|
||||
// 调用重构后的方法,直接传递request对象
|
||||
var latestData = await Data.GetLatestClearData(request, _freeSql);
|
||||
|
||||
if (latestData == null)
|
||||
{
|
||||
//await Send.NotFoundAsync(ct);
|
||||
Response = latestData ?? new ClearDataQrResponse();
|
||||
}
|
||||
else
|
||||
{
|
||||
Response = latestData;
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logger.LogError(ex, "获取最新清洗数据失败");
|
||||
await Send.ErrorsAsync(500, ct);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 根据部件二维码获取所有清洗记录端点
|
||||
/// </summary>
|
||||
public class GetByPartQrCodeRequest
|
||||
{
|
||||
/// <summary>
|
||||
/// 部件二维码(必填)
|
||||
/// </summary>
|
||||
public string PartQRCode { get; set; } = string.Empty;
|
||||
}
|
||||
|
||||
public class GetByPartQrCodeEndpoint : Endpoint<GetByPartQrCodeRequest, List<ClearDataQrResponse>>
|
||||
{
|
||||
private readonly IFreeSql _freeSql;
|
||||
|
||||
public GetByPartQrCodeEndpoint(IFreeSql freeSql)
|
||||
{
|
||||
_freeSql = freeSql;
|
||||
}
|
||||
|
||||
public override void Configure()
|
||||
{
|
||||
Get("/cleardata-qr/by-qrcode/{PartQRCode}");
|
||||
AllowAnonymous();
|
||||
|
||||
Summary(s =>
|
||||
{
|
||||
s.Summary = "根据部件二维码获取所有清洗记录";
|
||||
s.Description = "仅根据部件二维码查询该部件的所有清洗历史记录";
|
||||
s.Response<List<ClearDataQrResponse>>(200, "成功返回清洗数据列表");
|
||||
s.Response(400, "请求参数错误");
|
||||
s.Response(404, "未找到数据");
|
||||
s.Response(500, "服务器内部错误");
|
||||
});
|
||||
}
|
||||
|
||||
public override async Task HandleAsync(GetByPartQrCodeRequest request, CancellationToken ct)
|
||||
{
|
||||
try
|
||||
{
|
||||
// 验证参数
|
||||
if (string.IsNullOrWhiteSpace(request.PartQRCode))
|
||||
{
|
||||
AddError("PartQRCode", "部件二维码不能为空");
|
||||
await Send.ErrorsAsync(400, ct);
|
||||
return;
|
||||
}
|
||||
|
||||
// 调用重构后的方法
|
||||
var dataList = await Data.GetClearDataByPartQRCode(request.PartQRCode, _freeSql);
|
||||
|
||||
if (dataList == null || dataList.Count == 0)
|
||||
{
|
||||
//await Send.NotFoundAsync(ct);
|
||||
Response = dataList ?? new List<ClearDataQrResponse>();
|
||||
}
|
||||
else
|
||||
{
|
||||
Response = dataList;
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logger.LogError(ex, "根据部件二维码查询清洗数据失败");
|
||||
await Send.ErrorsAsync(500, ct);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user