ProductParamsViewModel.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. using MaterialDesignThemes.Wpf;
  2. using Prism.Commands;
  3. using Prism.Events;
  4. using Prism.Ioc;
  5. using Prism.Mvvm;
  6. using Prism.Regions;
  7. using Prism.Services.Dialogs;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Collections.ObjectModel;
  11. using System.Linq;
  12. using System.Threading.Tasks;
  13. using System.Windows;
  14. using Team.FFFeederService.Interfaces;
  15. using TeamAAS_VP.Controls;
  16. using TeamAAS_VP.Core;
  17. using TeamAAS_VP.Events;
  18. using TeamAAS_VP.Interfaces;
  19. using TeamAAS_VP.Models;
  20. using TeamAAS_VP.Resources.Languages;
  21. using TeamAAS_VP.Services;
  22. namespace TeamAAS_VP.ViewModels.Product
  23. {
  24. public class ProductParamsViewModel : BindableBase, IConfirmNavigationRequest
  25. {
  26. IRegionManager _regionManager;
  27. IEventAggregator _eventAggregator;
  28. IContainerProvider _container;
  29. IDialogService _dialogService;
  30. IConfigService _configService;
  31. IRobotService _robotService;
  32. ICameraService _cameraService;
  33. IFeederService _feederService;
  34. IPlcService _plcService;
  35. IProductService _productService;
  36. ISystemDatabaseService _systemDatabaseService;
  37. #region 属性
  38. private ProductModel _SelectProduct;
  39. /// <summary>
  40. /// 选中的产品
  41. /// </summary>
  42. public ProductModel SelectProduct
  43. {
  44. get { return _SelectProduct; }
  45. set { SetProperty(ref _SelectProduct, value); }
  46. }
  47. private Management _management;
  48. public Management management
  49. {
  50. get { return _management; }
  51. set { SetProperty(ref _management, value); }
  52. }
  53. #endregion
  54. #region 命令
  55. private DelegateCommand _NextCommand;
  56. public DelegateCommand NextCommand =>
  57. _NextCommand ?? (_NextCommand = new DelegateCommand(ExecuteNextCommand));
  58. private DelegateCommand _BackCommand;
  59. public DelegateCommand BackCommand =>
  60. _BackCommand ?? (_BackCommand = new DelegateCommand(ExecuteBackCommand));
  61. private DelegateCommand _LoadedCommand;
  62. public DelegateCommand LoadedCommand =>
  63. _LoadedCommand ?? (_LoadedCommand = new DelegateCommand(ExecuteLoadedCommand));
  64. #endregion
  65. #region 事件
  66. #endregion
  67. public ProductParamsViewModel(IRegionManager regionManager, IEventAggregator ea, IContainerProvider container, IDialogService dialogService,
  68. IConfigService configService, IRobotService robotService, ICameraService cameraService, IFeederService feederService, IPlcService plcService, IProductService productService,
  69. ISystemDatabaseService systemDatabaseService)
  70. {
  71. _regionManager = regionManager;
  72. _eventAggregator = ea;
  73. _container = container;
  74. _dialogService = dialogService;
  75. _configService = configService;
  76. _robotService = robotService;
  77. _cameraService = cameraService;
  78. _feederService = feederService;
  79. _plcService = plcService;
  80. _productService = productService;
  81. _systemDatabaseService = systemDatabaseService;
  82. //订阅权限登录事件
  83. _eventAggregator.GetEvent<UserLoginNotification>().Subscribe(LoginChange);
  84. management = _container.Resolve<Management>();
  85. }
  86. #region 方法
  87. /// <summary>
  88. /// 完成
  89. /// </summary>
  90. async void ExecuteNextCommand()
  91. {
  92. if (MessageBox.Show(Lang.是否保存产品.Replace("[{0}]", ""), Lang.保存产品, MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
  93. {
  94. var waiting = new WaitingControl();
  95. //show the dialog
  96. var task = DialogHost.Show(waiting, "RootDialog", null, null, null);
  97. await _productService.UpdateProductAsync(SelectProduct);
  98. var current = _productService.GetCurrentProduct();
  99. if (current!=null && current.IsLoad)
  100. {
  101. await management.WritePositionToPlcAsync();
  102. }
  103. if (DialogHost.IsDialogOpen("RootDialog"))
  104. {
  105. DialogHost.Close("RootDialog");
  106. }
  107. await task;
  108. }
  109. NavigationParameters param = new NavigationParameters();
  110. param.Add("SelectProduct", SelectProduct);
  111. _regionManager.RequestNavigate("ProductRegionContext", "ProductHome", param);
  112. }
  113. /// <summary>
  114. /// 上一步
  115. /// </summary>
  116. void ExecuteBackCommand()
  117. {
  118. NavigationParameters param = new NavigationParameters();
  119. param.Add("SelectProduct", SelectProduct);
  120. _regionManager.RequestNavigate("ProductRegionContext", "PlcPointParams", param);
  121. }
  122. void ExecuteLoadedCommand()
  123. {
  124. if (_systemDatabaseService.GetCurrentUser().userPart == Enums.UserPart.Operator)
  125. {
  126. IsAllowEdit = false;
  127. }
  128. else
  129. {
  130. IsAllowEdit = true;
  131. }
  132. }
  133. #endregion
  134. #region 继承
  135. /// <summary>
  136. /// 确认导航请求时调用。此方法允许您在导航之前执行一些操作。
  137. /// </summary>
  138. /// <param name="navigationContext"></param>
  139. /// <param name="continuationCallback"></param>
  140. public void ConfirmNavigationRequest(NavigationContext navigationContext, Action<bool> continuationCallback)
  141. {
  142. continuationCallback(true);
  143. }
  144. /// <summary>
  145. /// 接收导航请求时调用。传入导航参数,包含有关导航目标的信息。
  146. /// </summary>
  147. /// <param name="navigationContext"></param>
  148. /// <exception cref="NotImplementedException"></exception>
  149. public async void OnNavigatedTo(NavigationContext navigationContext)
  150. {
  151. try
  152. {
  153. SelectProduct = navigationContext.Parameters.GetValue<ProductModel>("SelectProduct");
  154. }
  155. catch (Exception ex)
  156. {
  157. LogHelper.WriteLogError("机器人点位界面进入时出错!", ex);
  158. }
  159. }
  160. /// <summary>
  161. /// 是否允许导航到此视图模型。
  162. /// </summary>
  163. /// <param name="navigationContext"></param>
  164. /// <returns></returns>
  165. public bool IsNavigationTarget(NavigationContext navigationContext)
  166. {
  167. return true;
  168. }
  169. /// <summary>
  170. /// 导航离开此视图模型时调用。您可以在此处执行清理操作或保存状态。
  171. /// </summary>
  172. /// <param name="navigationContext"></param>
  173. public void OnNavigatedFrom(NavigationContext navigationContext)
  174. {
  175. }
  176. #endregion
  177. private bool _IsAllowEdit = false;
  178. public bool IsAllowEdit
  179. {
  180. get { return _IsAllowEdit; }
  181. set { SetProperty(ref _IsAllowEdit, value); }
  182. }
  183. private void LoginChange(Models.User user)
  184. {
  185. if (user.userPart == Enums.UserPart.Operator)
  186. {
  187. IsAllowEdit = false;
  188. }
  189. else
  190. {
  191. IsAllowEdit = true;
  192. }
  193. }
  194. }
  195. }