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

180 lines
7.8 KiB
C#
Raw Normal View History

2024-06-13 16:23:37 +08:00
using Infrastructure.Attribute;
2024-06-13 17:03:54 +08:00
using Infrastructure.Extensions;
2024-06-13 16:23:37 +08:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZR.Model.MES.dev;
using ZR.Model.MES.dev.Dto;
2024-06-14 08:44:08 +08:00
using ZR.Model.System;
2024-06-13 16:23:37 +08:00
using ZR.Service.MES.dev.IService;
namespace ZR.Service.MES.dev
{
/// <summary>
/// 设备数据分析
/// </summary>
[AppService(ServiceType = typeof(IDeviceDataAnalysisServcie), ServiceLifetime = LifeTime.Transient)]
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);
List<int> children = nodeList.Where(n => n.ParentId == parentId).Select(n => n.Id).ToList();
if (children.Any())
{
childIds.AddRange(children);
}
else
{
result.Add(nodeList.First(n => n.Id == parentId));
}
}
return result;
}
2024-06-13 16:23:37 +08:00
/// <summary>
/// 故障类型 pie
/// </summary>
/// <param name="devicedefault"></param>
/// <returns></returns>
public List<DeviceStatusAnalysisResultDto> DefaultTypePie(DeviceDefaultDto devicedefault)
{
2024-06-14 14:35:26 +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
{
2024-06-14 14:35:26 +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();
2024-06-13 17:03:54 +08:00
}
2024-06-14 14:35:26 +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())
2024-06-14 14:35:26 +08:00
.Select((r, a, d) => new DeviceRepair_chart
2024-06-14 08:44:08 +08:00
{
2024-06-14 14:35:26 +08:00
Type = r.Type,
TypeName = d.DictLabel,
2024-06-14 08:44:08 +08:00
}).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-14 08:44:08 +08:00
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)
{
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();
}
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())
.Select((r, a, d) => new DeviceRepair_chart
{
Type = r.Type,
TypeName = d.DictLabel,
CreatedTime = (DateTime)r.CreatedTime
}).ToList();
//获取所有故障类型
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);
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>();
foreach (SysDictData dict in all_fault_type)
{
SeriesData_Item seriesData_Item = new SeriesData_Item();
seriesData_Item.Name = dict.DictLabel;
//这种故障在每天的值
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
.Where(it => it.CreatedTime >= startOfDay && it.CreatedTime <= endOfDay)
.Where(it => it.Type == dict.DictValue).Count();
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;
return result;
}
2024-06-13 16:23:37 +08:00
}
}