ProductionStatementViewModel.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  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. foreach (var item in Items)
  263. {
  264. if (item.Value.ContainsKey(category))
  265. {
  266. barSeries.Points.Add(new OxyPlot.DataPoint(DateTimeAxis.ToDouble(item.Key), (double)(item.Value[category])));
  267. }
  268. else
  269. {
  270. barSeries.Points.Add(new OxyPlot.DataPoint(DateTimeAxis.ToDouble(item.Key), 0));
  271. }
  272. }
  273. model.Series.Add(barSeries);
  274. }
  275. var Series = new LineSeries { Title = Lang.总产量 };
  276. Series.YAxisKey = "y1";
  277. Series.TrackerFormatString = $"{0}\r\n{2:dd}{Lang.日}: {4:0}";
  278. foreach (var item in Items)
  279. {
  280. Int64 count = 0;
  281. foreach (var item1 in item.Value)
  282. {
  283. count += item1.Value;
  284. }
  285. Series.Points.Add(new OxyPlot.DataPoint(DateTimeAxis.ToDouble(item.Key), (double)count));
  286. }
  287. model.Series.Add(Series);
  288. return model;
  289. }
  290. /// <summary>
  291. /// 创建周统计模型
  292. /// </summary>
  293. /// <param name="Items"></param>
  294. /// <returns></returns>
  295. private PlotModel CreateWeek(Dictionary<int, Dictionary<string, int>> Items)
  296. {
  297. var model = new PlotModel { Title = Lang.当前周产能统计 };
  298. // 添加图例说明
  299. model.Legends.Add(new Legend
  300. {
  301. LegendPlacement = LegendPlacement.Outside,
  302. LegendPosition = LegendPosition.RightMiddle,
  303. LegendOrientation = LegendOrientation.Vertical,
  304. LegendBorderThickness = 0,
  305. LegendTextColor = OxyColors.LightGray
  306. });
  307. // 定义第一个Y轴y1,显示数量
  308. var ay1 = new LinearAxis()
  309. {
  310. Key = "y1",
  311. Title = Lang.数量,
  312. Minimum = 0,
  313. Position = AxisPosition.Left,
  314. IsZoomEnabled = false,
  315. IsPanEnabled = false,
  316. TickStyle = TickStyle.None,
  317. MinorTickSize = 0,
  318. MajorGridlineStyle = LineStyle.Solid,
  319. MajorGridlineColor = OxyColor.Parse("#F3F3F3"),
  320. };
  321. // 定义X轴为日期轴
  322. var ax = new LinearAxis()
  323. {
  324. Minimum = 0,
  325. Maximum = 7.5,
  326. MajorStep = 1,
  327. Position = AxisPosition.Bottom,
  328. StringFormat = $"{Lang.周} 0",
  329. Angle = 0,
  330. IsZoomEnabled = false,
  331. IsPanEnabled = false,
  332. TickStyle = TickStyle.None,
  333. MinorTickSize = 0,
  334. MajorGridlineStyle = LineStyle.Solid,
  335. MajorGridlineColor = OxyColor.Parse("#F3F3F3"),
  336. };
  337. model.Axes.Add(ay1);
  338. model.Axes.Add(ax);
  339. if (Items.Count <= 0)
  340. {
  341. return model;
  342. }
  343. foreach (var category in Items.First().Value.Keys)
  344. {
  345. var barSeries = new LinearBarSeries { Title = category };
  346. barSeries.YAxisKey = "y1";
  347. barSeries.BarWidth = 10;
  348. // 点击时弹出的label内容
  349. barSeries.TrackerFormatString = $"{0}\r\n{Lang.周}{2:0}: {4:0}";
  350. foreach (var item in Items)
  351. {
  352. if (item.Value.ContainsKey(category))
  353. {
  354. barSeries.Points.Add(new OxyPlot.DataPoint(DateTimeAxis.ToDouble(item.Key), (double)(item.Value[category])));
  355. }
  356. else
  357. {
  358. barSeries.Points.Add(new OxyPlot.DataPoint(DateTimeAxis.ToDouble(item.Key), 0));
  359. }
  360. }
  361. model.Series.Add(barSeries);
  362. }
  363. var Series = new LineSeries { Title = Lang.当前周产能统计 };
  364. Series.YAxisKey = "y1";
  365. Series.TrackerFormatString = $"{0}\r\n{2:dd}{Lang.日}: {4:0}";
  366. foreach (var item in Items)
  367. {
  368. Int64 count = 0;
  369. foreach (var item1 in item.Value)
  370. {
  371. count += item1.Value;
  372. }
  373. Series.Points.Add(new OxyPlot.DataPoint(DateTimeAxis.ToDouble(item.Key), (double)count));
  374. }
  375. model.Series.Add(Series);
  376. return model;
  377. }
  378. /// <summary>
  379. /// 创建日统计模型
  380. /// </summary>
  381. /// <param name="Items"></param>
  382. /// <returns></returns>
  383. private PlotModel CreateDay(Dictionary<int, Dictionary<string, int>> Items)
  384. {
  385. var model = new PlotModel { Title = Lang.当前日产能统计 };
  386. // 添加图例说明
  387. model.Legends.Add(new Legend
  388. {
  389. LegendPlacement = LegendPlacement.Outside,
  390. LegendPosition = LegendPosition.RightMiddle,
  391. LegendOrientation = LegendOrientation.Vertical,
  392. LegendBorderThickness = 0,
  393. LegendTextColor = OxyColors.LightGray
  394. });
  395. // 定义第一个Y轴y1,显示数量
  396. var ay1 = new LinearAxis()
  397. {
  398. Key = "y1",
  399. Title = $"{Lang.数量}",
  400. Minimum = 0,
  401. Position = AxisPosition.Left,
  402. IsZoomEnabled = false,
  403. IsPanEnabled = false,
  404. TickStyle = TickStyle.None,
  405. MinorTickSize = 0,
  406. MajorGridlineStyle = LineStyle.Solid,
  407. MajorGridlineColor = OxyColor.Parse("#F3F3F3"),
  408. };
  409. // 定义X轴为小时
  410. var ax = new LinearAxis()
  411. {
  412. Minimum = 0,
  413. Maximum = 24.5,
  414. MajorStep = 1,
  415. StringFormat = "0 H",
  416. Position = AxisPosition.Bottom,
  417. Angle = 0,
  418. IsZoomEnabled = false,
  419. IsPanEnabled = false,
  420. TickStyle = TickStyle.None,
  421. MinorTickSize = 0,
  422. MajorGridlineStyle = LineStyle.Solid,
  423. MajorGridlineColor = OxyColor.Parse("#F3F3F3"),
  424. };
  425. model.Axes.Add(ay1);
  426. model.Axes.Add(ax);
  427. if (Items.Count<=0)
  428. {
  429. return model;
  430. }
  431. foreach (var category in Items.First().Value.Keys)
  432. {
  433. var barSeries = new LinearBarSeries { Title = category };
  434. barSeries.YAxisKey = "y1";
  435. barSeries.BarWidth = 10;
  436. // 点击时弹出的label内容
  437. barSeries.TrackerFormatString = "{0}\r\n{2:0}Hours: {4:0}";
  438. foreach (var item in Items)
  439. {
  440. barSeries.Points.Add(new OxyPlot.DataPoint(LinearAxis.ToDouble(item.Key), (double)(item.Value[category])));
  441. }
  442. model.Series.Add(barSeries);
  443. }
  444. var Series = new LineSeries { Title = Lang.总产量 };
  445. Series.YAxisKey = "y1";
  446. Series.TrackerFormatString = $"{0}\r\n{2:dd}{Lang.小时}: {4:0}";
  447. foreach (var item in Items)
  448. {
  449. Int64 count = 0;
  450. foreach (var item1 in item.Value)
  451. {
  452. count += item1.Value;
  453. }
  454. Series.Points.Add(new OxyPlot.DataPoint(DateTimeAxis.ToDouble(item.Key), (double)count));
  455. }
  456. model.Series.Add(Series);
  457. return model;
  458. }
  459. #endregion
  460. }
  461. }