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
{
///
/// 设备数据分析
///
[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, 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_list = Context.Queryable()
.LeftJoin((r, a) => r.FkDeviceId == a.Id)
.LeftJoin((r, a, d) => r.Type == d.DictValue)
.Where(predicate.ToExpression())
.Select((r, a, d) => new DeviceRepair_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;
}
///
/// 每日故障数量统计
///
///
///
public DeviceFaultBarResultDto FaultTypeBar(DeviceDefaultDto devicedefault)
{
DeviceFaultBarResultDto result = new DeviceFaultBarResultDto();
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, 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_list = Context.Queryable()
.LeftJoin((r, a) => r.FkDeviceId == a.Id)
.LeftJoin((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 all_fault_type = Context.Queryable().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 = new List();
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++;
}
seriesData_Item.Data = values;
SeriesData.Add(seriesData_Item);
}
result.XData = Xdata;
result.SeriesData = SeriesData;
return result;
}
///
/// 获取设备故障类别累计次数折线图
///
///
///
public DeviceFaultLineResultDto FaultTypeLine(DeviceDefaultDto devicedefault)
{
DeviceFaultLineResultDto result = new DeviceFaultLineResultDto();
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, 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_list = Context.Queryable()
.LeftJoin((r, a) => r.FkDeviceId == a.Id)
.LeftJoin((r, a, d) => r.Type == d.DictValue)
.Where(predicate.ToExpression())
.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)
{
repair.AccValue_SameType = Context.Queryable().Where(it => it.Type == repair.Type)
.Where(it => it.CreatedTime <= repair.CreatedTime).Count();
}
//获取所有故障类型
List all_fault_type = Context.Queryable().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 = new List();
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
.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;
}
///
/// 获取每日设备故障总数与维修完成数柱状图
///
///
///
public DeviceRepairBarResultDto FaultTypeBarByDay(DeviceDefaultDto devicedefault)
{
DeviceRepairBarResultDto result = new DeviceRepairBarResultDto();
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) => 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;
List SeriesData=new List();
SeriesData_Item_line2 faultAll = new SeriesData_Item_line2();
faultAll.Name = "报修数";
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 = "维修数";
List deviceRepair_list_success = Context.Queryable()
.LeftJoin((r, a) => r.FkDeviceId == a.Id)
.Where(predicate.ToExpression())
.Where((r,a)=>r.Status==2)
.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;
SeriesData.Add(repair_success_all);
result.SeriesData = SeriesData;
return result;
}
///
/// 获取每日点检巡检总数折线图
///
///
///
public TaskLineResultDto TaskLinebyDay(DeviceDefaultDto devicedefault)
{
TaskLineResultDto taskLine=new TaskLineResultDto();
var predicate = Expressionable.Create()
.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();
List InitDataCollection= Context.Queryable().Where(predicate).ToList();
List SeriesData=new List();
var SecondDataCollection= InitDataCollection.GroupBy(it => ((DateTime)it.CreatedTime).Date);
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();
int[] Data=new int[SecondDataCollection.Count()];
int index2 = 0;
foreach (var group in SecondDataCollection)
{
Data[index2] = group.Count();
index2++;
}
line3.Data = Data;
SeriesData.Add(line3);
taskLine.SeriesData = SeriesData;
return taskLine;
}
}
}