using Core.Dtos; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using OfficeOpenXml; using ProductionLineMonitor.Application.Services; using ProductionLineMonitor.Application.Services.FaultService; using ProductionLineMonitor.Core.Dtos; using ProductionLineMonitor.Core.Enums; using ProductionLineMonitor.Core.IRepositories; using ProductionLineMonitor.Core.Models; using ProductionLineMonitor.Core.Services; using System; using System.Collections.Generic; using System.IO; namespace ProductionLineMonitor.Web.API { [ApiController] public class ExternalController : ControllerBase { private readonly IExternalService _externalService; private readonly IFaultService _faultService; private readonly IUnitOfWork _unitOfWork; private readonly IExcelService _excelService; public ExternalController( IExternalService externalService, IFaultService faultService, IUnitOfWork unitOfWork, IExcelService excelService) { _externalService = externalService; _faultService = faultService; _unitOfWork = unitOfWork; _excelService = excelService; } [Route("/Api/EQPData")] [HttpPost] public IActionResult EQPData([FromBody] EQPDataDto dto) { _externalService.AddEQPData(dto); return Ok(ResultDto.Success()); } [Route("/Api/EQPData/FaultComparison")] [HttpPost] public IActionResult FaultComparison([FromBody] EQPData dto) { List mfc = new List(); foreach (var alarm in dto.Alarms) { if (alarm.AlarmInfo != null && alarm.AlarmInfo != "") { MachineFaultComparison machineFaultComparison = new MachineFaultComparison() { Id = Guid.NewGuid().ToString().ToUpper(), FaultTopic = dto.Topic, FaultCode = alarm.AlarmCode, FaultInfo = alarm.AlarmInfo, FaultLevel = AlarmLevelEnum.Error, FaultCategory = "", CreateTime = DateTime.Now }; mfc.Add(machineFaultComparison); } } _faultService.AddMachineFaultComparisons(dto.Topic, mfc); return Ok(ResultDto.Success()); } [Route("/Api/EQPData/FaultComparison/V1")] [HttpPost] public IActionResult FaultComparisonV1([FromBody] MachineFaultComparisonDto dto) { var machine = _unitOfWork.MachineRepository.FirstOrDefault(x => x.FaultTopic == dto.DeviceNo); if (machine == null) { return Ok(ResultDto.Fail($"未找到相关设备:{dto.DeviceNo}")); } List mfc = new List(); foreach (var alarm in dto.FaultComparisonDtos) { if (alarm.AlarmInfo != null && alarm.AlarmInfo != "") { MachineFaultComparison machineFaultComparison = new MachineFaultComparison() { Id = Guid.NewGuid().ToString().ToUpper(), FaultTopic = dto.DeviceNo, FaultCode = Convert.ToInt32(alarm.AlarmCode), FaultInfo = alarm.AlarmInfo, FaultLevel = alarm.AlarmLevel, FaultCategory = "", CreateTime = DateTime.Now }; mfc.Add(machineFaultComparison); } } _faultService.AddMachineFaultComparisons(dto.DeviceNo, mfc); return Ok(ResultDto.Success()); } [Route("/Api/EQPData/UploadFaultFile/V1/{topic}")] [HttpPost] public IActionResult UploadFaultFile(IFormFile file, string topic) { try { if (file == null || file.Length == 0) { return Ok(ResultDto.Fail("No file uploaded.")); } if (string.IsNullOrEmpty(topic)) { return Ok(ResultDto.Fail("No topic uploaded.")); } if (!_unitOfWork.MachineRepository.Any(x => x.FaultTopic == topic)) { return Ok(ResultDto.Fail("There is no relevant comparison information for the topic.")); } // 读取文件内容(示例) using var stream = new MemoryStream(); file.CopyTo(stream); //ExcelPackage.LicenseContext = LicenseContext.NonCommercial; // 在这里处理读取到的文件内容 using ExcelPackage excelPackage = new ExcelPackage(stream); ExcelWorksheet sheet = excelPackage.Workbook.Worksheets["Sheet1"]; List mfcs = new List(); int rowCount = sheet.Dimension.Rows; for (int row = 1; row <= rowCount; row++) { if (row == 1) continue; if (sheet.Cells[row, 1].Value == null) break; int no = int.Parse(sheet.Cells[row, 1].Value.ToString()); string alarmCode = sheet.Cells[row, 2].Value.ToString(); int alarmLevel = int.Parse(sheet.Cells[row, 3].Value.ToString()); string alarmInfo = sheet.Cells[row, 4].Value.ToString(); MachineFaultComparison alarmComparison = new MachineFaultComparison() { Id = Guid.NewGuid().ToString().ToUpper(), FaultTopic = topic, FaultCode = Convert.ToInt32(alarmCode), FaultInfo = alarmInfo, FaultLevel = (AlarmLevelEnum?)alarmLevel, FaultCategory = "", CreateTime = DateTime.Now }; var findAlarmComparison = mfcs.Find(x => x.FaultCode == alarmComparison.FaultCode); if (findAlarmComparison != null) { throw new Exception($"No:{no}, AlarmCode:{alarmComparison.FaultCode}, 已存在,请检查!"); } mfcs.Add(alarmComparison); } _faultService.AddMachineFaultComparisons(topic, mfcs); return Ok(ResultDto.Success()); } catch (Exception ex) { return Ok(ResultDto.Fail(ex.Message)); } } [Route("/Api/EquipmentOperationReport")] [HttpPost] public IActionResult EquipmentOperationReport(DateTime time) { try { _excelService.EquipmentOperationReport(time); } catch (Exception ex) { return Ok(ex.Message); } return Ok(); } } }