zhuangpei-mesbackend/DOAN.Service/MES/dev/DeviceDataAnalysisService.cs

802 lines
32 KiB
C#
Raw Normal View History

2025-06-12 19:10:15 +08:00
using System;
2024-06-13 16:23:37 +08:00
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
2024-07-01 16:04:10 +08:00
using DOAN.Model.MES.dev;
using DOAN.Model.MES.dev.Dto;
using DOAN.Model.System;
using DOAN.Service.MES.dev.IService;
2025-06-12 19:10:15 +08:00
using Infrastructure.Attribute;
using Infrastructure.Extensions;
using Microsoft.AspNetCore.Components.Forms;
2024-06-13 16:23:37 +08:00
2024-07-01 16:04:10 +08:00
namespace DOAN.Service.MES.dev
2024-06-13 16:23:37 +08:00
{
/// <summary>
/// 设备数据分析
/// </summary>
2025-06-12 19:10:15 +08:00
[AppService(
ServiceType = typeof(IDeviceDataAnalysisServcie),
ServiceLifetime = LifeTime.Transient
)]
2024-06-13 16:23:37 +08:00
public class DeviceDataAnalysisService : BaseService<DeviceAccount>, IDeviceDataAnalysisServcie
{
2024-06-13 17:03:54 +08:00
public static List<DeviceType> FindAllLeafNodes(List<DeviceType> nodeList, int targetId)
{
List<int> childIds = new List<int> { targetId };
List<DeviceType> result = new List<DeviceType>();
while (childIds.Any())
{
int parentId = childIds.First();
childIds.RemoveAt(0);
2025-06-12 19:10:15 +08:00
List<int> children = nodeList
.Where(n => n.ParentId == parentId)
.Select(n => n.Id)
.ToList();
2024-06-13 17:03:54 +08:00
if (children.Any())
{
childIds.AddRange(children);
}
else
{
result.Add(nodeList.First(n => n.Id == parentId));
}
}
return result;
}
2025-06-12 19:10:15 +08:00
2024-06-13 16:23:37 +08:00
/// <summary>
/// 故障类型 pie
/// </summary>
/// <param name="devicedefault"></param>
/// <returns></returns>
public List<DeviceStatusAnalysisResultDto> DefaultTypePie(DeviceDefaultDto devicedefault)
{
2025-06-12 19:10:15 +08:00
List<DeviceStatusAnalysisResultDto> resultList =
new List<DeviceStatusAnalysisResultDto>();
2024-06-13 17:03:54 +08:00
int[] all_device_type_array = null;
2024-06-14 14:35:26 +08:00
if (devicedefault.DevicetTypeId > 0)
2024-06-13 17:03:54 +08:00
{
2025-06-12 19:10:15 +08:00
List<DeviceType> all_device_type_list = FindAllLeafNodes(
Context.Queryable<DeviceType>().ToList(),
devicedefault.DevicetTypeId ?? 0
);
2024-06-14 14:35:26 +08:00
all_device_type_array = all_device_type_list.Select(it => it.Id).ToArray();
2024-06-13 17:03:54 +08:00
}
2025-06-12 19:10:15 +08:00
var predicate = Expressionable
.Create<DeviceRepair, DeviceAccount, SysDictData>()
.AndIF(
all_device_type_array != null && all_device_type_array.Length > 0,
(r, a, d) => all_device_type_array.Contains(a.FkDeviceType)
)
.AndIF(
!string.IsNullOrEmpty(devicedefault.DeviceName),
(r, a, d) => a.DeviceName.Contains(devicedefault.DeviceName)
)
.AndIF(
!string.IsNullOrEmpty(devicedefault.DeviceCode),
(r, a, d) => a.DeviceCode.Contains(devicedefault.DeviceCode)
)
.AndIF(
devicedefault.searchTime[0] > new DateTime(1991, 1, 1),
(r, a, d) => r.CreatedTime >= devicedefault.searchTime[0]
)
.AndIF(
devicedefault.searchTime[1] > new DateTime(1991, 1, 1),
(r, a, d) => r.CreatedTime <= devicedefault.searchTime[1]
)
.And((r, a, d) => d.DictType == "mes_device_fault_type");
List<DeviceRepair_chart> deviceRepair_list = Context
.Queryable<DeviceRepair>()
2024-06-13 16:23:37 +08:00
.LeftJoin<DeviceAccount>((r, a) => r.FkDeviceId == a.Id)
2024-06-14 14:35:26 +08:00
.LeftJoin<SysDictData>((r, a, d) => r.Type == d.DictValue)
2024-06-13 16:23:37 +08:00
.Where(predicate.ToExpression())
2025-06-12 19:10:15 +08:00
.Select(
(r, a, d) => new DeviceRepair_chart { Type = r.Type, TypeName = d.DictLabel, }
)
.ToList();
2024-06-13 16:23:37 +08:00
2024-06-14 14:35:26 +08:00
if (deviceRepair_list.Count > 0)
2024-06-13 16:23:37 +08:00
{
2024-06-14 08:44:08 +08:00
var TypeGroups = deviceRepair_list.GroupBy(it => it.TypeName);
2024-06-13 17:19:29 +08:00
foreach (var group in TypeGroups)
{
DeviceStatusAnalysisResultDto result = new DeviceStatusAnalysisResultDto();
result.name = group.Key;
result.value = group.Count();
resultList.Add(result);
}
2024-06-13 16:23:37 +08:00
}
2024-06-14 14:35:26 +08:00
2024-06-13 16:23:37 +08:00
return resultList;
}
2024-06-14 14:35:26 +08:00
/// <summary>
/// 每日故障数量统计
/// </summary>
/// <param name="devicedefault"></param>
/// <returns></returns>
public DeviceFaultBarResultDto FaultTypeBar(DeviceDefaultDto devicedefault)
{
2024-06-14 14:40:06 +08:00
DeviceFaultBarResultDto result = new DeviceFaultBarResultDto();
2024-06-14 14:35:26 +08:00
int[] all_device_type_array = null;
if (devicedefault.DevicetTypeId > 0)
{
2025-06-12 19:10:15 +08:00
List<DeviceType> all_device_type_list = FindAllLeafNodes(
Context.Queryable<DeviceType>().ToList(),
devicedefault.DevicetTypeId ?? 0
);
2024-06-14 14:35:26 +08:00
all_device_type_array = all_device_type_list.Select(it => it.Id).ToArray();
}
2025-06-12 19:10:15 +08:00
var predicate = Expressionable
.Create<DeviceRepair, DeviceAccount, SysDictData>()
.AndIF(
all_device_type_array != null && all_device_type_array.Length > 0,
(r, a, d) => all_device_type_array.Contains(a.FkDeviceType)
)
.AndIF(
!string.IsNullOrEmpty(devicedefault.DeviceName),
(r, a, d) => a.DeviceName.Contains(devicedefault.DeviceName)
)
.AndIF(
!string.IsNullOrEmpty(devicedefault.DeviceCode),
(r, a, d) => a.DeviceCode.Contains(devicedefault.DeviceCode)
)
.AndIF(
devicedefault.searchTime[0] > new DateTime(1991, 1, 1),
(r, a, d) => r.CreatedTime >= devicedefault.searchTime[0]
)
.AndIF(
devicedefault.searchTime[1] > new DateTime(1991, 1, 1),
(r, a, d) => r.CreatedTime <= devicedefault.searchTime[1]
)
.And((r, a, d) => d.DictType == "mes_device_fault_type");
List<DeviceRepair_chart> deviceRepair_list = Context
.Queryable<DeviceRepair>()
2024-06-14 14:35:26 +08:00
.LeftJoin<DeviceAccount>((r, a) => r.FkDeviceId == a.Id)
.LeftJoin<SysDictData>((r, a, d) => r.Type == d.DictValue)
.Where(predicate.ToExpression())
2025-06-12 19:10:15 +08:00
.Select(
(r, a, d) =>
new DeviceRepair_chart
{
Type = r.Type,
TypeName = d.DictLabel,
CreatedTime = (DateTime)r.CreatedTime
}
)
.ToList();
2024-06-14 14:35:26 +08:00
//获取所有故障类型
2025-06-12 19:10:15 +08:00
List<SysDictData> all_fault_type = Context
.Queryable<SysDictData>()
.Where(it => it.DictType == "mes_device_fault_type")
.ToList();
2024-06-14 14:35:26 +08:00
// 先按照天进行上卷聚合,然后按照故障种类进行上卷聚合
var first_groups = deviceRepair_list.GroupBy(it => it.CreatedTime.Date);
2024-06-14 15:15:20 +08:00
string[] Xdata = new string[first_groups.Count()];
2024-06-14 14:35:26 +08:00
int index = 0;
foreach (var group in first_groups)
{
2024-06-14 15:15:20 +08:00
Xdata[index] = group.Key.ToString("yyyy-MM-dd");
2024-06-14 14:35:26 +08:00
index++;
}
List<SeriesData_Item> SeriesData = new List<SeriesData_Item>();
2024-06-14 14:35:26 +08:00
foreach (SysDictData dict in all_fault_type)
{
SeriesData_Item seriesData_Item = new SeriesData_Item();
seriesData_Item.Name = dict.DictLabel;
//这种故障在每天的值
2024-06-14 14:35:26 +08:00
int[] values = new int[first_groups.Count()];
int index2 = 0;
foreach (var group in first_groups)
{
//凌晨
DateTime startOfDay = group.Key;
//午夜23:59:59
DateTime endOfDay = group.Key.AddDays(1).AddSeconds(-1);
values[index2] = deviceRepair_list
2025-06-12 19:10:15 +08:00
.Where(it => it.CreatedTime >= startOfDay && it.CreatedTime <= endOfDay)
.Where(it => it.Type == dict.DictValue)
.Count();
2024-06-14 14:35:26 +08:00
index2++;
}
2024-06-14 15:11:44 +08:00
seriesData_Item.Data = values;
2024-06-14 15:04:59 +08:00
SeriesData.Add(seriesData_Item);
2024-06-14 14:35:26 +08:00
}
result.XData = Xdata;
result.SeriesData = SeriesData;
2024-06-14 14:35:26 +08:00
return result;
}
/// <summary>
/// 获取设备故障类别累计次数折线图
/// </summary>
/// <param name="devicedefault"></param>
/// <returns></returns>
public DeviceFaultLineResultDto FaultTypeLine(DeviceDefaultDto devicedefault)
{
DeviceFaultLineResultDto result = new DeviceFaultLineResultDto();
int[] all_device_type_array = null;
if (devicedefault.DevicetTypeId > 0)
{
2025-06-12 19:10:15 +08:00
List<DeviceType> all_device_type_list = FindAllLeafNodes(
Context.Queryable<DeviceType>().ToList(),
devicedefault.DevicetTypeId ?? 0
);
all_device_type_array = all_device_type_list.Select(it => it.Id).ToArray();
}
2025-06-12 19:10:15 +08:00
var predicate = Expressionable
.Create<DeviceRepair, DeviceAccount, SysDictData>()
.AndIF(
all_device_type_array != null && all_device_type_array.Length > 0,
(r, a, d) => all_device_type_array.Contains(a.FkDeviceType)
)
.AndIF(
!string.IsNullOrEmpty(devicedefault.DeviceName),
(r, a, d) => a.DeviceName.Contains(devicedefault.DeviceName)
)
.AndIF(
!string.IsNullOrEmpty(devicedefault.DeviceCode),
(r, a, d) => a.DeviceCode.Contains(devicedefault.DeviceCode)
)
.AndIF(
devicedefault.searchTime[0] > new DateTime(1991, 1, 1),
(r, a, d) => r.CreatedTime >= devicedefault.searchTime[0]
)
.AndIF(
devicedefault.searchTime[1] > new DateTime(1991, 1, 1),
(r, a, d) => r.CreatedTime <= devicedefault.searchTime[1]
)
.And((r, a, d) => d.DictType == "mes_device_fault_type");
List<DeviceRepair_chart> deviceRepair_list = Context
.Queryable<DeviceRepair>()
.LeftJoin<DeviceAccount>((r, a) => r.FkDeviceId == a.Id)
.LeftJoin<SysDictData>((r, a, d) => r.Type == d.DictValue)
.Where(predicate.ToExpression())
2025-06-12 19:10:15 +08:00
.Select(
(r, a, d) =>
new DeviceRepair_chart
{
Id = r.Id,
Type = r.Type,
TypeName = d.DictLabel,
CreatedTime = (DateTime)r.CreatedTime
}
)
.ToList();
foreach (var repair in deviceRepair_list)
{
2025-06-12 19:10:15 +08:00
repair.AccValue_SameType = Context
.Queryable<DeviceRepair>()
.Where(it => it.Type == repair.Type)
.Where(it => it.CreatedTime <= repair.CreatedTime)
.Count();
}
//获取所有故障类型
2025-06-12 19:10:15 +08:00
List<SysDictData> all_fault_type = Context
.Queryable<SysDictData>()
.Where(it => it.DictType == "mes_device_fault_type")
.ToList();
// 先按照天进行上卷聚合,然后按照故障种类进行上卷聚合
var first_groups = deviceRepair_list.GroupBy(it => it.CreatedTime.Date);
string[] Xdata = new string[first_groups.Count()];
int index = 0;
foreach (var group in first_groups)
{
Xdata[index] = group.Key.ToString("yyyy-MM-dd");
index++;
}
//处理纵坐标
List<SeriesData_Item_line> SeriesData = new List<SeriesData_Item_line>();
foreach (SysDictData dict in all_fault_type)
{
SeriesData_Item_line seriesData_Item = new SeriesData_Item_line();
seriesData_Item.Name = dict.DictLabel;
seriesData_Item.Type = "line";
int[] values = new int[first_groups.Count()];
int index2 = 0;
foreach (var group in first_groups)
{
//凌晨
DateTime startOfDay = group.Key;
//午夜23:59:59
DateTime endOfDay = group.Key.AddDays(1).AddSeconds(-1);
values[index2] = deviceRepair_list
2025-06-12 19:10:15 +08:00
.Where(it => it.CreatedTime <= endOfDay)
.Where(it => it.Type == dict.DictValue)
.Sum(it => it.AccValue_SameType);
index2++;
}
seriesData_Item.Data = values;
SeriesData.Add(seriesData_Item);
}
result.XData = Xdata;
result.SeriesData = SeriesData;
return result;
}
/// <summary>
/// 获取每日设备故障总数与维修完成数柱状图
/// </summary>
/// <param name="devicedefault"></param>
/// <returns></returns>
public DeviceRepairBarResultDto FaultTypeBarByDay(DeviceDefaultDto devicedefault)
{
DeviceRepairBarResultDto result = new DeviceRepairBarResultDto();
int[] all_device_type_array = null;
if (devicedefault.DevicetTypeId > 0)
{
2025-06-12 19:10:15 +08:00
List<DeviceType> all_device_type_list = FindAllLeafNodes(
Context.Queryable<DeviceType>().ToList(),
devicedefault.DevicetTypeId ?? 0
);
all_device_type_array = all_device_type_list.Select(it => it.Id).ToArray();
}
2025-06-12 19:10:15 +08:00
var predicate = Expressionable
.Create<DeviceRepair, DeviceAccount>()
.AndIF(
all_device_type_array != null && all_device_type_array.Length > 0,
(r, a) => all_device_type_array.Contains(a.FkDeviceType)
)
.AndIF(
!string.IsNullOrEmpty(devicedefault.DeviceName),
(r, a) => a.DeviceName.Contains(devicedefault.DeviceName)
)
.AndIF(
!string.IsNullOrEmpty(devicedefault.DeviceCode),
(r, a) => a.DeviceCode.Contains(devicedefault.DeviceCode)
)
.AndIF(
devicedefault.searchTime[0] > new DateTime(1991, 1, 1),
(r, a) => r.CreatedTime >= devicedefault.searchTime[0]
)
.AndIF(
devicedefault.searchTime[1] > new DateTime(1991, 1, 1),
(r, a) => r.CreatedTime <= devicedefault.searchTime[1]
);
List<DeviceRepair_chart> deviceRepair_list = Context
.Queryable<DeviceRepair>()
.LeftJoin<DeviceAccount>((r, a) => r.FkDeviceId == a.Id)
.Where(predicate.ToExpression())
2025-06-12 19:10:15 +08:00
.Select(
(r, a) =>
new DeviceRepair_chart
{
Id = r.Id,
Type = r.Type,
Status = r.Status ?? 0,
CreatedTime = (DateTime)r.CreatedTime
}
)
.ToList();
// 先按照天进行上卷聚合
var first_groups = deviceRepair_list.GroupBy(it => it.CreatedTime.Date);
string[] Xdata = new string[first_groups.Count()];
int index = 0;
foreach (var group in first_groups)
{
Xdata[index] = group.Key.ToString("yyyy-MM-dd");
index++;
}
result.XData = Xdata;
2024-06-17 14:20:48 +08:00
List<SeriesData_Item_line2> SeriesData = new List<SeriesData_Item_line2>();
SeriesData_Item_line2 faultAll = new SeriesData_Item_line2();
faultAll.Name = "报修数";
2024-06-17 14:20:48 +08:00
int[] Data1 = new int[first_groups.Count()];
int index1 = 0;
foreach (var group in first_groups)
{
Data1[index1] = group.Count();
index1++;
}
faultAll.Data = Data1;
SeriesData.Add(faultAll);
SeriesData_Item_line2 repair_success_all = new SeriesData_Item_line2();
repair_success_all.Name = "维修数";
2025-06-12 19:10:15 +08:00
List<DeviceRepair_chart> deviceRepair_list_success = Context
.Queryable<DeviceRepair>()
.LeftJoin<DeviceAccount>((r, a) => r.FkDeviceId == a.Id)
.Where(predicate.ToExpression())
2024-06-17 14:20:48 +08:00
.Where((r, a) => r.Status == 2)
2025-06-12 19:10:15 +08:00
.Select(
(r, a) =>
new DeviceRepair_chart
{
Id = r.Id,
Type = r.Type,
Status = r.Status ?? 0,
CreatedTime = (DateTime)r.CreatedTime
}
)
.ToList();
// 先按照天进行上卷聚合
var first_groups_success = deviceRepair_list.GroupBy(it => it.CreatedTime.Date);
int[] Data2 = new int[first_groups_success.Count()];
int index2 = 0;
foreach (var group in first_groups_success)
{
Data2[index2] = group.Count();
index2++;
}
repair_success_all.Data = Data2;
2024-06-17 14:20:48 +08:00
SeriesData.Add(repair_success_all);
result.SeriesData = SeriesData;
return result;
}
/// <summary>
/// 获取每日点检巡检总数折线图
/// </summary>
/// <param name="devicedefault"></param>
/// <returns></returns>
public TaskLineResultDto TaskLinebyDay(DeviceDefaultDto devicedefault)
{
2024-06-17 14:20:48 +08:00
TaskLineResultDto taskLine = new TaskLineResultDto();
2025-06-12 19:10:15 +08:00
var predicate = Expressionable
.Create<DeviceTaskExecute>()
.AndIF(
devicedefault.searchTime[0] > new DateTime(1991, 1, 1),
(r) => r.CreatedTime >= devicedefault.searchTime[0]
)
.AndIF(
devicedefault.searchTime[1] > new DateTime(1991, 1, 1),
(r) => r.CreatedTime <= devicedefault.searchTime[1]
)
.ToExpression();
2025-06-12 19:10:15 +08:00
List<DeviceTaskExecute> InitDataCollection = Context
.Queryable<DeviceTaskExecute>()
.Where(predicate)
.ToList();
2024-06-17 14:20:48 +08:00
List<SeriesData_Item_line3> SeriesData = new List<SeriesData_Item_line3>();
2025-06-12 19:10:15 +08:00
var SecondDataCollection = InitDataCollection.GroupBy(it =>
((DateTime)it.CreatedTime).Date
);
2024-06-17 14:20:48 +08:00
string[] Xdata = new string[SecondDataCollection.Count()];
int index = 0;
foreach (var group in SecondDataCollection)
{
Xdata[index] = group.Key.ToString("yyyy-MM-dd");
index++;
}
taskLine.XData = Xdata;
SeriesData_Item_line3 line3 = new SeriesData_Item_line3();
2024-06-17 14:20:48 +08:00
int[] Data = new int[SecondDataCollection.Count()];
int index2 = 0;
foreach (var group in SecondDataCollection)
{
Data[index2] = group.Count();
2024-06-17 14:20:48 +08:00
index2++;
}
line3.Data = Data;
SeriesData.Add(line3);
taskLine.SeriesData = SeriesData;
return taskLine;
}
2025-06-12 19:10:15 +08:00
2024-06-17 16:19:27 +08:00
private PersonnelResponseResultDto dataAnalysis(DeviceDefaultDto devicedefault)
2024-06-17 14:20:48 +08:00
{
2025-06-12 19:10:15 +08:00
PersonnelResponseResultDto personnelResponseResultDto =
new PersonnelResponseResultDto();
2024-06-17 14:20:48 +08:00
personnelResponseResultDto.XData = [];
personnelResponseResultDto.SeriesData = null;
// 任务接受响应系列
PersonnelResponse_Series task_accept_series = new PersonnelResponse_Series();
2024-06-17 16:07:39 +08:00
2025-06-12 19:10:15 +08:00
var predicate = Expressionable
.Create<DeviceTaskExecute>()
.AndIF(
devicedefault.searchTime[0] > new DateTime(1991, 1, 1),
(r) => r.CreatedTime >= devicedefault.searchTime[0]
)
.AndIF(
devicedefault.searchTime[1] > new DateTime(1991, 1, 1),
(r) => r.CreatedTime <= devicedefault.searchTime[1]
)
.And(it => it.DistributedTime != DateTime.MinValue)
.And(it => it.StartTime != DateTime.MinValue)
.And(it => it.EndTime != DateTime.MinValue)
.ToExpression();
2024-06-17 14:20:48 +08:00
List<DeviceTaskExecute> InitDataCollection = Context
.Queryable<DeviceTaskExecute>()
.Where(predicate)
.ToList();
2024-06-17 16:16:05 +08:00
if (InitDataCollection.Count == 0)
{
personnelResponseResultDto.TaskTotal = 0;
personnelResponseResultDto.TaskMax = new TimeSpan(0);
personnelResponseResultDto.TaskMin = new TimeSpan(0);
personnelResponseResultDto.TaskAvg = new TimeSpan(0);
return personnelResponseResultDto;
}
2024-06-17 14:20:48 +08:00
personnelResponseResultDto.TaskTotal = InitDataCollection.Count;
2025-06-12 19:10:15 +08:00
PersonnelResponseDto[] SecondDataCollection = new PersonnelResponseDto[
InitDataCollection.Count
];
2024-06-17 16:07:39 +08:00
2024-06-17 14:20:48 +08:00
string[] XData = new string[InitDataCollection.Count];
int[] resultDate = new int[InitDataCollection.Count];
2024-06-17 14:28:22 +08:00
if (InitDataCollection.Count > 0)
2024-06-17 14:20:48 +08:00
{
2024-06-17 14:44:29 +08:00
int index = 0;
2024-06-17 14:28:22 +08:00
foreach (var item in InitDataCollection)
{
2024-06-17 14:44:29 +08:00
XData[index] = item.TaskName;
PersonnelResponseDto temp = new PersonnelResponseDto();
temp.Id = item.Id;
temp.TaskName = item.TaskName;
2024-06-17 16:07:39 +08:00
2025-06-12 19:10:15 +08:00
temp.Accept_response =
(item.StartTime ?? DateTime.Now) - (item.DistributedTime ?? DateTime.Now);
temp.Action_response =
(item.EndTime ?? DateTime.Now) - (item.StartTime ?? DateTime.Now);
2024-06-17 16:07:39 +08:00
2024-06-17 14:44:29 +08:00
SecondDataCollection[index] = temp;
2024-06-17 14:28:22 +08:00
index++;
}
2024-06-17 14:20:48 +08:00
}
2024-06-17 16:07:39 +08:00
2024-06-17 14:20:48 +08:00
#region
MarkPoint_ markPoint_ = new MarkPoint_();
TypeName[] data = new TypeName[2];
TypeName item1 = new TypeName();
item1.Type = "max";
item1.Name = "最大值";
data[0] = item1;
TypeName item2 = new TypeName();
item2.Type = "min";
item2.Name = "最小值";
data[1] = item2;
markPoint_.data = data;
task_accept_series.markPoint = markPoint_;
MarkPoint_ markLine = new MarkPoint_();
2024-06-17 15:06:41 +08:00
TypeName[] data2 = new TypeName[1];
2024-06-17 14:20:48 +08:00
TypeName item3 = new TypeName();
item3.Type = "average";
item3.Name = "平均值";
data2[0] = item3;
markLine.data = data2;
task_accept_series.markLine = markLine;
#endregion
2024-06-17 14:44:29 +08:00
if (devicedefault.searchType == 1)
2024-06-17 14:20:48 +08:00
{
task_accept_series.Name = "人员接受任务响应时间";
2025-06-12 19:10:15 +08:00
personnelResponseResultDto.TaskMax = SecondDataCollection.Max(it =>
it.Accept_response
);
personnelResponseResultDto.TaskMin = SecondDataCollection.Min(it =>
it.Accept_response
);
personnelResponseResultDto.TaskAvg = CalculateAverageTimeSpan(
SecondDataCollection.Select(it => it.Action_response).ToArray()
);
2024-06-17 14:20:48 +08:00
personnelResponseResultDto.SeriesData = task_accept_series;
2025-06-12 19:10:15 +08:00
resultDate = SecondDataCollection
.Select(it => (int)Math.Ceiling(it.Accept_response.TotalMinutes))
.ToArray();
2024-06-17 14:20:48 +08:00
}
2024-06-17 14:44:29 +08:00
else if (devicedefault.searchType == 2)
2024-06-17 14:20:48 +08:00
{
task_accept_series.Name = "人员处理任务响应时间";
2025-06-12 19:10:15 +08:00
personnelResponseResultDto.TaskMax = SecondDataCollection.Max(it =>
it.Action_response
);
personnelResponseResultDto.TaskMin = SecondDataCollection.Min(it =>
it.Action_response
);
personnelResponseResultDto.TaskAvg = CalculateAverageTimeSpan(
SecondDataCollection.Select(it => it.Action_response).ToArray()
);
2024-06-17 15:14:10 +08:00
personnelResponseResultDto.SeriesData = task_accept_series;
2025-06-12 19:10:15 +08:00
resultDate = SecondDataCollection
.Select(it => (int)Math.Ceiling(it.Action_response.TotalMinutes))
.ToArray();
2024-06-17 14:20:48 +08:00
}
2024-06-17 14:44:29 +08:00
personnelResponseResultDto.XData = XData;
2024-06-17 14:20:48 +08:00
personnelResponseResultDto.SeriesData.Data = resultDate;
2024-06-17 16:07:39 +08:00
return personnelResponseResultDto;
2024-06-17 16:07:39 +08:00
}
2025-06-12 19:10:15 +08:00
2024-06-17 16:07:39 +08:00
/// <summary>
/// 获取人员响应折线图
/// </summary>
/// <param name="devicedefault"></param>
/// <returns></returns>
public PersonnelResponseResultDto PersonResponse(DeviceDefaultDto devicedefault)
{
2024-06-17 16:19:27 +08:00
PersonnelResponseResultDto crount = dataAnalysis(devicedefault);
DeviceDefaultDto devicedefault_last = new DeviceDefaultDto();
devicedefault_last.searchType = devicedefault.searchType;
2025-06-12 19:10:15 +08:00
DateTime lastMondayMidnight,
lastSundayMidnight;
GetLastWeekBoundaries(
devicedefault.searchTime[0],
out lastMondayMidnight,
out lastSundayMidnight
);
devicedefault_last.searchTime = new DateTime[2]
{
lastMondayMidnight,
lastSundayMidnight
};
2024-06-17 16:07:39 +08:00
PersonnelResponseResultDto last = dataAnalysis(devicedefault_last);
2024-06-17 16:19:27 +08:00
crount.lastTotalPer = CalculatePercentageIncrease(crount.TaskTotal, last.TaskTotal);
2024-06-17 16:07:39 +08:00
crount.lastMaxPer = CalculatePercentageIncrease(crount.TaskMax, last.TaskMax);
crount.lastMinPer = CalculatePercentageIncrease(crount.TaskMin, last.TaskMin);
crount.lastAvgPer = CalculatePercentageIncrease(crount.TaskAvg, last.TaskAvg);
return crount;
2024-06-17 14:20:48 +08:00
}
2025-06-12 19:10:15 +08:00
private static int CalculatePercentageIncrease(int number1, int number2)
2024-06-17 16:19:27 +08:00
{
if (number2 == 0)
{
return 100;
}
2024-06-17 14:20:48 +08:00
return ((int)(number1 - number2) / number2) * 100;
2024-06-17 16:19:27 +08:00
}
2025-06-12 19:10:15 +08:00
private static int CalculatePercentageIncrease(TimeSpan timeSpan1, TimeSpan timeSpan2)
2024-06-17 16:07:39 +08:00
{
double totalSeconds1 = timeSpan1.TotalSeconds;
double totalSeconds2 = timeSpan2.TotalSeconds;
if (totalSeconds2 == 0)
{
2024-06-17 16:16:05 +08:00
return 100;
2024-06-17 16:07:39 +08:00
}
return (int)((totalSeconds1 - totalSeconds2) / totalSeconds2) * 100;
2024-06-17 16:07:39 +08:00
}
2025-06-12 19:10:15 +08:00
2024-06-17 14:20:48 +08:00
public static TimeSpan CalculateAverageTimeSpan(TimeSpan[] timeSpans)
{
// 将 TimeSpan 数组中的每个 TimeSpan 值转换为总秒数
double totalSeconds = timeSpans.Sum(ts => ts.TotalSeconds);
// 计算平均总秒数
double averageSeconds = totalSeconds / timeSpans.Length;
// 将平均总秒数转换为 TimeSpan 格式
TimeSpan averageTimeSpan = TimeSpan.FromSeconds(averageSeconds);
return averageTimeSpan;
}
2025-06-12 19:10:15 +08:00
public static void GetLastWeekBoundaries(
DateTime inputDateTime,
out DateTime lastMondayMidnight,
out DateTime lastSundayMidnight
)
2024-06-17 16:07:39 +08:00
{
DayOfWeek currentDayOfWeek = inputDateTime.DayOfWeek;
int daysToSubtract = (int)currentDayOfWeek + 6; // 6 days to subtract to get to last Monday
2024-06-17 14:20:48 +08:00
2024-06-17 16:07:39 +08:00
lastMondayMidnight = inputDateTime.Date.AddDays(-daysToSubtract).AddDays(-7).Date; // Last Monday midnight
2025-06-12 19:10:15 +08:00
lastSundayMidnight = lastMondayMidnight
.AddDays(6)
.AddHours(23)
.AddMinutes(59)
.AddSeconds(59); // Last Sunday midnight
2024-06-17 16:07:39 +08:00
}
2024-06-18 09:04:17 +08:00
/// <summary>
2025-06-12 19:10:15 +08:00
/// 获取大屏总数 - 报修部分分开查询
2024-06-18 09:04:17 +08:00
/// </summary>
/// <returns></returns>
public FullScreenTotal GetAllTotal()
{
2025-06-12 19:10:15 +08:00
var screenTotal = new FullScreenTotal();
// 设备可用数/停机数(保持原样)
screenTotal.UseDeviceTotal = Context
.Queryable<DeviceAccount>()
.Where(it => it.Status == 1)
.Count();
screenTotal.UnUseDeviceTotal = Context
.Queryable<DeviceAccount>()
.Where(it => it.Status == 0)
.Count();
// 累计任务数/已完成任务数(保持原样)
2024-06-18 09:04:17 +08:00
screenTotal.TaskTotal = Context.Queryable<DeviceTaskExecute>().Count();
2025-06-12 19:10:15 +08:00
screenTotal.FinishTaskTotal = Context
.Queryable<DeviceTaskExecute>()
.Where(it => it.Status == 2)
.Count();
// 报修分类:分开查询
var startOfMonth = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);
var endOfMonth = startOfMonth.AddMonths(1).AddSeconds(-1);
// 1. 本月总报修数
screenTotal.RepairTotal = Context
.Queryable<DeviceRepair>()
.Where(it => it.CreatedTime >= startOfMonth && it.CreatedTime <= endOfMonth)
.Count();
// 2. 本月已完成的报修数Status == 2
const int RepairFinishedStatus = 2;
screenTotal.FinishRepairTotal = Context
.Queryable<DeviceRepair>()
.Where(it =>
it.CreatedTime >= startOfMonth
&& it.CreatedTime <= endOfMonth
&& it.Status == RepairFinishedStatus
)
.Count();
2024-06-18 09:04:17 +08:00
2025-06-12 19:10:15 +08:00
return screenTotal;
}
2024-06-18 09:04:17 +08:00
2025-06-12 19:10:15 +08:00
public List<DeviceTaskExecute> GetTaskExecuteList(DeviceDataListDto parm)
{
var predicate = Expressionable
.Create<DeviceTaskExecute>()
.AndIF(parm.StartTime > DateTime.MinValue, it => it.CreatedTime >= parm.StartTime)
.AndIF(parm.EndTime > DateTime.MinValue, it => it.CreatedTime <= parm.EndTime);
2024-06-18 09:04:17 +08:00
2025-06-12 19:10:15 +08:00
var response = Context
.Queryable<DeviceTaskExecute>()
.Where(predicate.ToExpression())
.OrderByDescending(x => x.Id)
.ToList();
return response;
}
2024-06-18 09:04:17 +08:00
2025-06-12 19:10:15 +08:00
public List<DeviceDataListTableDto> GetDeviceRepairList(DeviceDataListDto parm)
{
var predicate = Expressionable
.Create<DeviceRepair,DeviceAccount>()
.AndIF(parm.StartTime > DateTime.MinValue, (dr,da) => dr.CreatedTime >= parm.StartTime)
.AndIF(parm.EndTime > DateTime.MinValue, (dr, da) => dr.CreatedTime <= parm.EndTime);
2024-06-18 09:04:17 +08:00
2025-06-12 19:10:15 +08:00
var response = Context
.Queryable<DeviceRepair, DeviceAccount>((dr, da) => new JoinQueryInfos(JoinType.Left,dr.FkDeviceId == da.Id))
.Where(predicate.ToExpression())
.OrderByDescending((dr, da) => dr.Id)
.Select<DeviceDataListTableDto>()
.ToList();
return response;
2024-06-18 09:04:17 +08:00
}
2024-06-13 16:23:37 +08:00
}
}