LineMonthData.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. using ProductionLineMonitor.Core.Models;
  2. using ProductionLineMonitor.Core.Utils;
  3. using ProductionLineMonitor.Web.Services;
  4. using ProductionLineMonitor.Web.Services.LineService;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. namespace ProductionLineMonitor.Application.Services.LineService.Dtos
  9. {
  10. public class LineMonthData
  11. {
  12. public LineMonthData(ProductionLine line, DateTime date, int moduleTypeLen, string moduleType)
  13. {
  14. DateTimeHelper.GetStartEndTime(date, out _startDate, out _endDate);
  15. Plans = MesApiService.GetProductionPlanByTimelotV1(
  16. line.Floor, line.Line,
  17. _startDate.ToString("yyyy-MM-dd"), _endDate.ToString("yyyy-MM-dd"));
  18. // 兼容 8/20日之前机种为8码数据
  19. if (date <= Convert.ToDateTime("2023-08-20 08:00:00"))
  20. {
  21. foreach (var item in Plans)
  22. {
  23. if (item.ModuleType != "" && item.ModuleType.Length > 10)
  24. {
  25. item.ModuleType = item.ModuleType[..10];
  26. }
  27. }
  28. }
  29. HourOuts = MesApiService.GetOutPutPerHours(
  30. line.HourDataTopic,
  31. _startDate.ToString("yyyy-MM-dd"), _endDate.ToString("yyyy-MM-dd"));
  32. Dates = DateTimeHelper.GetDateString(_startDate, _endDate).ToArray();
  33. NewDates = new string[Dates.Length];
  34. PlanCapacities = new string[Dates.Length];
  35. Capacities = new string[Dates.Length];
  36. Differences = new string[Dates.Length];
  37. AccumulatedDifferences = new string[Dates.Length];
  38. ModuleTypes = Plans
  39. .Where(x => x.ModuleType != "")
  40. .Select(x => x.ModuleType)
  41. .Distinct().ToArray();
  42. if (moduleType != "ALL")
  43. {
  44. Plans = Plans.Where(
  45. x => x.ModuleType != "" &&
  46. x.ModuleType.Length >= moduleType.Length &&
  47. x.ModuleType[..moduleType.Length] == moduleType)
  48. .OrderBy(o => o.ShiftDate)
  49. .ToList();
  50. HourOuts = HourOuts.Where(
  51. x => x.ModuleType != "" &&
  52. x.ModuleType.Length >= moduleType.Length &&
  53. x.ModuleType[..moduleType.Length] == moduleType)
  54. .ToList();
  55. var ts = Plans.OrderBy(o => o.ShiftDate)
  56. .ToList();
  57. var sValues = GetPlanGanttChart(ts);
  58. foreach (var item in sValues)
  59. {
  60. Serie serie = new Serie(Dates.Length)
  61. {
  62. Name = moduleType,
  63. Start = item.Start,
  64. End = item.End,
  65. Values = item.Values,
  66. PlanCapacities = item.Value,
  67. StartDate = item.StartDate,
  68. EndDate = item.EndDate
  69. };
  70. Series.Add(serie);
  71. }
  72. }
  73. else
  74. {
  75. foreach (var module in ModuleTypes)
  76. {
  77. var ts = Plans.Where(x => x.ModuleType == module)
  78. .OrderBy(o => o.ShiftDate)
  79. .ToList();
  80. if (ts.Count() == 0)
  81. {
  82. continue;
  83. }
  84. var sValues = GetPlanGanttChart(ts);
  85. foreach (var item in sValues)
  86. {
  87. Serie serie = new Serie(Dates.Length)
  88. {
  89. Name = module,
  90. Start = item.Start,
  91. End = item.End,
  92. Values = item.Values,
  93. PlanCapacities = item.Value,
  94. StartDate = item.StartDate,
  95. EndDate = item.EndDate
  96. };
  97. Series.Add(serie);
  98. }
  99. }
  100. }
  101. Series = Series.OrderBy(o => o.Start).ToList();
  102. for (int i = 0; i < Dates.Length; i++)
  103. {
  104. NewDates[i] = $"{Dates[i].Substring(8, 2)}";
  105. DateTime day = Convert.ToDateTime(Dates[i]);
  106. if (day.DayOfWeek == DayOfWeek.Sunday)
  107. {
  108. NewDates[i] += "\n周日";
  109. }
  110. int v1 = Plans.Where(x => x.ShiftDate == Dates[i]).Sum(x => x.PlanCapacity);
  111. PlanCapacities[i] = v1 == 0 ? "" : v1.ToString();
  112. DateTime d = Convert.ToDateTime(Dates[i]);
  113. if (d < DateTime.Now.AddDays(-1))
  114. {
  115. var outs = GetOuts(Dates[i]);
  116. int v2 = outs.Sum(x => x.OutPut);
  117. if (v1 == 0 && v2 == 0)
  118. Capacities[i] = "";
  119. else
  120. Capacities[i] = v2.ToString();
  121. int v3 = v2 - v1;
  122. if (v3 < 0)
  123. Differences[i] = Math.Abs(v3).ToString();
  124. else
  125. Differences[i] = "";
  126. if (i == 0)
  127. AccumulatedDifferences[i] = v3.ToString();
  128. else
  129. AccumulatedDifferences[i] = (Convert.ToInt32(AccumulatedDifferences[i - 1]) + v3).ToString();
  130. AccumulatedDifference = Convert.ToInt32(AccumulatedDifferences[i]);
  131. }
  132. }
  133. }
  134. private List<Position> GetPlanGanttChart(List<HomeService.Dtos.ProductionPlanDtoV1> ts)
  135. {
  136. int[] values = new int[Dates.Length + 2];
  137. int[] values1 = new int[Dates.Length + 2];
  138. // 生产:1 不生产:0
  139. for (int i = 1; i <= Dates.Length; i++)
  140. {
  141. int v1 = ts.Where(x => x.ShiftDate == Dates[i - 1]).Sum(x => x.PlanCapacity);
  142. values[i] = v1 == 0 ? 0 : 1;
  143. values1[i] = v1;
  144. }
  145. // 平滑过渡周日无排程
  146. for (int i = 0; i < values.Length; i++)
  147. {
  148. if (values[i] == 1)
  149. {
  150. continue;
  151. }
  152. if (i - 1 < 0 || i + 1 >= values.Length)
  153. {
  154. continue;
  155. }
  156. if (values[i - 1] == 1 && values[i + 1] == 1)
  157. {
  158. values[i] = 1;
  159. }
  160. }
  161. // 循环查找 0-1 或 1-0 变化坐标
  162. List<Position> xs = new List<Position>();
  163. for (int i = 1; i <= values.Length - 1; i++)
  164. {
  165. if (values[i] == 1 && values[i - 1] == 0)
  166. {
  167. for (int j = i; j <= values.Length - 1; j++)
  168. {
  169. if (values[j] == 0 && values[j - 1] == 1)
  170. {
  171. Position position = new Position(Dates.Length)
  172. {
  173. Start = i - 1,
  174. End = j - 2,
  175. };
  176. for (int k = position.Start; k <= position.End; k++)
  177. {
  178. position.Values[k] = "1";
  179. position.Value += values1[k + 1];
  180. }
  181. position.StartDate = (string)Dates.GetValue(position.Start);
  182. position.EndDate = (string)Dates.GetValue(position.End);
  183. xs.Add(position);
  184. i = j;
  185. break;
  186. }
  187. }
  188. }
  189. }
  190. return xs;
  191. }
  192. public string MonthTitle
  193. {
  194. get
  195. {
  196. return $"{_startDate:MM/dd} ~ {_endDate:MM/dd}";
  197. }
  198. }
  199. private IEnumerable<MachineDayOutPutPerHour> GetOuts(string date)
  200. {
  201. DateTime sTime = Convert.ToDateTime($"{date} 08:00:00");
  202. DateTime eTime = sTime.AddHours(24);
  203. return HourOuts.Where(x => x.DataTime >= sTime && x.DataTime < eTime);
  204. }
  205. private readonly DateTime _startDate;
  206. private readonly DateTime _endDate;
  207. private IList<HomeService.Dtos.ProductionPlanDtoV1> Plans { get; set; }
  208. = new List<HomeService.Dtos.ProductionPlanDtoV1>();
  209. private IList<MachineDayOutPutPerHour> HourOuts { get; set; }
  210. = new List<MachineDayOutPutPerHour>();
  211. /// <summary>
  212. /// 日期
  213. /// </summary>
  214. public string[] Dates { get; set; }
  215. /// <summary>
  216. /// 日期图表显示
  217. /// </summary>
  218. public string[] NewDates { get; set; }
  219. /// <summary>
  220. /// 计划产能
  221. /// </summary>
  222. public string[] PlanCapacities { get; set; }
  223. /// <summary>
  224. /// 差异产能
  225. /// </summary>
  226. public string[] Differences { get; set; }
  227. /// <summary>
  228. /// 实际产能
  229. /// </summary>
  230. public string[] Capacities { get; set; }
  231. /// <summary>
  232. /// 机种
  233. /// </summary>
  234. public string[] ModuleTypes { get; set; }
  235. /// <summary>
  236. /// 累计差异趋势
  237. /// </summary>
  238. public string[] AccumulatedDifferences { get; set; }
  239. /// <summary>
  240. /// 累计差异
  241. /// </summary>
  242. public int AccumulatedDifference { get; set; }
  243. public IList<Serie> Series { get; set; }
  244. = new List<Serie>();
  245. }
  246. public class Serie
  247. {
  248. public Serie(int len)
  249. {
  250. Values = new string[len];
  251. }
  252. public string Name { get; set; } = string.Empty;
  253. public string[] Values { get; set; }
  254. public List<int> Indexs { get; set; } = new List<int>();
  255. public int Start { get; set; }
  256. public int End { get; set; }
  257. public int PlanCapacities { get; set; }
  258. public string StartDate { get; set; } = string.Empty;
  259. public string EndDate { get; set; } = string.Empty;
  260. }
  261. public class Position
  262. {
  263. public Position(int len)
  264. {
  265. Values = new string[len];
  266. }
  267. public int Start { get; set; }
  268. public int End { get; set; }
  269. public string[] Values { get; set; }
  270. public int Value { get; set; }
  271. public string StartDate { get; set; } = string.Empty;
  272. public string EndDate { get; set; } = string.Empty;
  273. }
  274. }