123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805 |
- 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<MachineFaultComparison> 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<LineFaultDto> GetLineFault(string lineId, DateTime startTime, DateTime endTime, int durationThreshold)
- {
- List<LineFaultDto> dtos = new List<LineFaultDto>();
- 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<MachineFaultRecord>()
- join mfc in context.Set<MachineFaultComparison>()
- 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<LineFaultDto> keyInFaults = new List<LineFaultDto>();
- //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<LineAccumulatedFaultDto> GetAccumulatedFaultTop10ByLine(string lineId, DateTime startTime, DateTime endTime)
- {
- List<LineAccumulatedFaultDto> dtos = new List<LineAccumulatedFaultDto>();
- 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<MachineFaultRecord>()
- join mfc in context.Set<MachineFaultComparison>()
- 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<LineFaultDto> keyInFaults = new List<LineFaultDto>();
- //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<LineFaultDto> GetKeyInLineFault(int floor, int line, string date, string type, DateTime endTime, string machineType)
- {
- List<LineFaultDto> dtos = new List<LineFaultDto>();
- 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<LineFaultDto> FaultSplitToHour(IEnumerable<LineFaultDto> lineFaultDtos)
- {
- IList<LineFaultDto> dtos = new List<LineFaultDto>();
- foreach (var lineFaultDto in lineFaultDtos)
- {
- if (lineFaultDto.EndTime == null)
- continue;
- IList<DateTime> dateTimes = new List<DateTime>
- {
- 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<LineFaultDto> FilterDuplicateFaults(IEnumerable<LineFaultDto> lineFaultDtos)
- {
- IList<LineFaultDto> dtos = new List<LineFaultDto>();
- 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<LineAccumulatedFaultDto> GetAccumulatedFaultTopByLine(string lineId, DateTime startTime, DateTime endTime)
- {
- List<LineAccumulatedFaultDto> dtos = new List<LineAccumulatedFaultDto>();
- 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<MachineFaultRecord>()
- join mfc in context.Set<MachineFaultComparison>()
- 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<LineFaultDto> keyInFaults = new List<LineFaultDto>();
- //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<MachineFaultRecordDto> GetMachineFaultsByTop10(string machineId, DateTime day, string keywords)
- {
- List<MachineFaultRecordDto> dtos = new List<MachineFaultRecordDto>();
- 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<List<MachineFaultRecordDto>>(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<MachineFaultRecordDto> CheckSameTimeFault(List<MachineFaultRecordDto> lst)
- {
- List<MachineFaultRecordDto> faults = new List<MachineFaultRecordDto>();
- 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<FaultFrequency> GetFaultFrequencyTop10(string machineId, DateTime startTime, DateTime endTime, string keywords)
- {
- List<FaultFrequency> faultFrequencies = new List<FaultFrequency>();
- 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<FaultFrequencyByShift> GetFaultFrequencyTop10ByShift(string machineId, DateTime startTime, DateTime endTime, string keywords)
- {
- List<FaultFrequencyByShift> faultFrequencies = new List<FaultFrequencyByShift>();
- 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<FactoryShift> 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<MachineFaultRecord> faults, List<MachineFaultComparison> 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<FactoryShift> GetShifts(DateTime startTime, DateTime endTime)
- {
- List<FactoryShift> shifts = new List<FactoryShift>();
- 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;
- }
- }
- }
|