LineController.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  1. using Microsoft.AspNetCore.Hosting;
  2. using Microsoft.AspNetCore.Mvc;
  3. using Microsoft.DotNet.PlatformAbstractions;
  4. using Microsoft.Extensions.Hosting;
  5. using OfficeOpenXml;
  6. using OfficeOpenXml.Style;
  7. using ProductionLineMonitor.Application.Services.FaultService;
  8. using ProductionLineMonitor.Application.Services.LineService;
  9. using ProductionLineMonitor.Core.Dtos;
  10. using ProductionLineMonitor.Core.IRepositories;
  11. using ProductionLineMonitor.Core.Models;
  12. using ProductionLineMonitor.EntityFramework;
  13. using ProductionLineMonitor.Web.Services;
  14. using ProductionLineMonitor.Web.Services.LineService;
  15. using System;
  16. using System.Collections.Generic;
  17. using System.IO;
  18. using System.Linq;
  19. using System.Threading;
  20. namespace ProductionLineMonitor.Web.Controllers
  21. {
  22. public class LineController : BaseController
  23. {
  24. private readonly IUnitOfWork _unitOfWork;
  25. private readonly IFaultService _faultService;
  26. private readonly ILineService _lineService;
  27. private readonly IWebHostEnvironment _webHostEnvironment;
  28. public LineController(
  29. IUnitOfWork unitOfWork,
  30. IFaultService faultService,
  31. ILineService lineService,
  32. IWebHostEnvironment webHostEnvironment)
  33. {
  34. _unitOfWork = unitOfWork;
  35. _faultService = faultService;
  36. _lineService = lineService;
  37. _webHostEnvironment = webHostEnvironment;
  38. }
  39. public IActionResult Index()
  40. {
  41. return View();
  42. }
  43. public IActionResult GetLineTree()
  44. {
  45. var line = _unitOfWork.ProductionLineRepository.GetList();
  46. return Ok(line);
  47. }
  48. [HttpGet]
  49. public IActionResult GetLineData(string lineId, string date)
  50. {
  51. if (lineId == null || date == null)
  52. return Ok();
  53. var line = _unitOfWork.ProductionLineRepository.GetById(lineId);
  54. if (line == null)
  55. return Ok();
  56. ProductionLineViewModel vm = new ProductionLineViewModel(line.Floor, line.Line, date, line.HourDataTopic, line.LineName);
  57. var recipe = _unitOfWork.RecipeRepository.FirstOrDefault(x =>
  58. x.ProductionLineId == line.Id && x.ModuleType == vm.ModuleType);
  59. if (recipe != null)
  60. {
  61. if (recipe.TheoryRefueledTime == null)
  62. vm.TheoryRefueledTime = 0;
  63. else
  64. vm.TheoryRefueledTime = recipe.TheoryRefueledTime.Value;
  65. }
  66. return Ok(vm);
  67. }
  68. [HttpGet]
  69. public IActionResult GetLineAccumulatedFaultByMorning(string lineId, string date)
  70. {
  71. if (lineId == null || date == null)
  72. return Ok();
  73. DateTime startTime = Convert.ToDateTime($"{date} 08:00:00");
  74. DateTime endTime = startTime.AddHours(12);
  75. var rev = _faultService.GetAccumulatedFaultTop10ByLine(lineId, startTime, endTime);
  76. return Ok(rev);
  77. }
  78. [HttpGet]
  79. public IActionResult GetLineAccumulatedFaultByNight(string lineId, string date)
  80. {
  81. if (lineId == null || date == null)
  82. return Ok();
  83. DateTime startTime = Convert.ToDateTime($"{date} 20:00:00");
  84. DateTime endTime = startTime.AddHours(12);
  85. var rev = _faultService.GetAccumulatedFaultTop10ByLine(lineId, startTime, endTime);
  86. return Ok(rev);
  87. }
  88. [HttpGet]
  89. public IActionResult GetLineOverview(string date)
  90. {
  91. return Ok(_lineService.GetLineOverview(date));
  92. }
  93. [HttpGet]
  94. public IActionResult GetLineOverviewV1(string date)
  95. {
  96. return Ok(_lineService.GetLineOverviewV1(date));
  97. }
  98. [HttpGet]
  99. public IActionResult GetLineFault(string lineId, string date, int durationThreshold)
  100. {
  101. if (lineId == null || date == null)
  102. return Ok();
  103. DateTime startTime = Convert.ToDateTime($"{date} 08:00:00");
  104. DateTime endTime = startTime.AddHours(24);
  105. var rev = _faultService.GetLineFault(lineId, startTime, endTime, durationThreshold);
  106. var faultSplitToHours = _faultService.FaultSplitToHour(rev);
  107. faultSplitToHours = _faultService.FilterDuplicateFaults(faultSplitToHours);
  108. return Ok(faultSplitToHours);
  109. }
  110. [HttpGet]
  111. public IActionResult GetLineFaultDataV1(string id, string date)
  112. {
  113. List<LineDayFaultRecord> lineDayFaultRecords = new List<LineDayFaultRecord>();
  114. if (id == null || date == null)
  115. return Ok(lineDayFaultRecords);
  116. DateTime startTime = Convert.ToDateTime($"{date} 08:00:00");
  117. DateTime endTime = startTime.AddDays(1);
  118. var context = _unitOfWork.GetDbContext() as ProductionLineContext;
  119. var line = context.ProductionLines.Find(id);
  120. if (line == null)
  121. return Ok(lineDayFaultRecords);
  122. var machines = context.Machines.Where(x => x.ProductionLineId == line.Id && x.IsInclusionLineStatistics == true);
  123. var ProductionPlans = MesApiService.GetProductionPlans(line.Floor, line.Line, date);
  124. if (ProductionPlans.Count == 0)
  125. return Ok(lineDayFaultRecords);
  126. if (ProductionPlans.Count == 1)
  127. if (ProductionPlans[0].ModuleType == "未指定")
  128. return Ok(lineDayFaultRecords);
  129. var KeyInInfos = MesApiService.GetKeyInInfos(line.Floor, line.Line, date);
  130. foreach (var key in KeyInInfos)
  131. {
  132. if (key.KeyInType == 2 || key.KeyInType == 3 || key.KeyInType == 4 ||
  133. key.KeyInType == 6 || key.KeyInType == 7 || key.KeyInType == 9)
  134. if (key.AffectTime >= 1440)
  135. return Ok(lineDayFaultRecords);
  136. }
  137. foreach (var machine in machines)
  138. {
  139. var query = from mfr in context.Set<MachineFaultRecord>()
  140. join mfc in context.Set<MachineFaultComparison>()
  141. on mfr.FaultCode.Value equals mfc.FaultCode.Value
  142. where
  143. mfr.MachineId == machine.Id &&
  144. mfc.FaultTopic == machine.FaultTopic &&
  145. mfr.StartTime.Value >= startTime &&
  146. mfr.StartTime.Value < endTime
  147. select new { mfr, mfc };
  148. foreach (var item in query)
  149. {
  150. if (!item.mfc.FaultInfo.Contains("安全门")
  151. && !item.mfc.FaultInfo.Contains("门禁")
  152. && !item.mfc.FaultInfo.Contains("提示上料")
  153. && !item.mfc.FaultInfo.Contains("提示卸料")
  154. && item.mfr.EndTime != null)
  155. {
  156. LineDayFaultRecord lineDayFaultRecord = new LineDayFaultRecord();
  157. lineDayFaultRecord.MechineName = machine.Type;
  158. lineDayFaultRecord.FaultCode = item.mfr.FaultCode.ToString();
  159. lineDayFaultRecord.FaultInfo = item.mfc.FaultInfo;
  160. lineDayFaultRecord.StartTime = item.mfr.StartTime.Value;
  161. lineDayFaultRecord.DataSource = 0;
  162. if (item.mfr.EndTime == null || item.mfr.EndTime.Value > endTime)
  163. lineDayFaultRecord.EndTime = endTime;
  164. else
  165. lineDayFaultRecord.EndTime = item.mfr.EndTime.Value;
  166. lineDayFaultRecord.Duration = (lineDayFaultRecord.EndTime.Value - lineDayFaultRecord.StartTime).TotalMinutes;
  167. lineDayFaultRecords.Add(lineDayFaultRecord);
  168. }
  169. }
  170. lineDayFaultRecords.AddRange(GetKeyInLineDayFaultRecord(line.Floor, line.Line, date, machine.Type, endTime));
  171. switch (machine.Type)
  172. {
  173. case "AG + FPL":
  174. lineDayFaultRecords.AddRange(GetKeyInLineDayFaultRecord(line.Floor, line.Line, date, "TPA", endTime));
  175. lineDayFaultRecords.AddRange(GetKeyInLineDayFaultRecord(line.Floor, line.Line, date, "FPL", endTime));
  176. lineDayFaultRecords.AddRange(GetKeyInLineDayFaultRecord(line.Floor, line.Line, date, "FPL01", endTime));
  177. lineDayFaultRecords.AddRange(GetKeyInLineDayFaultRecord(line.Floor, line.Line, date, "FPL02", endTime));
  178. lineDayFaultRecords.AddRange(GetKeyInLineDayFaultRecord(line.Floor, line.Line, date, "FPL03", endTime));
  179. lineDayFaultRecords.AddRange(GetKeyInLineDayFaultRecord(line.Floor, line.Line, date, "FPL04", endTime));
  180. break;
  181. case "FPL":
  182. lineDayFaultRecords.AddRange(GetKeyInLineDayFaultRecord(line.Floor, line.Line, date, "FPL01", endTime));
  183. lineDayFaultRecords.AddRange(GetKeyInLineDayFaultRecord(line.Floor, line.Line, date, "FPL02", endTime));
  184. lineDayFaultRecords.AddRange(GetKeyInLineDayFaultRecord(line.Floor, line.Line, date, "FPL03", endTime));
  185. lineDayFaultRecords.AddRange(GetKeyInLineDayFaultRecord(line.Floor, line.Line, date, "FPL04", endTime));
  186. break;
  187. case "FOG":
  188. //lineDayFaultRecords.AddRange(GetKeyInLineDayFaultRecord(line.Floor, line.Line, date, "COG", endTime));
  189. break;
  190. default:
  191. break;
  192. }
  193. }
  194. foreach (var key in KeyInInfos)
  195. {
  196. if (key.KeyInType == 2 || key.KeyInType == 3 || key.KeyInType == 4 ||
  197. key.KeyInType == 6 || key.KeyInType == 7 || key.KeyInType == 9)
  198. lineDayFaultRecords.RemoveAll(x => x.StartTime >= key.StartTime && x.StartTime <= key.EndTime);
  199. }
  200. List<LineDayFaultRecord> top10 = lineDayFaultRecords.OrderByDescending(o => o.Duration).Take(10).ToList();
  201. return Ok(top10);
  202. }
  203. private List<LineDayFaultRecord> GetKeyInLineDayFaultRecord(int floor, int line, string date, string type, DateTime endTime)
  204. {
  205. List<LineDayFaultRecord> lineDayFaultRecords = new List<LineDayFaultRecord>();
  206. var fs = MesApiService.GetKeyInFaults(floor, line, date, type);
  207. foreach (var f in fs)
  208. {
  209. LineDayFaultRecord fLineDayFaultRecord = new LineDayFaultRecord();
  210. fLineDayFaultRecord.MechineName = type;
  211. fLineDayFaultRecord.FaultCode = f.FaultCode;
  212. fLineDayFaultRecord.FaultInfo = f.FaultInfo;
  213. fLineDayFaultRecord.StartTime = f.StartTime;
  214. fLineDayFaultRecord.DataSource = 1;
  215. if (f.EndTime == null)
  216. fLineDayFaultRecord.EndTime = endTime;
  217. else
  218. fLineDayFaultRecord.EndTime = f.EndTime.Value;
  219. fLineDayFaultRecord.Duration = (fLineDayFaultRecord.EndTime.Value - fLineDayFaultRecord.StartTime).TotalMinutes;
  220. lineDayFaultRecords.Add(fLineDayFaultRecord);
  221. }
  222. return lineDayFaultRecords;
  223. }
  224. [HttpGet]
  225. public IActionResult LineRealTimeInfo()
  226. {
  227. return View();
  228. }
  229. [HttpGet]
  230. public IActionResult GetKeyInInfos()
  231. {
  232. DateTime dateTime = DateTime.Now;
  233. if (dateTime.Hour < 8)
  234. {
  235. dateTime = dateTime.AddDays(-1);
  236. }
  237. string date = dateTime.ToString("yyyy-MM-dd");
  238. List<LineKeyInInfo> lineKeyInInfos = new List<LineKeyInInfo>();
  239. var lines = _unitOfWork.ProductionLineRepository.GetList();
  240. foreach (var line in lines)
  241. {
  242. LineKeyInInfo lineKeyInInfo = new LineKeyInInfo
  243. {
  244. Id = line.Id,
  245. Name = line.Name,
  246. Floor = line.Floor,
  247. Line = line.Line
  248. };
  249. lineKeyInInfos.Add(lineKeyInInfo);
  250. }
  251. List<Thread> threads = new List<Thread>();
  252. foreach (var item in lineKeyInInfos)
  253. {
  254. Thread thread = new Thread(() =>
  255. {
  256. item.ProductionPlans = MesApiService.GetProductionPlans(item.Floor, item.Line, date);
  257. item.KeyInInfos = MesApiService.GetKeyInInfos(item.Floor, item.Line, date);
  258. });
  259. thread.Start();
  260. threads.Add(thread);
  261. }
  262. foreach (var item in threads)
  263. {
  264. item.Join();
  265. }
  266. return Ok(lineKeyInInfos);
  267. }
  268. [HttpGet]
  269. public IActionResult GetKeyFaults(string id, string date)
  270. {
  271. var lineKeyFaults = new List<LineDayFaultRecord>();
  272. DateTime startTime = Convert.ToDateTime($"{date} 08:00:00");
  273. DateTime endTime = startTime.AddDays(1);
  274. var line = _unitOfWork.ProductionLineRepository.GetById(id);
  275. var machines = _unitOfWork.MachineRepository.GetList(
  276. x => x.ProductionLineId == id && x.IsInclusionLineStatistics == true)
  277. .OrderBy(o => o.ProductionLineOrder);
  278. foreach (var item in machines)
  279. {
  280. switch (item.Type)
  281. {
  282. case "AG + FPL":
  283. lineKeyFaults.AddRange(GetKeyInLineDayFaultRecord(line.Floor, line.Line, date, "TPA", endTime));
  284. lineKeyFaults.AddRange(GetKeyInLineDayFaultRecord(line.Floor, line.Line, date, "FPL", endTime));
  285. lineKeyFaults.AddRange(GetKeyInLineDayFaultRecord(line.Floor, line.Line, date, "FPL01", endTime));
  286. lineKeyFaults.AddRange(GetKeyInLineDayFaultRecord(line.Floor, line.Line, date, "FPL02", endTime));
  287. lineKeyFaults.AddRange(GetKeyInLineDayFaultRecord(line.Floor, line.Line, date, "FPL03", endTime));
  288. lineKeyFaults.AddRange(GetKeyInLineDayFaultRecord(line.Floor, line.Line, date, "FPL04", endTime));
  289. break;
  290. case "FPL":
  291. lineKeyFaults.AddRange(GetKeyInLineDayFaultRecord(line.Floor, line.Line, date, "FPL", endTime));
  292. lineKeyFaults.AddRange(GetKeyInLineDayFaultRecord(line.Floor, line.Line, date, "FPL01", endTime));
  293. lineKeyFaults.AddRange(GetKeyInLineDayFaultRecord(line.Floor, line.Line, date, "FPL02", endTime));
  294. lineKeyFaults.AddRange(GetKeyInLineDayFaultRecord(line.Floor, line.Line, date, "FPL03", endTime));
  295. lineKeyFaults.AddRange(GetKeyInLineDayFaultRecord(line.Floor, line.Line, date, "FPL04", endTime));
  296. break;
  297. case "FOG":
  298. //lineKeyFaults.AddRange(GetKeyInLineDayFaultRecord(line.Floor, line.Line, date, "COG", endTime));
  299. lineKeyFaults.AddRange(GetKeyInLineDayFaultRecord(line.Floor, line.Line, date, item.Type, endTime));
  300. break;
  301. default:
  302. lineKeyFaults.AddRange(GetKeyInLineDayFaultRecord(line.Floor, line.Line, date, item.Type, endTime));
  303. break;
  304. }
  305. }
  306. return Ok(lineKeyFaults);
  307. }
  308. [HttpGet]
  309. public IActionResult GetMonthData(string lineId, string date, string moduleType)
  310. {
  311. return Ok(_lineService.GetLineMonthData(lineId, date, moduleType));
  312. }
  313. [HttpGet]
  314. public IActionResult ExportineAccumulatedFaultByMorning(string lineId, string date)
  315. {
  316. if (lineId == null || date == null)
  317. return Ok();
  318. var line = _unitOfWork.ProductionLineRepository.GetById(lineId);
  319. DateTime startTime = Convert.ToDateTime($"{date} 08:00:00");
  320. DateTime endTime = startTime.AddHours(12);
  321. var rev = _faultService.GetAccumulatedFaultTopByLine(lineId, startTime, endTime);
  322. string sWebRootFolder = _webHostEnvironment.WebRootPath;
  323. string sFileName = $@"{line.Name} {date} 早班故障详情.xlsx";
  324. var path = Path.Combine(sWebRootFolder, sFileName);
  325. FileInfo file = new FileInfo(path);
  326. if (file.Exists)
  327. {
  328. file.Delete();
  329. file = new FileInfo(path);
  330. }
  331. //ExcelPackage.LicenseContext = LicenseContext.NonCommercial;
  332. using (ExcelPackage package = new ExcelPackage(file))
  333. {
  334. ExcelWorksheet worksheet = package.Workbook.Worksheets.Add("故障详情");
  335. worksheet.Cells.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
  336. worksheet.Cells[1, 1].Value = "NO";
  337. worksheet.Cells[1, 2].Value = "设备";
  338. worksheet.Cells[1, 3].Value = "数据来源";
  339. worksheet.Cells[1, 4].Value = "故障详情";
  340. worksheet.Cells[1, 5].Value = "累计次数";
  341. worksheet.Cells[1, 6].Value = "累计时间(min)";
  342. worksheet.Column(1).Width = 5;
  343. worksheet.Column(2).Width = 12;
  344. worksheet.Column(3).Width = 12;
  345. worksheet.Column(4).Width = 25;
  346. worksheet.Column(5).Width = 12;
  347. worksheet.Column(6).Width = 20;
  348. for (int i = 0; i < rev.Count(); i++)
  349. {
  350. worksheet.Cells[i + 2, 1].Value = i + 1;
  351. worksheet.Cells[i + 2, 2].Value = rev[i].MachineType;
  352. worksheet.Cells[i + 2, 3].Value = rev[i].DataSource == 1 ? "人工录入" : "自动上抛";
  353. worksheet.Cells[i + 2, 4].Value = rev[i].FaultInfo;
  354. worksheet.Cells[i + 2, 5].Value = rev[i].AccumulativeTotal;
  355. worksheet.Cells[i + 2, 6].Value = rev[i].AccumulativeTime;
  356. }
  357. package.Save();
  358. }
  359. var fileStream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
  360. return File(fileStream, "application/octet-stream", sFileName);
  361. }
  362. [HttpGet]
  363. public IActionResult ExportineAccumulatedFaultByNight(string lineId, string date)
  364. {
  365. if (lineId == null || date == null)
  366. return Ok();
  367. var line = _unitOfWork.ProductionLineRepository.GetById(lineId);
  368. DateTime startTime = Convert.ToDateTime($"{date} 20:00:00");
  369. DateTime endTime = startTime.AddHours(12);
  370. var rev = _faultService.GetAccumulatedFaultTopByLine(lineId, startTime, endTime);
  371. string sWebRootFolder = _webHostEnvironment.WebRootPath;
  372. string sFileName = $@"{line.Name} {date} 早班故障详情.xlsx";
  373. var path = Path.Combine(sWebRootFolder, sFileName);
  374. FileInfo file = new FileInfo(path);
  375. if (file.Exists)
  376. {
  377. file.Delete();
  378. file = new FileInfo(path);
  379. }
  380. //ExcelPackage.LicenseContext = LicenseContext.NonCommercial;
  381. using (ExcelPackage package = new ExcelPackage(file))
  382. {
  383. ExcelWorksheet worksheet = package.Workbook.Worksheets.Add("故障详情");
  384. worksheet.Cells.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
  385. worksheet.Cells[1, 1].Value = "NO";
  386. worksheet.Cells[1, 2].Value = "设备";
  387. worksheet.Cells[1, 3].Value = "数据来源";
  388. worksheet.Cells[1, 4].Value = "故障详情";
  389. worksheet.Cells[1, 5].Value = "累计次数";
  390. worksheet.Cells[1, 6].Value = "累计时间(min)";
  391. worksheet.Column(1).Width = 5;
  392. worksheet.Column(2).Width = 12;
  393. worksheet.Column(3).Width = 12;
  394. worksheet.Column(4).Width = 25;
  395. worksheet.Column(5).Width = 12;
  396. worksheet.Column(6).Width = 20;
  397. for (int i = 0; i < rev.Count(); i++)
  398. {
  399. worksheet.Cells[i + 2, 1].Value = i + 1;
  400. worksheet.Cells[i + 2, 2].Value = rev[i].MachineType;
  401. worksheet.Cells[i + 2, 3].Value = rev[i].DataSource == 1 ? "人工录入" : "自动上抛";
  402. worksheet.Cells[i + 2, 4].Value = rev[i].FaultInfo;
  403. worksheet.Cells[i + 2, 5].Value = rev[i].AccumulativeTotal;
  404. worksheet.Cells[i + 2, 6].Value = rev[i].AccumulativeTime;
  405. }
  406. package.Save();
  407. }
  408. var fileStream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
  409. return File(fileStream, "application/octet-stream", sFileName);
  410. }
  411. }
  412. }