ReportFormController.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. using Microsoft.AspNetCore.Mvc;
  2. using ProductionLineMonitor.Application.Services;
  3. using ProductionLineMonitor.Application.Services.OEEService;
  4. using ProductionLineMonitor.Application.Services.OEEService.Dtos;
  5. using ProductionLineMonitor.Core.Dtos;
  6. using ProductionLineMonitor.Core.IRepositories;
  7. using ProductionLineMonitor.Core.Models;
  8. using ProductionLineMonitor.Core.Services;
  9. using ProductionLineMonitor.Core.Utils;
  10. using System;
  11. using System.Collections.Generic;
  12. using System.Linq;
  13. namespace ProductionLineMonitor.Web.Controllers
  14. {
  15. public class ReportFormController : BaseController
  16. {
  17. private readonly IReportFormService _reportFormService;
  18. private readonly IOEEService _oeeService;
  19. private readonly IExcelService _excelService;
  20. private readonly IUnitOfWork _unitOfWork;
  21. public ReportFormController(
  22. IReportFormService reportFormService,
  23. IOEEService oeeService,
  24. IExcelService excelService,
  25. IUnitOfWork unitOfWork)
  26. {
  27. _reportFormService = reportFormService;
  28. _oeeService = oeeService;
  29. _excelService = excelService;
  30. _unitOfWork = unitOfWork;
  31. }
  32. [HttpGet]
  33. public IActionResult Index()
  34. {
  35. //_excelService.EquipmentOperationReport(DateTime.Parse("2024-09-12 08:00:00"));
  36. return View();
  37. }
  38. [HttpGet]
  39. public IActionResult EquipmentOperationReport(DateTime dateTime)
  40. {
  41. _excelService.EquipmentOperationReport(dateTime);
  42. return Ok();
  43. }
  44. [HttpPost]
  45. public IActionResult GetMachineStatistic(string id, DateTime startTime, DateTime endTime)
  46. {
  47. ReportFormDto reportFormDto = new ReportFormDto();
  48. reportFormDto.Date = DateTimeHelper.GetDate(startTime, endTime);
  49. var oees = _reportFormService.GetMachineOEE(id, startTime, endTime.AddDays(1));
  50. var moduleTypes = GetModuleTypes(oees);
  51. foreach (var moduleType in moduleTypes)
  52. {
  53. Form form = new Form();
  54. form.Name = moduleType;
  55. form.Oee = GetOee(moduleType, oees);
  56. form.Capacity = GetCapacity(moduleType, oees);
  57. reportFormDto.Forms.Add(form);
  58. }
  59. return Ok(ResultDto<ReportFormDto>.Success(reportFormDto));
  60. }
  61. [HttpPost]
  62. public IActionResult GetMachineStatisticV1(string id, DateTime startTime, DateTime endTime)
  63. {
  64. //MachineOEE machine = new MachineOEE();
  65. List<MachineOEEInfo> lst = _oeeService.GetOEE(id,
  66. startTime.ToString("yyyy-MM-dd"), endTime.ToString("yyyy-MM-dd"));
  67. return Ok(ResultDto<List<MachineOEEInfo>>.Success(lst));
  68. }
  69. [HttpPost]
  70. public IActionResult GetMachineStatisticV2(string id, DateTime startTime, DateTime endTime, int peroid)
  71. {
  72. //MachineOEE machine = new MachineOEE();
  73. Dictionary<string, List<MachineOEEInfoByPeriod>> lst = new Dictionary<string, List<MachineOEEInfoByPeriod>>();
  74. if (peroid == 3)
  75. {
  76. lst = _oeeService.GetOEEByPeriod(id,
  77. startTime.ToString("yyyy-MM"), endTime.ToString("yyyy-MM"), peroid);
  78. }
  79. else
  80. {
  81. lst = _oeeService.GetOEEByPeriod(id,
  82. startTime.ToString("yyyy-MM-dd"), endTime.ToString("yyyy-MM-dd"), peroid);
  83. }
  84. return Ok(ResultDto<Dictionary<string, List<MachineOEEInfoByPeriod>>>.Success(lst));
  85. }
  86. [HttpPost]
  87. public IActionResult GetOEEExtendSetting(string id)
  88. {
  89. var machine = _unitOfWork.Repository<Machine>().GetList().Where(x => x.Id == id);
  90. if (machine != null && machine.Count() != 0 && !String.IsNullOrEmpty(machine.First().OEEExtendSettings))
  91. {
  92. return Ok(ResultDto<string>.Success(machine.First().OEEExtendSettings));
  93. }
  94. else
  95. {
  96. return Ok(ResultDto<string>.Success(""));
  97. }
  98. }
  99. private List<object[]> GetOee(string moduleType, IEnumerable<MachineStatistic> oees)
  100. {
  101. var result = new List<object[]>();
  102. var data = oees.Where(x => x.ModuleType == moduleType);
  103. foreach (var item in data)
  104. {
  105. object[] o = new object[2];
  106. o[0] = item.Date.ToString("yyyy-MM-dd");
  107. if (item.OEE != null)
  108. {
  109. o[1] = Math.Round(item.OEE.Value * 100, 2);
  110. }
  111. result.Add(o);
  112. }
  113. return result;
  114. }
  115. private List<object[]> GetCapacity(string moduleType, IEnumerable<MachineStatistic> oees)
  116. {
  117. var result = new List<object[]>();
  118. var data = oees.Where(x => x.ModuleType == moduleType);
  119. foreach (var item in data)
  120. {
  121. object[] o = new object[2];
  122. o[0] = item.Date.ToString("yyyy-MM-dd");
  123. o[1] = item.Capacity;
  124. result.Add(o);
  125. }
  126. return result;
  127. }
  128. private List<string> GetModuleTypes(IEnumerable<MachineStatistic> oees)
  129. {
  130. List<string> result = new List<string>();
  131. foreach (var item in oees)
  132. {
  133. if (!result.Contains(item.ModuleType))
  134. {
  135. result.Add(item.ModuleType);
  136. }
  137. }
  138. return result;
  139. }
  140. }
  141. }