123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200 |
- 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<MachineFaultComparison> mfc = new List<MachineFaultComparison>();
- 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<MachineFaultComparison> mfc = new List<MachineFaultComparison>();
- 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<MachineFaultComparison> mfcs = new List<MachineFaultComparison>();
- 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();
- }
- }
- }
|