zhuangpei-mesbackend/ZR.Service/MES/dev/DeviceDataAnalysisService.cs
2024-06-14 08:44:08 +08:00

98 lines
3.9 KiB
C#

using Infrastructure.Attribute;
using Infrastructure.Extensions;
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;
using ZR.Model.System;
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
{
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;
}
/// <summary>
/// 故障类型 pie
/// </summary>
/// <param name="devicedefault"></param>
/// <returns></returns>
public List<DeviceStatusAnalysisResultDto> DefaultTypePie(DeviceDefaultDto devicedefault)
{
List<DeviceStatusAnalysisResultDto> resultList = new List<DeviceStatusAnalysisResultDto>();
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_Pie_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_Pie_chart
{
Type=r.Type,
TypeName=d.DictLabel,
}).ToList();
if(deviceRepair_list.Count > 0)
{
var TypeGroups = deviceRepair_list.GroupBy(it => it.TypeName);
foreach (var group in TypeGroups)
{
DeviceStatusAnalysisResultDto result = new DeviceStatusAnalysisResultDto();
result.name = group.Key;
result.value = group.Count();
resultList.Add(result);
}
}
return resultList;
}
}
}