123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477 |
- 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<LineDayFaultRecord> lineDayFaultRecords = new List<LineDayFaultRecord>();
- 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<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)
- {
- 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<LineDayFaultRecord> top10 = lineDayFaultRecords.OrderByDescending(o => o.Duration).Take(10).ToList();
- return Ok(top10);
- }
- private List<LineDayFaultRecord> GetKeyInLineDayFaultRecord(int floor, int line, string date, string type, DateTime endTime)
- {
- List<LineDayFaultRecord> lineDayFaultRecords = new List<LineDayFaultRecord>();
- 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<LineKeyInInfo> lineKeyInInfos = new List<LineKeyInInfo>();
-
- 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<Thread> threads = new List<Thread>();
- 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<LineDayFaultRecord>();
- 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);
- }
- }
- }
|