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.Service.MES.dev.IService;
namespace ZR.Service.MES.dev
{
///
/// 设备数据分析
///
[AppService(ServiceType = typeof(IDeviceDataAnalysisServcie), ServiceLifetime = LifeTime.Transient)]
public class DeviceDataAnalysisService : BaseService, IDeviceDataAnalysisServcie
{
public static List FindAllLeafNodes(List nodeList, int targetId)
{
List childIds = new List { targetId };
List result = new List();
while (childIds.Any())
{
int parentId = childIds.First();
childIds.RemoveAt(0);
List 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;
}
///
/// 故障类型 pie
///
///
///
public List DefaultTypePie(DeviceDefaultDto devicedefault)
{
List resultList = new List();
int[] all_device_type_array = null;
if (devicedefault.DevicetTypeId>0)
{
List all_device_type_list = FindAllLeafNodes(Context.Queryable().ToList(), devicedefault.DevicetTypeId??0);
all_device_type_array=all_device_type_list.Select(it=>it.Id).ToArray();
}
var predicate = Expressionable.Create()
.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_list= Context.Queryable()
.LeftJoin((r, a) => r.FkDeviceId == a.Id)
.Where(predicate.ToExpression())
.Select((r,a)=>r).ToList();
if(deviceRepair_list.Count > 0)
{
var TypeGroups = deviceRepair_list.GroupBy(it => it.Type);
foreach (var group in TypeGroups)
{
DeviceStatusAnalysisResultDto result = new DeviceStatusAnalysisResultDto();
result.name = group.Key;
result.value = group.Count();
resultList.Add(result);
}
}
return resultList;
}
}
}