ProductionStatementViewModel.cs 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034
  1. using OxyPlot;
  2. using OxyPlot.Annotations;
  3. using OxyPlot.Axes;
  4. using OxyPlot.Legends;
  5. using OxyPlot.Series;
  6. using Prism.Commands;
  7. using Prism.Events;
  8. using Prism.Ioc;
  9. using Prism.Mvvm;
  10. using System;
  11. using System.Collections.Generic;
  12. using System.IO;
  13. using System.Linq;
  14. using System.Threading.Tasks;
  15. using System.Windows.Documents;
  16. using TeamAAS_VP.Core;
  17. using TeamAAS_VP.Data;
  18. using TeamAAS_VP.Enums;
  19. using TeamAAS_VP.Events;
  20. using TeamAAS_VP.Interfaces;
  21. using TeamAAS_VP.Models;
  22. using TeamAAS_VP.Resources.Languages;
  23. using TeamAAS_VP.Services;
  24. using static MaterialDesignThemes.Wpf.Theme.ToolBar;
  25. namespace TeamAAS_VP.ViewModels.Statistics
  26. {
  27. public class ProductionStatementViewModel : BindableBase
  28. {
  29. IContainerProvider _container;
  30. ISystemDatabaseService _systemDatabaseService;
  31. IProductService _productService;
  32. IEventAggregator _eventAggregator;
  33. #region 属性
  34. private Int64 _TotalCount;
  35. /// <summary>
  36. /// 总产能
  37. /// </summary>
  38. public Int64 TotalCount
  39. {
  40. get { return _TotalCount; }
  41. set { SetProperty(ref _TotalCount,value); }
  42. }
  43. private Int64 _MonthCount;
  44. /// <summary>
  45. /// 月产能
  46. /// </summary>
  47. public Int64 MonthCount
  48. {
  49. get { return _MonthCount; }
  50. set { SetProperty(ref _MonthCount, value); }
  51. }
  52. private Int64 _WeekCount;
  53. /// <summary>
  54. /// 周产能
  55. /// </summary>
  56. public Int64 WeekCount
  57. {
  58. get { return _WeekCount; }
  59. set { SetProperty(ref _WeekCount, value); }
  60. }
  61. private Int64 _DayCount;
  62. /// <summary>
  63. /// 日产能
  64. /// </summary>
  65. public Int64 DayCount
  66. {
  67. get { return _DayCount; }
  68. set { SetProperty(ref _DayCount, value); }
  69. }
  70. private PlotModel _YieldChartModel;
  71. /// <summary>
  72. /// 良率图表Model的mvvm属性,可通知UI更新
  73. /// </summary>
  74. public PlotModel YieldChartModel
  75. {
  76. get { return _YieldChartModel; }
  77. set { SetProperty(ref _YieldChartModel, value); }
  78. }
  79. private PlotModel _MonthChartModel;
  80. /// <summary>
  81. /// 月产能
  82. /// </summary>
  83. public PlotModel MonthChartModel
  84. {
  85. get { return _MonthChartModel; }
  86. set { SetProperty(ref _MonthChartModel, value); }
  87. }
  88. private PlotModel _WeekChartModel;
  89. /// <summary>
  90. /// 周产能
  91. /// </summary>
  92. public PlotModel WeekChartModel
  93. {
  94. get { return _WeekChartModel; }
  95. set { SetProperty(ref _WeekChartModel, value); }
  96. }
  97. private PlotModel _DayChartModel;
  98. /// <summary>
  99. /// 日产能
  100. /// </summary>
  101. public PlotModel DayChartModel
  102. {
  103. get { return _DayChartModel; }
  104. set { SetProperty(ref _DayChartModel, value); }
  105. }
  106. private string _NowYieldt;
  107. /// <summary>
  108. /// 实时良率
  109. /// </summary>
  110. public string NowYieldt
  111. {
  112. get { return _NowYieldt; }
  113. set { SetProperty(ref _NowYieldt, value); }
  114. }
  115. private string _NowNumbers;
  116. /// <summary>
  117. /// 总检测数
  118. /// </summary>
  119. public string NowNumbers
  120. {
  121. get { return _NowNumbers; }
  122. set { SetProperty(ref _NowNumbers, value); }
  123. }
  124. private string _NowOkNumbers;
  125. /// <summary>
  126. /// 合格数量
  127. /// </summary>
  128. public string NowOkNumbers
  129. {
  130. get { return _NowOkNumbers; }
  131. set { SetProperty(ref _NowOkNumbers, value); }
  132. }
  133. private string _NowNgNumbers;
  134. /// <summary>
  135. /// 不良数量
  136. /// </summary>
  137. public string NowNgNumbers
  138. {
  139. get { return _NowNgNumbers; }
  140. set { SetProperty(ref _NowNgNumbers, value); }
  141. }
  142. private string _UphNumber;
  143. /// <summary>
  144. /// uph合格数量
  145. /// </summary>
  146. public string UphNumber
  147. {
  148. get { return _UphNumber; }
  149. set { SetProperty(ref _UphNumber, value); }
  150. }
  151. private string _HourNumber;
  152. /// <summary>
  153. /// uph连续小时数
  154. /// </summary>
  155. public string HourNumber
  156. {
  157. get { return _HourNumber; }
  158. set { SetProperty(ref _HourNumber, value); }
  159. }
  160. #endregion
  161. #region 命令
  162. private DelegateCommand _LoadedCommand;
  163. public DelegateCommand LoadedCommand =>
  164. _LoadedCommand ?? (_LoadedCommand = new DelegateCommand(ExecuteLoadedCommand));
  165. private DelegateCommand _SaveConfigCommand;
  166. public DelegateCommand SaveConfigCommand =>
  167. _SaveConfigCommand ?? (_SaveConfigCommand = new DelegateCommand(SaveConfig));
  168. #endregion
  169. #region 事件
  170. #endregion
  171. public ProductionStatementViewModel(IContainerProvider container, ISystemDatabaseService systemDatabaseService, IProductService productService)
  172. {
  173. _container= container;
  174. _systemDatabaseService = systemDatabaseService;
  175. _productService = productService;
  176. }
  177. #region 方法
  178. async void ExecuteLoadedCommand()
  179. {
  180. //var spcvlaue = Management.GetLatest20PressureSPCAsync();
  181. var currentproduct = _productService.GetCurrentProduct();
  182. if (currentproduct == null) return;
  183. await App.Current.Dispatcher.InvokeAsync(new Action(async () => {
  184. YieldChartModel = CreateYieldChartModel(await _systemDatabaseService.GetProductionByCategoriesAsync(currentproduct.Name));
  185. TotalCount = await _systemDatabaseService.GetOverallProductionAsync(currentproduct.Name);
  186. MonthCount = await _systemDatabaseService.GetCurrentMonthProductionAsync(currentproduct.Name);
  187. WeekCount = await _systemDatabaseService.GetCurrentWeekProductionAsync(currentproduct.Name);
  188. DayCount = await _systemDatabaseService.GetTodayProductionAsync(currentproduct.Name);
  189. WeekChartModel = CreateAssemblyPressureSPC(await GetLatest20PressureSPCAsync());
  190. MonthChartModel = CreateMonth(await _systemDatabaseService.GetMonthlyProductionByDayAndCategoryAsync(currentproduct.Name));
  191. //WeekChartModel = CreateWeek(await _systemDatabaseService.GetWeeklyProductionByDayAndCategoryAsync(currentproduct.Name));
  192. DayChartModel = CreateDay(await _systemDatabaseService.GetDailyProductionByHourAndCategoryAsync(currentproduct.Name));
  193. await GetGapYieldStatisticsAsync();
  194. await ReadUphAlarmConfig();
  195. }));
  196. }
  197. // 保存配置方法
  198. private void SaveConfig()
  199. {
  200. try
  201. {
  202. UPhAlarm data = new UPhAlarm
  203. {
  204. Hour = HourNumber,
  205. Number = UphNumber
  206. };
  207. string path = @"..\Config\UphAlarm.cfg";
  208. Directory.CreateDirectory(Path.GetDirectoryName(path));
  209. FileHelper.WriteJsonFile(data, path);
  210. //_eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = $"{Lang.成功}!", Duration = 0.4 });
  211. }
  212. catch (Exception ex)
  213. {
  214. }
  215. }
  216. public Task ReadUphAlarmConfig()
  217. {
  218. try
  219. {
  220. string path = @"..\Config\UphAlarm.cfg";
  221. if (File.Exists(path))
  222. {
  223. // 读取JSON文件
  224. var data = FileHelper.ReadJsonFile<UPhAlarm>(path);
  225. if (data != null)
  226. {
  227. // 把配置值加载到属性
  228. HourNumber = data.Hour;
  229. UphNumber = data.Number;
  230. Management.uphhour = int.Parse(HourNumber);
  231. Management.uphnumber = int.Parse(UphNumber);
  232. }
  233. }
  234. }
  235. catch (Exception ex)
  236. {
  237. Console.WriteLine($"读取配置失败:{ex.Message}");
  238. }
  239. return Task.CompletedTask;
  240. }
  241. public class UPhAlarm
  242. {
  243. public string Hour { get; set; }
  244. public string Number { get; set; }
  245. }
  246. /// <summary>
  247. /// 实时监控 GAP 检测良率(最近20条生产拍照记录)
  248. /// </summary>
  249. /// <returns>总数量、合格数、良率(%)、状态</returns>
  250. public async Task GetGapYieldStatisticsAsync()
  251. {
  252. try
  253. {
  254. var currentproduct = _productService.GetCurrentProduct();
  255. if (currentproduct == null)
  256. {
  257. return ;
  258. }
  259. var records = await _systemDatabaseService.GetLatest20AssemblyPressureAsync(1);
  260. if (records == null || records.Count == 0)
  261. return ;
  262. double min = currentproduct.GlobalParamEx.DownPressure.PressureDownLimit;
  263. double max = currentproduct.GlobalParamEx.DownPressure.PressureUpLimit;
  264. // 3. 统计良率
  265. double total = records.Count;
  266. double okCount = records.Count(g =>Convert.ToDouble(g.Split('|')[0]) >= min && Convert.ToDouble(g.Split('|')[0]) <= max);
  267. double yieldRate = Math.Round((double)okCount / total * 100, 2);
  268. NowYieldt = yieldRate.ToString("F2")+"%";//良率
  269. NowNumbers = total.ToString();//总数
  270. NowOkNumbers = okCount.ToString();//OK数
  271. NowNgNumbers=(total-okCount).ToString();
  272. return ;
  273. }
  274. catch (Exception ex)
  275. {
  276. LogHelper.WriteLogError("GAP良率统计异常", ex);
  277. return ;
  278. }
  279. }
  280. /// <summary>
  281. /// 创建产量图表模型
  282. /// </summary>
  283. /// <returns></returns>
  284. private PlotModel CreateYieldChartModel(Dictionary<string,int> Items)
  285. {
  286. List<PieSlice> PieSlices = new List<PieSlice>();
  287. PieSlices = new List<PieSlice>();
  288. foreach (var item in Items)
  289. {
  290. PieSlices.Add(new PieSlice(item.Key, (double)item.Value));
  291. }
  292. var model = new PlotModel { Title = Lang.产能统计 };
  293. var series = new PieSeries
  294. {
  295. StrokeThickness = 1.0,
  296. InsideLabelPosition = 0.5,
  297. AngleSpan = 360,
  298. StartAngle = 0
  299. };
  300. foreach (var slice in PieSlices)
  301. {
  302. series.Slices.Add(slice);
  303. }
  304. model.Series.Add(series);
  305. return model;
  306. }
  307. /// <summary>
  308. /// 创建Spc压力控制图
  309. /// </summary>
  310. /// <param name="spcPoints"></param>
  311. /// <returns></returns>
  312. private PlotModel CreateAssemblyPressureSPC(List<SpcPoint> spcPoints)
  313. {
  314. if (spcPoints == null || spcPoints.Count == 0)
  315. return new PlotModel { Title = "暂无数据" };
  316. var model = new PlotModel { Title = "组装压力SPC控制图", TitleFontSize = 14 };
  317. model.Legends.Add(new Legend
  318. {
  319. LegendPlacement = LegendPlacement.Outside,
  320. LegendPosition = LegendPosition.RightMiddle,
  321. LegendOrientation = LegendOrientation.Vertical,
  322. LegendBorderThickness = 0,
  323. LegendTextColor = OxyColors.LightGray
  324. });
  325. double ucl = spcPoints.First().UCL;
  326. double cl = spcPoints.First().CL;
  327. double lcl = spcPoints.First().LCL;
  328. string sn = spcPoints.First().SN;
  329. double maxValue = spcPoints.Max(p => p.Value);
  330. double minValue = spcPoints.Min(p => p.Value);
  331. double yx = spcPoints.Count;
  332. double allMax = new[] { maxValue, ucl }.Max();
  333. double allMin = new[] { minValue, lcl }.Min();
  334. double yMax = allMax > 0 ? allMax * 1.05 : allMax * 0.95;
  335. double yMin = allMin < 0 ? allMin * 1.05 : allMin * 0.95;
  336. yMin = Math.Min(yMin, allMin - 3);
  337. yMax = Math.Max(yMax, allMax + 3);
  338. var xAxis = new LinearAxis
  339. {
  340. Key = "X",
  341. Title = "序号",
  342. Minimum = 0,
  343. Maximum = yx + 1,
  344. Position = AxisPosition.Bottom,
  345. IsZoomEnabled = false,
  346. IsPanEnabled = false,
  347. TickStyle = TickStyle.Outside,
  348. MajorGridlineStyle = LineStyle.Solid,
  349. MajorGridlineColor = OxyColor.Parse("#F3F3F3")
  350. };
  351. model.Axes.Add(xAxis);
  352. var yAxis = new LinearAxis
  353. {
  354. Key = "Y",
  355. Title = "压力值",
  356. Minimum = yMin,
  357. Maximum = yMax,
  358. Position = AxisPosition.Left,
  359. IsZoomEnabled = false,
  360. IsPanEnabled = false,
  361. TickStyle = TickStyle.Outside,
  362. MajorGridlineStyle = LineStyle.Solid,
  363. MajorGridlineColor = OxyColor.Parse("#F3F3F3")
  364. };
  365. model.Axes.Add(yAxis);
  366. var valueSeries = new LineSeries
  367. {
  368. Title = "实际压力",
  369. Color = OxyColors.Blue,
  370. MarkerType = MarkerType.Circle,
  371. MarkerSize = 4,
  372. MarkerStroke = OxyColors.Blue,
  373. MarkerFill = OxyColors.White,
  374. YAxisKey = "Y",
  375. XAxisKey = "X"
  376. };
  377. var uclSeries = new LineSeries
  378. {
  379. Title = $"上限 = {ucl:F2}",
  380. Color = OxyColors.Red,
  381. LineStyle = LineStyle.Dash,
  382. YAxisKey = "Y",
  383. XAxisKey = "X"
  384. };
  385. var clSeries = new LineSeries
  386. {
  387. Title = $"平均值 = {cl:F2}",
  388. Color = OxyColors.Green,
  389. LineStyle = LineStyle.Solid,
  390. YAxisKey = "Y",
  391. XAxisKey = "X"
  392. };
  393. var lclSeries = new LineSeries
  394. {
  395. Title = $"下限 = {lcl:F2}",
  396. Color = OxyColors.Red,
  397. LineStyle = LineStyle.Dash,
  398. YAxisKey = "Y",
  399. XAxisKey = "X"
  400. };
  401. foreach (var point in spcPoints)
  402. {
  403. valueSeries.Points.Add(new OxyPlot.DataPoint(point.Index, point.Value));
  404. uclSeries.Points.Add(new OxyPlot.DataPoint(point.Index, point.UCL));
  405. clSeries.Points.Add(new OxyPlot.DataPoint(point.Index, point.CL));
  406. lclSeries.Points.Add(new OxyPlot.DataPoint(point.Index, point.LCL));
  407. }
  408. var uclAnnotation = new LineAnnotation
  409. {
  410. Type = LineAnnotationType.Horizontal,
  411. Y = ucl,
  412. Text = $"UCL {ucl:F2}",
  413. TextColor = OxyColors.Red,
  414. Color = OxyColors.Red,
  415. LineStyle = LineStyle.None,
  416. TextPosition = new OxyPlot.DataPoint(20.5, ucl)
  417. };
  418. var clAnnotation = new LineAnnotation
  419. {
  420. Type = LineAnnotationType.Horizontal,
  421. Y = cl,
  422. Text = $"CL {cl:F2}",
  423. TextColor = OxyColors.Green,
  424. Color = OxyColors.Green,
  425. LineStyle = LineStyle.None,
  426. TextPosition = new OxyPlot.DataPoint(20.5, cl)
  427. };
  428. var lclAnnotation = new LineAnnotation
  429. {
  430. Type = LineAnnotationType.Horizontal,
  431. Y = lcl,
  432. Text = $"LCL {lcl:F2}",
  433. TextColor = OxyColors.Red,
  434. Color = OxyColors.Red,
  435. LineStyle = LineStyle.None,
  436. TextPosition = new OxyPlot.DataPoint(20.5, lcl)
  437. };
  438. model.Annotations.Add(uclAnnotation);
  439. model.Annotations.Add(clAnnotation);
  440. model.Annotations.Add(lclAnnotation);
  441. model.Series.Add(valueSeries);
  442. model.Series.Add(uclSeries);
  443. model.Series.Add(clSeries);
  444. model.Series.Add(lclSeries);
  445. return model;
  446. }
  447. /// <summary>
  448. /// 创建月统计模型(总产能蓝色柱 + OK量绿色柱,Tooltip显示正确日期+标签)
  449. /// </summary>
  450. private PlotModel CreateMonth(Dictionary<DateTime, Dictionary<string, int>> Items)
  451. {
  452. var model = new PlotModel { Title = Lang.当前月产能统计 };
  453. model.Legends.Add(new Legend
  454. {
  455. LegendPlacement = LegendPlacement.Outside,
  456. LegendPosition = LegendPosition.RightMiddle,
  457. LegendOrientation = LegendOrientation.Vertical,
  458. LegendBorderThickness = 0,
  459. LegendTextColor = OxyColors.LightGray
  460. });
  461. var ay1 = new LinearAxis()
  462. {
  463. Key = "y1",
  464. Title = Lang.数量,
  465. TitlePosition = 1,
  466. Minimum = 0,
  467. Position = AxisPosition.Left,
  468. IsZoomEnabled = false,
  469. IsPanEnabled = false,
  470. TickStyle = TickStyle.None,
  471. MinorTickSize = 0,
  472. MajorGridlineStyle = LineStyle.Solid,
  473. MajorGridlineColor = OxyColor.Parse("#F3F3F3"),
  474. };
  475. DateTime minDate, maxDate;
  476. if (Items == null || Items.Count == 0)
  477. {
  478. var today = DateTime.Now.Date;
  479. minDate = today.AddDays(-0.5);
  480. maxDate = today.AddDays(0.5);
  481. }
  482. else
  483. {
  484. minDate = Items.Keys.First().AddDays(-0.5);
  485. maxDate = Items.Keys.Last().AddDays(0.5);
  486. }
  487. var ax = new DateTimeAxis()
  488. {
  489. Minimum = DateTimeAxis.ToDouble(minDate),
  490. Maximum = DateTimeAxis.ToDouble(maxDate),
  491. StringFormat = $"dd {Lang.日}",
  492. MajorStep = 1,
  493. Position = AxisPosition.Bottom,
  494. Angle = 45,
  495. IsZoomEnabled = false,
  496. IsPanEnabled = false,
  497. TickStyle = TickStyle.None,
  498. MinorTickSize = 0,
  499. MajorGridlineStyle = LineStyle.Solid,
  500. MajorGridlineColor = OxyColor.Parse("#F3F3F3"),
  501. };
  502. model.Axes.Add(ay1);
  503. model.Axes.Add(ax);
  504. // 1. 总产能(蓝色柱)
  505. var totalSeries = new LinearBarSeries
  506. {
  507. Title = "总产能",
  508. YAxisKey = "y1",
  509. BarWidth = 10,
  510. FillColor = OxyColors.SteelBlue,
  511. StrokeColor = OxyColors.SteelBlue,
  512. TrackerFormatString = "总产能\n{2:MM月dd日}: {4:0}"
  513. };
  514. // 2. OK量(绿色柱)
  515. var okSeries = new LinearBarSeries
  516. {
  517. Title = "OK量",
  518. YAxisKey = "y1",
  519. BarWidth = 10,
  520. FillColor = OxyColors.Green,
  521. StrokeColor = OxyColors.Green,
  522. TrackerFormatString = "OK量\n{2:MM月dd日}: {4:0}"
  523. };
  524. foreach (var item in Items)
  525. {
  526. double baseX = DateTimeAxis.ToDouble(item.Key);
  527. long totalCount = item.Value.Sum(kv => kv.Value);
  528. int okCount = item.Value.ContainsKey("OK") ? item.Value["OK"] : 0;
  529. totalSeries.Points.Add(new OxyPlot.DataPoint(baseX , totalCount));
  530. okSeries.Points.Add(new OxyPlot.DataPoint(baseX + 0.2, okCount));
  531. }
  532. model.Series.Add(totalSeries);
  533. model.Series.Add(okSeries);
  534. return model;
  535. }
  536. /// <summary>
  537. /// 创建月统计模型
  538. /// </summary>
  539. /// <param name="Items"></param>
  540. /// <returns></returns>
  541. private PlotModel CreateMonth2(Dictionary<DateTime, Dictionary<string, int>> Items)
  542. {
  543. var model = new PlotModel { Title = Lang.当前月产能统计 };
  544. // 添加图例说明
  545. model.Legends.Add(new Legend
  546. {
  547. LegendPlacement = LegendPlacement.Outside,
  548. LegendPosition = LegendPosition.RightMiddle,
  549. LegendOrientation = LegendOrientation.Vertical,
  550. LegendBorderThickness = 0,
  551. LegendTextColor = OxyColors.LightGray
  552. }); ;
  553. // 定义第一个Y轴y1,显示数量
  554. var ay1 = new LinearAxis()
  555. {
  556. Key = "y1",
  557. Title= Lang.数量,
  558. TitlePosition=1,
  559. Minimum = 0,
  560. Position = AxisPosition.Left,
  561. IsZoomEnabled = false,
  562. IsPanEnabled = false,
  563. TickStyle = TickStyle.None,
  564. MinorTickSize = 0,
  565. MajorGridlineStyle = LineStyle.Solid,
  566. MajorGridlineColor = OxyColor.Parse("#F3F3F3"),
  567. };
  568. // 定义X轴为日期轴
  569. var minValue = DateTimeAxis.ToDouble(Items.Keys.ElementAt(0));
  570. var maxValue = DateTimeAxis.ToDouble(Items.Keys.ElementAt(Items.Keys.Count-1));
  571. var ax = new DateTimeAxis()
  572. {
  573. Minimum = minValue,
  574. Maximum = maxValue,
  575. StringFormat = $"dd{Lang.日}",
  576. MajorStep = 1,
  577. Position = AxisPosition.Bottom,
  578. Angle = 45,
  579. IsZoomEnabled = false,
  580. IsPanEnabled = false,
  581. TickStyle = TickStyle.None,
  582. MinorTickSize = 0,
  583. MajorGridlineStyle = LineStyle.Solid,
  584. MajorGridlineColor = OxyColor.Parse("#F3F3F3"),
  585. };
  586. model.Axes.Add(ay1);
  587. model.Axes.Add(ax);
  588. foreach (var category in Items.First().Value.Keys)
  589. {
  590. var barSeries = new LinearBarSeries { Title = category };
  591. barSeries.YAxisKey = "y1";
  592. barSeries.BarWidth = 10;
  593. // 点击时弹出的label内容
  594. barSeries.TrackerFormatString = $"{0}\r\n{2:dd}{Lang.日}: {4:0}";
  595. foreach (var item in Items)
  596. {
  597. barSeries.Points.Add(new OxyPlot.DataPoint(DateTimeAxis.ToDouble(item.Key), (double)(item.Value[category])));
  598. }
  599. model.Series.Add(barSeries);
  600. }
  601. var Series = new LineSeries { Title = Lang.总产量 };
  602. Series.YAxisKey = "y1";
  603. Series.TrackerFormatString = $"{0}\r\n{2:dd}{Lang.日}: {4:0}";
  604. foreach (var item in Items)
  605. {
  606. Int64 count = 0;
  607. foreach (var item1 in item.Value)
  608. {
  609. count += item1.Value;
  610. }
  611. Series.Points.Add(new OxyPlot.DataPoint(DateTimeAxis.ToDouble(item.Key), (double)count));
  612. }
  613. model.Series.Add(Series);
  614. return model;
  615. }
  616. /// <summary>
  617. /// 创建周统计模型
  618. /// </summary>
  619. /// <param name="Items"></param>
  620. /// <returns></returns>
  621. private PlotModel CreateWeek(Dictionary<int, Dictionary<string, int>> Items)
  622. {
  623. var model = new PlotModel { Title = Lang.当前周产能统计 };
  624. // 添加图例说明
  625. model.Legends.Add(new Legend
  626. {
  627. LegendPlacement = LegendPlacement.Outside,
  628. LegendPosition = LegendPosition.RightMiddle,
  629. LegendOrientation = LegendOrientation.Vertical,
  630. LegendBorderThickness = 0,
  631. LegendTextColor = OxyColors.LightGray
  632. });
  633. // 定义第一个Y轴y1,显示数量
  634. var ay1 = new LinearAxis()
  635. {
  636. Key = "y1",
  637. Title = Lang.数量,
  638. Minimum = 0,
  639. Position = AxisPosition.Left,
  640. IsZoomEnabled = false,
  641. IsPanEnabled = false,
  642. TickStyle = TickStyle.None,
  643. MinorTickSize = 0,
  644. MajorGridlineStyle = LineStyle.Solid,
  645. MajorGridlineColor = OxyColor.Parse("#F3F3F3"),
  646. };
  647. // 定义X轴为日期轴
  648. var ax = new LinearAxis()
  649. {
  650. Minimum = 0,
  651. Maximum = 7.5,
  652. MajorStep = 1,
  653. Position = AxisPosition.Bottom,
  654. StringFormat= $"{Lang.周} 0",
  655. Angle = 0,
  656. IsZoomEnabled = false,
  657. IsPanEnabled = false,
  658. TickStyle = TickStyle.None,
  659. MinorTickSize = 0,
  660. MajorGridlineStyle = LineStyle.Solid,
  661. MajorGridlineColor = OxyColor.Parse("#F3F3F3"),
  662. };
  663. model.Axes.Add(ay1);
  664. model.Axes.Add(ax);
  665. foreach (var category in Items.First().Value.Keys)
  666. {
  667. var barSeries = new LinearBarSeries { Title = category };
  668. barSeries.YAxisKey = "y1";
  669. barSeries.BarWidth = 10;
  670. // 点击时弹出的label内容
  671. barSeries.TrackerFormatString = $"{0}\r\n{Lang.周}{2:0}: {4:0}";
  672. foreach (var item in Items)
  673. {
  674. barSeries.Points.Add(new OxyPlot.DataPoint(LinearAxis.ToDouble(item.Key), (double)(item.Value[category])));
  675. }
  676. model.Series.Add(barSeries);
  677. }
  678. var Series = new LineSeries { Title = Lang.当前周产能统计 };
  679. Series.YAxisKey = "y1";
  680. Series.TrackerFormatString = $"{0}\r\n{2:dd}{Lang.日}: {4:0}";
  681. foreach (var item in Items)
  682. {
  683. Int64 count = 0;
  684. foreach (var item1 in item.Value)
  685. {
  686. count += item1.Value;
  687. }
  688. Series.Points.Add(new OxyPlot.DataPoint(DateTimeAxis.ToDouble(item.Key), (double)count));
  689. }
  690. model.Series.Add(Series);
  691. return model;
  692. }
  693. /// <summary>
  694. /// 创建日统计模型(总产能蓝色柱 + OK量绿色柱)
  695. /// </summary>
  696. /// <param name="Items"></param>
  697. /// <returns></returns>
  698. private PlotModel CreateDay(Dictionary<int, Dictionary<string, int>> Items)
  699. {
  700. var model = new PlotModel { Title = Lang.当前日产能统计 };
  701. // 添加图例说明
  702. model.Legends.Add(new Legend
  703. {
  704. LegendPlacement = LegendPlacement.Outside,
  705. LegendPosition = LegendPosition.RightMiddle,
  706. LegendOrientation = LegendOrientation.Vertical,
  707. LegendBorderThickness = 0,
  708. LegendTextColor = OxyColors.LightGray
  709. });
  710. // 定义第一个Y轴y1,显示数量
  711. var ay1 = new LinearAxis()
  712. {
  713. Key = "y1",
  714. Title = $"{Lang.数量}",
  715. Minimum = 0,
  716. Position = AxisPosition.Left,
  717. IsZoomEnabled = false,
  718. IsPanEnabled = false,
  719. TickStyle = TickStyle.None,
  720. MinorTickSize = 0,
  721. MajorGridlineStyle = LineStyle.Solid,
  722. MajorGridlineColor = OxyColor.Parse("#F3F3F3"),
  723. };
  724. // 定义X轴为小时
  725. var ax = new LinearAxis()
  726. {
  727. Minimum = 0,
  728. Maximum = 24.5,
  729. MajorStep = 1,
  730. StringFormat = "0 H",
  731. Position = AxisPosition.Bottom,
  732. Angle = 0,
  733. IsZoomEnabled = false,
  734. IsPanEnabled = false,
  735. TickStyle = TickStyle.None,
  736. MinorTickSize = 0,
  737. MajorGridlineStyle = LineStyle.Solid,
  738. MajorGridlineColor = OxyColor.Parse("#F3F3F3"),
  739. };
  740. model.Axes.Add(ay1);
  741. model.Axes.Add(ax);
  742. if (Items.Count == 0)
  743. {
  744. return model;
  745. }
  746. // 1. 总产能(蓝色柱)
  747. var totalSeries = new LinearBarSeries
  748. {
  749. Title = "总产能",
  750. YAxisKey = "y1",
  751. BarWidth = 10,
  752. FillColor = OxyColors.SteelBlue,
  753. StrokeColor = OxyColors.SteelBlue,
  754. TrackerFormatString = "总产能\n{2:0}时: {4:0}"
  755. };
  756. // 2. OK量(绿色柱)
  757. var okSeries = new LinearBarSeries
  758. {
  759. Title = "OK量",
  760. YAxisKey = "y1",
  761. BarWidth = 10,
  762. FillColor = OxyColors.Green,
  763. StrokeColor = OxyColors.Green,
  764. TrackerFormatString = "OK量\n{2:0}时: {4:0}"
  765. };
  766. // 遍历每小时数据
  767. foreach (var item in Items)
  768. {
  769. double baseX = LinearAxis.ToDouble(item.Key);
  770. long totalCount = item.Value.Sum(kv => kv.Value);
  771. int okCount = item.Value.ContainsKey("OK") ? item.Value["OK"] : 0;
  772. totalSeries.Points.Add(new OxyPlot.DataPoint(baseX , totalCount));
  773. okSeries.Points.Add(new OxyPlot.DataPoint(baseX + 0.2, okCount));
  774. }
  775. model.Series.Add(totalSeries);
  776. model.Series.Add(okSeries);
  777. return model;
  778. }
  779. /// <summary>
  780. /// 创建日统计模型
  781. /// </summary>
  782. /// <param name="Items"></param>
  783. /// <returns></returns>
  784. private PlotModel CreateDay2(Dictionary<int, Dictionary<string, int>> Items)
  785. {
  786. var model = new PlotModel { Title = Lang.当前日产能统计 };
  787. // 添加图例说明
  788. model.Legends.Add(new Legend
  789. {
  790. LegendPlacement = LegendPlacement.Outside,
  791. LegendPosition = LegendPosition.RightMiddle,
  792. LegendOrientation = LegendOrientation.Vertical,
  793. LegendBorderThickness = 0,
  794. LegendTextColor = OxyColors.LightGray
  795. });
  796. // 定义第一个Y轴y1,显示数量
  797. var ay1 = new LinearAxis()
  798. {
  799. Key = "y1",
  800. Title = $"{Lang.数量}",
  801. Minimum = 0,
  802. Position = AxisPosition.Left,
  803. IsZoomEnabled = false,
  804. IsPanEnabled = false,
  805. TickStyle = TickStyle.None,
  806. MinorTickSize = 0,
  807. MajorGridlineStyle = LineStyle.Solid,
  808. MajorGridlineColor = OxyColor.Parse("#F3F3F3"),
  809. };
  810. // 定义X轴为小时
  811. var ax = new LinearAxis()
  812. {
  813. Minimum = 0,
  814. Maximum = 24.5,
  815. MajorStep = 1,
  816. StringFormat = "0 H",
  817. Position = AxisPosition.Bottom,
  818. Angle = 0,
  819. IsZoomEnabled = false,
  820. IsPanEnabled = false,
  821. TickStyle = TickStyle.None,
  822. MinorTickSize = 0,
  823. MajorGridlineStyle = LineStyle.Solid,
  824. MajorGridlineColor = OxyColor.Parse("#F3F3F3"),
  825. };
  826. model.Axes.Add(ay1);
  827. model.Axes.Add(ax);
  828. if (Items.Count==0)
  829. {
  830. return model;
  831. }
  832. foreach (var category in Items.First().Value.Keys)
  833. {
  834. var barSeries = new LinearBarSeries { Title = category };
  835. barSeries.YAxisKey = "y1";
  836. barSeries.BarWidth = 10;
  837. // 点击时弹出的label内容
  838. barSeries.TrackerFormatString = "{0}\r\n{2:0}Hours: {4:0}";
  839. foreach (var item in Items)
  840. {
  841. barSeries.Points.Add(new OxyPlot.DataPoint(LinearAxis.ToDouble(item.Key), (double)(item.Value[category])));
  842. }
  843. model.Series.Add(barSeries);
  844. }
  845. var Series = new LineSeries { Title = Lang.总产量 };
  846. Series.YAxisKey = "y1";
  847. Series.TrackerFormatString = $"{0}\r\n{2:dd}{Lang.小时}: {4:0}";
  848. foreach (var item in Items)
  849. {
  850. Int64 count = 0;
  851. foreach (var item1 in item.Value)
  852. {
  853. count += item1.Value;
  854. }
  855. Series.Points.Add(new OxyPlot.DataPoint(DateTimeAxis.ToDouble(item.Key), (double)count));
  856. }
  857. model.Series.Add(Series);
  858. return model;
  859. }
  860. #endregion
  861. #region Spc
  862. public async Task<List<SpcPoint>> GetLatest20PressureSPCAsync()
  863. {
  864. //var pressureList = await _systemDatabaseService.GetLatest20CameraResultAsync();
  865. var pressureList1 = await _systemDatabaseService.GetLatest20AssemblyPressureAsync(1);
  866. return GenerateAssemblyPressureSPC(pressureList1);
  867. }
  868. /// <summary>
  869. /// 根据20条组装压力值 → 生成SPC控制图数据
  870. /// </summary>
  871. public List<SpcPoint> GenerateAssemblyPressureSPC(List<string> values)
  872. {
  873. // 获取当前产品
  874. var currentProduct = _productService.GetCurrentProduct();
  875. List<SpcPoint> spcList = new List<SpcPoint>();
  876. if (values == null || values.Count == 0)
  877. return spcList;
  878. double UCL = currentProduct.GlobalParamEx.DownPressure.PressureUpLimit;
  879. double CL = (currentProduct.GlobalParamEx.DownPressure.PressureUpLimit + currentProduct.GlobalParamEx.DownPressure.PressureDownLimit) / 2;
  880. double LCL = currentProduct.GlobalParamEx.DownPressure.PressureDownLimit;
  881. // 生成每个点
  882. for (int i = 0; i < values.Count; i++)
  883. {
  884. double val =Convert.ToDouble( values[i].Split('|')[0]);
  885. string sn = values[i].Split('|')[1];
  886. spcList.Add(new SpcPoint
  887. {
  888. Index = i + 1,
  889. Value = val,
  890. UCL = UCL,
  891. CL = CL,
  892. LCL = LCL,
  893. SN =sn,
  894. IsOutOfControl = val > UCL || val < LCL
  895. });
  896. }
  897. return spcList;
  898. }
  899. /// <summary>
  900. /// SPC 控制图点实体
  901. /// </summary>
  902. public class SpcPoint
  903. {
  904. public string SN { get; set; }
  905. public int Index { get; set; }
  906. public double Value { get; set; }
  907. public double UCL { get; set; }
  908. public double CL { get; set; }
  909. public double LCL { get; set; }
  910. public bool IsOutOfControl { get; set; }
  911. }
  912. #endregion
  913. }
  914. }