FaultController.cs 14 KB

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