ProductionStatementViewModel.cs 16 KB

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