根据新需求更改

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,8 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System;
namespace MoviconHub.App.Models
{
@@ -11,11 +7,22 @@ namespace MoviconHub.App.Models
/// </summary>
public class ClearAction
{
/// <summary>
/// 清洗事件
/// </summary>
public event EventHandler<string> ClearActionEvent;
/// <summary>
/// 设备状态改变事件
/// </summary>
public event EventHandler<StateInfo> DeviceStateChangeEvent;
// 状态变更并发锁:确保在多线程环境下,状态切换与事件发布的原子性,避免竞态条件
private readonly object _stateLock = new object();
// 是否已存在当前状态:用于首次赋值时不发布“上一状态”事件的标记
private bool _hasCurrentState = false;
private bool _ClearEnd;
/// <summary>
@@ -61,7 +68,74 @@ namespace MoviconHub.App.Models
}
}
/// <summary>
/// 当前状态信息
/// </summary>
public StateInfo CurStateInfo { get; set; } = new StateInfo();
private int _DeviceState;
/// <summary>
/// 设备状态
/// </summary>
public int DeviceState
{
get { return _DeviceState; }
set
{
if (_DeviceState != value)
{
lock (_stateLock)
{
var now = DateTime.Now;
// 若已有当前状态,先发布上一个状态的维持区间
if (_hasCurrentState)
{
var prevStateInfo = new StateInfo
{
StartTime = CurStateInfo.StartTime,
EndTime = now,
State = CurStateInfo.State
};
var handler = DeviceStateChangeEvent;
handler?.BeginInvoke(this, prevStateInfo, null, null);
}
// 更新为新的当前状态
_DeviceState = value;
CurStateInfo = new StateInfo
{
StartTime = now,
EndTime = default, // 结束时间在下次状态变化时确定
State = value
};
_hasCurrentState = true;
}
}
}
}
}
/// <summary>
/// 状态信息
/// </summary>
public class StateInfo
{
/// <summary>
/// 开始时间
/// </summary>
public DateTime StartTime { get; set; }
/// <summary>
/// 结束时间
/// </summary>
public DateTime EndTime { get; set; }
/// <summary>
/// 值
/// </summary>
public int State { get; set; }
}
}

View File

@@ -181,6 +181,112 @@ namespace MoviconHub.App.Models
[Column(Name = "Test_SteamSurveillance", StringLength = 100, IsNullable = true)]
public string Test_SteamSurveillance { get; set; }
///// <summary>
///// ​零部件车间桁架平移原位​​
///// </summary>
//[Column(Name = "Test_TrussMoveHome", StringLength = 100, IsNullable = true)]
//public string Test_TrussMoveHome { get; set; }
///// <summary>
///// ​零部件车间桁架平移后退极限位​​
///// </summary>
//[Column(Name = "Test_TrussMoveBackwardLimit", StringLength = 100, IsNullable = true)]
//public string Test_TrussMoveBackwardLimit { get; set; }
///// <summary>
///// ​零部件车间桁架平移前进极限位​​
///// </summary>
//[Column(Name = "Test_TrussMoveForwardLimit", StringLength = 100, IsNullable = true)]
//public string Test_TrussMoveForwardLimit { get; set; }
///// <summary>
///// ​零部件车间桁架吊装浸泡池吊装位​​
///// </summary>
//[Column(Name = "Test_TrussLiftPoolPos", StringLength = 100, IsNullable = true)]
//public string Test_TrussLiftPoolPos { get; set; }
///// <summary>
///// ​零部件车间桁架吊装下降极限位​​
///// </summary>
//[Column(Name = "Test_TrussLiftLowerLimit", StringLength = 100, IsNullable = true)]
//public string Test_TrussLiftLowerLimit { get; set; }
///// <summary>
///// ​零部件车间桁架吊装原位​​
///// </summary>
//[Column(Name = "Test_TrussLiftHome", StringLength = 100, IsNullable = true)]
//public string Test_TrussLiftHome { get; set; }
///// <summary>
///// ​零部件车间桁架吊装上升极限位​​
///// </summary>
//[Column(Name = "Test_TrussLiftRiseLimit", StringLength = 100, IsNullable = true)]
//public string Test_TrussLiftRiseLimit { get; set; }
///// <summary>
///// ​零部件车间桁架吊装漫射吊装位​​
///// </summary>
//[Column(Name = "Test_TrussLiftWashPos", StringLength = 100, IsNullable = true)]
//public string Test_TrussLiftWashPos { get; set; }
///// <summary>
///// ROB1_第7轴接近开关ROB1原位
///// </summary>
//[Column(Name = "Test_Rob1Axis7Home", StringLength = 100, IsNullable = true)]
//public string Test_Rob1Axis7Home { get; set; }
///// <summary>
///// ROB1_第7轴接近开关前进极限位正常
///// </summary>
//[Column(Name = "Test_Rob1Axis7ForwardLimitOK", StringLength = 100, IsNullable = true)]
//public string Test_Rob1Axis7ForwardLimitOK { get; set; }
///// <summary>
///// ROB1_第7轴接近开关后退极限位正常
///// </summary>
//[Column(Name = "Test_Rob1Axis7BackwardLimitOK", StringLength = 100, IsNullable = true)]
//public string Test_Rob1Axis7BackwardLimitOK { get; set; }
///// <summary>
///// ROB2_第7轴接近开关ROB2原位
///// </summary>
//[Column(Name = "Test_Rob2Axis7Home", StringLength = 100, IsNullable = true)]
//public string Test_Rob2Axis7Home { get; set; }
///// <summary>
///// ROB2_第7轴接近开关前进极限位正常
///// </summary>
//[Column(Name = "Test_Rob2Axis7ForwardLimitOK", StringLength = 100, IsNullable = true)]
//public string Test_Rob2Axis7ForwardLimitOK { get; set; }
///// <summary>
///// ROB2_第7轴接近开关后退极限位正常
///// </summary>
//[Column(Name = "Test_Rob2Axis7BackwardLimitOK", StringLength = 100, IsNullable = true)]
//public string Test_Rob2Axis7BackwardLimitOK { get; set; }
/// <summary>
/// 创建时间
/// </summary>

View File

@@ -0,0 +1,60 @@
using FreeSql.DataAnnotations;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MoviconHub.App.Models
{
/// <summary>
/// 设备状态改变
/// </summary>
[Table(Name = "DeviceStatusChange")]
public class DeviceStatusChange
{
/// <summary>
/// 主键ID
/// </summary>
[Column(IsPrimary = true, IsIdentity = true)]
public long Id { get; set; }
/// <summary>
/// 设备码
/// </summary>
[Column(Name = "DeviceCode", StringLength = 100, IsNullable = true)]
public string DeviceCode { get; set; }
/// <summary>
/// 设备名称
/// </summary>
[Column(Name = "DeviceName", StringLength = 100, IsNullable = true)]
public string DeviceName { get; set; }
/// <summary>
/// 开始时间
/// </summary>
[Column(Name = "StartTime", IsNullable = true)]
public DateTime StartTime { get; set; }
/// <summary>
/// 结束时间
/// </summary>
[Column(Name = "EndTime", IsNullable = true)]
public DateTime EndTime { get; set; }
/// <summary>
/// 设备状态
/// </summary>
[Column(Name = "DeviceState", IsNullable = true)]
public int? DeviceState { get; set; }
/// <summary>
/// 创建时间
/// </summary>
[Column(ServerTime = DateTimeKind.Local, CanUpdate = false)]
public DateTime CreateTime { get; set; }
}
}

View File

@@ -209,6 +209,89 @@ namespace MoviconHub.App.Models
[JsonProperty("Test_SteamSurveillance")]
public string Test_SteamSurveillance { get; set; }
/// <summary>
/// ​零部件车间桁架平移原位​​
/// </summary>
[JsonProperty("Test_TrussMoveHome")]
public string Test_TrussMoveHome { get; set; }
/// <summary>
/// ​零部件车间桁架平移后退极限位​​
/// </summary>
[JsonProperty("Test_TrussMoveBackwardLimit")]
public string Test_TrussMoveBackwardLimit { get; set; }
/// <summary>
/// ​零部件车间桁架平移前进极限位​​
/// </summary>
[JsonProperty("Test_TrussMoveForwardLimit")]
public string Test_TrussMoveForwardLimit { get; set; }
/// <summary>
/// ​零部件车间桁架吊装浸泡池吊装位​​
/// </summary>
[JsonProperty("Test_TrussLiftPoolPos")]
public string Test_TrussLiftPoolPos { get; set; }
/// <summary>
/// ​零部件车间桁架吊装下降极限位​​
/// </summary>
[JsonProperty("Test_TrussLiftLowerLimit")]
public string Test_TrussLiftLowerLimit { get; set; }
/// <summary>
/// ​零部件车间桁架吊装原位​​
/// </summary>
[JsonProperty("Test_TrussLiftHome")]
public string Test_TrussLiftHome { get; set; }
/// <summary>
/// ​零部件车间桁架吊装上升极限位​​
/// </summary>
[JsonProperty("Test_TrussLiftRiseLimit")]
public string Test_TrussLiftRiseLimit { get; set; }
/// <summary>
/// ​零部件车间桁架吊装漫射吊装位​​
/// </summary>
[JsonProperty("Test_TrussLiftWashPos")]
public string Test_TrussLiftWashPos { get; set; }
/// <summary>
/// ROB1_第7轴接近开关ROB1原位
/// </summary>
[JsonProperty("Test_Rob1Axis7Home")]
public string Test_Rob1Axis7Home { get; set; }
/// <summary>
/// ROB1_第7轴接近开关前进极限位正常
/// </summary>
[JsonProperty("Test_Rob1Axis7ForwardLimitOK")]
public string Test_Rob1Axis7ForwardLimitOK { get; set; }
/// <summary>
/// ROB1_第7轴接近开关后退极限位正常
/// </summary>
[JsonProperty("Test_Rob1Axis7BackwardLimitOK")]
public string Test_Rob1Axis7BackwardLimitOK { get; set; }
/// <summary>
/// ROB2_第7轴接近开关ROB2原位
/// </summary>
[JsonProperty("Test_Rob2Axis7Home")]
public string Test_Rob2Axis7Home { get; set; }
/// <summary>
/// ROB2_第7轴接近开关前进极限位正常
/// </summary>
[JsonProperty("Test_Rob2Axis7ForwardLimitOK")]
public string Test_Rob2Axis7ForwardLimitOK { get; set; }
/// <summary>
/// ROB2_第7轴接近开关后退极限位正常
/// </summary>
[JsonProperty("Test_Rob2Axis7BackwardLimitOK")]
public string Test_Rob2Axis7BackwardLimitOK { get; set; }
/// <summary>
/// 测试数据值集合

View File

@@ -128,6 +128,7 @@
<Compile Include="Models\DeviceAlarm.cs" />
<Compile Include="Models\DeviceState.cs" />
<Compile Include="Models\DeviceStateStaticModel.cs" />
<Compile Include="Models\DeviceStatusChange.cs" />
<Compile Include="Models\FaultDetails.cs" />
<Compile Include="Models\DeviceStatusData.cs" />
<Compile Include="Models\EnvironmentData.cs" />

View File

@@ -257,7 +257,7 @@ namespace MoviconHub.App.Services
Test_WaterTankAdd = webSocketData.TestData.Test_WaterTankAdd,
Test_CleaningAgentTankHeat = webSocketData.TestData.Test_CleaningAgentTankHeat,
Test_CleaningAgentTankAdd = webSocketData.TestData.Test_CleaningAgentTankAdd,
// 监控信息
Test_ElectricSurveillance = webSocketData.TestData.Test_ElectricSurveillance,
Test_SteamSurveillance = webSocketData.TestData.Test_SteamSurveillance

View File

@@ -70,6 +70,7 @@ namespace MoviconHub.App
ClearActionInstance = new ClearAction();
ClearActionInstance.ClearActionEvent += ClearActionInstance_ClearActionEvent;
ClearActionInstance.DeviceStateChangeEvent += ClearActionInstance_DeviceStateChangeEvent;
ListAlarmModels = new List<AlarmModel>()
{
@@ -437,6 +438,29 @@ namespace MoviconHub.App
}
private void ClearActionInstance_DeviceStateChangeEvent(object sender, StateInfo Value)
{
var Data = new DeviceStatusChange()
{
DeviceCode = "942010002",
DeviceName = "机车构架及大部件自动化智能清洗设备",
DeviceState = Value.State,
StartTime = Value.StartTime,
EndTime = Value.EndTime,
};
var Result = FRemoteSqlContext.FDb.Insert<DeviceStatusChange>(Data).ExecuteInserted();
if (Result != null && Result.Count > 0)
{
Logger.Info("设备数据状态改变保存完成");
BeginInvoke(new Action(() =>
{
MainText.AppendText($"时间:{DateTime.Now}-Msg:设备数据状态改变保存完成 {Environment.NewLine}");
}));
}
}
/// <summary>
/// 清洗动作实例信息
/// </summary>
@@ -532,7 +556,23 @@ namespace MoviconHub.App
// 监控信息
Test_ElectricSurveillance = CurDBServices.RealtimeData.TestData.Test_ElectricSurveillance,
Test_SteamSurveillance = CurDBServices.RealtimeData.TestData.Test_SteamSurveillance
Test_SteamSurveillance = CurDBServices.RealtimeData.TestData.Test_SteamSurveillance,
//Test_Rob1Axis7BackwardLimitOK = CurDBServices.RealtimeData.TestData.Test_Rob1Axis7BackwardLimitOK,
//Test_Rob1Axis7ForwardLimitOK = CurDBServices.RealtimeData.TestData.Test_Rob1Axis7ForwardLimitOK,
//Test_Rob1Axis7Home = CurDBServices.RealtimeData.TestData.Test_Rob1Axis7Home,
//Test_Rob2Axis7BackwardLimitOK = CurDBServices.RealtimeData.TestData.Test_Rob2Axis7BackwardLimitOK,
//Test_Rob2Axis7ForwardLimitOK = CurDBServices.RealtimeData.TestData.Test_Rob2Axis7ForwardLimitOK,
//Test_Rob2Axis7Home = CurDBServices.RealtimeData.TestData.Test_Rob2Axis7Home,
//Test_TrussLiftHome = CurDBServices.RealtimeData.TestData.Test_TrussLiftHome,
//Test_TrussLiftLowerLimit = CurDBServices.RealtimeData.TestData.Test_TrussLiftLowerLimit,
//Test_TrussLiftPoolPos = CurDBServices.RealtimeData.TestData.Test_TrussLiftPoolPos,
//Test_TrussLiftRiseLimit = CurDBServices.RealtimeData.TestData.Test_TrussLiftRiseLimit,
//Test_TrussLiftWashPos = CurDBServices.RealtimeData.TestData.Test_TrussLiftWashPos,
//Test_TrussMoveBackwardLimit = CurDBServices.RealtimeData.TestData.Test_TrussMoveBackwardLimit,
//Test_TrussMoveForwardLimit = CurDBServices.RealtimeData.TestData.Test_TrussMoveForwardLimit,
//Test_TrussMoveHome = CurDBServices.RealtimeData.TestData.Test_TrussMoveHome,
};
var Result = FRemoteSqlContext.FDb.Insert<ClearData>(curRunData).ExecuteInserted();
@@ -594,6 +634,8 @@ namespace MoviconHub.App
/// </summary>
private OperateResult<Int16[]> OperateResultDeviceStateStatic { get; set; }
private OperateResult<Int16> OperateResultDeviceStateChange { get; set; }
/// <summary>
/// 通信初始化
/// </summary>
@@ -677,18 +719,26 @@ namespace MoviconHub.App
}
}
OperateResultDeviceStateChange = MelsecMcNetDrive.ReadInt16("D4000");
if (OperateResultDeviceStateChange.IsSuccess)
{
ClearActionInstance.DeviceState = OperateResultDeviceStateChange.Content;
}
//OperateResultClearEnd = MelsecMcNetDrive.ReadBool("M210");
//if (OperateResultClearEnd.IsSuccess)
//{
// ClearActionInstance.ClearEnd = OperateResultClearEnd.Content;
//}
//设备清洗结束
OperateResultClearEnd = MelsecMcNetDrive.ReadBool("M211");
if (OperateResultClearEnd.IsSuccess)
{
ClearActionInstance.ClearEnd = OperateResultClearEnd.Content;
}
//设备关机
OperateResultDeviceClose = MelsecMcNetDrive.ReadBool("M210");
if (OperateResultDeviceClose.IsSuccess)
{

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,

View File

@@ -1,4 +1,4 @@
namespace MoviconWebApi.Common
namespace MoviconWebApi.Common
{
/// <summary>
/// 统一的API响应结构
@@ -20,6 +20,11 @@
/// </summary>
public T? data { get; set; }
/// <summary>
/// 分页信息(可选,仅在列表接口分页时返回)
/// </summary>
public Pagination? pagination { get; set; }
/// <summary>
/// 创建成功响应
/// </summary>

View File

@@ -0,0 +1,33 @@
namespace MoviconWebApi.Common
{
/// <summary>
/// 分页信息
/// </summary>
public class Pagination
{
/// <summary>
/// 总条数
/// </summary>
public long total { get; set; }
/// <summary>
/// 当前条数
/// </summary>
public int count { get; set; }
/// <summary>
/// 当前页数从1开始
/// </summary>
public int pageNo { get; set; }
/// <summary>
/// 总页数
/// </summary>
public int totalPage { get; set; }
/// <summary>
/// 每页条数
/// </summary>
public int pageSize { get; set; }
}
}