ExternalController.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. using Core.Dtos;
  2. using Microsoft.AspNetCore.Http;
  3. using Microsoft.AspNetCore.Mvc;
  4. using OfficeOpenXml;
  5. using ProductionLineMonitor.Application.Services;
  6. using ProductionLineMonitor.Application.Services.FaultService;
  7. using ProductionLineMonitor.Core.Dtos;
  8. using ProductionLineMonitor.Core.Enums;
  9. using ProductionLineMonitor.Core.IRepositories;
  10. using ProductionLineMonitor.Core.Models;
  11. using ProductionLineMonitor.Core.Services;
  12. using System;
  13. using System.Collections.Generic;
  14. using System.IO;
  15. namespace ProductionLineMonitor.Web.API
  16. {
  17. [ApiController]
  18. public class ExternalController : ControllerBase
  19. {
  20. private readonly IExternalService _externalService;
  21. private readonly IFaultService _faultService;
  22. private readonly IUnitOfWork _unitOfWork;
  23. private readonly IExcelService _excelService;
  24. public ExternalController(
  25. IExternalService externalService,
  26. IFaultService faultService,
  27. IUnitOfWork unitOfWork,
  28. IExcelService excelService)
  29. {
  30. _externalService = externalService;
  31. _faultService = faultService;
  32. _unitOfWork = unitOfWork;
  33. _excelService = excelService;
  34. }
  35. [Route("/Api/EQPData")]
  36. [HttpPost]
  37. public IActionResult EQPData([FromBody] EQPDataDto dto)
  38. {
  39. _externalService.AddEQPData(dto);
  40. return Ok(ResultDto.Success());
  41. }
  42. [Route("/Api/EQPData/FaultComparison")]
  43. [HttpPost]
  44. public IActionResult FaultComparison([FromBody] EQPData dto)
  45. {
  46. List<MachineFaultComparison> mfc = new List<MachineFaultComparison>();
  47. foreach (var alarm in dto.Alarms)
  48. {
  49. if (alarm.AlarmInfo != null && alarm.AlarmInfo != "")
  50. {
  51. MachineFaultComparison machineFaultComparison = new MachineFaultComparison()
  52. {
  53. Id = Guid.NewGuid().ToString().ToUpper(),
  54. FaultTopic = dto.Topic,
  55. FaultCode = alarm.AlarmCode,
  56. FaultInfo = alarm.AlarmInfo,
  57. FaultLevel = AlarmLevelEnum.Error,
  58. FaultCategory = "",
  59. CreateTime = DateTime.Now
  60. };
  61. mfc.Add(machineFaultComparison);
  62. }
  63. }
  64. _faultService.AddMachineFaultComparisons(dto.Topic, mfc);
  65. return Ok(ResultDto.Success());
  66. }
  67. [Route("/Api/EQPData/FaultComparison/V1")]
  68. [HttpPost]
  69. public IActionResult FaultComparisonV1([FromBody] MachineFaultComparisonDto dto)
  70. {
  71. var machine = _unitOfWork.MachineRepository.FirstOrDefault(x => x.FaultTopic == dto.DeviceNo);
  72. if (machine == null)
  73. {
  74. return Ok(ResultDto.Fail($"未找到相关设备:{dto.DeviceNo}"));
  75. }
  76. List<MachineFaultComparison> mfc = new List<MachineFaultComparison>();
  77. foreach (var alarm in dto.FaultComparisonDtos)
  78. {
  79. if (alarm.AlarmInfo != null && alarm.AlarmInfo != "")
  80. {
  81. MachineFaultComparison machineFaultComparison = new MachineFaultComparison()
  82. {
  83. Id = Guid.NewGuid().ToString().ToUpper(),
  84. FaultTopic = dto.DeviceNo,
  85. FaultCode = Convert.ToInt32(alarm.AlarmCode),
  86. FaultInfo = alarm.AlarmInfo,
  87. FaultLevel = alarm.AlarmLevel,
  88. FaultCategory = "",
  89. CreateTime = DateTime.Now
  90. };
  91. mfc.Add(machineFaultComparison);
  92. }
  93. }
  94. _faultService.AddMachineFaultComparisons(dto.DeviceNo, mfc);
  95. return Ok(ResultDto.Success());
  96. }
  97. [Route("/Api/EQPData/UploadFaultFile/V1/{topic}")]
  98. [HttpPost]
  99. public IActionResult UploadFaultFile(IFormFile file, string topic)
  100. {
  101. try
  102. {
  103. if (file == null || file.Length == 0)
  104. {
  105. return Ok(ResultDto.Fail("No file uploaded."));
  106. }
  107. if (string.IsNullOrEmpty(topic))
  108. {
  109. return Ok(ResultDto.Fail("No topic uploaded."));
  110. }
  111. if (!_unitOfWork.MachineRepository.Any(x => x.FaultTopic == topic))
  112. {
  113. return Ok(ResultDto.Fail("There is no relevant comparison information for the topic."));
  114. }
  115. // 读取文件内容(示例)
  116. using var stream = new MemoryStream();
  117. file.CopyTo(stream);
  118. //ExcelPackage.LicenseContext = LicenseContext.NonCommercial;
  119. // 在这里处理读取到的文件内容
  120. using ExcelPackage excelPackage = new ExcelPackage(stream);
  121. ExcelWorksheet sheet = excelPackage.Workbook.Worksheets["Sheet1"];
  122. List<MachineFaultComparison> mfcs = new List<MachineFaultComparison>();
  123. int rowCount = sheet.Dimension.Rows;
  124. for (int row = 1; row <= rowCount; row++)
  125. {
  126. if (row == 1)
  127. continue;
  128. if (sheet.Cells[row, 1].Value == null)
  129. break;
  130. int no = int.Parse(sheet.Cells[row, 1].Value.ToString());
  131. string alarmCode = sheet.Cells[row, 2].Value.ToString();
  132. int alarmLevel = int.Parse(sheet.Cells[row, 3].Value.ToString());
  133. string alarmInfo = sheet.Cells[row, 4].Value.ToString();
  134. MachineFaultComparison alarmComparison = new MachineFaultComparison()
  135. {
  136. Id = Guid.NewGuid().ToString().ToUpper(),
  137. FaultTopic = topic,
  138. FaultCode = Convert.ToInt32(alarmCode),
  139. FaultInfo = alarmInfo,
  140. FaultLevel = (AlarmLevelEnum?)alarmLevel,
  141. FaultCategory = "",
  142. CreateTime = DateTime.Now
  143. };
  144. var findAlarmComparison = mfcs.Find(x => x.FaultCode == alarmComparison.FaultCode);
  145. if (findAlarmComparison != null)
  146. {
  147. throw new Exception($"No:{no}, AlarmCode:{alarmComparison.FaultCode}, 已存在,请检查!");
  148. }
  149. mfcs.Add(alarmComparison);
  150. }
  151. _faultService.AddMachineFaultComparisons(topic, mfcs);
  152. return Ok(ResultDto.Success());
  153. }
  154. catch (Exception ex)
  155. {
  156. return Ok(ResultDto.Fail(ex.Message));
  157. }
  158. }
  159. [Route("/Api/EquipmentOperationReport")]
  160. [HttpPost]
  161. public IActionResult EquipmentOperationReport(DateTime time)
  162. {
  163. try
  164. {
  165. _excelService.EquipmentOperationReport(time);
  166. }
  167. catch (Exception ex)
  168. {
  169. return Ok(ex.Message);
  170. }
  171. return Ok();
  172. }
  173. }
  174. }