ProductionStatementViewModel.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  1. using OxyPlot;
  2. using OxyPlot.Axes;
  3. using OxyPlot.Legends;
  4. using OxyPlot.Series;
  5. using Prism.Commands;
  6. using Prism.Ioc;
  7. using Prism.Mvvm;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Collections.ObjectModel;
  11. using System.Linq;
  12. using System.Windows.Documents;
  13. using TeamAAS_VP.Core;
  14. using TeamAAS_VP.Data;
  15. using TeamAAS_VP.Interfaces;
  16. using TeamAAS_VP.Models;
  17. using TeamAAS_VP.Resources.Languages;
  18. using TeamAAS_VP.Services;
  19. using static MaterialDesignThemes.Wpf.Theme.ToolBar;
  20. namespace TeamAAS_VP.ViewModels.Statistics
  21. {
  22. public class ProductionStatementViewModel : BindableBase
  23. {
  24. IContainerProvider _container;
  25. IProductService _productService;
  26. ISystemDatabaseService _systemDatabaseService;
  27. Management management;
  28. #region 属性
  29. private Int64 _TotalCount;
  30. /// <summary>
  31. /// 总产能
  32. /// </summary>
  33. public Int64 TotalCount
  34. {
  35. get { return _TotalCount; }
  36. set { SetProperty(ref _TotalCount, value); }
  37. }
  38. private Int64 _MonthCount;
  39. /// <summary>
  40. /// 月产能
  41. /// </summary>
  42. public Int64 MonthCount
  43. {
  44. get { return _MonthCount; }
  45. set { SetProperty(ref _MonthCount, value); }
  46. }
  47. private Int64 _WeekCount;
  48. /// <summary>
  49. /// 周产能
  50. /// </summary>
  51. public Int64 WeekCount
  52. {
  53. get { return _WeekCount; }
  54. set { SetProperty(ref _WeekCount, value); }
  55. }
  56. private Int64 _DayCount;
  57. /// <summary>
  58. /// 日产能
  59. /// </summary>
  60. public Int64 DayCount
  61. {
  62. get { return _DayCount; }
  63. set { SetProperty(ref _DayCount, value); }
  64. }
  65. private PlotModel _YieldChartModel;
  66. /// <summary>
  67. /// 良率图表Model的mvvm属性,可通知UI更新
  68. /// </summary>
  69. public PlotModel YieldChartModel
  70. {
  71. get { return _YieldChartModel; }
  72. set { SetProperty(ref _YieldChartModel, value); }
  73. }
  74. private PlotModel _MonthChartModel;
  75. /// <summary>
  76. /// 月产能
  77. /// </summary>
  78. public PlotModel MonthChartModel
  79. {
  80. get { return _MonthChartModel; }
  81. set { SetProperty(ref _MonthChartModel, value); }
  82. }
  83. private PlotModel _WeekChartModel;
  84. /// <summary>
  85. /// 周产能
  86. /// </summary>
  87. public PlotModel WeekChartModel
  88. {
  89. get { return _WeekChartModel; }
  90. set { SetProperty(ref _WeekChartModel, value); }
  91. }
  92. private PlotModel _DayChartModel;
  93. /// <summary>
  94. /// 日产能
  95. /// </summary>
  96. public PlotModel DayChartModel
  97. {
  98. get { return _DayChartModel; }
  99. set { SetProperty(ref _DayChartModel, value); }
  100. }
  101. private Int64 _ThrowNumber;
  102. /// <summary>
  103. /// 抛料数
  104. /// </summary>
  105. public Int64 ThrowNumber
  106. {
  107. get { return _ThrowNumber; }
  108. set { SetProperty(ref _ThrowNumber, value); }
  109. }
  110. private Int64 _NgNumber;
  111. /// <summary>
  112. /// ng数量
  113. /// </summary>
  114. public Int64 NgNumber
  115. {
  116. get { return _NgNumber; }
  117. set { SetProperty(ref _NgNumber, value); }
  118. }
  119. private double _Yield;
  120. /// <summary>
  121. /// 良率
  122. /// </summary>
  123. public double Yield
  124. {
  125. get { return _Yield; }
  126. set { SetProperty(ref _Yield, value); }
  127. }
  128. #endregion
  129. #region 命令
  130. private DelegateCommand _LoadedCommand;
  131. public DelegateCommand LoadedCommand =>
  132. _LoadedCommand ?? (_LoadedCommand = new DelegateCommand(ExecuteLoadedCommand));
  133. #endregion
  134. #region 事件
  135. #endregion
  136. public ProductionStatementViewModel(IContainerProvider container, ISystemDatabaseService systemDatabaseService, IProductService productService)
  137. {
  138. _container = container;
  139. _systemDatabaseService = systemDatabaseService;
  140. _productService = productService;
  141. }
  142. #region 方法
  143. async void ExecuteLoadedCommand()
  144. {
  145. if (management == null)
  146. management = _container.Resolve<Management>();
  147. if (management.CurrentProduct == null) return;
  148. await App.Current.Dispatcher.InvokeAsync(new Action(async () => {
  149. YieldChartModel = CreateYieldChartModel(await _systemDatabaseService.GetProductionByCategoriesAsync(management.CurrentProduct.Name));
  150. TotalCount = await _systemDatabaseService.GetOverallProductionAsync(management.CurrentProduct.Name);
  151. MonthCount = await _systemDatabaseService.GetCurrentMonthProductionAsync(management.CurrentProduct.Name);
  152. WeekCount = await _systemDatabaseService.GetCurrentWeekProductionAsync(management.CurrentProduct.Name);
  153. DayCount = await _systemDatabaseService.GetTodayProductionAsync(management.CurrentProduct.Name);
  154. MonthChartModel = CreateMonth(await _systemDatabaseService.GetMonthlyProductionByDayAndCategoryAsync(management.CurrentProduct.Name));
  155. WeekChartModel = CreateWeek(await _systemDatabaseService.GetWeeklyProductionByDayAndCategoryAsync(management.CurrentProduct.Name));
  156. DayChartModel = CreateDay(await _systemDatabaseService.GetDailyProductionByHourAndCategoryAsync(management.CurrentProduct.Name));
  157. double TotalQuantityToday2 = 0;
  158. ObservableCollection<ProductModel> ProductList = new ObservableCollection<ProductModel>(_productService.GetAllProducts());
  159. foreach (var product in ProductList)
  160. {
  161. TotalQuantityToday2 += await _systemDatabaseService.GetTodayProductionAsync(product.Name);
  162. }
  163. ThrowNumber = 0;
  164. NgNumber = await _systemDatabaseService.GetNGProductionAsync(management.CurrentProduct.Name);
  165. Yield = (TotalQuantityToday2 - (double)NgNumber) / TotalQuantityToday2 * 100;
  166. }));
  167. }
  168. /// <summary>
  169. /// 创建产量图表模型
  170. /// </summary>
  171. /// <returns></returns>
  172. private PlotModel CreateYieldChartModel(Dictionary<string, int> Items)
  173. {
  174. List<PieSlice> PieSlices = new List<PieSlice>();
  175. PieSlices = new List<PieSlice>();
  176. foreach (var item in Items)
  177. {
  178. PieSlices.Add(new PieSlice(item.Key, (double)item.Value));
  179. }
  180. var model = new PlotModel { Title = "产能统计" };
  181. var series = new PieSeries
  182. {
  183. StrokeThickness = 1.0,
  184. InsideLabelPosition = 0.5,
  185. AngleSpan = 360,
  186. StartAngle = 0
  187. };
  188. foreach (var slice in PieSlices)
  189. {
  190. series.Slices.Add(slice);
  191. }
  192. model.Series.Add(series);
  193. return model;
  194. }
  195. /// <summary>
  196. /// 创建月统计模型
  197. /// </summary>
  198. /// <param name="Items"></param>
  199. /// <returns></returns>
  200. private PlotModel CreateMonth(Dictionary<DateTime, Dictionary<string, int>> Items)
  201. {
  202. var model = new PlotModel { Title = Lang.当前月产能统计 };
  203. // 添加图例说明
  204. model.Legends.Add(new Legend
  205. {
  206. LegendPlacement = LegendPlacement.Outside,
  207. LegendPosition = LegendPosition.RightMiddle,
  208. LegendOrientation = LegendOrientation.Vertical,
  209. LegendBorderThickness = 0,
  210. LegendTextColor = OxyColors.LightGray
  211. }); ;
  212. // 定义第一个Y轴y1,显示数量
  213. var ay1 = new LinearAxis()
  214. {
  215. Key = "y1",
  216. Title = Lang.数量,
  217. TitlePosition = 1,
  218. Minimum = 0,
  219. Position = AxisPosition.Left,
  220. IsZoomEnabled = false,
  221. IsPanEnabled = false,
  222. TickStyle = TickStyle.None,
  223. MinorTickSize = 0,
  224. MajorGridlineStyle = LineStyle.Solid,
  225. MajorGridlineColor = OxyColor.Parse("#F3F3F3"),
  226. };
  227. var now = DateTime.UtcNow;
  228. var monthStart = DateTimeAxis.ToDouble(new DateTime(now.Year, now.Month, 1));
  229. var temp = new DateTime(now.Year, now.Month, 1);
  230. var monthEnd = DateTimeAxis.ToDouble(temp.AddMonths(1).AddDays(-1));
  231. // 定义X轴为日期轴
  232. //var minValue = DateTimeAxis.ToDouble(Items.Keys.ElementAt(0));
  233. //var maxValue = DateTimeAxis.ToDouble(Items.Keys.ElementAt(Items.Keys.Count - 1));
  234. var ax = new DateTimeAxis()
  235. {
  236. Minimum = monthStart,
  237. Maximum = monthEnd,
  238. StringFormat = $"dd{Lang.日}",
  239. MajorStep = 1,
  240. Position = AxisPosition.Bottom,
  241. Angle = 45,
  242. IsZoomEnabled = false,
  243. IsPanEnabled = false,
  244. TickStyle = TickStyle.None,
  245. MinorTickSize = 0,
  246. MajorGridlineStyle = LineStyle.Solid,
  247. MajorGridlineColor = OxyColor.Parse("#F3F3F3"),
  248. };
  249. model.Axes.Add(ay1);
  250. model.Axes.Add(ax);
  251. if (Items.Count <= 0)
  252. {
  253. return model;
  254. }
  255. foreach (var category in Items.First().Value.Keys)
  256. {
  257. var barSeries = new LinearBarSeries { Title = category };
  258. barSeries.YAxisKey = "y1";
  259. barSeries.BarWidth = 10;
  260. // 点击时弹出的label内容
  261. //barSeries.TrackerFormatString = $"{0}\r\n{2:dd}{Lang.日}: {4:0}";
  262. barSeries.TrackerFormatString = "{0}\r\n{2:dd}: {4:0}";
  263. foreach (var item in Items)
  264. {
  265. if (item.Value.ContainsKey(category))
  266. {
  267. barSeries.Points.Add(new OxyPlot.DataPoint(DateTimeAxis.ToDouble(item.Key), (double)(item.Value[category])));
  268. }
  269. else
  270. {
  271. barSeries.Points.Add(new OxyPlot.DataPoint(DateTimeAxis.ToDouble(item.Key), 0));
  272. }
  273. }
  274. model.Series.Add(barSeries);
  275. }
  276. var Series = new LineSeries { Title = Lang.总产量 };
  277. Series.YAxisKey = "y1";
  278. //Series.TrackerFormatString = $"{0}\r\n{2:dd}{Lang.日}: {4:0}";
  279. Series.TrackerFormatString = "{0}\r\n{2:dd}: {4:0}";
  280. foreach (var item in Items)
  281. {
  282. Int64 count = 0;
  283. foreach (var item1 in item.Value)
  284. {
  285. count += item1.Value;
  286. }
  287. Series.Points.Add(new OxyPlot.DataPoint(DateTimeAxis.ToDouble(item.Key), (double)count));
  288. }
  289. model.Series.Add(Series);
  290. return model;
  291. }
  292. /// <summary>
  293. /// 创建周统计模型
  294. /// </summary>
  295. /// <param name="Items"></param>
  296. /// <returns></returns>
  297. private PlotModel CreateWeek(Dictionary<int, Dictionary<string, int>> Items)
  298. {
  299. var model = new PlotModel { Title = Lang.当前周产能统计 };
  300. // 添加图例说明
  301. model.Legends.Add(new Legend
  302. {
  303. LegendPlacement = LegendPlacement.Outside,
  304. LegendPosition = LegendPosition.RightMiddle,
  305. LegendOrientation = LegendOrientation.Vertical,
  306. LegendBorderThickness = 0,
  307. LegendTextColor = OxyColors.LightGray
  308. });
  309. // 定义第一个Y轴y1,显示数量
  310. var ay1 = new LinearAxis()
  311. {
  312. Key = "y1",
  313. Title = Lang.数量,
  314. Minimum = 0,
  315. Position = AxisPosition.Left,
  316. IsZoomEnabled = false,
  317. IsPanEnabled = false,
  318. TickStyle = TickStyle.None,
  319. MinorTickSize = 0,
  320. MajorGridlineStyle = LineStyle.Solid,
  321. MajorGridlineColor = OxyColor.Parse("#F3F3F3"),
  322. };
  323. // 定义X轴为日期轴
  324. var ax = new LinearAxis()
  325. {
  326. Minimum = 0,
  327. Maximum = 7.5,
  328. MajorStep = 1,
  329. Position = AxisPosition.Bottom,
  330. StringFormat = $"{Lang.周} 0",
  331. Angle = 0,
  332. IsZoomEnabled = false,
  333. IsPanEnabled = false,
  334. TickStyle = TickStyle.None,
  335. MinorTickSize = 0,
  336. MajorGridlineStyle = LineStyle.Solid,
  337. MajorGridlineColor = OxyColor.Parse("#F3F3F3"),
  338. };
  339. model.Axes.Add(ay1);
  340. model.Axes.Add(ax);
  341. if (Items.Count <= 0)
  342. {
  343. return model;
  344. }
  345. foreach (var category in Items.First().Value.Keys)
  346. {
  347. var barSeries = new LinearBarSeries { Title = category };
  348. barSeries.YAxisKey = "y1";
  349. barSeries.BarWidth = 10;
  350. // 点击时弹出的label内容
  351. //barSeries.TrackerFormatString = $"{0}\r\n{Lang.周}{2:0}: {44:0}";
  352. barSeries.TrackerFormatString = "{0}\r\n{2:0}: {4:0}";
  353. foreach (var item in Items)
  354. {
  355. if (item.Value.ContainsKey(category))
  356. {
  357. barSeries.Points.Add(new OxyPlot.DataPoint(DateTimeAxis.ToDouble(item.Key), (double)(item.Value[category])));
  358. }
  359. else
  360. {
  361. barSeries.Points.Add(new OxyPlot.DataPoint(DateTimeAxis.ToDouble(item.Key), 0));
  362. }
  363. }
  364. model.Series.Add(barSeries);
  365. }
  366. var Series = new LineSeries { Title = Lang.当前周产能统计 };
  367. Series.YAxisKey = "y1";
  368. //Series.TrackerFormatString = $"{0}\r\n{2:dd}{Lang.日}: {4:0}";
  369. Series.TrackerFormatString = "{0}\r\n{2:0}: {4:0}";
  370. foreach (var item in Items)
  371. {
  372. Int64 count = 0;
  373. foreach (var item1 in item.Value)
  374. {
  375. count += item1.Value;
  376. }
  377. Series.Points.Add(new OxyPlot.DataPoint(DateTimeAxis.ToDouble(item.Key), (double)count));
  378. }
  379. model.Series.Add(Series);
  380. return model;
  381. }
  382. /// <summary>
  383. /// 创建日统计模型
  384. /// </summary>
  385. /// <param name="Items"></param>
  386. /// <returns></returns>
  387. private PlotModel CreateDay(Dictionary<int, Dictionary<string, int>> Items)
  388. {
  389. var model = new PlotModel { Title = Lang.当前日产能统计 };
  390. // 添加图例说明
  391. model.Legends.Add(new Legend
  392. {
  393. LegendPlacement = LegendPlacement.Outside,
  394. LegendPosition = LegendPosition.RightMiddle,
  395. LegendOrientation = LegendOrientation.Vertical,
  396. LegendBorderThickness = 0,
  397. LegendTextColor = OxyColors.LightGray
  398. });
  399. // 定义第一个Y轴y1,显示数量
  400. var ay1 = new LinearAxis()
  401. {
  402. Key = "y1",
  403. Title = $"{Lang.数量}",
  404. Minimum = 0,
  405. Position = AxisPosition.Left,
  406. IsZoomEnabled = false,
  407. IsPanEnabled = false,
  408. TickStyle = TickStyle.None,
  409. MinorTickSize = 0,
  410. MajorGridlineStyle = LineStyle.Solid,
  411. MajorGridlineColor = OxyColor.Parse("#F3F3F3"),
  412. };
  413. // 定义X轴为小时
  414. var ax = new LinearAxis()
  415. {
  416. Minimum = 0,
  417. Maximum = 24.5,
  418. MajorStep = 1,
  419. StringFormat = "0 H",
  420. Position = AxisPosition.Bottom,
  421. Angle = 0,
  422. IsZoomEnabled = false,
  423. IsPanEnabled = false,
  424. TickStyle = TickStyle.None,
  425. MinorTickSize = 0,
  426. MajorGridlineStyle = LineStyle.Solid,
  427. MajorGridlineColor = OxyColor.Parse("#F3F3F3"),
  428. };
  429. model.Axes.Add(ay1);
  430. model.Axes.Add(ax);
  431. if (Items.Count <= 0)
  432. {
  433. return model;
  434. }
  435. foreach (var category in Items.First().Value.Keys)
  436. {
  437. var barSeries = new LinearBarSeries { Title = category };
  438. barSeries.YAxisKey = "y1";
  439. barSeries.BarWidth = 10;
  440. // 点击时弹出的label内容
  441. barSeries.TrackerFormatString = "{0}\r\n{2:0}H: {4:0}";
  442. foreach (var item in Items)
  443. {
  444. if (item.Value.ContainsKey(category))
  445. {
  446. barSeries.Points.Add(new OxyPlot.DataPoint(DateTimeAxis.ToDouble(item.Key), (double)(item.Value[category])));
  447. }
  448. else
  449. {
  450. barSeries.Points.Add(new OxyPlot.DataPoint(DateTimeAxis.ToDouble(item.Key), 0));
  451. }
  452. }
  453. model.Series.Add(barSeries);
  454. }
  455. var Series = new LineSeries { Title = Lang.总产量 };
  456. Series.YAxisKey = "y1";
  457. //Series.TrackerFormatString = $"{0}\r\n{2:dd}{Lang.小时}: {4:0}";
  458. Series.TrackerFormatString = "{0}\r\n{2:0}H: {4:0}";
  459. foreach (var item in Items)
  460. {
  461. Int64 count = 0;
  462. foreach (var item1 in item.Value)
  463. {
  464. count += item1.Value;
  465. }
  466. Series.Points.Add(new OxyPlot.DataPoint(DateTimeAxis.ToDouble(item.Key), (double)count));
  467. }
  468. model.Series.Add(Series);
  469. return model;
  470. }
  471. #endregion
  472. }
  473. }