HomeViewModel.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. using MahApps.Metro.Controls;
  2. using MaterialDesignThemes.Wpf;
  3. using NPOI.SS.Formula.Functions;
  4. using OxyPlot;
  5. using OxyPlot.Axes;
  6. using OxyPlot.Legends;
  7. using OxyPlot.Series;
  8. using Prism.Commands;
  9. using Prism.Events;
  10. using Prism.Ioc;
  11. using Prism.Mvvm;
  12. using Prism.Regions;
  13. using System;
  14. using System.Collections.Generic;
  15. using System.Collections.ObjectModel;
  16. using System.Linq;
  17. using System.Threading.Tasks;
  18. using System.Windows;
  19. using System.Windows.Media;
  20. using TeamAAS_VP;
  21. using TeamAAS_VP.Controls;
  22. using TeamAAS_VP.Core;
  23. using TeamAAS_VP.Data;
  24. using TeamAAS_VP.Enums;
  25. using TeamAAS_VP.Events;
  26. using TeamAAS_VP.Interfaces;
  27. using TeamAAS_VP.Models;
  28. using TeamAAS_VP.Resources.Languages;
  29. using TeamAAS_VP.Services;
  30. using TeamAAS_VP.ViewModels.DebugMod;
  31. using TeamAAS_VP.Views.DebugMod;
  32. namespace TeamAAS_VP.ViewModels
  33. {
  34. public class HomeViewModel : BindableBase, IConfirmNavigationRequest
  35. {
  36. IEventAggregator _eventAggregator;
  37. IContainerProvider _container;
  38. IProductService _productService;
  39. IConfigService _configService;
  40. IRemoteCommandService _remoteCommandService;
  41. ISystemDatabaseService _systemDatabaseService;
  42. #region 属性
  43. private ObservableCollection<TaskMessage> messageQueue=new ObservableCollection<TaskMessage>();
  44. /// <summary>
  45. /// 运行信息队列
  46. /// </summary>
  47. public ObservableCollection<TaskMessage> MessageQueue
  48. {
  49. get { return messageQueue; }
  50. set { SetProperty(ref messageQueue,value); }
  51. }
  52. private ObservableCollection<ProductModel> _ProductList;
  53. public ObservableCollection<ProductModel> ProductList
  54. {
  55. get { return _ProductList; }
  56. set { SetProperty(ref _ProductList, value); }
  57. }
  58. private Management _management;
  59. public Management management
  60. {
  61. get { return _management; }
  62. set { SetProperty(ref _management, value); }
  63. }
  64. private double _CTTotalSeconds;
  65. public double CTTotalSeconds
  66. {
  67. get { return _CTTotalSeconds; }
  68. set { SetProperty(ref _CTTotalSeconds, value); }
  69. }
  70. private ProductModel _SelectProduct;
  71. public ProductModel SelectProduct
  72. {
  73. get { return _SelectProduct; }
  74. set { SetProperty(ref _SelectProduct,value); }
  75. }
  76. private bool CanLoad
  77. {
  78. get
  79. {
  80. if (SelectProduct!=null && IsLoadProduct)
  81. {
  82. return true;
  83. }
  84. else
  85. {
  86. return false;
  87. }
  88. }
  89. }
  90. private bool _IsLoadProduct = true;
  91. public bool IsLoadProduct
  92. {
  93. get { return _IsLoadProduct; }
  94. set { SetProperty(ref _IsLoadProduct, value); }
  95. }
  96. private bool _isCanExecute = true;
  97. public bool isCanExecute
  98. {
  99. get { return _isCanExecute; }
  100. set { SetProperty(ref _isCanExecute, value); }
  101. }
  102. private ProductModel _CurrentProduct;
  103. public ProductModel CurrentProduct
  104. {
  105. get { return _CurrentProduct; }
  106. set { SetProperty(ref _CurrentProduct, value); }
  107. }
  108. #endregion
  109. #region 命令
  110. public DelegateCommand LoadedCommand { get; set; }
  111. private DelegateCommand<ProductModel> _LoadProductCommand;
  112. public DelegateCommand<ProductModel> LoadProductCommand =>
  113. _LoadProductCommand ?? (_LoadProductCommand = new DelegateCommand<ProductModel>(ExecuteLoadProductCommand).ObservesCanExecute(()=> CanLoad).ObservesProperty(()=> SelectProduct).ObservesProperty(()=> IsLoadProduct));
  114. private DelegateCommand _ResetCounterCommand;
  115. public DelegateCommand ResetCounterCommand =>
  116. _ResetCounterCommand ?? (_ResetCounterCommand = new DelegateCommand(ExecuteResetCounterCommand).ObservesCanExecute(() => DoCondition).ObservesProperty(()=>isCanExecute));
  117. private bool DoCondition
  118. {
  119. get { return isCanExecute; }
  120. }
  121. private DelegateCommand<string> _CopyCommand;
  122. public DelegateCommand<string> CopyCommand =>
  123. _CopyCommand ?? (_CopyCommand = new DelegateCommand<string>(ExecuteCopyCommand));
  124. #endregion
  125. #region 事件
  126. #endregion
  127. public HomeViewModel(IEventAggregator ea, IContainerProvider container, IProductService productService, IConfigService configService, IRemoteCommandService remoteCommandService
  128. , ISystemDatabaseService systemDatabaseService)
  129. {
  130. _eventAggregator = ea;
  131. _container = container;
  132. _productService = productService;
  133. LoadedCommand = new DelegateCommand(OnLoad);
  134. _eventAggregator.GetEvent<TaskMessageNotification>().Subscribe(AddMessageInvoke);
  135. //订阅权限登录事件
  136. _eventAggregator.GetEvent<UserLoginNotification>().Subscribe(LoginChange);
  137. management = _container.Resolve<Management>();
  138. _configService = configService;
  139. _productService.OnProductChanged += _productService_OnProductChanged;
  140. _remoteCommandService = remoteCommandService;
  141. _remoteCommandService.CycleTimingTicked += _remoteCommandService_CycleTimingTicked;
  142. _systemDatabaseService = systemDatabaseService;
  143. }
  144. private void _remoteCommandService_CycleTimingTicked(object sender, TimeSpan e)
  145. {
  146. try
  147. {
  148. if (App.Current!=null)
  149. {
  150. App.Current.Invoke(() =>
  151. {
  152. CTTotalSeconds = e.TotalSeconds;
  153. });
  154. }
  155. }
  156. catch (Exception)
  157. {
  158. }
  159. }
  160. #region 方法
  161. private void OnLoad()
  162. {
  163. }
  164. async void ExecuteLoadProductCommand(ProductModel product)
  165. {
  166. if (product == null) return;
  167. var view = new ShowMessage(Lang.是否加载产品, $"{Lang.加载产品}:{product.Name}?");
  168. //show the dialog
  169. var result = await DialogHost.Show(view, "RootDialog", null, null, null);
  170. if (result != null && result is bool)
  171. {
  172. if (((bool)result))
  173. {
  174. IsLoadProduct = false;
  175. var waiting = new WaitingControl();
  176. //show the dialog
  177. var task = DialogHost.Show(waiting, "RootDialog", null, null, null);
  178. await _productService.LoadProductAsync(product.ID);
  179. _productService.SetCurrentProduct(product.ID);
  180. if (DialogHost.IsDialogOpen("RootDialog"))
  181. {
  182. DialogHost.Close("RootDialog");
  183. }
  184. await task;
  185. //DatabaseHelper.AddLoadProductRecord(product.ID, product.Name, false);
  186. IsLoadProduct = true;
  187. }
  188. }
  189. }
  190. void ExecuteCopyCommand(string textToCopy)
  191. {
  192. Clipboard.SetText(textToCopy);
  193. }
  194. /// <summary>
  195. /// 添加消息至消息队列
  196. /// </summary>
  197. /// <param name="msg"></param>
  198. /// <param name="color"></param>
  199. public void AddMessage(string msg, Color color)
  200. {
  201. App.Current.Dispatcher.BeginInvoke(new Action(() => {
  202. try
  203. {
  204. MessageQueue.Add(new TaskMessage()
  205. {
  206. DT = DateTime.Now,
  207. Message = msg,
  208. FontColol = new SolidColorBrush(color)
  209. });
  210. LogHelper.WriteLogInfo(msg);
  211. }
  212. catch (Exception)
  213. {
  214. }
  215. }));
  216. }
  217. /// <summary>
  218. /// 添加消息至消息队列
  219. /// </summary>
  220. /// <param name="msg"></param>
  221. /// <param name="color"></param>
  222. public void AddMessage(string msg, MessageLevel level)
  223. {
  224. MessageStruct messageStruct = new MessageStruct() { Message = msg, level = level };
  225. App.Current.Dispatcher.BeginInvoke(new Action(() => {
  226. AddMessage(messageStruct);
  227. }));
  228. }
  229. /// <summary>
  230. /// 添加消息至消息队列
  231. /// </summary>
  232. /// <param name="messageStruct"></param>
  233. public void AddMessage(MessageStruct messageStruct)
  234. {
  235. try
  236. {
  237. if (MessageQueue.Count >= 20)
  238. {
  239. MessageQueue.RemoveAt(0);// 从队列头部移除最早的消息
  240. }
  241. Color color = Colors.Black;
  242. switch (messageStruct.level)
  243. {
  244. case MessageLevel.Info:
  245. color = Colors.Black;
  246. break;
  247. case MessageLevel.Debug:
  248. color = Colors.Green;
  249. break;
  250. case MessageLevel.Alarm:
  251. color = Colors.Orange;
  252. break;
  253. case MessageLevel.Error:
  254. color = Colors.Red;
  255. break;
  256. }
  257. MessageQueue.Add(new TaskMessage()
  258. {
  259. DT = DateTime.Now,
  260. Message = messageStruct.Message,
  261. FontColol = new SolidColorBrush(color)
  262. });
  263. LogHelper.WriteLogInfo(messageStruct.Message);
  264. }
  265. catch (Exception)
  266. {
  267. }
  268. }
  269. public void AddMessageInvoke(MessageStruct messageStruct)
  270. {
  271. App.Current.Dispatcher.BeginInvoke(new Action(() => {
  272. AddMessage(messageStruct);
  273. }));
  274. }
  275. private void LoginChange(Models.User user)
  276. {
  277. }
  278. /// <summary>
  279. /// 重置计数
  280. /// </summary>
  281. async void ExecuteResetCounterCommand()
  282. {
  283. if (management.CurrentProduct == null) return;
  284. var view = new ShowMessage(Lang.计数清零, Lang.是否重置当前产品的计数);
  285. //show the dialog
  286. var result = await DialogHost.Show(view, "RootDialog", null, null, null);
  287. if (result != null && result is bool)
  288. {
  289. if (((bool)result))
  290. {
  291. isCanExecute = false;
  292. await _systemDatabaseService.ResetProductionAsync(management.CurrentProduct.Name);
  293. isCanExecute = true;
  294. }
  295. }
  296. }
  297. #endregion
  298. #region 继承
  299. /// <summary>
  300. /// 确认导航请求时调用。此方法允许您在导航之前执行一些操作。
  301. /// </summary>
  302. /// <param name="navigationContext"></param>
  303. /// <param name="continuationCallback"></param>
  304. public void ConfirmNavigationRequest(NavigationContext navigationContext, Action<bool> continuationCallback)
  305. {
  306. continuationCallback(true);
  307. }
  308. /// <summary>
  309. /// 接收导航请求时调用。传入导航参数,包含有关导航目标的信息。
  310. /// </summary>
  311. /// <param name="navigationContext"></param>
  312. /// <exception cref="NotImplementedException"></exception>
  313. public async void OnNavigatedTo(NavigationContext navigationContext)
  314. {
  315. try
  316. {
  317. ProductList = new ObservableCollection<ProductModel>(_productService.GetAllProducts());
  318. }
  319. catch (Exception ex)
  320. {
  321. LogHelper.WriteLogError("主界面界面进入时出错!", ex);
  322. }
  323. }
  324. /// <summary>
  325. /// 是否允许导航到此视图模型。
  326. /// </summary>
  327. /// <param name="navigationContext"></param>
  328. /// <returns></returns>
  329. public bool IsNavigationTarget(NavigationContext navigationContext)
  330. {
  331. return true;
  332. }
  333. /// <summary>
  334. /// 导航离开此视图模型时调用。您可以在此处执行清理操作或保存状态。
  335. /// </summary>
  336. /// <param name="navigationContext"></param>
  337. public void OnNavigatedFrom(NavigationContext navigationContext)
  338. {
  339. }
  340. #endregion
  341. private void _productService_OnProductChanged(ProductModel obj)
  342. {
  343. App.Current.Dispatcher.BeginInvoke(new Action(() => {
  344. CurrentProduct = obj;
  345. }));
  346. }
  347. }
  348. }