ProductionStatementViewModel.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  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. ISystemDatabaseService _systemDatabaseService;
  26. IProductService _productService;
  27. #region 属性
  28. private Management _management;
  29. public Management management
  30. {
  31. get { return _management; }
  32. set { SetProperty(ref _management, value); }
  33. }
  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 Int64 _ThrowNumber;
  107. ///// <summary>
  108. ///// 抛料数
  109. ///// </summary>
  110. //public Int64 ThrowNumber
  111. //{
  112. // get { return _ThrowNumber; }
  113. // set { SetProperty(ref _ThrowNumber, value); }
  114. //}
  115. //private Int64 _NgNumber;
  116. ///// <summary>
  117. ///// ng数量
  118. ///// </summary>
  119. //public Int64 NgNumber
  120. //{
  121. // get { return _NgNumber; }
  122. // set { SetProperty(ref _NgNumber, value); }
  123. //}
  124. //private double _Yield;
  125. ///// <summary>
  126. ///// 良率
  127. ///// </summary>
  128. //public double Yield
  129. //{
  130. // get { return _Yield; }
  131. // set { SetProperty(ref _Yield, value); }
  132. //}
  133. #endregion
  134. #region 命令
  135. private DelegateCommand _LoadedCommand;
  136. public DelegateCommand LoadedCommand =>
  137. _LoadedCommand ?? (_LoadedCommand = new DelegateCommand(ExecuteLoadedCommand));
  138. #endregion
  139. #region 事件
  140. #endregion
  141. public ProductionStatementViewModel(IContainerProvider container, ISystemDatabaseService systemDatabaseService, IProductService productService)
  142. {
  143. _container = container;
  144. _systemDatabaseService = systemDatabaseService;
  145. _productService = productService;
  146. management = _container.Resolve<Management>();
  147. }
  148. #region 方法
  149. async void ExecuteLoadedCommand()
  150. {
  151. var currentproduct = _productService.GetCurrentProduct();
  152. if (currentproduct == null) return;
  153. await App.Current.Dispatcher.InvokeAsync(new Action(async () =>
  154. {
  155. YieldChartModel = CreateYieldChartModel(await _systemDatabaseService.GetProductionByCategoriesAsync(currentproduct.Name));
  156. TotalCount = await _systemDatabaseService.GetOverallProductionAsync(currentproduct.Name);
  157. MonthCount = await _systemDatabaseService.GetCurrentMonthProductionAsync(currentproduct.Name);
  158. WeekCount = await _systemDatabaseService.GetCurrentWeekProductionAsync(currentproduct.Name);
  159. DayCount = await _systemDatabaseService.GetTodayProductionAsync(currentproduct.Name);
  160. MonthChartModel = CreateMonth(await _systemDatabaseService.GetMonthlyProductionByDayAndCategoryAsync(currentproduct.Name));
  161. WeekChartModel = CreateWeek(await _systemDatabaseService.GetWeeklyProductionByDayAndCategoryAsync(currentproduct.Name));
  162. DayChartModel = CreateDay(await _systemDatabaseService.GetDailyProductionByHourAndCategoryAsync(currentproduct.Name));
  163. //var TotalQuantityToday = 0;
  164. //var CurrentUPH = 0;
  165. //ThrowNumber = await _systemDatabaseService.GetThrowNumberAsync();
  166. //NgNumber = 0;
  167. //Yield = 0;
  168. //ObservableCollection<ProductModel> ProductList = new ObservableCollection<ProductModel>(_productService.GetAllProducts());
  169. //foreach (var product in ProductList)
  170. //{
  171. // //TotalQuantity += await _systemDatabaseService.GetOverallProductionAsync(product.Name);
  172. // TotalQuantityToday += await _systemDatabaseService.GetTodayProductionAsync(product.Name);
  173. // CurrentUPH += await _systemDatabaseService.GetLastHourProductionAsync(product.Name);
  174. // NgNumber += await _systemDatabaseService.GetNGProductionAsync(product.Name);
  175. //}
  176. //Yield += ((double)TotalQuantityToday - (double)NgNumber) / (double)TotalQuantityToday * 100;
  177. }));
  178. }
  179. /// <summary>
  180. /// 创建产量图表模型
  181. /// </summary>
  182. /// <returns></returns>
  183. private PlotModel CreateYieldChartModel(Dictionary<string, int> Items)
  184. {
  185. List<PieSlice> PieSlices = new List<PieSlice>();
  186. PieSlices = new List<PieSlice>();
  187. foreach (var item in Items)
  188. {
  189. PieSlices.Add(new PieSlice(item.Key, (double)item.Value));
  190. }
  191. var model = new PlotModel { Title = Lang.产能统计 };
  192. var series = new PieSeries
  193. {
  194. StrokeThickness = 1.0,
  195. InsideLabelPosition = 0.5,
  196. AngleSpan = 360,
  197. StartAngle = 0
  198. };
  199. foreach (var slice in PieSlices)
  200. {
  201. series.Slices.Add(slice);
  202. }
  203. model.Series.Add(series);
  204. return model;
  205. }
  206. /// <summary>
  207. /// 创建月统计模型
  208. /// </summary>
  209. /// <param name="Items"></param>
  210. /// <returns></returns>
  211. private PlotModel CreateMonth(Dictionary<DateTime, Dictionary<string, int>> Items)
  212. {
  213. var model = new PlotModel { Title = Lang.当前月产能统计 };
  214. // 添加图例说明
  215. model.Legends.Add(new Legend
  216. {
  217. LegendPlacement = LegendPlacement.Outside,
  218. LegendPosition = LegendPosition.RightMiddle,
  219. LegendOrientation = LegendOrientation.Vertical,
  220. LegendBorderThickness = 0,
  221. LegendTextColor = OxyColors.LightGray
  222. }); ;
  223. // 定义第一个Y轴y1,显示数量
  224. var ay1 = new LinearAxis()
  225. {
  226. Key = "y1",
  227. Title = Lang.数量,
  228. TitlePosition = 1,
  229. Minimum = 0,
  230. Position = AxisPosition.Left,
  231. IsZoomEnabled = false,
  232. IsPanEnabled = false,
  233. TickStyle = TickStyle.None,
  234. MinorTickSize = 0,
  235. MajorGridlineStyle = LineStyle.Solid,
  236. MajorGridlineColor = OxyColor.Parse("#F3F3F3"),
  237. };
  238. var now = DateTime.Now;
  239. var monthStart = DateTimeAxis.ToDouble(new DateTime(now.Year, now.Month, 1));
  240. var temp = new DateTime(now.Year, now.Month, 1);
  241. var monthEnd = DateTimeAxis.ToDouble(temp.AddMonths(1).AddDays(-1));
  242. // 定义X轴为日期轴
  243. //var minValue = DateTimeAxis.ToDouble(Items.Keys.ElementAt(0));
  244. //var maxValue = DateTimeAxis.ToDouble(Items.Keys.ElementAt(Items.Keys.Count - 1));
  245. var ax = new DateTimeAxis()
  246. {
  247. Minimum = monthStart,
  248. Maximum = monthEnd,
  249. StringFormat = $"dd{Lang.日}",
  250. MajorStep = 1,
  251. Position = AxisPosition.Bottom,
  252. Angle = 45,
  253. IsZoomEnabled = false,
  254. IsPanEnabled = false,
  255. TickStyle = TickStyle.None,
  256. MinorTickSize = 0,
  257. MajorGridlineStyle = LineStyle.Solid,
  258. MajorGridlineColor = OxyColor.Parse("#F3F3F3"),
  259. };
  260. model.Axes.Add(ay1);
  261. model.Axes.Add(ax);
  262. if (Items.Count <= 0)
  263. {
  264. return model;
  265. }
  266. foreach (var category in Items.First().Value.Keys)
  267. {
  268. var barSeries = new LinearBarSeries { Title = category };
  269. barSeries.YAxisKey = "y1";
  270. barSeries.BarWidth = 10;
  271. // 点击时弹出的label内容
  272. barSeries.TrackerFormatString = $"{0}\r\n{2:dd}{Lang.日}: {4:0}";
  273. foreach (var item in Items)
  274. {
  275. if (item.Value.ContainsKey(category))
  276. {
  277. barSeries.Points.Add(new OxyPlot.DataPoint(DateTimeAxis.ToDouble(item.Key), (double)(item.Value[category])));
  278. }
  279. else
  280. {
  281. barSeries.Points.Add(new OxyPlot.DataPoint(DateTimeAxis.ToDouble(item.Key), 0));
  282. }
  283. }
  284. model.Series.Add(barSeries);
  285. }
  286. var Series = new LineSeries { Title = Lang.总产量 };
  287. Series.YAxisKey = "y1";
  288. Series.TrackerFormatString = $"{0}\r\n{2:dd}{Lang.日}: {4:0}";
  289. foreach (var item in Items)
  290. {
  291. Int64 count = 0;
  292. foreach (var item1 in item.Value)
  293. {
  294. count += item1.Value;
  295. }
  296. Series.Points.Add(new OxyPlot.DataPoint(DateTimeAxis.ToDouble(item.Key), (double)count));
  297. }
  298. model.Series.Add(Series);
  299. return model;
  300. }
  301. /// <summary>
  302. /// 创建周统计模型
  303. /// </summary>
  304. /// <param name="Items"></param>
  305. /// <returns></returns>
  306. private PlotModel CreateWeek(Dictionary<int, Dictionary<string, int>> Items)
  307. {
  308. var model = new PlotModel { Title = Lang.当前周产能统计 };
  309. // 添加图例说明
  310. model.Legends.Add(new Legend
  311. {
  312. LegendPlacement = LegendPlacement.Outside,
  313. LegendPosition = LegendPosition.RightMiddle,
  314. LegendOrientation = LegendOrientation.Vertical,
  315. LegendBorderThickness = 0,
  316. LegendTextColor = OxyColors.LightGray
  317. });
  318. // 定义第一个Y轴y1,显示数量
  319. var ay1 = new LinearAxis()
  320. {
  321. Key = "y1",
  322. Title = Lang.数量,
  323. Minimum = 0,
  324. Position = AxisPosition.Left,
  325. IsZoomEnabled = false,
  326. IsPanEnabled = false,
  327. TickStyle = TickStyle.None,
  328. MinorTickSize = 0,
  329. MajorGridlineStyle = LineStyle.Solid,
  330. MajorGridlineColor = OxyColor.Parse("#F3F3F3"),
  331. };
  332. // 定义X轴为日期轴
  333. var ax = new LinearAxis()
  334. {
  335. Minimum = 0,
  336. Maximum = 7.5,
  337. MajorStep = 1,
  338. Position = AxisPosition.Bottom,
  339. StringFormat = $"{Lang.周} 0",
  340. Angle = 0,
  341. IsZoomEnabled = false,
  342. IsPanEnabled = false,
  343. TickStyle = TickStyle.None,
  344. MinorTickSize = 0,
  345. MajorGridlineStyle = LineStyle.Solid,
  346. MajorGridlineColor = OxyColor.Parse("#F3F3F3"),
  347. };
  348. model.Axes.Add(ay1);
  349. model.Axes.Add(ax);
  350. if (Items.Count <= 0)
  351. {
  352. return model;
  353. }
  354. foreach (var category in Items.First().Value.Keys)
  355. {
  356. var barSeries = new LinearBarSeries { Title = category };
  357. barSeries.YAxisKey = "y1";
  358. barSeries.BarWidth = 10;
  359. // 点击时弹出的label内容
  360. barSeries.TrackerFormatString = $"{0}\r\n{Lang.周}{2:0}: {4:0}";
  361. foreach (var item in Items)
  362. {
  363. if (item.Value.ContainsKey(category))
  364. {
  365. barSeries.Points.Add(new OxyPlot.DataPoint(DateTimeAxis.ToDouble(item.Key), (double)(item.Value[category])));
  366. }
  367. else
  368. {
  369. barSeries.Points.Add(new OxyPlot.DataPoint(DateTimeAxis.ToDouble(item.Key), 0));
  370. }
  371. }
  372. model.Series.Add(barSeries);
  373. }
  374. var Series = new LineSeries { Title = Lang.当前周产能统计 };
  375. Series.YAxisKey = "y1";
  376. Series.TrackerFormatString = $"{0}\r\n{2:dd}{Lang.日}: {4:0}";
  377. foreach (var item in Items)
  378. {
  379. Int64 count = 0;
  380. foreach (var item1 in item.Value)
  381. {
  382. count += item1.Value;
  383. }
  384. Series.Points.Add(new OxyPlot.DataPoint(DateTimeAxis.ToDouble(item.Key), (double)count));
  385. }
  386. model.Series.Add(Series);
  387. return model;
  388. }
  389. /// <summary>
  390. /// 创建日统计模型
  391. /// </summary>
  392. /// <param name="Items"></param>
  393. /// <returns></returns>
  394. private PlotModel CreateDay(Dictionary<int, Dictionary<string, int>> Items)
  395. {
  396. var model = new PlotModel { Title = Lang.当前日产能统计 };
  397. // 添加图例说明
  398. model.Legends.Add(new Legend
  399. {
  400. LegendPlacement = LegendPlacement.Outside,
  401. LegendPosition = LegendPosition.RightMiddle,
  402. LegendOrientation = LegendOrientation.Vertical,
  403. LegendBorderThickness = 0,
  404. LegendTextColor = OxyColors.LightGray
  405. });
  406. // 定义第一个Y轴y1,显示数量
  407. var ay1 = new LinearAxis()
  408. {
  409. Key = "y1",
  410. Title = $"{Lang.数量}",
  411. Minimum = 0,
  412. Position = AxisPosition.Left,
  413. IsZoomEnabled = false,
  414. IsPanEnabled = false,
  415. TickStyle = TickStyle.None,
  416. MinorTickSize = 0,
  417. MajorGridlineStyle = LineStyle.Solid,
  418. MajorGridlineColor = OxyColor.Parse("#F3F3F3"),
  419. };
  420. // 定义X轴为小时
  421. var ax = new LinearAxis()
  422. {
  423. Minimum = 0,
  424. Maximum = 24.5,
  425. MajorStep = 1,
  426. StringFormat = "0 H",
  427. Position = AxisPosition.Bottom,
  428. Angle = 0,
  429. IsZoomEnabled = false,
  430. IsPanEnabled = false,
  431. TickStyle = TickStyle.None,
  432. MinorTickSize = 0,
  433. MajorGridlineStyle = LineStyle.Solid,
  434. MajorGridlineColor = OxyColor.Parse("#F3F3F3"),
  435. };
  436. model.Axes.Add(ay1);
  437. model.Axes.Add(ax);
  438. if (Items.Count<=0)
  439. {
  440. return model;
  441. }
  442. foreach (var category in Items.First().Value.Keys)
  443. {
  444. var barSeries = new LinearBarSeries { Title = category };
  445. barSeries.YAxisKey = "y1";
  446. barSeries.BarWidth = 10;
  447. // 点击时弹出的label内容
  448. barSeries.TrackerFormatString = "{0}\r\n{2:0}Hours: {4:0}";
  449. foreach (var item in Items)
  450. {
  451. barSeries.Points.Add(new OxyPlot.DataPoint(LinearAxis.ToDouble(item.Key), (double)(item.Value[category])));
  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. foreach (var item in Items)
  459. {
  460. Int64 count = 0;
  461. foreach (var item1 in item.Value)
  462. {
  463. count += item1.Value;
  464. }
  465. Series.Points.Add(new OxyPlot.DataPoint(DateTimeAxis.ToDouble(item.Key), (double)count));
  466. }
  467. model.Series.Add(Series);
  468. return model;
  469. }
  470. #endregion
  471. }
  472. }