123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165 |
- 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<ElectricEnergyMeterDataDto> ElectricEnergyMeterDtos { get; set; }
- = new List<ElectricEnergyMeterDataDto>();
- }
- 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<LineEnergyConsumptionDto> LineDtos { get; set; }
- = new List<LineEnergyConsumptionDto>();
- }
- }
|