ProductionStatementViewModel.cs 16 KB

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