根据新需求更改

This commit is contained in:
2025-09-17 17:22:09 +08:00
parent e7adae128e
commit a954427d41
21 changed files with 633 additions and 126 deletions

View File

@@ -1,10 +1,10 @@
using MoviconWebApi.Entities;
using MoviconWebApi.Entities;
namespace MoviconWebApi.API.ClearDataApi
{
public static class Data
{
internal static async Task<List<ClearDataResponse>?> GetClearData(ClearDataRequest request, IFreeSql freeSql)
internal static async Task<(List<ClearDataResponse> Items, long Total)> GetClearData(ClearDataRequest request, IFreeSql freeSql)
{
try
{
@@ -29,11 +29,19 @@ namespace MoviconWebApi.API.ClearDataApi
query = query.Where(x => x.CreateTime <= endTime);
}
// 按创建时间降序排序
query = query.OrderByDescending(x => x.CreateTime);
// 统计总数
var total = await query.CountAsync();
// 执行查询并映射到响应模型
var result = await query.ToListAsync(x => new ClearDataResponse
// 参数兜底与限制
var pageNo = request.PageNo <= 0 ? 1 : request.PageNo;
var pageSize = request.PageSize <= 0 ? 100 : request.PageSize;
if (pageSize > 100) pageSize = 100;
// 按创建时间降序排序 + 分页 + 映射到响应模型
var result = await query
.OrderByDescending(x => x.CreateTime)
.Page(pageNo, pageSize)
.ToListAsync(x => new ClearDataResponse
{
DeviceCode = x.DeviceCode,
DeviceName = x.DeviceName,
@@ -66,13 +74,13 @@ namespace MoviconWebApi.API.ClearDataApi
CreateTime = x.CreateTime.ToString("yyyy-MM-dd HH:mm:ss")
});
return result;
return (result ?? new List<ClearDataResponse>(), total);
}
catch (Exception ex)
{
// 记录异常日志(可以根据实际需求添加日志记录)
Console.WriteLine($"获取清洗数据失败:{ex.Message}");
return null;
throw;
}
}

View File

@@ -1,7 +1,6 @@
using Azure;
using Azure;
using Azure.Core;
using FastEndpoints;
using MoviconWebApi.API.ClearDataQrApi;
using MoviconWebApi.Common;
namespace MoviconWebApi.API.ClearDataApi
@@ -46,35 +45,30 @@ namespace MoviconWebApi.API.ClearDataApi
{
try
{
// 调用Data层方法获取数据
var dataList = await Data.GetClearData(request, _freeSql);
// 参数兜底
if (request.PageNo <= 0) request.PageNo = 1;
if (request.PageSize <= 0) request.PageSize = 100;
if (request.PageSize > 100) request.PageSize = 100;
if (dataList == null || dataList.Count == 0)
// 新的分页数据(使用全限定名避免命名冲突)
(List<ClearDataResponse>, long) result = await MoviconWebApi.API.ClearDataApi.Data.GetClearData(request, _freeSql);
var items = result.Item1;
var total = result.Item2;
var resp = ApiResponse<List<ClearDataResponse>>.Success(items ?? new List<ClearDataResponse>(),
items != null && items.Count > 0 ? "请求成功" : "暂无数据");
// 组装分页
resp.pagination = new MoviconWebApi.Common.Pagination
{
// 未找到数据返回404
//await Send.NotFoundAsync(ct);
//Response = dataList ?? new List<ClearDataQrResponse>();
total = total,
count = items?.Count ?? 0,
pageNo = request.PageNo,
totalPage = request.PageSize > 0 ? (int)Math.Ceiling((double)total / request.PageSize) : 0,
pageSize = request.PageSize
};
//await Send.OkAsync(new List<ClearDataResponse>(), ct);
// 没有数据
Response = ApiResponse<List<ClearDataResponse>>.Success(
new List<ClearDataResponse>(),
"暂无数据"
);
}
else
{
//await Send.OkAsync(dataList, ct);
// 现在的代码
Response = ApiResponse<List<ClearDataResponse>>.Success(
dataList,
"查询成功"
);
}
Response = resp;
}
catch (Exception ex)
{
@@ -84,7 +78,8 @@ namespace MoviconWebApi.API.ClearDataApi
// 返回错误响应
Response = ApiResponse<List<ClearDataResponse>>.Error(
"500",
"服务器内部错误"
"服务器内部错误",
new List<ClearDataResponse>()
);
//// 记录错误日志

View File

@@ -1,4 +1,4 @@
using Newtonsoft.Json;
using Newtonsoft.Json;
namespace MoviconWebApi.API.ClearDataApi
{
@@ -19,6 +19,16 @@ namespace MoviconWebApi.API.ClearDataApi
/// </summary>
public string? EndTime { get; set; }
/// <summary>
/// 当前页码从1开始
/// </summary>
public int PageNo { get; set; } = 1;
/// <summary>
/// 分页大小
/// </summary>
public int PageSize { get; set; } = 100;
}
public class ClearDataResponse

View File

@@ -1,4 +1,4 @@
using MoviconWebApi.Entities;
using MoviconWebApi.Entities;
namespace MoviconWebApi.API.ClearDataQrApi
{
@@ -13,7 +13,7 @@ namespace MoviconWebApi.API.ClearDataQrApi
/// <param name="request">查询请求参数</param>
/// <param name="freeSql">FreeSql实例</param>
/// <returns>清洗数据列表</returns>
public static async Task<List<ClearDataQrResponse>> GetClearDataByQr(
public static async Task<(List<ClearDataQrResponse> Data, long Total)> GetClearDataByQr(
ClearDataQrRequest request,
IFreeSql freeSql)
{
@@ -34,11 +34,19 @@ namespace MoviconWebApi.API.ClearDataQrApi
query = query.Where(x => x.part_qrid == request.PartQRCode);
}
// 按创建时间降序排序
query = query.OrderByDescending(x => x.CreateTime);
// 统计总数
var total = await query.CountAsync();
// 执行查询并映射到响应模型
var result = await query.ToListAsync(x => new ClearDataQrResponse
// 参数兜底
var pageNo = request.PageNo <= 0 ? 1 : request.PageNo;
var pageSize = request.PageSize <= 0 ? 100 : request.PageSize;
if (pageSize > 100) pageSize = 100;
// 按创建时间降序排序 + 分页 + 映射
var result = await query
.OrderByDescending(x => x.CreateTime)
.Page(pageNo, pageSize)
.ToListAsync(x => new ClearDataQrResponse
{
DeviceCode = x.DeviceCode,
DeviceName = x.DeviceName,
@@ -71,7 +79,7 @@ namespace MoviconWebApi.API.ClearDataQrApi
CreateTime = x.CreateTime.ToString("yyyy-MM-dd HH:mm:ss")
});
return result;
return (result ?? new List<ClearDataQrResponse>(), total);
}
catch (Exception ex)
{

View File

@@ -1,4 +1,4 @@
using Azure;
using Azure;
using FastEndpoints;
using Microsoft.Extensions.Logging;
using MoviconWebApi.Common;
@@ -36,18 +36,29 @@ namespace MoviconWebApi.API.ClearDataQrApi
{
try
{
var dataList = await Data.GetClearDataByQr(request, _freeSql);
// 参数兜底
if (request.PageNo <= 0) request.PageNo = 1;
if (request.PageSize <= 0) request.PageSize = 100;
if (request.PageSize > 100) request.PageSize = 100;
if (dataList == null || dataList.Count == 0)
var result = await Data.GetClearDataByQr(request, _freeSql);
var dataList = result.Item1;
var total = result.Item2;
var resp = ApiResponse<List<ClearDataQrResponse>>.Success(
dataList ?? new List<ClearDataQrResponse>(),
(dataList != null && dataList.Count > 0) ? "请求成功" : "暂无数据");
resp.pagination = new MoviconWebApi.Common.Pagination
{
Response = ApiResponse<List<ClearDataQrResponse>>.Success(
new List<ClearDataQrResponse>(), "暂无数据");// "暂无数据"
}
else
{
Response = ApiResponse<List<ClearDataQrResponse>>.Success(
dataList, "查询成功");// "查询成功"
}
total = total,
count = dataList?.Count ?? 0,
pageNo = request.PageNo,
totalPage = request.PageSize > 0 ? (int)Math.Ceiling((double)total / request.PageSize) : 0,
pageSize = request.PageSize
};
Response = resp;
}
catch (Exception ex)
{
@@ -57,7 +68,8 @@ namespace MoviconWebApi.API.ClearDataQrApi
Logger.LogError(ex, "根据二维码查询清洗数据失败");
Response = ApiResponse<List<ClearDataQrResponse>>.Error(
"500",
"服务器内部错误"
"服务器内部错误",
new List<ClearDataQrResponse>()
);
}

View File

@@ -1,4 +1,4 @@
namespace MoviconWebApi.API.ClearDataQrApi
namespace MoviconWebApi.API.ClearDataQrApi
{
/// <summary>
/// 清洗数据二维码查询请求模型
@@ -14,6 +14,16 @@
/// 部件二维码
/// </summary>
public string? PartQRCode { get; set; }
/// <summary>
/// 当前页码从1开始
/// </summary>
public int PageNo { get; set; } = 1;
/// <summary>
/// 分页大小
/// </summary>
public int PageSize { get; set; } = 100;
}
/// <summary>

View File

@@ -1,4 +1,5 @@
using MoviconWebApi.Entities;
using MoviconWebApi.Entities;
using FreeSql;
namespace MoviconWebApi.API.ClearStaticApi
{
@@ -22,56 +23,67 @@ namespace MoviconWebApi.API.ClearStaticApi
var firstDayOfMonth = new DateTime(today.Year, today.Month, 1);
var firstDayOfYear = new DateTime(today.Year, 1, 1);
// 构建基础查询
var query = freeSql.Select<DeviceState>();
// 构建基础查询
//var queryDeviceState = freeSql.Select<DeviceState>();
var CurRunClearData = freeSql.Select<CurRunClearState>().Where(a => a.Test_PartsEquipmentStatus == "1").OrderByDescending(a => a.CreateTime).First();
// 根据设备编号过滤
if (!string.IsNullOrWhiteSpace(request.DeviceCode))
// 本地辅助函数把分钟字符串汇总为小时四舍五入到2位小数
static decimal SumMinutesToHours(IEnumerable<string?> minutesList)
{
query = query.Where(x => x.DeviceCode == request.DeviceCode);
decimal totalMinutes = 0m;
foreach (var s in minutesList)
{
if (string.IsNullOrWhiteSpace(s)) continue;
if (decimal.TryParse(s, out var m)) totalMinutes += m;
}
return Math.Round(totalMinutes / 60m, 2);
}
// 计算累计作业数量
var totalJobCount = await query.CountAsync();
// 帮助方法:为每个统计项单独构建基础选择器(避免条件累积)
ISelect<ClearData> BuildSelector()
{
var sel = freeSql.Select<ClearData>();
if (!string.IsNullOrWhiteSpace(request.DeviceCode))
sel = sel.Where(x => x.DeviceCode == request.DeviceCode);
return sel;
}
// 计算累计作业数量(记录数即作业数)
var totalJobCount = await BuildSelector().CountAsync();
// 计算本年度作业数
var yearJobCount = await query
var yearJobCount = await BuildSelector()
.Where(x => x.CreateTime >= firstDayOfYear)
.CountAsync();
// 计算本月作业数
var monthJobCount = await query
var monthJobCount = await BuildSelector()
.Where(x => x.CreateTime >= firstDayOfMonth)
.CountAsync();
// 计算今日作业数
var todayJobCount = await query
var todayJobCount = await BuildSelector()
.Where(x => x.CreateTime >= today)
.CountAsync();
// 计算累计作业时长
// 注意这里假设ClearData表中有一个字段表示清洗时长可能需要根据实际情况调整
var totalJobHours = await query
.SumAsync(x => Convert.ToDecimal(x.RunTime)) / 60; // 假设时长单位是秒,转换为小时
// 计算累计作业时长(来自 ClearData.Test_FrameworkPerModelCleaningDuration单位分钟
var totalJobHours = SumMinutesToHours(
await BuildSelector().ToListAsync(x => x.Test_FrameworkPerModelCleaningDuration)
);
// 计算本年作业时长
var yearJobHours = await query
.Where(x => x.CreateTime >= firstDayOfYear)
.SumAsync(x => Convert.ToDecimal(x.RunTime)) / 60;
var yearJobHours = SumMinutesToHours(
await BuildSelector().Where(x => x.CreateTime >= firstDayOfYear)
.ToListAsync(x => x.Test_FrameworkPerModelCleaningDuration)
);
// 计算本月作业时长
var monthJobHours = await query
.Where(x => x.CreateTime >= firstDayOfMonth)
.SumAsync(x => Convert.ToDecimal(x.RunTime)) / 60;
var monthJobHours = SumMinutesToHours(
await BuildSelector().Where(x => x.CreateTime >= firstDayOfMonth)
.ToListAsync(x => x.Test_FrameworkPerModelCleaningDuration)
);
// 计算今日作业时长
var todayJobHours = await query
.Where(x => x.CreateTime >= today)
.SumAsync(x => Convert.ToDecimal(x.RunTime)) / 60;
var todayJobHours = SumMinutesToHours(
await BuildSelector().Where(x => x.CreateTime >= today)
.ToListAsync(x => x.Test_FrameworkPerModelCleaningDuration)
);
// 返回结果
return new ClearStaticResponse
@@ -84,10 +96,6 @@ namespace MoviconWebApi.API.ClearStaticApi
YearJobHours = Math.Round(yearJobHours, 2),
MonthJobHours = Math.Round(monthJobHours, 2),
TodayJobHours = Math.Round(todayJobHours, 2),
CurrentVehicleModel = CurRunClearData != null ? CurRunClearData.vehicle_model : "",
CurrentLocomotiveNumber = CurRunClearData != null ? CurRunClearData.locomotive_number : "",
CurrentRepairProcess = CurRunClearData != null ? CurRunClearData.repair_process : "",
CurrentWheelNumber = "", // 假设part_num字段存储车轮编号
UpdateTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")
};

View File

@@ -56,25 +56,25 @@
/// </summary>
public decimal TodayJobHours { get; set; }
/// <summary>
/// 当前机型
/// </summary>
public string? CurrentVehicleModel { get; set; }
///// <summary>
///// 当前机型
///// </summary>
//public string? CurrentVehicleModel { get; set; }
/// <summary>
/// 当前车号
/// </summary>
public string? CurrentLocomotiveNumber { get; set; }
///// <summary>
///// 当前车号
///// </summary>
//public string? CurrentLocomotiveNumber { get; set; }
/// <summary>
/// 当前修程
/// </summary>
public string? CurrentRepairProcess { get; set; }
///// <summary>
///// 当前修程
///// </summary>
//public string? CurrentRepairProcess { get; set; }
/// <summary>
/// 车轮编号
/// </summary>
public string? CurrentWheelNumber { get; set; }
///// <summary>
///// 车轮编号
///// </summary>
//public string? CurrentWheelNumber { get; set; }
/// <summary>
/// 更新时间

View File

@@ -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)
{

View File

@@ -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,

View File

@@ -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>

View File

@@ -1,4 +1,4 @@
using Azure;
using Azure;
using FastEndpoints;
using MoviconWebApi.Common;
@@ -73,7 +73,9 @@ namespace MoviconWebApi.API.DeviceStateApi
{
try
{
var (items, total) = await Data.GetDeviceStatePagedAsync(_db, req);
var result = await Data.GetDeviceStatePagedAsync(_db, req);
var items = result.Item1;
var total = result.Item2;
var pagedResponse = new DeviceStatePagedResponse
{
Items = items,