根据新需求更改
This commit is contained in:
@@ -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")
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user