ProductionStatementViewModel.cs 15 KB

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