ProductionStatementViewModel.cs 16 KB

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