using NPOI.SS.Formula.Functions; using ProductionLineMonitor.Core.IRepositories; using ProductionLineMonitor.Core.Utils; using ProductionLineMonitor.Web.Services; using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ProductionLineMonitor.Application.Services.EnergyConsumptionService.Dtos { public class LineEnergyConsumptionDto { public LineEnergyConsumptionDto(string id, string name) { LineId = id; LineName = name; } public string LineId { get; set; } public string LineName { get; set; } public double ElectricEnergiy { get { return Math.Round(MachineElectricEnergiy.Sum(), 2); } } public double[] MachineElectricEnergiy { get; set; } = new double[7]; public IList ElectricEnergyMeterDtos { get; set; } = new List(); } public class EnergyConsumptionReportDto { public EnergyConsumptionReportDto(string startDate, string endDate, IUnitOfWork unitOfWork) { StartDate = Convert.ToDateTime($"{startDate} 08:00:00"); EndDate = Convert.ToDateTime($"{endDate} 08:00:00").AddDays(1); var lines = unitOfWork.ProductionLineRepository.GetList() .OrderBy(o => o.Order).ToList(); var machines = unitOfWork.MachineRepository.GetList() .OrderBy(o => o.ProductionLineOrder).ToList(); foreach (var line in lines) { if (machines.Where(x => x.ProductionLineId == line.Id).Count() > 0) { LineEnergyConsumptionDto dto = new LineEnergyConsumptionDto(line.Id, line.Name); LineDtos.Add(dto); } } foreach (var line in LineDtos) { var ms = machines.Where(x => x.ProductionLineId == line.LineId); double electricEnergiy = 0; foreach (var machine in ms) { int index = -1; switch (machine.Type) { case "AG + FPL": index = 0; break; case "FPL": index = 0; break; case "COG": index = 1; break; case "FOG": index = 2; break; case "BT": index = 3; break; case "PS": index = 4; break; case "TP": index = 5; break; case "EC": index = 6; break; default: break; } if (index != -1) { var ees = MesApiService.GetPowerByEqpAndMonth(machine.Topic, startDate, endDate); if (ees != null) { DateTime[] dts = ees .OrderBy(o => o.DataTime) .Select(x => x.DataTime) .Distinct().ToArray(); if (dts.Count() >= 2) { int i = dts.Length - 1; double electricEnergiy0 = ees.Where(x => x.DataTime == dts[i - 1]).Sum(x => x.ElectricEnergy); double electricEnergiy1 = ees.Where(x => x.DataTime == dts[i]).Sum(x => x.ElectricEnergy); electricEnergiy = electricEnergiy1 - electricEnergiy0; } } line.MachineElectricEnergiy[index] = Math.Round(electricEnergiy, 2); } } } } public EnergyConsumptionReportDto(DateTime startDate, DateTime endDate, IUnitOfWork unitOfWork) { StartDate = startDate; EndDate = endDate; var lines = unitOfWork.ProductionLineRepository.GetList() .OrderBy(o => o.Order).ToList(); var machines = unitOfWork.MachineRepository.GetList() .OrderBy(o => o.ProductionLineOrder).ToList(); foreach (var line in lines) { if (machines.Where(x => x.ProductionLineId == line.Id).Count() > 0) { LineEnergyConsumptionDto dto = new LineEnergyConsumptionDto(line.Id, line.Name); LineDtos.Add(dto); } } foreach (var line in LineDtos) { var ms = machines.Where(x => x.ProductionLineId == line.LineId); double electricEnergiy = 0; foreach (var machine in ms) { int index = -1; switch (machine.Type) { case "AG + FPL": index = 0; break; case "FPL": index = 0; break; case "COG": index = 1; break; case "FOG": index = 2; break; case "BT": index = 3; break; case "PS": index = 4; break; case "TP": index = 5; break; case "EC": index = 6; break; default: break; } if (index != -1) { var ees = MesApiService.GetPowerByEqpAndMonth( machine.Topic, startDate.ToString("yyyy-MM-dd HH:mm:ss"), endDate.ToString("yyyy-MM-dd HH:mm:ss")); if (ees != null) { DateTime[] dts = ees .OrderBy(o => o.DataTime) .Select(x => x.DataTime) .Distinct().ToArray(); if (dts.Count() >= 2) { int i = dts.Length - 1; double electricEnergiy0 = ees.Where(x => x.DataTime == dts[i - 1]).Sum(x => x.ElectricEnergy); double electricEnergiy1 = ees.Where(x => x.DataTime == dts[i]).Sum(x => x.ElectricEnergy); electricEnergiy = electricEnergiy1 - electricEnergiy0; } } line.MachineElectricEnergiy[index] = Math.Round(electricEnergiy, 2); } } } } public DateTime StartDate { get; set; } public DateTime EndDate { get; set; } public List LineDtos { get; set; } = new List(); } }