FaultController.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. using AutoMapper;
  2. using Microsoft.AspNetCore.Mvc;
  3. using ProductionLineMonitor.Application.Services.FaultService;
  4. using ProductionLineMonitor.Core.Dtos;
  5. using ProductionLineMonitor.Core.IRepositories;
  6. using ProductionLineMonitor.Core.Models;
  7. using ProductionLineMonitor.Core.Utils;
  8. using ProductionLineMonitor.EntityFramework;
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Linq;
  12. namespace ProductionLineMonitor.Web.Controllers
  13. {
  14. public class FaultController : BaseController
  15. {
  16. private readonly IFaultService _faultService;
  17. private readonly IUnitOfWork _unitOfWork;
  18. private readonly IMapper _mapper;
  19. public FaultController(IFaultService faultService, IUnitOfWork unitOfWork, IMapper mapper)
  20. {
  21. _faultService = faultService;
  22. _unitOfWork = unitOfWork;
  23. _mapper = mapper;
  24. }
  25. [HttpGet]
  26. public IActionResult Index()
  27. {
  28. return View();
  29. }
  30. [HttpGet]
  31. public IActionResult GetFaults(int pageNumber, int pageSize, string keyword)
  32. {
  33. pageNumber = pageNumber == 0 ? 1 : pageNumber;
  34. pageSize = pageSize == 0 ? 10 : pageSize;
  35. keyword ??= "";
  36. //return Ok(_faultService.GetMachineFaultRecords(pageNumber, pageSize, keyword));
  37. return Ok();
  38. }
  39. [HttpGet]
  40. public IActionResult GetTOP10Faults(string id, DateTime startTime, DateTime endTime, string filterString = null)
  41. {
  42. startTime = startTime.AddHours(8);
  43. endTime = endTime.AddHours(32);
  44. List<MachineFaultRecordDtoV1> machineFaultRecordDtos = new List<MachineFaultRecordDtoV1>();
  45. var machine = _unitOfWork.MachineRepository.GetById(id);
  46. if (machine == null)
  47. {
  48. return Ok(machineFaultRecordDtos);
  49. }
  50. var faults = _unitOfWork.MachineFaultRecordRepository.GetList(
  51. x => x.MachineId == machine.Id
  52. && x.StartTime >= startTime
  53. && x.StartTime < endTime
  54. && x.MachineState == 2).ToList();
  55. var mfcs = _unitOfWork.MachineFaultComparisonRepository.GetList(
  56. x => x.FaultTopic == machine.FaultTopic).ToList();
  57. foreach (var fault in faults)
  58. {
  59. var f = machineFaultRecordDtos.Find(x => x.FaultCode == fault.FaultCode);
  60. if (f == null)
  61. {
  62. MachineFaultRecordDtoV1 m = new MachineFaultRecordDtoV1()
  63. {
  64. FaultCode = fault.FaultCode,
  65. };
  66. if (fault.StartTime.Value.Hour >= 8 && fault.StartTime.Value.Hour < 20)
  67. {
  68. m.ShiftCount += fault.TriggerNumber.Value;
  69. }
  70. else
  71. {
  72. m.NightShiftCount += fault.TriggerNumber.Value;
  73. }
  74. machineFaultRecordDtos.Add(m);
  75. }
  76. else
  77. {
  78. if (fault.StartTime.Value.Hour >= 8 && fault.StartTime.Value.Hour < 20)
  79. {
  80. f.ShiftCount += fault.TriggerNumber.Value;
  81. }
  82. else
  83. {
  84. f.NightShiftCount += fault.TriggerNumber.Value;
  85. }
  86. }
  87. }
  88. foreach (var item in machineFaultRecordDtos)
  89. {
  90. var mfc = mfcs.FirstOrDefault(x => x.FaultCode == item.FaultCode);
  91. if (mfc == null)
  92. {
  93. item.FaultInfo = item.FaultCode.ToString() + " " + "未知故障!";
  94. }
  95. else
  96. {
  97. item.FaultInfo = item.FaultCode.ToString() + " " + mfc.FaultInfo;
  98. }
  99. }
  100. if (filterString != null)
  101. {
  102. string[] strs = filterString.Split(',');
  103. machineFaultRecordDtos = machineFaultRecordDtos.Where(x =>
  104. strs.All(s => !x.FaultInfo.Contains(s))).ToList();
  105. }
  106. machineFaultRecordDtos = machineFaultRecordDtos.OrderByDescending(o => o.Count).Take(10).ToList();
  107. return Ok(machineFaultRecordDtos);
  108. }
  109. [HttpGet]
  110. public IActionResult GetFaultTrend(string id, string faultInfo, DateTime startTime, DateTime endTime)
  111. {
  112. ECharts dto = new ECharts();
  113. startTime = startTime.AddHours(8).AddDays(-7);
  114. endTime = endTime.AddHours(32).AddDays(+2);
  115. if (endTime > DateTime.Now)
  116. endTime = Convert.ToDateTime($"{DateTime.Now.AddDays(+1):yyyy-MM-dd} 08:00:00");
  117. int index = faultInfo.IndexOf(' ');
  118. if (index == -1)
  119. return Ok(dto);
  120. string str = faultInfo[..index];
  121. int faultCode = Convert.ToInt32(str);
  122. var machine = _unitOfWork.MachineRepository.GetById(id);
  123. if (machine == null)
  124. return Ok(dto);
  125. var faults = _unitOfWork.MachineFaultRecordRepository
  126. .GetList(
  127. x => x.MachineId == id
  128. && x.FaultCode == faultCode
  129. && x.StartTime >= startTime
  130. && x.StartTime < endTime
  131. && x.MachineState == 2)
  132. .OrderBy(o => o.StartTime).ToList();
  133. var machineFaultComparison = _unitOfWork.MachineFaultComparisonRepository
  134. .FirstOrDefault(
  135. x => x.FaultTopic == machine.FaultTopic
  136. && x.FaultCode == faultCode);
  137. Serie serie = new Serie();
  138. if (machineFaultComparison != null)
  139. serie.Name = $"{faultCode} {machineFaultComparison.FaultInfo}";
  140. else
  141. serie.Name = $"{faultCode} 未知故障!";
  142. var dates = DateTimeHelper.GetDateTime(startTime, endTime);
  143. foreach (var item in dates)
  144. {
  145. //DateTime sTime = Convert.ToDateTime($"{item:yyyy-MM-dd} 08:00:00");
  146. //DateTime eTime = Convert.ToDateTime($"{item.AddDays(1):yyyy-MM-dd} 08:00:00");
  147. //var fs = faults.Where(x => x.StartTime >= sTime && x.StartTime < eTime);
  148. //int count = 0;
  149. //if (fs != null)
  150. // foreach (var f in fs)
  151. // count += f.TriggerNumber.Value;
  152. //object[] o = new object[2];
  153. //o[0] = item.ToString("yyyy-MM-dd");
  154. //o[1] = count;
  155. //serie.Data.Add(o);
  156. DateTime sTime1 = Convert.ToDateTime($"{item:yyyy-MM-dd} 08:00:00");
  157. DateTime eTime1 = Convert.ToDateTime($"{item:yyyy-MM-dd} 20:00:00");
  158. var fs1 = faults.Where(x => x.StartTime >= sTime1 && x.StartTime < eTime1);
  159. int count1 = 0;
  160. if (fs1 != null)
  161. foreach (var f in fs1)
  162. count1 += f.TriggerNumber.Value;
  163. object[] o1 = new object[2];
  164. o1[0] = item.ToString("MM/dd\n白班");
  165. o1[1] = count1;
  166. serie.Data.Add(o1);
  167. DateTime sTime2 = Convert.ToDateTime($"{item:yyyy-MM-dd} 20:00:00");
  168. DateTime eTime2 = Convert.ToDateTime($"{item.AddDays(1):yyyy-MM-dd} 08:00:00");
  169. var fs2 = faults.Where(x => x.StartTime >= sTime2 && x.StartTime < eTime2);
  170. int count2 = 0;
  171. if (fs2 != null)
  172. foreach (var f in fs2)
  173. count2 += f.TriggerNumber.Value;
  174. object[] o2 = new object[2];
  175. o2[0] = item.ToString("MM/dd\n夜班");
  176. o2[1] = count2;
  177. serie.Data.Add(o2);
  178. }
  179. dto.Series.Add(serie);
  180. return Ok(dto);
  181. }
  182. [HttpGet]
  183. public IActionResult GetFaultTrendV1(string id, string faultInfo, DateTime day)
  184. {
  185. ECharts dto = new ECharts();
  186. int index = faultInfo.IndexOf(' ');
  187. if (index == -1)
  188. return Ok(dto);
  189. string str = faultInfo[..index];
  190. int faultCode = Convert.ToInt32(str);
  191. DateTime startTime = Convert.ToDateTime($"{day.AddDays(-10):yyyy-MM-dd} 08:00:00");
  192. DateTime endTime = Convert.ToDateTime($"{day.AddDays(+10):yyyy-MM-dd} 08:00:00");
  193. if (endTime > DateTime.Now)
  194. endTime = Convert.ToDateTime($"{DateTime.Now.AddDays(+1):yyyy-MM-dd} 08:00:00");
  195. var machine = _unitOfWork.MachineRepository.GetById(id);
  196. if (machine == null)
  197. return Ok(dto);
  198. var faults = _unitOfWork.MachineFaultRecordRepository
  199. .GetList(
  200. x => x.MachineId == id
  201. && x.FaultCode == faultCode
  202. && x.StartTime >= startTime
  203. && x.StartTime < endTime)
  204. .OrderBy(o => o.StartTime);
  205. var machineFaultComparison = _unitOfWork.MachineFaultComparisonRepository
  206. .FirstOrDefault(
  207. x => x.FaultTopic == machine.FaultTopic
  208. && x.FaultCode == faultCode);
  209. Serie serie = new Serie();
  210. if (machineFaultComparison != null)
  211. serie.Name = $"{faultCode} {machineFaultComparison.FaultInfo}";
  212. else
  213. serie.Name = $"{faultCode} 未知故障!";
  214. var dates = DateTimeHelper.GetDateTime(startTime, endTime);
  215. foreach (var item in dates)
  216. {
  217. DateTime sTime = Convert.ToDateTime($"{item:yyyy-MM-dd} 08:00:00");
  218. DateTime eTime = Convert.ToDateTime($"{item.AddDays(1):yyyy-MM-dd} 08:00:00");
  219. var fs = faults.Where(x => x.StartTime >= sTime && x.StartTime < eTime).ToList();
  220. int count = 0;
  221. if (fs != null)
  222. foreach (var f in fs)
  223. count += f.TriggerNumber.Value;
  224. object[] o = new object[2];
  225. o[0] = item.ToString("yyyy-MM-dd");
  226. o[1] = count;
  227. serie.Data.Add(o);
  228. }
  229. dto.Series.Add(serie);
  230. return Ok(dto);
  231. }
  232. [HttpGet]
  233. public IActionResult GetLineDayTop10Faults(string lineId, DateTime day, string keywords)
  234. {
  235. List<MachineFaultRecordDto> dtos = new List<MachineFaultRecordDto>();
  236. var line = _unitOfWork.ProductionLineRepository.GetById(lineId);
  237. if (line != null)
  238. {
  239. var machines = _unitOfWork.MachineRepository.GetList(
  240. x => x.ProductionLineId == line.Id
  241. && x.IsInclusionLineStatistics == true);
  242. foreach (var item in machines)
  243. {
  244. dtos.AddRange(GetMachineFaultsByTop10(item.Id, day, keywords));
  245. }
  246. }
  247. return Ok(dtos.OrderByDescending(o => o.Duration).Take(10).ToList());
  248. }
  249. [HttpGet]
  250. public IActionResult GetMachineDayTop10Faults(string machineId, DateTime day, string keywords)
  251. {
  252. List<MachineFaultRecordDto> dtos = GetMachineFaultsByTop10(machineId, day, keywords);
  253. return Ok(dtos);
  254. }
  255. private List<MachineFaultRecordDto> GetMachineFaultsByTop10(string machineId, DateTime day, string keywords)
  256. {
  257. return _faultService.GetMachineFaultsByTop10(machineId, day, keywords);
  258. }
  259. public IActionResult ExceptExcel(DateTime startTime, DateTime endTime)
  260. {
  261. IEnumerable<MachineFaultRecordDto> lst = new List<MachineFaultRecordDto>();
  262. return Ok();
  263. }
  264. public IActionResult GetFaults(DateTime startTime, DateTime endTime, string machineId)
  265. {
  266. var context = _unitOfWork.GetDbContext() as ProductionLineContext;
  267. var machine = context.Machines.Find(machineId);
  268. var query = from mfr in context.Set<MachineFaultRecord>()
  269. join mfc in context.Set<MachineFaultComparison>()
  270. on mfr.FaultCode.Value equals mfc.FaultCode.Value
  271. where
  272. mfr.MachineId == machine.Id &&
  273. mfc.FaultTopic == machine.FaultTopic &&
  274. mfr.StartTime.Value >= startTime &&
  275. mfr.StartTime.Value < endTime
  276. select new { mfr, mfc };
  277. foreach (var item in query)
  278. {
  279. }
  280. return Ok();
  281. }
  282. public IActionResult ExportFaults(DateTime startTime, DateTime endTime)
  283. {
  284. var stream = System.IO.File.OpenRead("");//excel表转换成流
  285. return File(stream, "application/vnd.android.package-archive", "");
  286. }
  287. [HttpGet]
  288. public IActionResult GetFault(string id, string faultInfo, DateTime startTime, DateTime endTime)
  289. {
  290. startTime = startTime.AddHours(8);
  291. endTime = endTime.AddHours(32);
  292. List<MachineFaultRecordDtoV1> machineFaultRecordDtos = new List<MachineFaultRecordDtoV1>();
  293. var machine = _unitOfWork.MachineRepository.GetById(id);
  294. if (machine == null)
  295. {
  296. return Ok(machineFaultRecordDtos);
  297. }
  298. var faults = _unitOfWork.MachineFaultRecordRepository.GetList(
  299. x => x.MachineId == machine.Id
  300. && x.StartTime >= startTime
  301. && x.StartTime < endTime
  302. && x.MachineState == 2).ToList();
  303. var mfcs = _unitOfWork.MachineFaultComparisonRepository.GetList(
  304. x => x.FaultTopic == machine.FaultTopic).ToList();
  305. return Ok();
  306. }
  307. }
  308. }