ProductionStatementViewModel.cs 15 KB

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