ProductionStatementViewModel.cs 37 KB

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