FaultService.cs 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805
  1. using AutoMapper;
  2. using Microsoft.Data.Sqlite;
  3. using Org.BouncyCastle.Crypto.IO;
  4. using ProductionLineMonitor.Application.Services.Dtos;
  5. using ProductionLineMonitor.Application.Services.FaultService.Dtos;
  6. using ProductionLineMonitor.Core.Dtos;
  7. using ProductionLineMonitor.Core.IRepositories;
  8. using ProductionLineMonitor.Core.Models;
  9. using ProductionLineMonitor.EntityFramework;
  10. using ProductionLineMonitor.Web.Services;
  11. using System;
  12. using System.Collections.Generic;
  13. using System.Linq;
  14. using System.Security.Cryptography.X509Certificates;
  15. namespace ProductionLineMonitor.Application.Services.FaultService
  16. {
  17. public class FaultService : IFaultService
  18. {
  19. private readonly IUnitOfWork _unitOfWork;
  20. private readonly IMapper _mapper;
  21. public FaultService(IUnitOfWork unitOfWork, IMapper mapper)
  22. {
  23. _unitOfWork = unitOfWork;
  24. _mapper = mapper;
  25. }
  26. public void AddMachineFaultComparisons(string faultTopic, IEnumerable<MachineFaultComparison> machineFaultComparisons)
  27. {
  28. string sql = "delete from MachineFaultComparisons where FaultTopic=@FaultTopic";
  29. _unitOfWork.ExecuteSql(sql, new[] { new SqliteParameter("@FaultTopic", faultTopic) });
  30. _unitOfWork.MachineFaultComparisonRepository.Create(machineFaultComparisons);
  31. _unitOfWork.SaveChanges();
  32. }
  33. public IEnumerable<LineFaultDto> GetLineFault(string lineId, DateTime startTime, DateTime endTime, int durationThreshold)
  34. {
  35. List<LineFaultDto> dtos = new List<LineFaultDto>();
  36. if (!(_unitOfWork.GetDbContext() is ProductionLineContext context))
  37. return dtos;
  38. var line = context.ProductionLines?.Find(lineId);
  39. if (line == null)
  40. return dtos;
  41. var machines = context.Machines
  42. .Where(x => x.ProductionLineId == line.Id && x.IsInclusionLineStatistics == true)
  43. .OrderBy(o => o.ProductionLineOrder);
  44. foreach (var machine in machines)
  45. {
  46. var query = from mfr in context.Set<MachineFaultRecord>()
  47. join mfc in context.Set<MachineFaultComparison>()
  48. on mfr.FaultCode equals mfc.FaultCode
  49. where
  50. mfr.MachineId == machine.Id &&
  51. mfc.FaultTopic == machine.FaultTopic &&
  52. mfr.StartTime >= startTime &&
  53. mfr.StartTime < endTime
  54. orderby
  55. mfr.StartTime
  56. select new { mfr, mfc };
  57. MachineFaultRecord? temp = null;
  58. foreach (var item in query)
  59. {
  60. if (!item.mfc.FaultInfo.Contains("安全门")
  61. && !item.mfc.FaultInfo.Contains("门禁")
  62. && !item.mfc.FaultInfo.Contains("提示上料")
  63. && !item.mfc.FaultInfo.Contains("提示卸料")
  64. && item.mfr.StartTime != null
  65. && item.mfr.EndTime != null)
  66. {
  67. if (temp != null)
  68. {
  69. if (item.mfr.StartTime >= temp.StartTime && item.mfr.EndTime <= temp.EndTime)
  70. {
  71. continue;
  72. }
  73. }
  74. LineFaultDto dto = new LineFaultDto()
  75. {
  76. MachineType = machine.Type,
  77. FaultCode = item.mfr.FaultCode.ToString(),
  78. FaultInfo = item.mfc.FaultInfo,
  79. StartTime = item.mfr.StartTime.Value,
  80. EndTime = item.mfr.EndTime.Value,
  81. MachineState = item.mfr.MachineState == null ? 0 : item.mfr.MachineState.Value,
  82. TriggerNumber = item.mfr.TriggerNumber == null ? 0 : item.mfr.TriggerNumber.Value,
  83. State = item.mfr.State == null ? 0 : item.mfr.State.Value,
  84. DataSource = 0
  85. };
  86. if (dto.EndTime > endTime)
  87. {
  88. dto.EndTime = endTime;
  89. }
  90. dto.Duration = (dto.EndTime - dto.StartTime).Value.TotalMinutes;
  91. if (dto.Duration >= durationThreshold)
  92. {
  93. dtos.Add(dto);
  94. temp = item.mfr;
  95. }
  96. }
  97. }
  98. }
  99. List<LineFaultDto> keyInFaults = new List<LineFaultDto>();
  100. //keyInFaults.AddRange(GetKeyInLineFault(line.Floor, line.Line, startTime.ToString("yyyy-MM-dd"), "COG", endTime, "COG"));
  101. keyInFaults.AddRange(GetKeyInLineFault(line.Floor, line.Line, startTime.ToString("yyyy-MM-dd"), "BC机", endTime, "BC机"));
  102. foreach (var machine in machines)
  103. {
  104. switch (machine.Type)
  105. {
  106. case "AG + FPL":
  107. keyInFaults.AddRange(GetKeyInLineFault(line.Floor, line.Line, startTime.ToString("yyyy-MM-dd"), "TPA", endTime, machine.Type));
  108. keyInFaults.AddRange(GetKeyInLineFault(line.Floor, line.Line, startTime.ToString("yyyy-MM-dd"), "FPL", endTime, machine.Type));
  109. keyInFaults.AddRange(GetKeyInLineFault(line.Floor, line.Line, startTime.ToString("yyyy-MM-dd"), "FPL01", endTime, machine.Type));
  110. keyInFaults.AddRange(GetKeyInLineFault(line.Floor, line.Line, startTime.ToString("yyyy-MM-dd"), "FPL02", endTime, machine.Type));
  111. keyInFaults.AddRange(GetKeyInLineFault(line.Floor, line.Line, startTime.ToString("yyyy-MM-dd"), "FPL03", endTime, machine.Type));
  112. keyInFaults.AddRange(GetKeyInLineFault(line.Floor, line.Line, startTime.ToString("yyyy-MM-dd"), "FPL04", endTime, machine.Type));
  113. break;
  114. case "FPL":
  115. keyInFaults.AddRange(GetKeyInLineFault(line.Floor, line.Line, startTime.ToString("yyyy-MM-dd"), "TPA", endTime, machine.Type));
  116. keyInFaults.AddRange(GetKeyInLineFault(line.Floor, line.Line, startTime.ToString("yyyy-MM-dd"), "FPL", endTime, machine.Type));
  117. keyInFaults.AddRange(GetKeyInLineFault(line.Floor, line.Line, startTime.ToString("yyyy-MM-dd"), "FPL01", endTime, machine.Type));
  118. keyInFaults.AddRange(GetKeyInLineFault(line.Floor, line.Line, startTime.ToString("yyyy-MM-dd"), "FPL02", endTime, machine.Type));
  119. keyInFaults.AddRange(GetKeyInLineFault(line.Floor, line.Line, startTime.ToString("yyyy-MM-dd"), "FPL03", endTime, machine.Type));
  120. keyInFaults.AddRange(GetKeyInLineFault(line.Floor, line.Line, startTime.ToString("yyyy-MM-dd"), "FPL04", endTime, machine.Type));
  121. break;
  122. case "FOG":
  123. keyInFaults.AddRange(GetKeyInLineFault(line.Floor, line.Line, startTime.ToString("yyyy-MM-dd"), "FOG", endTime, machine.Type));
  124. break;
  125. case "AOI":
  126. keyInFaults.AddRange(GetKeyInLineFault(line.Floor, line.Line, startTime.ToString("yyyy-MM-dd"), "LOT2-AOI", endTime, machine.Type));
  127. break;
  128. default:
  129. keyInFaults.AddRange(GetKeyInLineFault(line.Floor, line.Line, startTime.ToString("yyyy-MM-dd"), machine.Type, endTime, machine.Type));
  130. break;
  131. }
  132. }
  133. foreach (var keyInFault in keyInFaults)
  134. {
  135. if (keyInFault.StartTime != null
  136. && keyInFault.EndTime != null)
  137. {
  138. LineFaultDto dto = new LineFaultDto
  139. {
  140. MachineType = keyInFault.MachineType,
  141. FaultCode = keyInFault.FaultCode.ToString(),
  142. FaultInfo = keyInFault.FaultInfo,
  143. StartTime = keyInFault.StartTime,
  144. EndTime = keyInFault.EndTime,
  145. DataSource = 1
  146. };
  147. dto.Duration = (keyInFault.EndTime.Value - keyInFault.StartTime).TotalMinutes;
  148. if (dto.Duration >= durationThreshold)
  149. {
  150. dtos.Add(dto);
  151. }
  152. }
  153. }
  154. return dtos;
  155. }
  156. public IEnumerable<LineAccumulatedFaultDto> GetAccumulatedFaultTop10ByLine(string lineId, DateTime startTime, DateTime endTime)
  157. {
  158. List<LineAccumulatedFaultDto> dtos = new List<LineAccumulatedFaultDto>();
  159. if (!(_unitOfWork.GetDbContext() is ProductionLineContext context))
  160. return dtos;
  161. var line = context.ProductionLines?.Find(lineId);
  162. if (line == null)
  163. return dtos;
  164. var machines = context.Machines
  165. .Where(x => x.ProductionLineId == line.Id && x.IsInclusionLineStatistics == true)
  166. .OrderBy(o => o.ProductionLineOrder);
  167. foreach (var machine in machines)
  168. {
  169. var query = from mfr in context.Set<MachineFaultRecord>()
  170. join mfc in context.Set<MachineFaultComparison>()
  171. on mfr.FaultCode equals mfc.FaultCode
  172. where
  173. mfr.MachineId == machine.Id &&
  174. mfc.FaultTopic == machine.FaultTopic &&
  175. mfr.StartTime >= startTime &&
  176. mfr.StartTime < endTime
  177. orderby
  178. mfr.StartTime
  179. select new { mfr, mfc };
  180. foreach (var item in query)
  181. {
  182. if (!item.mfc.FaultInfo.Contains("安全门")
  183. && !item.mfc.FaultInfo.Contains("门禁")
  184. && !item.mfc.FaultInfo.Contains("提示上料")
  185. && !item.mfc.FaultInfo.Contains("提示卸料")
  186. && item.mfr.StartTime != null
  187. && item.mfr.EndTime != null)
  188. {
  189. LineAccumulatedFaultDto statisticDto = new LineAccumulatedFaultDto
  190. {
  191. MachineType = machine.Type,
  192. FaultCode = item.mfr.FaultCode.ToString(),
  193. FaultInfo = item.mfc.FaultInfo
  194. };
  195. var temp = dtos.Find(
  196. x => x.MachineType == statisticDto.MachineType
  197. && x.FaultInfo == statisticDto.FaultInfo);
  198. if (temp == null)
  199. {
  200. statisticDto.AccumulativeTotal = 1;
  201. if (item.mfr.EndTime.Value > endTime)
  202. statisticDto.AccumulativeTime = (endTime - item.mfr.StartTime.Value).TotalMinutes;
  203. else
  204. statisticDto.AccumulativeTime = (item.mfr.EndTime.Value - item.mfr.StartTime.Value).TotalMinutes;
  205. dtos.Add(statisticDto);
  206. }
  207. else
  208. {
  209. temp.AccumulativeTotal += 1;
  210. if (item.mfr.EndTime.Value > endTime)
  211. temp.AccumulativeTime += (endTime - item.mfr.StartTime.Value).TotalMinutes;
  212. else
  213. temp.AccumulativeTime += (item.mfr.EndTime.Value - item.mfr.StartTime.Value).TotalMinutes;
  214. }
  215. }
  216. }
  217. }
  218. List<LineFaultDto> keyInFaults = new List<LineFaultDto>();
  219. //keyInFaults.AddRange(GetKeyInLineFault(line.Floor, line.Line, startTime.ToString("yyyy-MM-dd"), "COG", endTime, "COG"));
  220. keyInFaults.AddRange(GetKeyInLineFault(line.Floor, line.Line, startTime.ToString("yyyy-MM-dd"), "BC机", endTime, "BC机"));
  221. foreach (var machine in machines)
  222. {
  223. switch (machine.Type)
  224. {
  225. case "AG + FPL":
  226. keyInFaults.AddRange(GetKeyInLineFault(line.Floor, line.Line, startTime.ToString("yyyy-MM-dd"), "TPA", endTime, machine.Type));
  227. keyInFaults.AddRange(GetKeyInLineFault(line.Floor, line.Line, startTime.ToString("yyyy-MM-dd"), "FPL", endTime, machine.Type));
  228. keyInFaults.AddRange(GetKeyInLineFault(line.Floor, line.Line, startTime.ToString("yyyy-MM-dd"), "FPL01", endTime, machine.Type));
  229. keyInFaults.AddRange(GetKeyInLineFault(line.Floor, line.Line, startTime.ToString("yyyy-MM-dd"), "FPL02", endTime, machine.Type));
  230. keyInFaults.AddRange(GetKeyInLineFault(line.Floor, line.Line, startTime.ToString("yyyy-MM-dd"), "FPL03", endTime, machine.Type));
  231. keyInFaults.AddRange(GetKeyInLineFault(line.Floor, line.Line, startTime.ToString("yyyy-MM-dd"), "FPL04", endTime, machine.Type));
  232. break;
  233. case "FPL":
  234. keyInFaults.AddRange(GetKeyInLineFault(line.Floor, line.Line, startTime.ToString("yyyy-MM-dd"), "TPA", endTime, machine.Type));
  235. keyInFaults.AddRange(GetKeyInLineFault(line.Floor, line.Line, startTime.ToString("yyyy-MM-dd"), "FPL", endTime, machine.Type));
  236. break;
  237. case "FOG":
  238. keyInFaults.AddRange(GetKeyInLineFault(line.Floor, line.Line, startTime.ToString("yyyy-MM-dd"), "FOG", endTime, machine.Type));
  239. break;
  240. case "AOI":
  241. keyInFaults.AddRange(GetKeyInLineFault(line.Floor, line.Line, startTime.ToString("yyyy-MM-dd"), "LOT2-AOI", endTime, machine.Type));
  242. break;
  243. default:
  244. keyInFaults.AddRange(GetKeyInLineFault(line.Floor, line.Line, startTime.ToString("yyyy-MM-dd"), machine.Type, endTime, machine.Type));
  245. break;
  246. }
  247. }
  248. foreach (var item in keyInFaults)
  249. {
  250. if (item.StartTime != null
  251. && item.EndTime != null
  252. && item.StartTime >= startTime
  253. && item.StartTime < endTime)
  254. {
  255. LineAccumulatedFaultDto statisticDto = new LineAccumulatedFaultDto
  256. {
  257. MachineType = item.MachineType,
  258. FaultCode = item.FaultCode.ToString(),
  259. FaultInfo = item.FaultInfo,
  260. DataSource = 1
  261. };
  262. var temp = dtos.Find(
  263. x => x.MachineType == statisticDto.MachineType
  264. && x.FaultInfo == statisticDto.FaultInfo
  265. && x.DataSource == statisticDto.DataSource);
  266. if (temp == null)
  267. {
  268. statisticDto.AccumulativeTotal = 1;
  269. statisticDto.AccumulativeTime = (item.EndTime.Value - item.StartTime).TotalMinutes;
  270. dtos.Add(statisticDto);
  271. }
  272. else
  273. {
  274. temp.AccumulativeTotal += 1;
  275. temp.AccumulativeTime += (item.EndTime.Value - item.StartTime).TotalMinutes;
  276. }
  277. }
  278. }
  279. dtos = dtos.Where(x => x.AccumulativeTime >= 1).OrderByDescending(o => o.AccumulativeTime).Take(10).ToList();
  280. return dtos;
  281. }
  282. private List<LineFaultDto> GetKeyInLineFault(int floor, int line, string date, string type, DateTime endTime, string machineType)
  283. {
  284. List<LineFaultDto> dtos = new List<LineFaultDto>();
  285. var fs = MesApiService.GetKeyInFaults(floor, line, date, type);
  286. foreach (var f in fs)
  287. {
  288. LineFaultDto dto = new LineFaultDto
  289. {
  290. MachineType = machineType,
  291. FaultCode = f.FaultCode,
  292. FaultInfo = f.FaultInfo,
  293. StartTime = f.StartTime
  294. };
  295. if (f.EndTime == null)
  296. dto.EndTime = endTime;
  297. else
  298. dto.EndTime = f.EndTime.Value;
  299. dto.Duration = (dto.EndTime.Value - dto.StartTime).TotalMinutes;
  300. dtos.Add(dto);
  301. }
  302. return dtos;
  303. }
  304. public IEnumerable<LineFaultDto> FaultSplitToHour(IEnumerable<LineFaultDto> lineFaultDtos)
  305. {
  306. IList<LineFaultDto> dtos = new List<LineFaultDto>();
  307. foreach (var lineFaultDto in lineFaultDtos)
  308. {
  309. if (lineFaultDto.EndTime == null)
  310. continue;
  311. IList<DateTime> dateTimes = new List<DateTime>
  312. {
  313. lineFaultDto.StartTime
  314. };
  315. DateTime d1 = lineFaultDto.StartTime;
  316. int j = 1;
  317. while (d1 < lineFaultDto.EndTime.Value)
  318. {
  319. d1 = lineFaultDto.StartTime.AddHours(j);
  320. DateTime d2 = Convert.ToDateTime($"{d1:yyyy-MM-dd HH}:00:00");
  321. j++;
  322. if (d2 < lineFaultDto.EndTime.Value)
  323. {
  324. dateTimes.Add(d2);
  325. }
  326. }
  327. dateTimes.Add(lineFaultDto.EndTime.Value);
  328. for (int i = 0; i < dateTimes.Count - 1; i++)
  329. {
  330. LineFaultDto dto = new LineFaultDto
  331. {
  332. MachineType = lineFaultDto.MachineType,
  333. FaultCode = lineFaultDto.FaultCode,
  334. FaultInfo = lineFaultDto.FaultInfo,
  335. DataSource = lineFaultDto.DataSource,
  336. StartTime = dateTimes[i],
  337. EndTime = dateTimes[i + 1]
  338. };
  339. dto.Duration = (dto.EndTime - dto.StartTime).Value.TotalMinutes;
  340. dtos.Add(dto);
  341. }
  342. }
  343. return dtos;
  344. }
  345. public IEnumerable<LineFaultDto> FilterDuplicateFaults(IEnumerable<LineFaultDto> lineFaultDtos)
  346. {
  347. IList<LineFaultDto> dtos = new List<LineFaultDto>();
  348. LineFaultDto? temp = null;
  349. foreach (var item in lineFaultDtos)
  350. {
  351. if (temp == null)
  352. {
  353. temp = item;
  354. }
  355. else
  356. {
  357. if (item.StartTime >= temp.StartTime && item.EndTime <= temp.EndTime)
  358. {
  359. continue;
  360. }
  361. temp = item;
  362. }
  363. dtos.Add(item);
  364. }
  365. return dtos;
  366. }
  367. public List<LineAccumulatedFaultDto> GetAccumulatedFaultTopByLine(string lineId, DateTime startTime, DateTime endTime)
  368. {
  369. List<LineAccumulatedFaultDto> dtos = new List<LineAccumulatedFaultDto>();
  370. if (!(_unitOfWork.GetDbContext() is ProductionLineContext context))
  371. return dtos;
  372. var line = context.ProductionLines?.Find(lineId);
  373. if (line == null)
  374. return dtos;
  375. var machines = context.Machines
  376. .Where(x => x.ProductionLineId == line.Id && x.IsInclusionLineStatistics == true)
  377. .OrderBy(o => o.ProductionLineOrder);
  378. foreach (var machine in machines)
  379. {
  380. var query = from mfr in context.Set<MachineFaultRecord>()
  381. join mfc in context.Set<MachineFaultComparison>()
  382. on mfr.FaultCode equals mfc.FaultCode
  383. where
  384. mfr.MachineId == machine.Id &&
  385. mfc.FaultTopic == machine.FaultTopic &&
  386. mfr.StartTime >= startTime &&
  387. mfr.StartTime < endTime
  388. orderby
  389. mfr.StartTime
  390. select new { mfr, mfc };
  391. foreach (var item in query)
  392. {
  393. if (!item.mfc.FaultInfo.Contains("安全门")
  394. && !item.mfc.FaultInfo.Contains("门禁")
  395. && !item.mfc.FaultInfo.Contains("提示上料")
  396. && !item.mfc.FaultInfo.Contains("提示卸料")
  397. && item.mfr.StartTime != null
  398. && item.mfr.EndTime != null)
  399. {
  400. LineAccumulatedFaultDto statisticDto = new LineAccumulatedFaultDto
  401. {
  402. MachineType = machine.Type,
  403. FaultCode = item.mfr.FaultCode.ToString(),
  404. FaultInfo = item.mfc.FaultInfo
  405. };
  406. var temp = dtos.Find(
  407. x => x.MachineType == statisticDto.MachineType
  408. && x.FaultInfo == statisticDto.FaultInfo);
  409. if (temp == null)
  410. {
  411. statisticDto.AccumulativeTotal = 1;
  412. if (item.mfr.EndTime.Value > endTime)
  413. statisticDto.AccumulativeTime = (endTime - item.mfr.StartTime.Value).TotalMinutes;
  414. else
  415. statisticDto.AccumulativeTime = (item.mfr.EndTime.Value - item.mfr.StartTime.Value).TotalMinutes;
  416. dtos.Add(statisticDto);
  417. }
  418. else
  419. {
  420. temp.AccumulativeTotal += 1;
  421. if (item.mfr.EndTime.Value > endTime)
  422. temp.AccumulativeTime += (endTime - item.mfr.StartTime.Value).TotalMinutes;
  423. else
  424. temp.AccumulativeTime += (item.mfr.EndTime.Value - item.mfr.StartTime.Value).TotalMinutes;
  425. }
  426. }
  427. }
  428. }
  429. List<LineFaultDto> keyInFaults = new List<LineFaultDto>();
  430. //keyInFaults.AddRange(GetKeyInLineFault(line.Floor, line.Line, startTime.ToString("yyyy-MM-dd"), "COG", endTime, "COG"));
  431. keyInFaults.AddRange(GetKeyInLineFault(line.Floor, line.Line, startTime.ToString("yyyy-MM-dd"), "BC机", endTime, "BC机"));
  432. foreach (var machine in machines)
  433. {
  434. switch (machine.Type)
  435. {
  436. case "AG + FPL":
  437. keyInFaults.AddRange(GetKeyInLineFault(line.Floor, line.Line, startTime.ToString("yyyy-MM-dd"), "TPA", endTime, machine.Type));
  438. keyInFaults.AddRange(GetKeyInLineFault(line.Floor, line.Line, startTime.ToString("yyyy-MM-dd"), "FPL", endTime, machine.Type));
  439. keyInFaults.AddRange(GetKeyInLineFault(line.Floor, line.Line, startTime.ToString("yyyy-MM-dd"), "FPL01", endTime, machine.Type));
  440. keyInFaults.AddRange(GetKeyInLineFault(line.Floor, line.Line, startTime.ToString("yyyy-MM-dd"), "FPL02", endTime, machine.Type));
  441. keyInFaults.AddRange(GetKeyInLineFault(line.Floor, line.Line, startTime.ToString("yyyy-MM-dd"), "FPL03", endTime, machine.Type));
  442. keyInFaults.AddRange(GetKeyInLineFault(line.Floor, line.Line, startTime.ToString("yyyy-MM-dd"), "FPL04", endTime, machine.Type));
  443. break;
  444. case "FPL":
  445. keyInFaults.AddRange(GetKeyInLineFault(line.Floor, line.Line, startTime.ToString("yyyy-MM-dd"), "TPA", endTime, machine.Type));
  446. keyInFaults.AddRange(GetKeyInLineFault(line.Floor, line.Line, startTime.ToString("yyyy-MM-dd"), "FPL", endTime, machine.Type));
  447. break;
  448. case "FOG":
  449. keyInFaults.AddRange(GetKeyInLineFault(line.Floor, line.Line, startTime.ToString("yyyy-MM-dd"), "FOG", endTime, machine.Type));
  450. break;
  451. case "AOI":
  452. keyInFaults.AddRange(GetKeyInLineFault(line.Floor, line.Line, startTime.ToString("yyyy-MM-dd"), "LOT2-AOI", endTime, machine.Type));
  453. break;
  454. default:
  455. keyInFaults.AddRange(GetKeyInLineFault(line.Floor, line.Line, startTime.ToString("yyyy-MM-dd"), machine.Type, endTime, machine.Type));
  456. break;
  457. }
  458. }
  459. foreach (var item in keyInFaults)
  460. {
  461. if (item.StartTime != null
  462. && item.EndTime != null
  463. && item.StartTime >= startTime
  464. && item.StartTime < endTime)
  465. {
  466. LineAccumulatedFaultDto statisticDto = new LineAccumulatedFaultDto
  467. {
  468. MachineType = item.MachineType,
  469. FaultCode = item.FaultCode.ToString(),
  470. FaultInfo = item.FaultInfo,
  471. DataSource = 1
  472. };
  473. var temp = dtos.Find(
  474. x => x.MachineType == statisticDto.MachineType
  475. && x.FaultInfo == statisticDto.FaultInfo
  476. && x.DataSource == statisticDto.DataSource);
  477. if (temp == null)
  478. {
  479. statisticDto.AccumulativeTotal = 1;
  480. statisticDto.AccumulativeTime = (item.EndTime.Value - item.StartTime).TotalMinutes;
  481. dtos.Add(statisticDto);
  482. }
  483. else
  484. {
  485. temp.AccumulativeTotal += 1;
  486. temp.AccumulativeTime += (item.EndTime.Value - item.StartTime).TotalMinutes;
  487. }
  488. }
  489. }
  490. dtos = dtos.Where(x => x.AccumulativeTime >= 1).OrderByDescending(o => o.AccumulativeTime).ToList();
  491. return dtos;
  492. }
  493. public List<MachineFaultRecordDto> GetMachineFaultsByTop10(string machineId, DateTime day, string keywords)
  494. {
  495. List<MachineFaultRecordDto> dtos = new List<MachineFaultRecordDto>();
  496. var machine = _unitOfWork.MachineRepository.GetById(machineId);
  497. if (machine != null)
  498. {
  499. var faults = _unitOfWork.MachineFaultRecordRepository.GetList(
  500. x => x.MachineId == machineId);
  501. DateTime startTime = day.AddHours(+8);
  502. DateTime endTime = day.AddDays(+1).AddHours(+8);
  503. var lst = _unitOfWork.MachineFaultRecordRepository
  504. .GetList(x =>
  505. x.MachineId == machineId
  506. && x.StartTime >= startTime
  507. && x.StartTime < endTime)
  508. .OrderBy(o => o.StartTime)
  509. .ToList();
  510. dtos = _mapper.Map<List<MachineFaultRecordDto>>(lst);
  511. foreach (var faultRecord in dtos)
  512. {
  513. faultRecord.MachineName = machine.Name;
  514. MachineFaultComparison machineFaultComparison = _unitOfWork.MachineFaultComparisonRepository
  515. .FirstOrDefault(x => x.FaultTopic == machine.FaultTopic && x.FaultCode == faultRecord.FaultCode);
  516. if (machineFaultComparison != null)
  517. {
  518. faultRecord.FaultLevel = (int)machineFaultComparison.FaultLevel;
  519. faultRecord.FaultInfo = machineFaultComparison.FaultInfo;
  520. faultRecord.FaultCategory = machineFaultComparison.FaultCategory;
  521. }
  522. else
  523. {
  524. faultRecord.FaultLevel = 0;
  525. faultRecord.FaultInfo = "未知故障";
  526. faultRecord.FaultCategory = "未知";
  527. }
  528. if (faultRecord.StartTime != null && faultRecord.EndTime != null)
  529. faultRecord.Duration = (faultRecord.EndTime.Value - faultRecord.StartTime.Value).TotalMinutes;
  530. else
  531. faultRecord.Duration = 0;
  532. }
  533. if (keywords != null)
  534. {
  535. string[] strs = keywords.Split(',');
  536. dtos = dtos.Where(x =>
  537. strs.All(s => !x.FaultInfo.Contains(s))).ToList();
  538. }
  539. //dtos = dtos.Where(x =>
  540. // !x.FaultInfo.Contains(x.FaultInfo)).ToList();
  541. dtos = CheckSameTimeFault(dtos);
  542. dtos = dtos.OrderByDescending(o => o.Duration).Take(10).ToList();
  543. }
  544. return dtos;
  545. }
  546. private List<MachineFaultRecordDto> CheckSameTimeFault(List<MachineFaultRecordDto> lst)
  547. {
  548. List<MachineFaultRecordDto> faults = new List<MachineFaultRecordDto>();
  549. MachineFaultRecordDto lastFault = null;
  550. foreach (var l in lst)
  551. {
  552. if (lastFault != null)
  553. {
  554. if (l.EndTime != null && (l.StartTime < lastFault.StartTime || l.EndTime > lastFault.EndTime))
  555. {
  556. faults.Add(l);
  557. lastFault = l;
  558. }
  559. }
  560. else
  561. {
  562. faults.Add(l);
  563. lastFault = l;
  564. }
  565. }
  566. return faults;
  567. }
  568. public List<FaultFrequency> GetFaultFrequencyTop10(string machineId, DateTime startTime, DateTime endTime, string keywords)
  569. {
  570. List<FaultFrequency> faultFrequencies = new List<FaultFrequency>();
  571. var machine = _unitOfWork.MachineRepository.FirstOrDefault(x => x.Id == machineId);
  572. var faults = _unitOfWork.MachineFaultRecordRepository.GetList(
  573. x => x.MachineId == machineId
  574. && x.StartTime >= startTime
  575. && x.StartTime < endTime
  576. && x.MachineState == 2).ToList();
  577. var mfcs = _unitOfWork.MachineFaultComparisonRepository.GetList(
  578. x => x.FaultTopic == machine.FaultTopic).ToList();
  579. foreach (var fault in faults)
  580. {
  581. var f = faultFrequencies.Find(x => x.FaultCode == fault.FaultCode);
  582. if (f == null)
  583. {
  584. FaultFrequency m = new FaultFrequency()
  585. {
  586. FaultCode = fault.FaultCode.Value,
  587. Count = fault.TriggerNumber.Value
  588. };
  589. faultFrequencies.Add(m);
  590. }
  591. else
  592. {
  593. f.Count += fault.TriggerNumber.Value;
  594. }
  595. }
  596. foreach (var item in faultFrequencies)
  597. {
  598. var mfc = mfcs.FirstOrDefault(x => x.FaultCode == item.FaultCode);
  599. if (mfc == null)
  600. {
  601. item.FaultInfo = item.FaultCode.ToString() + " " + "未知故障!";
  602. }
  603. else
  604. {
  605. item.FaultInfo = item.FaultCode.ToString() + " " + mfc.FaultInfo;
  606. }
  607. }
  608. if (keywords != null)
  609. {
  610. string[] strs = keywords.Split(',');
  611. faultFrequencies = faultFrequencies.Where(x =>
  612. strs.All(s => !x.FaultInfo.Contains(s))).ToList();
  613. }
  614. return faultFrequencies.OrderByDescending(o => o.Count).Take(10).ToList();
  615. }
  616. public List<FaultFrequencyByShift> GetFaultFrequencyTop10ByShift(string machineId, DateTime startTime, DateTime endTime, string keywords)
  617. {
  618. List<FaultFrequencyByShift> faultFrequencies = new List<FaultFrequencyByShift>();
  619. var machine = _unitOfWork.MachineRepository.FirstOrDefault(x => x.Id == machineId);
  620. var faults = _unitOfWork.MachineFaultRecordRepository.GetList(
  621. x => x.MachineId == machineId
  622. && x.StartTime >= startTime
  623. && x.StartTime < endTime
  624. && x.MachineState == 2).ToList();
  625. var mfcs = _unitOfWork.MachineFaultComparisonRepository.GetList(
  626. x => x.FaultTopic == machine.FaultTopic).ToList();
  627. List<FactoryShift> shifts = GetShifts(startTime, endTime);
  628. foreach (var shift in shifts)
  629. {
  630. FaultFrequencyByShift faultFrequencyByShift = GetFaultFrequency(
  631. faults, mfcs, keywords, shift);
  632. faultFrequencies.Add(faultFrequencyByShift);
  633. }
  634. return faultFrequencies;
  635. }
  636. private FaultFrequencyByShift GetFaultFrequency(List<MachineFaultRecord> faults, List<MachineFaultComparison> mfcs, string keywords, FactoryShift shift)
  637. {
  638. FaultFrequencyByShift faultFrequencyByShift = new FaultFrequencyByShift()
  639. {
  640. Shift = $"{shift.Date} {shift.Shifts}"
  641. };
  642. var fs = faults.Where(x => x.StartTime >= shift.StartTime && x.StartTime < shift.EndTime);
  643. foreach (var f in fs)
  644. {
  645. var temp = faultFrequencyByShift.FaultFrequencies.Find(x => x.FaultCode == f.FaultCode);
  646. if (temp != null)
  647. {
  648. if (f.TriggerNumber != null)
  649. {
  650. temp.Count += f.TriggerNumber.Value;
  651. }
  652. }
  653. else
  654. {
  655. FaultFrequency faultFrequency = new FaultFrequency
  656. {
  657. FaultCode = f.FaultCode != null ? f.FaultCode.Value : 0,
  658. Count = f.TriggerNumber != null ? f.TriggerNumber.Value : 1
  659. };
  660. faultFrequencyByShift.FaultFrequencies.Add(faultFrequency);
  661. }
  662. }
  663. faultFrequencyByShift.FaultFrequencies = faultFrequencyByShift.FaultFrequencies.OrderBy(x => x.Count).ToList();
  664. foreach (var item in faultFrequencyByShift.FaultFrequencies)
  665. {
  666. var mfc = mfcs.FirstOrDefault(x => x.FaultCode == item.FaultCode);
  667. if (mfc != null)
  668. item.FaultInfo = mfc.FaultInfo;
  669. else
  670. item.FaultInfo = "未知故障";
  671. }
  672. if (!string.IsNullOrEmpty(keywords))
  673. {
  674. string[] strs = keywords.Split(',');
  675. faultFrequencyByShift.FaultFrequencies = faultFrequencyByShift.FaultFrequencies.Where(x =>
  676. strs.All(s => !x.FaultInfo.Contains(s))).ToList();
  677. }
  678. // faultFrequencyByShift.FaultFrequencies = faultFrequencyByShift.FaultFrequencies.OrderByDescending(o => o.Count).Take(10).ToList();
  679. return faultFrequencyByShift;
  680. }
  681. private List<FactoryShift> GetShifts(DateTime startTime, DateTime endTime)
  682. {
  683. List<FactoryShift> shifts = new List<FactoryShift>();
  684. for (DateTime i = startTime; i <= endTime; i = i.AddDays(1))
  685. {
  686. FactoryShift shift = new FactoryShift
  687. {
  688. Date = i.ToString("yyyy-MM-dd"),
  689. Shifts = "白班",
  690. StartTime = i,
  691. EndTime = i.AddHours(12)
  692. };
  693. shifts.Add(shift);
  694. FactoryShift shift1 = new FactoryShift
  695. {
  696. Date = i.ToString("yyyy-MM-dd"),
  697. Shifts = "夜班",
  698. StartTime = i.AddHours(12),
  699. EndTime = i.AddHours(24)
  700. };
  701. shifts.Add(shift1);
  702. }
  703. return shifts;
  704. }
  705. }
  706. }