ProductionStatementViewModel.cs 15 KB

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