using AutoMapper; using Microsoft.AspNetCore.Mvc; using ProductionLineMonitor.Application.Services.FaultService; using ProductionLineMonitor.Core.Dtos; using ProductionLineMonitor.Core.IRepositories; using ProductionLineMonitor.Core.Models; using ProductionLineMonitor.Core.Utils; using ProductionLineMonitor.EntityFramework; using System; using System.Collections.Generic; using System.Linq; namespace ProductionLineMonitor.Web.Controllers { public class FaultController : BaseController { private readonly IFaultService _faultService; private readonly IUnitOfWork _unitOfWork; private readonly IMapper _mapper; public FaultController(IFaultService faultService, IUnitOfWork unitOfWork, IMapper mapper) { _faultService = faultService; _unitOfWork = unitOfWork; _mapper = mapper; } [HttpGet] public IActionResult Index() { return View(); } [HttpGet] public IActionResult GetFaults(int pageNumber, int pageSize, string keyword) { pageNumber = pageNumber == 0 ? 1 : pageNumber; pageSize = pageSize == 0 ? 10 : pageSize; keyword ??= ""; //return Ok(_faultService.GetMachineFaultRecords(pageNumber, pageSize, keyword)); return Ok(); } [HttpGet] public IActionResult GetTOP10Faults(string id, DateTime startTime, DateTime endTime, string filterString = null) { startTime = startTime.AddHours(8); endTime = endTime.AddHours(32); List machineFaultRecordDtos = new List(); var machine = _unitOfWork.MachineRepository.GetById(id); if (machine == null) { return Ok(machineFaultRecordDtos); } var faults = _unitOfWork.MachineFaultRecordRepository.GetList( x => x.MachineId == machine.Id && 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 = machineFaultRecordDtos.Find(x => x.FaultCode == fault.FaultCode); if (f == null) { MachineFaultRecordDtoV1 m = new MachineFaultRecordDtoV1() { FaultCode = fault.FaultCode, }; if (fault.StartTime.Value.Hour >= 8 && fault.StartTime.Value.Hour < 20) { m.ShiftCount += fault.TriggerNumber.Value; } else { m.NightShiftCount += fault.TriggerNumber.Value; } machineFaultRecordDtos.Add(m); } else { if (fault.StartTime.Value.Hour >= 8 && fault.StartTime.Value.Hour < 20) { f.ShiftCount += fault.TriggerNumber.Value; } else { f.NightShiftCount += fault.TriggerNumber.Value; } } } foreach (var item in machineFaultRecordDtos) { 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 (filterString != null) { string[] strs = filterString.Split(','); machineFaultRecordDtos = machineFaultRecordDtos.Where(x => strs.All(s => !x.FaultInfo.Contains(s))).ToList(); } machineFaultRecordDtos = machineFaultRecordDtos.OrderByDescending(o => o.Count).Take(10).ToList(); return Ok(machineFaultRecordDtos); } [HttpGet] public IActionResult GetFaultTrend(string id, string faultInfo, DateTime startTime, DateTime endTime) { ECharts dto = new ECharts(); startTime = startTime.AddHours(8).AddDays(-7); endTime = endTime.AddHours(32).AddDays(+2); if (endTime > DateTime.Now) endTime = Convert.ToDateTime($"{DateTime.Now.AddDays(+1):yyyy-MM-dd} 08:00:00"); int index = faultInfo.IndexOf(' '); if (index == -1) return Ok(dto); string str = faultInfo[..index]; int faultCode = Convert.ToInt32(str); var machine = _unitOfWork.MachineRepository.GetById(id); if (machine == null) return Ok(dto); var faults = _unitOfWork.MachineFaultRecordRepository .GetList( x => x.MachineId == id && x.FaultCode == faultCode && x.StartTime >= startTime && x.StartTime < endTime && x.MachineState == 2) .OrderBy(o => o.StartTime).ToList(); var machineFaultComparison = _unitOfWork.MachineFaultComparisonRepository .FirstOrDefault( x => x.FaultTopic == machine.FaultTopic && x.FaultCode == faultCode); Serie serie = new Serie(); if (machineFaultComparison != null) serie.Name = $"{faultCode} {machineFaultComparison.FaultInfo}"; else serie.Name = $"{faultCode} 未知故障!"; var dates = DateTimeHelper.GetDateTime(startTime, endTime); foreach (var item in dates) { //DateTime sTime = Convert.ToDateTime($"{item:yyyy-MM-dd} 08:00:00"); //DateTime eTime = Convert.ToDateTime($"{item.AddDays(1):yyyy-MM-dd} 08:00:00"); //var fs = faults.Where(x => x.StartTime >= sTime && x.StartTime < eTime); //int count = 0; //if (fs != null) // foreach (var f in fs) // count += f.TriggerNumber.Value; //object[] o = new object[2]; //o[0] = item.ToString("yyyy-MM-dd"); //o[1] = count; //serie.Data.Add(o); DateTime sTime1 = Convert.ToDateTime($"{item:yyyy-MM-dd} 08:00:00"); DateTime eTime1 = Convert.ToDateTime($"{item:yyyy-MM-dd} 20:00:00"); var fs1 = faults.Where(x => x.StartTime >= sTime1 && x.StartTime < eTime1); int count1 = 0; if (fs1 != null) foreach (var f in fs1) count1 += f.TriggerNumber.Value; object[] o1 = new object[2]; o1[0] = item.ToString("MM/dd\n白班"); o1[1] = count1; serie.Data.Add(o1); DateTime sTime2 = Convert.ToDateTime($"{item:yyyy-MM-dd} 20:00:00"); DateTime eTime2 = Convert.ToDateTime($"{item.AddDays(1):yyyy-MM-dd} 08:00:00"); var fs2 = faults.Where(x => x.StartTime >= sTime2 && x.StartTime < eTime2); int count2 = 0; if (fs2 != null) foreach (var f in fs2) count2 += f.TriggerNumber.Value; object[] o2 = new object[2]; o2[0] = item.ToString("MM/dd\n夜班"); o2[1] = count2; serie.Data.Add(o2); } dto.Series.Add(serie); return Ok(dto); } [HttpGet] public IActionResult GetFaultTrendV1(string id, string faultInfo, DateTime day) { ECharts dto = new ECharts(); int index = faultInfo.IndexOf(' '); if (index == -1) return Ok(dto); string str = faultInfo[..index]; int faultCode = Convert.ToInt32(str); DateTime startTime = Convert.ToDateTime($"{day.AddDays(-10):yyyy-MM-dd} 08:00:00"); DateTime endTime = Convert.ToDateTime($"{day.AddDays(+10):yyyy-MM-dd} 08:00:00"); if (endTime > DateTime.Now) endTime = Convert.ToDateTime($"{DateTime.Now.AddDays(+1):yyyy-MM-dd} 08:00:00"); var machine = _unitOfWork.MachineRepository.GetById(id); if (machine == null) return Ok(dto); var faults = _unitOfWork.MachineFaultRecordRepository .GetList( x => x.MachineId == id && x.FaultCode == faultCode && x.StartTime >= startTime && x.StartTime < endTime) .OrderBy(o => o.StartTime); var machineFaultComparison = _unitOfWork.MachineFaultComparisonRepository .FirstOrDefault( x => x.FaultTopic == machine.FaultTopic && x.FaultCode == faultCode); Serie serie = new Serie(); if (machineFaultComparison != null) serie.Name = $"{faultCode} {machineFaultComparison.FaultInfo}"; else serie.Name = $"{faultCode} 未知故障!"; var dates = DateTimeHelper.GetDateTime(startTime, endTime); foreach (var item in dates) { DateTime sTime = Convert.ToDateTime($"{item:yyyy-MM-dd} 08:00:00"); DateTime eTime = Convert.ToDateTime($"{item.AddDays(1):yyyy-MM-dd} 08:00:00"); var fs = faults.Where(x => x.StartTime >= sTime && x.StartTime < eTime).ToList(); int count = 0; if (fs != null) foreach (var f in fs) count += f.TriggerNumber.Value; object[] o = new object[2]; o[0] = item.ToString("yyyy-MM-dd"); o[1] = count; serie.Data.Add(o); } dto.Series.Add(serie); return Ok(dto); } [HttpGet] public IActionResult GetLineDayTop10Faults(string lineId, DateTime day, string keywords) { List dtos = new List(); var line = _unitOfWork.ProductionLineRepository.GetById(lineId); if (line != null) { var machines = _unitOfWork.MachineRepository.GetList( x => x.ProductionLineId == line.Id && x.IsInclusionLineStatistics == true); foreach (var item in machines) { dtos.AddRange(GetMachineFaultsByTop10(item.Id, day, keywords)); } } return Ok(dtos.OrderByDescending(o => o.Duration).Take(10).ToList()); } [HttpGet] public IActionResult GetMachineDayTop10Faults(string machineId, DateTime day, string keywords) { List dtos = GetMachineFaultsByTop10(machineId, day, keywords); return Ok(dtos); } private List GetMachineFaultsByTop10(string machineId, DateTime day, string keywords) { return _faultService.GetMachineFaultsByTop10(machineId, day, keywords); } public IActionResult ExceptExcel(DateTime startTime, DateTime endTime) { IEnumerable lst = new List(); return Ok(); } public IActionResult GetFaults(DateTime startTime, DateTime endTime, string machineId) { var context = _unitOfWork.GetDbContext() as ProductionLineContext; var machine = context.Machines.Find(machineId); var query = from mfr in context.Set() join mfc in context.Set() on mfr.FaultCode.Value equals mfc.FaultCode.Value where mfr.MachineId == machine.Id && mfc.FaultTopic == machine.FaultTopic && mfr.StartTime.Value >= startTime && mfr.StartTime.Value < endTime select new { mfr, mfc }; foreach (var item in query) { } return Ok(); } public IActionResult ExportFaults(DateTime startTime, DateTime endTime) { var stream = System.IO.File.OpenRead("");//excel表转换成流 return File(stream, "application/vnd.android.package-archive", ""); } [HttpGet] public IActionResult GetFault(string id, string faultInfo, DateTime startTime, DateTime endTime) { startTime = startTime.AddHours(8); endTime = endTime.AddHours(32); List machineFaultRecordDtos = new List(); var machine = _unitOfWork.MachineRepository.GetById(id); if (machine == null) { return Ok(machineFaultRecordDtos); } var faults = _unitOfWork.MachineFaultRecordRepository.GetList( x => x.MachineId == machine.Id && x.StartTime >= startTime && x.StartTime < endTime && x.MachineState == 2).ToList(); var mfcs = _unitOfWork.MachineFaultComparisonRepository.GetList( x => x.FaultTopic == machine.FaultTopic).ToList(); return Ok(); } } }