using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Mvc; using Microsoft.DotNet.PlatformAbstractions; using Microsoft.Extensions.Hosting; using OfficeOpenXml; using OfficeOpenXml.Style; using ProductionLineMonitor.Application.Services.FaultService; using ProductionLineMonitor.Application.Services.LineService; using ProductionLineMonitor.Core.Dtos; using ProductionLineMonitor.Core.IRepositories; using ProductionLineMonitor.Core.Models; using ProductionLineMonitor.EntityFramework; using ProductionLineMonitor.Web.Services; using ProductionLineMonitor.Web.Services.LineService; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Threading; namespace ProductionLineMonitor.Web.Controllers { public class LineController : BaseController { private readonly IUnitOfWork _unitOfWork; private readonly IFaultService _faultService; private readonly ILineService _lineService; private readonly IWebHostEnvironment _webHostEnvironment; public LineController( IUnitOfWork unitOfWork, IFaultService faultService, ILineService lineService, IWebHostEnvironment webHostEnvironment) { _unitOfWork = unitOfWork; _faultService = faultService; _lineService = lineService; _webHostEnvironment = webHostEnvironment; } public IActionResult Index() { return View(); } public IActionResult GetLineTree() { var line = _unitOfWork.ProductionLineRepository.GetList(); return Ok(line); } [HttpGet] public IActionResult GetLineData(string lineId, string date) { if (lineId == null || date == null) return Ok(); var line = _unitOfWork.ProductionLineRepository.GetById(lineId); if (line == null) return Ok(); ProductionLineViewModel vm = new ProductionLineViewModel(line.Floor, line.Line, date, line.HourDataTopic, line.LineName); var recipe = _unitOfWork.RecipeRepository.FirstOrDefault(x => x.ProductionLineId == line.Id && x.ModuleType == vm.ModuleType); if (recipe != null) { if (recipe.TheoryRefueledTime == null) vm.TheoryRefueledTime = 0; else vm.TheoryRefueledTime = recipe.TheoryRefueledTime.Value; } return Ok(vm); } [HttpGet] public IActionResult GetLineAccumulatedFaultByMorning(string lineId, string date) { if (lineId == null || date == null) return Ok(); DateTime startTime = Convert.ToDateTime($"{date} 08:00:00"); DateTime endTime = startTime.AddHours(12); var rev = _faultService.GetAccumulatedFaultTop10ByLine(lineId, startTime, endTime); return Ok(rev); } [HttpGet] public IActionResult GetLineAccumulatedFaultByNight(string lineId, string date) { if (lineId == null || date == null) return Ok(); DateTime startTime = Convert.ToDateTime($"{date} 20:00:00"); DateTime endTime = startTime.AddHours(12); var rev = _faultService.GetAccumulatedFaultTop10ByLine(lineId, startTime, endTime); return Ok(rev); } [HttpGet] public IActionResult GetLineOverview(string date) { return Ok(_lineService.GetLineOverview(date)); } [HttpGet] public IActionResult GetLineOverviewV1(string date) { return Ok(_lineService.GetLineOverviewV1(date)); } [HttpGet] public IActionResult GetLineFault(string lineId, string date, int durationThreshold) { if (lineId == null || date == null) return Ok(); DateTime startTime = Convert.ToDateTime($"{date} 08:00:00"); DateTime endTime = startTime.AddHours(24); var rev = _faultService.GetLineFault(lineId, startTime, endTime, durationThreshold); var faultSplitToHours = _faultService.FaultSplitToHour(rev); faultSplitToHours = _faultService.FilterDuplicateFaults(faultSplitToHours); return Ok(faultSplitToHours); } [HttpGet] public IActionResult GetLineFaultDataV1(string id, string date) { List lineDayFaultRecords = new List(); if (id == null || date == null) return Ok(lineDayFaultRecords); DateTime startTime = Convert.ToDateTime($"{date} 08:00:00"); DateTime endTime = startTime.AddDays(1); var context = _unitOfWork.GetDbContext() as ProductionLineContext; var line = context.ProductionLines.Find(id); if (line == null) return Ok(lineDayFaultRecords); var machines = context.Machines.Where(x => x.ProductionLineId == line.Id && x.IsInclusionLineStatistics == true); var ProductionPlans = MesApiService.GetProductionPlans(line.Floor, line.Line, date); if (ProductionPlans.Count == 0) return Ok(lineDayFaultRecords); if (ProductionPlans.Count == 1) if (ProductionPlans[0].ModuleType == "未指定") return Ok(lineDayFaultRecords); var KeyInInfos = MesApiService.GetKeyInInfos(line.Floor, line.Line, date); foreach (var key in KeyInInfos) { if (key.KeyInType == 2 || key.KeyInType == 3 || key.KeyInType == 4 || key.KeyInType == 6 || key.KeyInType == 7 || key.KeyInType == 9) if (key.AffectTime >= 1440) return Ok(lineDayFaultRecords); } foreach (var machine in machines) { 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) { if (!item.mfc.FaultInfo.Contains("安全门") && !item.mfc.FaultInfo.Contains("门禁") && !item.mfc.FaultInfo.Contains("提示上料") && !item.mfc.FaultInfo.Contains("提示卸料") && item.mfr.EndTime != null) { LineDayFaultRecord lineDayFaultRecord = new LineDayFaultRecord(); lineDayFaultRecord.MechineName = machine.Type; lineDayFaultRecord.FaultCode = item.mfr.FaultCode.ToString(); lineDayFaultRecord.FaultInfo = item.mfc.FaultInfo; lineDayFaultRecord.StartTime = item.mfr.StartTime.Value; lineDayFaultRecord.DataSource = 0; if (item.mfr.EndTime == null || item.mfr.EndTime.Value > endTime) lineDayFaultRecord.EndTime = endTime; else lineDayFaultRecord.EndTime = item.mfr.EndTime.Value; lineDayFaultRecord.Duration = (lineDayFaultRecord.EndTime.Value - lineDayFaultRecord.StartTime).TotalMinutes; lineDayFaultRecords.Add(lineDayFaultRecord); } } lineDayFaultRecords.AddRange(GetKeyInLineDayFaultRecord(line.Floor, line.Line, date, machine.Type, endTime)); switch (machine.Type) { case "AG + FPL": lineDayFaultRecords.AddRange(GetKeyInLineDayFaultRecord(line.Floor, line.Line, date, "TPA", endTime)); lineDayFaultRecords.AddRange(GetKeyInLineDayFaultRecord(line.Floor, line.Line, date, "FPL", endTime)); lineDayFaultRecords.AddRange(GetKeyInLineDayFaultRecord(line.Floor, line.Line, date, "FPL01", endTime)); lineDayFaultRecords.AddRange(GetKeyInLineDayFaultRecord(line.Floor, line.Line, date, "FPL02", endTime)); lineDayFaultRecords.AddRange(GetKeyInLineDayFaultRecord(line.Floor, line.Line, date, "FPL03", endTime)); lineDayFaultRecords.AddRange(GetKeyInLineDayFaultRecord(line.Floor, line.Line, date, "FPL04", endTime)); break; case "FPL": lineDayFaultRecords.AddRange(GetKeyInLineDayFaultRecord(line.Floor, line.Line, date, "FPL01", endTime)); lineDayFaultRecords.AddRange(GetKeyInLineDayFaultRecord(line.Floor, line.Line, date, "FPL02", endTime)); lineDayFaultRecords.AddRange(GetKeyInLineDayFaultRecord(line.Floor, line.Line, date, "FPL03", endTime)); lineDayFaultRecords.AddRange(GetKeyInLineDayFaultRecord(line.Floor, line.Line, date, "FPL04", endTime)); break; case "FOG": //lineDayFaultRecords.AddRange(GetKeyInLineDayFaultRecord(line.Floor, line.Line, date, "COG", endTime)); break; default: break; } } foreach (var key in KeyInInfos) { if (key.KeyInType == 2 || key.KeyInType == 3 || key.KeyInType == 4 || key.KeyInType == 6 || key.KeyInType == 7 || key.KeyInType == 9) lineDayFaultRecords.RemoveAll(x => x.StartTime >= key.StartTime && x.StartTime <= key.EndTime); } List top10 = lineDayFaultRecords.OrderByDescending(o => o.Duration).Take(10).ToList(); return Ok(top10); } private List GetKeyInLineDayFaultRecord(int floor, int line, string date, string type, DateTime endTime) { List lineDayFaultRecords = new List(); var fs = MesApiService.GetKeyInFaults(floor, line, date, type); foreach (var f in fs) { LineDayFaultRecord fLineDayFaultRecord = new LineDayFaultRecord(); fLineDayFaultRecord.MechineName = type; fLineDayFaultRecord.FaultCode = f.FaultCode; fLineDayFaultRecord.FaultInfo = f.FaultInfo; fLineDayFaultRecord.StartTime = f.StartTime; fLineDayFaultRecord.DataSource = 1; if (f.EndTime == null) fLineDayFaultRecord.EndTime = endTime; else fLineDayFaultRecord.EndTime = f.EndTime.Value; fLineDayFaultRecord.Duration = (fLineDayFaultRecord.EndTime.Value - fLineDayFaultRecord.StartTime).TotalMinutes; lineDayFaultRecords.Add(fLineDayFaultRecord); } return lineDayFaultRecords; } [HttpGet] public IActionResult LineRealTimeInfo() { return View(); } [HttpGet] public IActionResult GetKeyInInfos() { DateTime dateTime = DateTime.Now; if (dateTime.Hour < 8) { dateTime = dateTime.AddDays(-1); } string date = dateTime.ToString("yyyy-MM-dd"); List lineKeyInInfos = new List(); var lines = _unitOfWork.ProductionLineRepository.GetList(); foreach (var line in lines) { LineKeyInInfo lineKeyInInfo = new LineKeyInInfo { Id = line.Id, Name = line.Name, Floor = line.Floor, Line = line.Line }; lineKeyInInfos.Add(lineKeyInInfo); } List threads = new List(); foreach (var item in lineKeyInInfos) { Thread thread = new Thread(() => { item.ProductionPlans = MesApiService.GetProductionPlans(item.Floor, item.Line, date); item.KeyInInfos = MesApiService.GetKeyInInfos(item.Floor, item.Line, date); }); thread.Start(); threads.Add(thread); } foreach (var item in threads) { item.Join(); } return Ok(lineKeyInInfos); } [HttpGet] public IActionResult GetKeyFaults(string id, string date) { var lineKeyFaults = new List(); DateTime startTime = Convert.ToDateTime($"{date} 08:00:00"); DateTime endTime = startTime.AddDays(1); var line = _unitOfWork.ProductionLineRepository.GetById(id); var machines = _unitOfWork.MachineRepository.GetList( x => x.ProductionLineId == id && x.IsInclusionLineStatistics == true) .OrderBy(o => o.ProductionLineOrder); foreach (var item in machines) { switch (item.Type) { case "AG + FPL": lineKeyFaults.AddRange(GetKeyInLineDayFaultRecord(line.Floor, line.Line, date, "TPA", endTime)); lineKeyFaults.AddRange(GetKeyInLineDayFaultRecord(line.Floor, line.Line, date, "FPL", endTime)); lineKeyFaults.AddRange(GetKeyInLineDayFaultRecord(line.Floor, line.Line, date, "FPL01", endTime)); lineKeyFaults.AddRange(GetKeyInLineDayFaultRecord(line.Floor, line.Line, date, "FPL02", endTime)); lineKeyFaults.AddRange(GetKeyInLineDayFaultRecord(line.Floor, line.Line, date, "FPL03", endTime)); lineKeyFaults.AddRange(GetKeyInLineDayFaultRecord(line.Floor, line.Line, date, "FPL04", endTime)); break; case "FPL": lineKeyFaults.AddRange(GetKeyInLineDayFaultRecord(line.Floor, line.Line, date, "FPL", endTime)); lineKeyFaults.AddRange(GetKeyInLineDayFaultRecord(line.Floor, line.Line, date, "FPL01", endTime)); lineKeyFaults.AddRange(GetKeyInLineDayFaultRecord(line.Floor, line.Line, date, "FPL02", endTime)); lineKeyFaults.AddRange(GetKeyInLineDayFaultRecord(line.Floor, line.Line, date, "FPL03", endTime)); lineKeyFaults.AddRange(GetKeyInLineDayFaultRecord(line.Floor, line.Line, date, "FPL04", endTime)); break; case "FOG": //lineKeyFaults.AddRange(GetKeyInLineDayFaultRecord(line.Floor, line.Line, date, "COG", endTime)); lineKeyFaults.AddRange(GetKeyInLineDayFaultRecord(line.Floor, line.Line, date, item.Type, endTime)); break; default: lineKeyFaults.AddRange(GetKeyInLineDayFaultRecord(line.Floor, line.Line, date, item.Type, endTime)); break; } } return Ok(lineKeyFaults); } [HttpGet] public IActionResult GetMonthData(string lineId, string date, string moduleType) { return Ok(_lineService.GetLineMonthData(lineId, date, moduleType)); } [HttpGet] public IActionResult ExportineAccumulatedFaultByMorning(string lineId, string date) { if (lineId == null || date == null) return Ok(); var line = _unitOfWork.ProductionLineRepository.GetById(lineId); DateTime startTime = Convert.ToDateTime($"{date} 08:00:00"); DateTime endTime = startTime.AddHours(12); var rev = _faultService.GetAccumulatedFaultTopByLine(lineId, startTime, endTime); string sWebRootFolder = _webHostEnvironment.WebRootPath; string sFileName = $@"{line.Name} {date} 早班故障详情.xlsx"; var path = Path.Combine(sWebRootFolder, sFileName); FileInfo file = new FileInfo(path); if (file.Exists) { file.Delete(); file = new FileInfo(path); } //ExcelPackage.LicenseContext = LicenseContext.NonCommercial; using (ExcelPackage package = new ExcelPackage(file)) { ExcelWorksheet worksheet = package.Workbook.Worksheets.Add("故障详情"); worksheet.Cells.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center; worksheet.Cells[1, 1].Value = "NO"; worksheet.Cells[1, 2].Value = "设备"; worksheet.Cells[1, 3].Value = "数据来源"; worksheet.Cells[1, 4].Value = "故障详情"; worksheet.Cells[1, 5].Value = "累计次数"; worksheet.Cells[1, 6].Value = "累计时间(min)"; worksheet.Column(1).Width = 5; worksheet.Column(2).Width = 12; worksheet.Column(3).Width = 12; worksheet.Column(4).Width = 25; worksheet.Column(5).Width = 12; worksheet.Column(6).Width = 20; for (int i = 0; i < rev.Count(); i++) { worksheet.Cells[i + 2, 1].Value = i + 1; worksheet.Cells[i + 2, 2].Value = rev[i].MachineType; worksheet.Cells[i + 2, 3].Value = rev[i].DataSource == 1 ? "人工录入" : "自动上抛"; worksheet.Cells[i + 2, 4].Value = rev[i].FaultInfo; worksheet.Cells[i + 2, 5].Value = rev[i].AccumulativeTotal; worksheet.Cells[i + 2, 6].Value = rev[i].AccumulativeTime; } package.Save(); } var fileStream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); return File(fileStream, "application/octet-stream", sFileName); } [HttpGet] public IActionResult ExportineAccumulatedFaultByNight(string lineId, string date) { if (lineId == null || date == null) return Ok(); var line = _unitOfWork.ProductionLineRepository.GetById(lineId); DateTime startTime = Convert.ToDateTime($"{date} 20:00:00"); DateTime endTime = startTime.AddHours(12); var rev = _faultService.GetAccumulatedFaultTopByLine(lineId, startTime, endTime); string sWebRootFolder = _webHostEnvironment.WebRootPath; string sFileName = $@"{line.Name} {date} 早班故障详情.xlsx"; var path = Path.Combine(sWebRootFolder, sFileName); FileInfo file = new FileInfo(path); if (file.Exists) { file.Delete(); file = new FileInfo(path); } //ExcelPackage.LicenseContext = LicenseContext.NonCommercial; using (ExcelPackage package = new ExcelPackage(file)) { ExcelWorksheet worksheet = package.Workbook.Worksheets.Add("故障详情"); worksheet.Cells.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center; worksheet.Cells[1, 1].Value = "NO"; worksheet.Cells[1, 2].Value = "设备"; worksheet.Cells[1, 3].Value = "数据来源"; worksheet.Cells[1, 4].Value = "故障详情"; worksheet.Cells[1, 5].Value = "累计次数"; worksheet.Cells[1, 6].Value = "累计时间(min)"; worksheet.Column(1).Width = 5; worksheet.Column(2).Width = 12; worksheet.Column(3).Width = 12; worksheet.Column(4).Width = 25; worksheet.Column(5).Width = 12; worksheet.Column(6).Width = 20; for (int i = 0; i < rev.Count(); i++) { worksheet.Cells[i + 2, 1].Value = i + 1; worksheet.Cells[i + 2, 2].Value = rev[i].MachineType; worksheet.Cells[i + 2, 3].Value = rev[i].DataSource == 1 ? "人工录入" : "自动上抛"; worksheet.Cells[i + 2, 4].Value = rev[i].FaultInfo; worksheet.Cells[i + 2, 5].Value = rev[i].AccumulativeTotal; worksheet.Cells[i + 2, 6].Value = rev[i].AccumulativeTime; } package.Save(); } var fileStream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); return File(fileStream, "application/octet-stream", sFileName); } } }