EnergyConsumptionReportDto.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. using NPOI.SS.Formula.Functions;
  2. using ProductionLineMonitor.Core.IRepositories;
  3. using ProductionLineMonitor.Core.Utils;
  4. using ProductionLineMonitor.Web.Services;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using System.Text;
  9. namespace ProductionLineMonitor.Application.Services.EnergyConsumptionService.Dtos
  10. {
  11. public class LineEnergyConsumptionDto
  12. {
  13. public LineEnergyConsumptionDto(string id, string name)
  14. {
  15. LineId = id;
  16. LineName = name;
  17. }
  18. public string LineId { get; set; }
  19. public string LineName { get; set; }
  20. public double ElectricEnergiy
  21. {
  22. get
  23. {
  24. return Math.Round(MachineElectricEnergiy.Sum(), 2);
  25. }
  26. }
  27. public double[] MachineElectricEnergiy { get; set; } = new double[7];
  28. public IList<ElectricEnergyMeterDataDto> ElectricEnergyMeterDtos { get; set; }
  29. = new List<ElectricEnergyMeterDataDto>();
  30. }
  31. public class EnergyConsumptionReportDto
  32. {
  33. public EnergyConsumptionReportDto(string startDate, string endDate, IUnitOfWork unitOfWork)
  34. {
  35. StartDate = Convert.ToDateTime($"{startDate} 08:00:00");
  36. EndDate = Convert.ToDateTime($"{endDate} 08:00:00").AddDays(1);
  37. var lines = unitOfWork.ProductionLineRepository.GetList()
  38. .OrderBy(o => o.Order).ToList();
  39. var machines = unitOfWork.MachineRepository.GetList()
  40. .OrderBy(o => o.ProductionLineOrder).ToList();
  41. foreach (var line in lines)
  42. {
  43. if (machines.Where(x => x.ProductionLineId == line.Id).Count() > 0)
  44. {
  45. LineEnergyConsumptionDto dto = new LineEnergyConsumptionDto(line.Id, line.Name);
  46. LineDtos.Add(dto);
  47. }
  48. }
  49. foreach (var line in LineDtos)
  50. {
  51. var ms = machines.Where(x => x.ProductionLineId == line.LineId);
  52. double electricEnergiy = 0;
  53. foreach (var machine in ms)
  54. {
  55. int index = -1;
  56. switch (machine.Type)
  57. {
  58. case "AG + FPL": index = 0; break;
  59. case "FPL": index = 0; break;
  60. case "COG": index = 1; break;
  61. case "FOG": index = 2; break;
  62. case "BT": index = 3; break;
  63. case "PS": index = 4; break;
  64. case "TP": index = 5; break;
  65. case "EC": index = 6; break;
  66. default: break;
  67. }
  68. if (index != -1)
  69. {
  70. var ees = MesApiService.GetPowerByEqpAndMonth(machine.Topic, startDate, endDate);
  71. if (ees != null)
  72. {
  73. DateTime[] dts = ees
  74. .OrderBy(o => o.DataTime)
  75. .Select(x => x.DataTime)
  76. .Distinct().ToArray();
  77. if (dts.Count() >= 2)
  78. {
  79. int i = dts.Length - 1;
  80. double electricEnergiy0 = ees.Where(x => x.DataTime == dts[i - 1]).Sum(x => x.ElectricEnergy);
  81. double electricEnergiy1 = ees.Where(x => x.DataTime == dts[i]).Sum(x => x.ElectricEnergy);
  82. electricEnergiy = electricEnergiy1 - electricEnergiy0;
  83. }
  84. }
  85. line.MachineElectricEnergiy[index] = Math.Round(electricEnergiy, 2);
  86. }
  87. }
  88. }
  89. }
  90. public EnergyConsumptionReportDto(DateTime startDate, DateTime endDate, IUnitOfWork unitOfWork)
  91. {
  92. StartDate = startDate;
  93. EndDate = endDate;
  94. var lines = unitOfWork.ProductionLineRepository.GetList()
  95. .OrderBy(o => o.Order).ToList();
  96. var machines = unitOfWork.MachineRepository.GetList()
  97. .OrderBy(o => o.ProductionLineOrder).ToList();
  98. foreach (var line in lines)
  99. {
  100. if (machines.Where(x => x.ProductionLineId == line.Id).Count() > 0)
  101. {
  102. LineEnergyConsumptionDto dto = new LineEnergyConsumptionDto(line.Id, line.Name);
  103. LineDtos.Add(dto);
  104. }
  105. }
  106. foreach (var line in LineDtos)
  107. {
  108. var ms = machines.Where(x => x.ProductionLineId == line.LineId);
  109. double electricEnergiy = 0;
  110. foreach (var machine in ms)
  111. {
  112. int index = -1;
  113. switch (machine.Type)
  114. {
  115. case "AG + FPL": index = 0; break;
  116. case "FPL": index = 0; break;
  117. case "COG": index = 1; break;
  118. case "FOG": index = 2; break;
  119. case "BT": index = 3; break;
  120. case "PS": index = 4; break;
  121. case "TP": index = 5; break;
  122. case "EC": index = 6; break;
  123. default: break;
  124. }
  125. if (index != -1)
  126. {
  127. var ees = MesApiService.GetPowerByEqpAndMonth(
  128. machine.Topic,
  129. startDate.ToString("yyyy-MM-dd HH:mm:ss"),
  130. endDate.ToString("yyyy-MM-dd HH:mm:ss"));
  131. if (ees != null)
  132. {
  133. DateTime[] dts = ees
  134. .OrderBy(o => o.DataTime)
  135. .Select(x => x.DataTime)
  136. .Distinct().ToArray();
  137. if (dts.Count() >= 2)
  138. {
  139. int i = dts.Length - 1;
  140. double electricEnergiy0 = ees.Where(x => x.DataTime == dts[i - 1]).Sum(x => x.ElectricEnergy);
  141. double electricEnergiy1 = ees.Where(x => x.DataTime == dts[i]).Sum(x => x.ElectricEnergy);
  142. electricEnergiy = electricEnergiy1 - electricEnergiy0;
  143. }
  144. }
  145. line.MachineElectricEnergiy[index] = Math.Round(electricEnergiy, 2);
  146. }
  147. }
  148. }
  149. }
  150. public DateTime StartDate { get; set; }
  151. public DateTime EndDate { get; set; }
  152. public List<LineEnergyConsumptionDto> LineDtos { get; set; }
  153. = new List<LineEnergyConsumptionDto>();
  154. }
  155. }