using AutoMapper; using Microsoft.Data.Sqlite; using Org.BouncyCastle.Crypto.IO; using ProductionLineMonitor.Application.Services.Dtos; using ProductionLineMonitor.Application.Services.FaultService.Dtos; using ProductionLineMonitor.Core.Dtos; using ProductionLineMonitor.Core.IRepositories; using ProductionLineMonitor.Core.Models; using ProductionLineMonitor.EntityFramework; using ProductionLineMonitor.Web.Services; using System; using System.Collections.Generic; using System.Linq; using System.Security.Cryptography.X509Certificates; namespace ProductionLineMonitor.Application.Services.FaultService { public class FaultService : IFaultService { private readonly IUnitOfWork _unitOfWork; private readonly IMapper _mapper; public FaultService(IUnitOfWork unitOfWork, IMapper mapper) { _unitOfWork = unitOfWork; _mapper = mapper; } public void AddMachineFaultComparisons(string faultTopic, IEnumerable machineFaultComparisons) { string sql = "delete from MachineFaultComparisons where FaultTopic=@FaultTopic"; _unitOfWork.ExecuteSql(sql, new[] { new SqliteParameter("@FaultTopic", faultTopic) }); _unitOfWork.MachineFaultComparisonRepository.Create(machineFaultComparisons); _unitOfWork.SaveChanges(); } public IEnumerable GetLineFault(string lineId, DateTime startTime, DateTime endTime, int durationThreshold) { List dtos = new List(); if (!(_unitOfWork.GetDbContext() is ProductionLineContext context)) return dtos; var line = context.ProductionLines?.Find(lineId); if (line == null) return dtos; var machines = context.Machines .Where(x => x.ProductionLineId == line.Id && x.IsInclusionLineStatistics == true) .OrderBy(o => o.ProductionLineOrder); foreach (var machine in machines) { var query = from mfr in context.Set() join mfc in context.Set() on mfr.FaultCode equals mfc.FaultCode where mfr.MachineId == machine.Id && mfc.FaultTopic == machine.FaultTopic && mfr.StartTime >= startTime && mfr.StartTime < endTime orderby mfr.StartTime select new { mfr, mfc }; MachineFaultRecord? temp = null; foreach (var item in query) { if (!item.mfc.FaultInfo.Contains("安全门") && !item.mfc.FaultInfo.Contains("门禁") && !item.mfc.FaultInfo.Contains("提示上料") && !item.mfc.FaultInfo.Contains("提示卸料") && item.mfr.StartTime != null && item.mfr.EndTime != null) { if (temp != null) { if (item.mfr.StartTime >= temp.StartTime && item.mfr.EndTime <= temp.EndTime) { continue; } } LineFaultDto dto = new LineFaultDto() { MachineType = machine.Type, FaultCode = item.mfr.FaultCode.ToString(), FaultInfo = item.mfc.FaultInfo, StartTime = item.mfr.StartTime.Value, EndTime = item.mfr.EndTime.Value, MachineState = item.mfr.MachineState == null ? 0 : item.mfr.MachineState.Value, TriggerNumber = item.mfr.TriggerNumber == null ? 0 : item.mfr.TriggerNumber.Value, State = item.mfr.State == null ? 0 : item.mfr.State.Value, DataSource = 0 }; if (dto.EndTime > endTime) { dto.EndTime = endTime; } dto.Duration = (dto.EndTime - dto.StartTime).Value.TotalMinutes; if (dto.Duration >= durationThreshold) { dtos.Add(dto); temp = item.mfr; } } } } List keyInFaults = new List(); //keyInFaults.AddRange(GetKeyInLineFault(line.Floor, line.Line, startTime.ToString("yyyy-MM-dd"), "COG", endTime, "COG")); keyInFaults.AddRange(GetKeyInLineFault(line.Floor, line.Line, startTime.ToString("yyyy-MM-dd"), "BC机", endTime, "BC机")); foreach (var machine in machines) { switch (machine.Type) { case "AG + FPL": keyInFaults.AddRange(GetKeyInLineFault(line.Floor, line.Line, startTime.ToString("yyyy-MM-dd"), "TPA", endTime, machine.Type)); keyInFaults.AddRange(GetKeyInLineFault(line.Floor, line.Line, startTime.ToString("yyyy-MM-dd"), "FPL", endTime, machine.Type)); keyInFaults.AddRange(GetKeyInLineFault(line.Floor, line.Line, startTime.ToString("yyyy-MM-dd"), "FPL01", endTime, machine.Type)); keyInFaults.AddRange(GetKeyInLineFault(line.Floor, line.Line, startTime.ToString("yyyy-MM-dd"), "FPL02", endTime, machine.Type)); keyInFaults.AddRange(GetKeyInLineFault(line.Floor, line.Line, startTime.ToString("yyyy-MM-dd"), "FPL03", endTime, machine.Type)); keyInFaults.AddRange(GetKeyInLineFault(line.Floor, line.Line, startTime.ToString("yyyy-MM-dd"), "FPL04", endTime, machine.Type)); break; case "FPL": keyInFaults.AddRange(GetKeyInLineFault(line.Floor, line.Line, startTime.ToString("yyyy-MM-dd"), "TPA", endTime, machine.Type)); keyInFaults.AddRange(GetKeyInLineFault(line.Floor, line.Line, startTime.ToString("yyyy-MM-dd"), "FPL", endTime, machine.Type)); keyInFaults.AddRange(GetKeyInLineFault(line.Floor, line.Line, startTime.ToString("yyyy-MM-dd"), "FPL01", endTime, machine.Type)); keyInFaults.AddRange(GetKeyInLineFault(line.Floor, line.Line, startTime.ToString("yyyy-MM-dd"), "FPL02", endTime, machine.Type)); keyInFaults.AddRange(GetKeyInLineFault(line.Floor, line.Line, startTime.ToString("yyyy-MM-dd"), "FPL03", endTime, machine.Type)); keyInFaults.AddRange(GetKeyInLineFault(line.Floor, line.Line, startTime.ToString("yyyy-MM-dd"), "FPL04", endTime, machine.Type)); break; case "FOG": keyInFaults.AddRange(GetKeyInLineFault(line.Floor, line.Line, startTime.ToString("yyyy-MM-dd"), "FOG", endTime, machine.Type)); break; case "AOI": keyInFaults.AddRange(GetKeyInLineFault(line.Floor, line.Line, startTime.ToString("yyyy-MM-dd"), "LOT2-AOI", endTime, machine.Type)); break; default: keyInFaults.AddRange(GetKeyInLineFault(line.Floor, line.Line, startTime.ToString("yyyy-MM-dd"), machine.Type, endTime, machine.Type)); break; } } foreach (var keyInFault in keyInFaults) { if (keyInFault.StartTime != null && keyInFault.EndTime != null) { LineFaultDto dto = new LineFaultDto { MachineType = keyInFault.MachineType, FaultCode = keyInFault.FaultCode.ToString(), FaultInfo = keyInFault.FaultInfo, StartTime = keyInFault.StartTime, EndTime = keyInFault.EndTime, DataSource = 1 }; dto.Duration = (keyInFault.EndTime.Value - keyInFault.StartTime).TotalMinutes; if (dto.Duration >= durationThreshold) { dtos.Add(dto); } } } return dtos; } public IEnumerable GetAccumulatedFaultTop10ByLine(string lineId, DateTime startTime, DateTime endTime) { List dtos = new List(); if (!(_unitOfWork.GetDbContext() is ProductionLineContext context)) return dtos; var line = context.ProductionLines?.Find(lineId); if (line == null) return dtos; var machines = context.Machines .Where(x => x.ProductionLineId == line.Id && x.IsInclusionLineStatistics == true) .OrderBy(o => o.ProductionLineOrder); foreach (var machine in machines) { var query = from mfr in context.Set() join mfc in context.Set() on mfr.FaultCode equals mfc.FaultCode where mfr.MachineId == machine.Id && mfc.FaultTopic == machine.FaultTopic && mfr.StartTime >= startTime && mfr.StartTime < endTime orderby mfr.StartTime select new { mfr, mfc }; foreach (var item in query) { if (!item.mfc.FaultInfo.Contains("安全门") && !item.mfc.FaultInfo.Contains("门禁") && !item.mfc.FaultInfo.Contains("提示上料") && !item.mfc.FaultInfo.Contains("提示卸料") && item.mfr.StartTime != null && item.mfr.EndTime != null) { LineAccumulatedFaultDto statisticDto = new LineAccumulatedFaultDto { MachineType = machine.Type, FaultCode = item.mfr.FaultCode.ToString(), FaultInfo = item.mfc.FaultInfo }; var temp = dtos.Find( x => x.MachineType == statisticDto.MachineType && x.FaultInfo == statisticDto.FaultInfo); if (temp == null) { statisticDto.AccumulativeTotal = 1; if (item.mfr.EndTime.Value > endTime) statisticDto.AccumulativeTime = (endTime - item.mfr.StartTime.Value).TotalMinutes; else statisticDto.AccumulativeTime = (item.mfr.EndTime.Value - item.mfr.StartTime.Value).TotalMinutes; dtos.Add(statisticDto); } else { temp.AccumulativeTotal += 1; if (item.mfr.EndTime.Value > endTime) temp.AccumulativeTime += (endTime - item.mfr.StartTime.Value).TotalMinutes; else temp.AccumulativeTime += (item.mfr.EndTime.Value - item.mfr.StartTime.Value).TotalMinutes; } } } } List keyInFaults = new List(); //keyInFaults.AddRange(GetKeyInLineFault(line.Floor, line.Line, startTime.ToString("yyyy-MM-dd"), "COG", endTime, "COG")); keyInFaults.AddRange(GetKeyInLineFault(line.Floor, line.Line, startTime.ToString("yyyy-MM-dd"), "BC机", endTime, "BC机")); foreach (var machine in machines) { switch (machine.Type) { case "AG + FPL": keyInFaults.AddRange(GetKeyInLineFault(line.Floor, line.Line, startTime.ToString("yyyy-MM-dd"), "TPA", endTime, machine.Type)); keyInFaults.AddRange(GetKeyInLineFault(line.Floor, line.Line, startTime.ToString("yyyy-MM-dd"), "FPL", endTime, machine.Type)); keyInFaults.AddRange(GetKeyInLineFault(line.Floor, line.Line, startTime.ToString("yyyy-MM-dd"), "FPL01", endTime, machine.Type)); keyInFaults.AddRange(GetKeyInLineFault(line.Floor, line.Line, startTime.ToString("yyyy-MM-dd"), "FPL02", endTime, machine.Type)); keyInFaults.AddRange(GetKeyInLineFault(line.Floor, line.Line, startTime.ToString("yyyy-MM-dd"), "FPL03", endTime, machine.Type)); keyInFaults.AddRange(GetKeyInLineFault(line.Floor, line.Line, startTime.ToString("yyyy-MM-dd"), "FPL04", endTime, machine.Type)); break; case "FPL": keyInFaults.AddRange(GetKeyInLineFault(line.Floor, line.Line, startTime.ToString("yyyy-MM-dd"), "TPA", endTime, machine.Type)); keyInFaults.AddRange(GetKeyInLineFault(line.Floor, line.Line, startTime.ToString("yyyy-MM-dd"), "FPL", endTime, machine.Type)); break; case "FOG": keyInFaults.AddRange(GetKeyInLineFault(line.Floor, line.Line, startTime.ToString("yyyy-MM-dd"), "FOG", endTime, machine.Type)); break; case "AOI": keyInFaults.AddRange(GetKeyInLineFault(line.Floor, line.Line, startTime.ToString("yyyy-MM-dd"), "LOT2-AOI", endTime, machine.Type)); break; default: keyInFaults.AddRange(GetKeyInLineFault(line.Floor, line.Line, startTime.ToString("yyyy-MM-dd"), machine.Type, endTime, machine.Type)); break; } } foreach (var item in keyInFaults) { if (item.StartTime != null && item.EndTime != null && item.StartTime >= startTime && item.StartTime < endTime) { LineAccumulatedFaultDto statisticDto = new LineAccumulatedFaultDto { MachineType = item.MachineType, FaultCode = item.FaultCode.ToString(), FaultInfo = item.FaultInfo, DataSource = 1 }; var temp = dtos.Find( x => x.MachineType == statisticDto.MachineType && x.FaultInfo == statisticDto.FaultInfo && x.DataSource == statisticDto.DataSource); if (temp == null) { statisticDto.AccumulativeTotal = 1; statisticDto.AccumulativeTime = (item.EndTime.Value - item.StartTime).TotalMinutes; dtos.Add(statisticDto); } else { temp.AccumulativeTotal += 1; temp.AccumulativeTime += (item.EndTime.Value - item.StartTime).TotalMinutes; } } } dtos = dtos.Where(x => x.AccumulativeTime >= 1).OrderByDescending(o => o.AccumulativeTime).Take(10).ToList(); return dtos; } private List GetKeyInLineFault(int floor, int line, string date, string type, DateTime endTime, string machineType) { List dtos = new List(); var fs = MesApiService.GetKeyInFaults(floor, line, date, type); foreach (var f in fs) { LineFaultDto dto = new LineFaultDto { MachineType = machineType, FaultCode = f.FaultCode, FaultInfo = f.FaultInfo, StartTime = f.StartTime }; if (f.EndTime == null) dto.EndTime = endTime; else dto.EndTime = f.EndTime.Value; dto.Duration = (dto.EndTime.Value - dto.StartTime).TotalMinutes; dtos.Add(dto); } return dtos; } public IEnumerable FaultSplitToHour(IEnumerable lineFaultDtos) { IList dtos = new List(); foreach (var lineFaultDto in lineFaultDtos) { if (lineFaultDto.EndTime == null) continue; IList dateTimes = new List { lineFaultDto.StartTime }; DateTime d1 = lineFaultDto.StartTime; int j = 1; while (d1 < lineFaultDto.EndTime.Value) { d1 = lineFaultDto.StartTime.AddHours(j); DateTime d2 = Convert.ToDateTime($"{d1:yyyy-MM-dd HH}:00:00"); j++; if (d2 < lineFaultDto.EndTime.Value) { dateTimes.Add(d2); } } dateTimes.Add(lineFaultDto.EndTime.Value); for (int i = 0; i < dateTimes.Count - 1; i++) { LineFaultDto dto = new LineFaultDto { MachineType = lineFaultDto.MachineType, FaultCode = lineFaultDto.FaultCode, FaultInfo = lineFaultDto.FaultInfo, DataSource = lineFaultDto.DataSource, StartTime = dateTimes[i], EndTime = dateTimes[i + 1] }; dto.Duration = (dto.EndTime - dto.StartTime).Value.TotalMinutes; dtos.Add(dto); } } return dtos; } public IEnumerable FilterDuplicateFaults(IEnumerable lineFaultDtos) { IList dtos = new List(); LineFaultDto? temp = null; foreach (var item in lineFaultDtos) { if (temp == null) { temp = item; } else { if (item.StartTime >= temp.StartTime && item.EndTime <= temp.EndTime) { continue; } temp = item; } dtos.Add(item); } return dtos; } public List GetAccumulatedFaultTopByLine(string lineId, DateTime startTime, DateTime endTime) { List dtos = new List(); if (!(_unitOfWork.GetDbContext() is ProductionLineContext context)) return dtos; var line = context.ProductionLines?.Find(lineId); if (line == null) return dtos; var machines = context.Machines .Where(x => x.ProductionLineId == line.Id && x.IsInclusionLineStatistics == true) .OrderBy(o => o.ProductionLineOrder); foreach (var machine in machines) { var query = from mfr in context.Set() join mfc in context.Set() on mfr.FaultCode equals mfc.FaultCode where mfr.MachineId == machine.Id && mfc.FaultTopic == machine.FaultTopic && mfr.StartTime >= startTime && mfr.StartTime < endTime orderby mfr.StartTime select new { mfr, mfc }; foreach (var item in query) { if (!item.mfc.FaultInfo.Contains("安全门") && !item.mfc.FaultInfo.Contains("门禁") && !item.mfc.FaultInfo.Contains("提示上料") && !item.mfc.FaultInfo.Contains("提示卸料") && item.mfr.StartTime != null && item.mfr.EndTime != null) { LineAccumulatedFaultDto statisticDto = new LineAccumulatedFaultDto { MachineType = machine.Type, FaultCode = item.mfr.FaultCode.ToString(), FaultInfo = item.mfc.FaultInfo }; var temp = dtos.Find( x => x.MachineType == statisticDto.MachineType && x.FaultInfo == statisticDto.FaultInfo); if (temp == null) { statisticDto.AccumulativeTotal = 1; if (item.mfr.EndTime.Value > endTime) statisticDto.AccumulativeTime = (endTime - item.mfr.StartTime.Value).TotalMinutes; else statisticDto.AccumulativeTime = (item.mfr.EndTime.Value - item.mfr.StartTime.Value).TotalMinutes; dtos.Add(statisticDto); } else { temp.AccumulativeTotal += 1; if (item.mfr.EndTime.Value > endTime) temp.AccumulativeTime += (endTime - item.mfr.StartTime.Value).TotalMinutes; else temp.AccumulativeTime += (item.mfr.EndTime.Value - item.mfr.StartTime.Value).TotalMinutes; } } } } List keyInFaults = new List(); //keyInFaults.AddRange(GetKeyInLineFault(line.Floor, line.Line, startTime.ToString("yyyy-MM-dd"), "COG", endTime, "COG")); keyInFaults.AddRange(GetKeyInLineFault(line.Floor, line.Line, startTime.ToString("yyyy-MM-dd"), "BC机", endTime, "BC机")); foreach (var machine in machines) { switch (machine.Type) { case "AG + FPL": keyInFaults.AddRange(GetKeyInLineFault(line.Floor, line.Line, startTime.ToString("yyyy-MM-dd"), "TPA", endTime, machine.Type)); keyInFaults.AddRange(GetKeyInLineFault(line.Floor, line.Line, startTime.ToString("yyyy-MM-dd"), "FPL", endTime, machine.Type)); keyInFaults.AddRange(GetKeyInLineFault(line.Floor, line.Line, startTime.ToString("yyyy-MM-dd"), "FPL01", endTime, machine.Type)); keyInFaults.AddRange(GetKeyInLineFault(line.Floor, line.Line, startTime.ToString("yyyy-MM-dd"), "FPL02", endTime, machine.Type)); keyInFaults.AddRange(GetKeyInLineFault(line.Floor, line.Line, startTime.ToString("yyyy-MM-dd"), "FPL03", endTime, machine.Type)); keyInFaults.AddRange(GetKeyInLineFault(line.Floor, line.Line, startTime.ToString("yyyy-MM-dd"), "FPL04", endTime, machine.Type)); break; case "FPL": keyInFaults.AddRange(GetKeyInLineFault(line.Floor, line.Line, startTime.ToString("yyyy-MM-dd"), "TPA", endTime, machine.Type)); keyInFaults.AddRange(GetKeyInLineFault(line.Floor, line.Line, startTime.ToString("yyyy-MM-dd"), "FPL", endTime, machine.Type)); break; case "FOG": keyInFaults.AddRange(GetKeyInLineFault(line.Floor, line.Line, startTime.ToString("yyyy-MM-dd"), "FOG", endTime, machine.Type)); break; case "AOI": keyInFaults.AddRange(GetKeyInLineFault(line.Floor, line.Line, startTime.ToString("yyyy-MM-dd"), "LOT2-AOI", endTime, machine.Type)); break; default: keyInFaults.AddRange(GetKeyInLineFault(line.Floor, line.Line, startTime.ToString("yyyy-MM-dd"), machine.Type, endTime, machine.Type)); break; } } foreach (var item in keyInFaults) { if (item.StartTime != null && item.EndTime != null && item.StartTime >= startTime && item.StartTime < endTime) { LineAccumulatedFaultDto statisticDto = new LineAccumulatedFaultDto { MachineType = item.MachineType, FaultCode = item.FaultCode.ToString(), FaultInfo = item.FaultInfo, DataSource = 1 }; var temp = dtos.Find( x => x.MachineType == statisticDto.MachineType && x.FaultInfo == statisticDto.FaultInfo && x.DataSource == statisticDto.DataSource); if (temp == null) { statisticDto.AccumulativeTotal = 1; statisticDto.AccumulativeTime = (item.EndTime.Value - item.StartTime).TotalMinutes; dtos.Add(statisticDto); } else { temp.AccumulativeTotal += 1; temp.AccumulativeTime += (item.EndTime.Value - item.StartTime).TotalMinutes; } } } dtos = dtos.Where(x => x.AccumulativeTime >= 1).OrderByDescending(o => o.AccumulativeTime).ToList(); return dtos; } public List GetMachineFaultsByTop10(string machineId, DateTime day, string keywords) { List dtos = new List(); var machine = _unitOfWork.MachineRepository.GetById(machineId); if (machine != null) { var faults = _unitOfWork.MachineFaultRecordRepository.GetList( x => x.MachineId == machineId); DateTime startTime = day.AddHours(+8); DateTime endTime = day.AddDays(+1).AddHours(+8); var lst = _unitOfWork.MachineFaultRecordRepository .GetList(x => x.MachineId == machineId && x.StartTime >= startTime && x.StartTime < endTime) .OrderBy(o => o.StartTime) .ToList(); dtos = _mapper.Map>(lst); foreach (var faultRecord in dtos) { faultRecord.MachineName = machine.Name; MachineFaultComparison machineFaultComparison = _unitOfWork.MachineFaultComparisonRepository .FirstOrDefault(x => x.FaultTopic == machine.FaultTopic && x.FaultCode == faultRecord.FaultCode); if (machineFaultComparison != null) { faultRecord.FaultLevel = (int)machineFaultComparison.FaultLevel; faultRecord.FaultInfo = machineFaultComparison.FaultInfo; faultRecord.FaultCategory = machineFaultComparison.FaultCategory; } else { faultRecord.FaultLevel = 0; faultRecord.FaultInfo = "未知故障"; faultRecord.FaultCategory = "未知"; } if (faultRecord.StartTime != null && faultRecord.EndTime != null) faultRecord.Duration = (faultRecord.EndTime.Value - faultRecord.StartTime.Value).TotalMinutes; else faultRecord.Duration = 0; } if (keywords != null) { string[] strs = keywords.Split(','); dtos = dtos.Where(x => strs.All(s => !x.FaultInfo.Contains(s))).ToList(); } //dtos = dtos.Where(x => // !x.FaultInfo.Contains(x.FaultInfo)).ToList(); dtos = CheckSameTimeFault(dtos); dtos = dtos.OrderByDescending(o => o.Duration).Take(10).ToList(); } return dtos; } private List CheckSameTimeFault(List lst) { List faults = new List(); MachineFaultRecordDto lastFault = null; foreach (var l in lst) { if (lastFault != null) { if (l.EndTime != null && (l.StartTime < lastFault.StartTime || l.EndTime > lastFault.EndTime)) { faults.Add(l); lastFault = l; } } else { faults.Add(l); lastFault = l; } } return faults; } public List GetFaultFrequencyTop10(string machineId, DateTime startTime, DateTime endTime, string keywords) { List faultFrequencies = new List(); var machine = _unitOfWork.MachineRepository.FirstOrDefault(x => x.Id == machineId); var faults = _unitOfWork.MachineFaultRecordRepository.GetList( x => x.MachineId == machineId && x.StartTime >= startTime && x.StartTime < endTime && x.MachineState == 2).ToList(); var mfcs = _unitOfWork.MachineFaultComparisonRepository.GetList( x => x.FaultTopic == machine.FaultTopic).ToList(); foreach (var fault in faults) { var f = faultFrequencies.Find(x => x.FaultCode == fault.FaultCode); if (f == null) { FaultFrequency m = new FaultFrequency() { FaultCode = fault.FaultCode.Value, Count = fault.TriggerNumber.Value }; faultFrequencies.Add(m); } else { f.Count += fault.TriggerNumber.Value; } } foreach (var item in faultFrequencies) { var mfc = mfcs.FirstOrDefault(x => x.FaultCode == item.FaultCode); if (mfc == null) { item.FaultInfo = item.FaultCode.ToString() + " " + "未知故障!"; } else { item.FaultInfo = item.FaultCode.ToString() + " " + mfc.FaultInfo; } } if (keywords != null) { string[] strs = keywords.Split(','); faultFrequencies = faultFrequencies.Where(x => strs.All(s => !x.FaultInfo.Contains(s))).ToList(); } return faultFrequencies.OrderByDescending(o => o.Count).Take(10).ToList(); } public List GetFaultFrequencyTop10ByShift(string machineId, DateTime startTime, DateTime endTime, string keywords) { List faultFrequencies = new List(); var machine = _unitOfWork.MachineRepository.FirstOrDefault(x => x.Id == machineId); var faults = _unitOfWork.MachineFaultRecordRepository.GetList( x => x.MachineId == machineId && x.StartTime >= startTime && x.StartTime < endTime && x.MachineState == 2).ToList(); var mfcs = _unitOfWork.MachineFaultComparisonRepository.GetList( x => x.FaultTopic == machine.FaultTopic).ToList(); List shifts = GetShifts(startTime, endTime); foreach (var shift in shifts) { FaultFrequencyByShift faultFrequencyByShift = GetFaultFrequency( faults, mfcs, keywords, shift); faultFrequencies.Add(faultFrequencyByShift); } return faultFrequencies; } private FaultFrequencyByShift GetFaultFrequency(List faults, List mfcs, string keywords, FactoryShift shift) { FaultFrequencyByShift faultFrequencyByShift = new FaultFrequencyByShift() { Shift = $"{shift.Date} {shift.Shifts}" }; var fs = faults.Where(x => x.StartTime >= shift.StartTime && x.StartTime < shift.EndTime); foreach (var f in fs) { var temp = faultFrequencyByShift.FaultFrequencies.Find(x => x.FaultCode == f.FaultCode); if (temp != null) { if (f.TriggerNumber != null) { temp.Count += f.TriggerNumber.Value; } } else { FaultFrequency faultFrequency = new FaultFrequency { FaultCode = f.FaultCode != null ? f.FaultCode.Value : 0, Count = f.TriggerNumber != null ? f.TriggerNumber.Value : 1 }; faultFrequencyByShift.FaultFrequencies.Add(faultFrequency); } } faultFrequencyByShift.FaultFrequencies = faultFrequencyByShift.FaultFrequencies.OrderBy(x => x.Count).ToList(); foreach (var item in faultFrequencyByShift.FaultFrequencies) { var mfc = mfcs.FirstOrDefault(x => x.FaultCode == item.FaultCode); if (mfc != null) item.FaultInfo = mfc.FaultInfo; else item.FaultInfo = "未知故障"; } if (!string.IsNullOrEmpty(keywords)) { string[] strs = keywords.Split(','); faultFrequencyByShift.FaultFrequencies = faultFrequencyByShift.FaultFrequencies.Where(x => strs.All(s => !x.FaultInfo.Contains(s))).ToList(); } // faultFrequencyByShift.FaultFrequencies = faultFrequencyByShift.FaultFrequencies.OrderByDescending(o => o.Count).Take(10).ToList(); return faultFrequencyByShift; } private List GetShifts(DateTime startTime, DateTime endTime) { List shifts = new List(); for (DateTime i = startTime; i <= endTime; i = i.AddDays(1)) { FactoryShift shift = new FactoryShift { Date = i.ToString("yyyy-MM-dd"), Shifts = "白班", StartTime = i, EndTime = i.AddHours(12) }; shifts.Add(shift); FactoryShift shift1 = new FactoryShift { Date = i.ToString("yyyy-MM-dd"), Shifts = "夜班", StartTime = i.AddHours(12), EndTime = i.AddHours(24) }; shifts.Add(shift1); } return shifts; } } }