MonthOverview.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. using NPOI.HSSF.Record.Chart;
  2. using NPOI.OpenXmlFormats.Dml;
  3. using NPOI.SS.Formula.Functions;
  4. using OfficeOpenXml.VBA;
  5. using Org.BouncyCastle.Crypto.EC;
  6. using ProductionLineMonitor.Core.Models;
  7. using ProductionLineMonitor.Core.Utils;
  8. using ProductionLineMonitor.Web.Services;
  9. using ProductionLineMonitor.Web.Services.LineService;
  10. using System;
  11. using System.Collections;
  12. using System.Collections.Generic;
  13. using System.Linq;
  14. using System.Text;
  15. using static OfficeOpenXml.ExcelErrorValue;
  16. namespace ProductionLineMonitor.Application.Services.HomeService.Dtos
  17. {
  18. public class MonthOverview
  19. {
  20. public MonthOverview(
  21. string mark, int year, int month, string moduleType,
  22. IList<ProductionLine> lst)
  23. {
  24. _year = year;
  25. _month = month;
  26. _rangeEndDate = Convert.ToDateTime($"{_year}-{_month}-20 08:00:00");
  27. _startDate = _rangeEndDate.AddMonths(-1).AddDays(+1);
  28. DateTime _date = DateTime.Now;
  29. if (_date <= _rangeEndDate)
  30. _endDate = Convert.ToDateTime($"{_date.AddDays(-1):yyyy-MM-dd} 08:00:00");
  31. else
  32. _endDate = _rangeEndDate;
  33. Dates = DateTimeHelper.GetDateString(_startDate, _rangeEndDate).ToArray();
  34. _planCapacities = new int[Dates.Length];
  35. _capacities = new int[Dates.Length];
  36. NewDates = new string[Dates.Length];
  37. PlanCapacities = new string[Dates.Length];
  38. Capacities = new string[Dates.Length];
  39. Differences = new string[Dates.Length];
  40. for (int i = 0; i < lst.Count; i++)
  41. {
  42. var plans = MesApiService.GetProductionPlanByTimelotV1(lst[i].Floor, lst[i].Line,
  43. _startDate.ToString("yyyy-MM-dd"), _rangeEndDate.ToString("yyyy-MM-dd"))
  44. .Where(x => string.IsNullOrEmpty(moduleType) != true && x.ModuleType.Length >= 10 && x.ModuleType[..10] == moduleType);
  45. var outs = MesApiService.GetOutPutPerHours(lst[i].HourDataTopic,
  46. _startDate.ToString("yyyy-MM-dd"), _endDate.ToString("yyyy-MM-dd"))
  47. .Where(x => string.IsNullOrEmpty(moduleType) != true && x.ModuleType.Length >= 10 && x.ModuleType[..10] == moduleType);
  48. for (int j = 0; j < Dates.Length; j++)
  49. {
  50. NewDates[j] = $"{Dates[j].Substring(8, 2)}";
  51. _planCapacities[j] += plans.Where(x => x.ShiftDate == Dates[j]).Sum(x => x.PlanCapacity);
  52. if (_planCapacities[j] == 0)
  53. PlanCapacities[j] = "";
  54. else
  55. PlanCapacities[j] = _planCapacities[j].ToString();
  56. _capacities[j] += GetOuts(Dates[j], outs).Sum(x => x.OutPut);
  57. if (_capacities[j] == 0)
  58. Capacities[j] = "";
  59. else
  60. Capacities[j] = _capacities[j].ToString();
  61. if (_planCapacities[j] == 0 || _capacities[j] == 0)
  62. {
  63. Differences[j] = "";
  64. }
  65. else
  66. {
  67. int d = _planCapacities[j] - _capacities[j];
  68. Differences[j] = d > 0 ? d.ToString() : "";
  69. }
  70. }
  71. }
  72. }
  73. public MonthOverview(int year, int month, string moduleType,
  74. IList<ProductionLine> lst, IList<string> moduleTypes, int line)
  75. {
  76. _year = year;
  77. _month = month;
  78. _rangeEndDate = Convert.ToDateTime($"{_year}-{_month}-20 08:00:00");
  79. _startDate = _rangeEndDate.AddMonths(-1).AddDays(+1);
  80. DateTime _date = DateTime.Now;
  81. if (_date < _rangeEndDate.AddDays(1))
  82. _endDate = Convert.ToDateTime($"{_date.AddDays(-1):yyyy-MM-dd} 08:00:00");
  83. else
  84. _endDate = _rangeEndDate;
  85. Dates = DateTimeHelper.GetDateString(_startDate, _rangeEndDate).ToArray();
  86. _planCapacities = new int[Dates.Length];
  87. _capacities = new int[Dates.Length];
  88. NewDates = new string[Dates.Length];
  89. PlanCapacities = new string[Dates.Length];
  90. Capacities = new string[Dates.Length];
  91. Differences = new string[Dates.Length];
  92. AddX();
  93. AddLinePlans(line, lst, moduleType, moduleTypes);
  94. _ps = new List<ProductionPlanDtoV1>();
  95. _os = new List<MachineDayOutPutPerHour>();
  96. GetPsOrOsByLine(line, lst);
  97. GetPsOrOsByModuleType(moduleType, moduleTypes);
  98. AddY();
  99. Difference = _os.Sum(x => x.OutPut) -
  100. _ps.Where(x => Convert.ToDateTime($"{x.ShiftDate} 08:00:00") <= _endDate)
  101. .Sum(x => x.PlanCapacity);
  102. AddKeyInInfos(line, lst);
  103. AddDifferenceInfosTop5();
  104. }
  105. private void AddLinePlans(int line, IList<ProductionLine> lines, string moduleType, IList<string> moduleTypes)
  106. {
  107. List<ProductionPlanDtoV1> ps = new List<ProductionPlanDtoV1>();
  108. if (moduleType != "ALL" && line != 0)
  109. {
  110. var l = lines.FirstOrDefault(x => x.Line == line);
  111. if (l == null)
  112. {
  113. return;
  114. }
  115. ps = MesApiService.GetProductionPlanByTimelotV1(l.Floor, l.Line,
  116. _startDate.ToString("yyyy-MM-dd"), _rangeEndDate.ToString("yyyy-MM-dd"));
  117. ps = ps.Where(x => string.IsNullOrEmpty(moduleType) != true
  118. && x.ModuleType.Length >= 10
  119. && x.ModuleType == moduleType).ToList();
  120. LinePlans.Add(new LinePlan(Dates, ps, l.Line));
  121. return;
  122. }
  123. if (moduleType != "ALL" && line == 0)
  124. {
  125. for (int i = 0; i < lines.Count; i++)
  126. {
  127. ps = MesApiService.GetProductionPlanByTimelotV1(lines[i].Floor, lines[i].Line,
  128. _startDate.ToString("yyyy-MM-dd"), _rangeEndDate.ToString("yyyy-MM-dd"));
  129. ps = ps.Where(x => string.IsNullOrEmpty(moduleType) != true
  130. && x.ModuleType.Length >= 10
  131. && x.ModuleType == moduleType).ToList();
  132. LinePlans.Add(new LinePlan(Dates, ps, lines[i].Line));
  133. }
  134. return;
  135. }
  136. if (moduleType == "ALL" && line == 0)
  137. {
  138. for (int i = 0; i < lines.Count; i++)
  139. {
  140. foreach (var item in moduleTypes)
  141. {
  142. ps = MesApiService.GetProductionPlanByTimelotV1(lines[i].Floor, lines[i].Line,
  143. _startDate.ToString("yyyy-MM-dd"), _rangeEndDate.ToString("yyyy-MM-dd"));
  144. ps = ps.Where(x => string.IsNullOrEmpty(item) != true
  145. && x.ModuleType.Length >= 10
  146. && x.ModuleType == item).ToList();
  147. LinePlans.Add(new LinePlan(Dates, ps, lines[i].Line));
  148. }
  149. }
  150. return;
  151. }
  152. if (moduleType == "ALL" && line != 0)
  153. {
  154. var l1 = lines.FirstOrDefault(x => x.Line == line);
  155. if (l1 == null)
  156. {
  157. return;
  158. }
  159. foreach (var item in moduleTypes)
  160. {
  161. ps = MesApiService.GetProductionPlanByTimelotV1(l1.Floor, l1.Line,
  162. _startDate.ToString("yyyy-MM-dd"), _rangeEndDate.ToString("yyyy-MM-dd"));
  163. ps = ps.Where(x => string.IsNullOrEmpty(item) != true
  164. && x.ModuleType.Length >= 10
  165. && x.ModuleType == item).ToList();
  166. LinePlans.Add(new LinePlan(Dates, ps, l1.Line));
  167. }
  168. return;
  169. }
  170. }
  171. private void AddKeyInInfos(int line, IList<ProductionLine> lines)
  172. {
  173. for (int i = 0; i < Differences.Length; i++)
  174. {
  175. if (Differences[i] != "")
  176. {
  177. DateTime start = Convert.ToDateTime($"{Dates[i]} 8:00:00");
  178. if (line == 0)
  179. {
  180. foreach (var item in lines)
  181. {
  182. KeyInInfos.AddRange(MesApiService.GetAlarmByKeyIn(item.Floor, item.Line,
  183. start, start.AddDays(1)));
  184. }
  185. }
  186. else
  187. {
  188. var l = lines.FirstOrDefault(x => x.Line == line);
  189. if (l != null)
  190. {
  191. KeyInInfos.AddRange(MesApiService.GetAlarmByKeyIn(l.Floor, l.Line,
  192. start, start.AddDays(1)));
  193. }
  194. }
  195. }
  196. }
  197. }
  198. private void GetPsOrOsByLine(int line, IList<ProductionLine> lines)
  199. {
  200. if (line == 0)
  201. {
  202. for (int i = 0; i < lines.Count(); i++)
  203. {
  204. List<ProductionPlanDtoV1> ps = MesApiService.GetProductionPlanByTimelotV1(lines[i].Floor, lines[i].Line,
  205. _startDate.ToString("yyyy-MM-dd"), _rangeEndDate.ToString("yyyy-MM-dd"));
  206. _ps.AddRange(ps);
  207. _os.AddRange(MesApiService.GetOutPutPerHours(lines[i].HourDataTopic,
  208. _startDate.ToString("yyyy-MM-dd"), _endDate.ToString("yyyy-MM-dd")));
  209. }
  210. return;
  211. }
  212. var l = lines.FirstOrDefault(x => x.Line == line);
  213. if (l == null)
  214. {
  215. return;
  216. }
  217. List<ProductionPlanDtoV1> ps1 = MesApiService.GetProductionPlanByTimelotV1(l.Floor, l.Line,
  218. _startDate.ToString("yyyy-MM-dd"), _rangeEndDate.ToString("yyyy-MM-dd"));
  219. _ps.AddRange(ps1);
  220. _os.AddRange(MesApiService.GetOutPutPerHours(l.HourDataTopic,
  221. _startDate.ToString("yyyy-MM-dd"), _endDate.ToString("yyyy-MM-dd")));
  222. }
  223. private void GetPsOrOsByModuleType(string moduleType, IList<string> moduleTypes)
  224. {
  225. if (moduleType != "ALL")
  226. {
  227. _ps = _ps.Where(x => string.IsNullOrEmpty(moduleType) != true
  228. && x.ModuleType.Length >= 10
  229. && x.ModuleType[..moduleType.Length] == moduleType).ToList();
  230. _os = _os.Where(x => string.IsNullOrEmpty(moduleType) != true
  231. && x.ModuleType.Length >= 10
  232. && x.ModuleType[..moduleType.Length] == moduleType).ToList();
  233. }
  234. else
  235. {
  236. List<ProductionPlanDtoV1> p1 = _ps.ToList();
  237. List<MachineDayOutPutPerHour> o1 = _os.ToList();
  238. _ps.Clear();
  239. _os.Clear();
  240. foreach (var item in moduleTypes)
  241. {
  242. _ps.AddRange(p1.Where(x => string.IsNullOrEmpty(item) != true
  243. && x.ModuleType.Length >= 10
  244. && x.ModuleType == item).ToList());
  245. _os.AddRange(o1.Where(x => string.IsNullOrEmpty(item) != true
  246. && x.ModuleType.Length >= 10
  247. && x.ModuleType.Length >= item.Length
  248. && x.ModuleType[..item.Length] == item).ToList());
  249. }
  250. }
  251. }
  252. private void AddX()
  253. {
  254. for (int j = 0; j < Dates.Length; j++)
  255. {
  256. NewDates[j] = $"{Dates[j].Substring(8, 2)}";
  257. DateTime d = Convert.ToDateTime(Dates[j]);
  258. if (d.DayOfWeek == DayOfWeek.Sunday)
  259. {
  260. NewDates[j] += "\n周日";
  261. }
  262. }
  263. }
  264. private void AddY()
  265. {
  266. for (int j = 0; j < Dates.Length; j++)
  267. {
  268. int plan = _ps.Where(x => x.ShiftDate == Dates[j]).Sum(x => x.PlanCapacity);
  269. _planCapacities[j] += plan;
  270. if (_planCapacities[j] == 0)
  271. PlanCapacities[j] = "";
  272. else
  273. PlanCapacities[j] = _planCapacities[j].ToString();
  274. _capacities[j] += GetOuts(Dates[j], _os).Sum(x => x.OutPut);
  275. if (_capacities[j] == 0)
  276. Capacities[j] = "";
  277. else
  278. Capacities[j] = _capacities[j].ToString();
  279. if (_planCapacities[j] == 0 || _capacities[j] == 0)
  280. {
  281. Differences[j] = "";
  282. }
  283. else
  284. {
  285. int d = _planCapacities[j] - _capacities[j];
  286. Differences[j] = d > 0 ? d.ToString() : "";
  287. }
  288. }
  289. }
  290. private void AddDifferenceInfosTop5()
  291. {
  292. List<DifferenceInfo> differenceInfos = new List<DifferenceInfo>();
  293. foreach (var item in KeyInInfos)
  294. {
  295. var d = differenceInfos.FirstOrDefault(x => x.Description == item.Description);
  296. if (d == null)
  297. {
  298. differenceInfos.Add(new DifferenceInfo()
  299. {
  300. Description = item.Description,
  301. Frequency = 1,
  302. DifferenceTime = item.AffectTime,
  303. TypeName = item.KeyInTypeName
  304. });
  305. }
  306. else
  307. {
  308. d.Frequency += 1;
  309. d.DifferenceTime += item.AffectTime;
  310. }
  311. }
  312. DifferenceInfos = differenceInfos.OrderByDescending(o => o.DifferenceTime).Take(5).ToList();
  313. }
  314. private IEnumerable<MachineDayOutPutPerHour> GetOuts(string date, IEnumerable<MachineDayOutPutPerHour> HourOuts)
  315. {
  316. DateTime sTime = Convert.ToDateTime($"{date} 08:00:00");
  317. DateTime eTime = sTime.AddHours(24);
  318. return HourOuts.Where(x => x.DataTime >= sTime && x.DataTime < eTime);
  319. }
  320. private readonly DateTime _startDate;
  321. private readonly DateTime _endDate;
  322. private readonly DateTime _rangeEndDate;
  323. private readonly int _year;
  324. private readonly int _month;
  325. private readonly int[] _planCapacities;
  326. private readonly int[] _capacities;
  327. private List<ProductionPlanDtoV1> _ps;
  328. private List<MachineDayOutPutPerHour> _os;
  329. public string[] Dates { get; set; }
  330. public string[] NewDates { get; set; }
  331. public string[] PlanCapacities { get; set; }
  332. public string[] Capacities { get; set; }
  333. public string[] Differences { get; set; }
  334. public int Difference { get; set; }
  335. public List<KeyInInfo> KeyInInfos { get; set; } = new List<KeyInInfo>();
  336. public List<DifferenceInfo> DifferenceInfos { get; set; } = new List<DifferenceInfo>();
  337. public List<LinePlan> LinePlans { get; set; } = new List<LinePlan>();
  338. }
  339. public class DifferenceInfo
  340. {
  341. public string TypeName { get; set; } = string.Empty;
  342. public string Description { get; set; } = string.Empty;
  343. public string MachineType { get; set; } = string.Empty;
  344. public int Frequency { get; set; }
  345. public int DifferenceTime { get; set; }
  346. }
  347. public class LinePlan
  348. {
  349. public LinePlan(string[] dates, List<ProductionPlanDtoV1> plans, int line)
  350. {
  351. Line = line;
  352. PlanDates = new string[dates.Length];
  353. PlanCapacity = 0;
  354. int[] values = new int[dates.Length + 2];
  355. for (int i = 1; i <= dates.Length; i++)
  356. {
  357. int plan = plans.Where(x => x.ShiftDate == dates[i - 1]).Sum(x => x.PlanCapacity);
  358. PlanCapacity += plan;
  359. values[i] = plan == 0 ? 0 : 1;
  360. }
  361. // 平滑过渡周日无排程
  362. for (int i = 0; i < values.Length; i++)
  363. {
  364. if (values[i] == 1)
  365. {
  366. continue;
  367. }
  368. if (i - 1 < 0 || i + 1 >= values.Length)
  369. {
  370. continue;
  371. }
  372. if (values[i - 1] == 1 && values[i + 1] == 1)
  373. {
  374. values[i] = 1;
  375. }
  376. }
  377. for (int i = 1; i < values.Length - 1; i++)
  378. {
  379. if (values[i] == 1)
  380. {
  381. PlanDates[i - 1] = "1";
  382. }
  383. else
  384. {
  385. PlanDates[i - 1] = "";
  386. }
  387. }
  388. Start = Array.IndexOf(PlanDates, "1");
  389. }
  390. public int Line { get; set; }
  391. public int PlanCapacity { get; set; }
  392. public string[] PlanDates { get; set; }
  393. public int Start { get; set; }
  394. }
  395. }