ProductionStatementViewModel.cs 18 KB

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