123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368 |
- 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<MachineFaultRecordDtoV1> machineFaultRecordDtos = new List<MachineFaultRecordDtoV1>();
- 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<MachineFaultRecordDto> dtos = new List<MachineFaultRecordDto>();
- 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<MachineFaultRecordDto> dtos = GetMachineFaultsByTop10(machineId, day, keywords);
- return Ok(dtos);
- }
- private List<MachineFaultRecordDto> GetMachineFaultsByTop10(string machineId, DateTime day, string keywords)
- {
- return _faultService.GetMachineFaultsByTop10(machineId, day, keywords);
- }
- public IActionResult ExceptExcel(DateTime startTime, DateTime endTime)
- {
- IEnumerable<MachineFaultRecordDto> lst = new List<MachineFaultRecordDto>();
- 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<MachineFaultRecord>()
- join mfc in context.Set<MachineFaultComparison>()
- 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<MachineFaultRecordDtoV1> machineFaultRecordDtos = new List<MachineFaultRecordDtoV1>();
- 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();
- }
- }
- }
|