using Microsoft.AspNetCore.Mvc; using ProductionLineMonitor.Application.Services; using ProductionLineMonitor.Application.Services.OEEService; using ProductionLineMonitor.Application.Services.OEEService.Dtos; using ProductionLineMonitor.Core.Dtos; using ProductionLineMonitor.Core.Models; using ProductionLineMonitor.Core.Services; using ProductionLineMonitor.Core.Utils; using System; using System.Collections.Generic; using System.Linq; namespace ProductionLineMonitor.Web.Controllers { public class ReportFormController : BaseController { private readonly IReportFormService _reportFormService; private readonly IOEEService _oeeService; private readonly IExcelService _excelService; public ReportFormController( IReportFormService reportFormService, IOEEService oeeService, IExcelService excelService) { _reportFormService = reportFormService; _oeeService = oeeService; _excelService = excelService; } [HttpGet] public IActionResult Index() { //_excelService.EquipmentOperationReport(DateTime.Parse("2024-09-12 08:00:00")); return View(); } [HttpGet] public IActionResult EquipmentOperationReport(DateTime dateTime) { _excelService.EquipmentOperationReport(dateTime); return Ok(); } [HttpPost] public IActionResult GetMachineStatistic(string id, DateTime startTime, DateTime endTime) { ReportFormDto reportFormDto = new ReportFormDto(); reportFormDto.Date = DateTimeHelper.GetDate(startTime, endTime); var oees = _reportFormService.GetMachineOEE(id, startTime, endTime.AddDays(1)); var moduleTypes = GetModuleTypes(oees); foreach (var moduleType in moduleTypes) { Form form = new Form(); form.Name = moduleType; form.Oee = GetOee(moduleType, oees); form.Capacity = GetCapacity(moduleType, oees); reportFormDto.Forms.Add(form); } return Ok(ResultDto.Success(reportFormDto)); } [HttpPost] public IActionResult GetMachineStatisticV1(string id, DateTime startTime, DateTime endTime) { //MachineOEE machine = new MachineOEE(); List lst = _oeeService.GetOEE(id, startTime.ToString("yyyy-MM-dd"), endTime.ToString("yyyy-MM-dd")); return Ok(ResultDto>.Success(lst)); } private List GetOee(string moduleType, IEnumerable oees) { var result = new List(); var data = oees.Where(x => x.ModuleType == moduleType); foreach (var item in data) { object[] o = new object[2]; o[0] = item.Date.ToString("yyyy-MM-dd"); if (item.OEE != null) { o[1] = Math.Round(item.OEE.Value * 100, 2); } result.Add(o); } return result; } private List GetCapacity(string moduleType, IEnumerable oees) { var result = new List(); var data = oees.Where(x => x.ModuleType == moduleType); foreach (var item in data) { object[] o = new object[2]; o[0] = item.Date.ToString("yyyy-MM-dd"); o[1] = item.Capacity; result.Add(o); } return result; } private List GetModuleTypes(IEnumerable oees) { List result = new List(); foreach (var item in oees) { if (!result.Contains(item.ModuleType)) { result.Add(item.ModuleType); } } return result; } } }