AddOrEditDeviceViewModel.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. using AutoMapper;
  2. using BizService;
  3. using Model.Dto;
  4. using Prism.Commands;
  5. using Prism.Events;
  6. using Prism.Mvvm;
  7. using Prism.Services.Dialogs;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Linq;
  11. using System.Text;
  12. using System.Threading.Tasks;
  13. using System.Windows;
  14. namespace PLCTool.ViewModels.BasicConfigViewModel
  15. {
  16. public class AddOrEditDeviceViewModel : BindableBase, IDialogAware
  17. {
  18. private readonly IEventAggregator _aggregator;
  19. private readonly IBasicDeviceService _iBasicDeviceService;
  20. private readonly IBasicDeviceKindService _iBasicDeviceKindService;
  21. private readonly IBasicProjectService _iBasicProjectService;
  22. private readonly IMapper _mapper;
  23. private long id = 0;
  24. public AddOrEditDeviceViewModel(IEventAggregator aggregator, IBasicDeviceService iBasicDeviceService, IBasicDeviceKindService iBasicDeviceKindService,IBasicProjectService iBasicProjectService, IMapper mapper)
  25. {
  26. _aggregator = aggregator;
  27. _iBasicDeviceService = iBasicDeviceService;
  28. _iBasicDeviceKindService = iBasicDeviceKindService;
  29. _iBasicProjectService= iBasicProjectService;
  30. _mapper = mapper;
  31. CloseCommand = new DelegateCommand(Close);
  32. SureCommand = new DelegateCommand<string>(Sure);
  33. CancelCommand = new DelegateCommand(Close);
  34. DeviceKindNameList= _iBasicDeviceKindService.FindAllDeviceKind();
  35. ProjectNameList = _iBasicProjectService.FindAllProject();
  36. }
  37. #region idialog接口实现
  38. public string Title { set; get; } = "新增设备";
  39. public event Action<IDialogResult> RequestClose;
  40. public bool CanCloseDialog()
  41. {
  42. return true;
  43. }
  44. public void OnDialogClosed()
  45. {
  46. }
  47. public void OnDialogOpened(IDialogParameters parameters)
  48. {
  49. var getMsg = parameters.GetValues<BasDeviceDto>("Key");
  50. ///值不为空,表示修改
  51. if (getMsg != null)
  52. {
  53. foreach (var item in getMsg)
  54. {
  55. if (item != null)
  56. {
  57. Title = "修改设备";
  58. DeviceName = item.DeviceName;
  59. DeviceNo = item.DeviceNo;
  60. ProjectName = _iBasicProjectService.Find((int)item.ProjectId)?.project_name;
  61. DeviceKindName = _iBasicDeviceKindService.Find((int)item.DeviceKindId)?.devicekind_name;
  62. Remark = item.Remark;
  63. }
  64. }
  65. }
  66. }
  67. #endregion
  68. #region 私有方法
  69. private void Close()
  70. {
  71. RequestClose?.Invoke(new DialogResult(ButtonResult.Cancel));
  72. }
  73. /// <summary>
  74. /// 确认
  75. /// </summary>
  76. /// <param name="obj"></param>
  77. private void Sure(string obj)
  78. {
  79. if (string.IsNullOrEmpty(DeviceName))
  80. {
  81. MessageBox.Show("请填写设备名称!", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
  82. return;
  83. }
  84. if (string.IsNullOrEmpty(DeviceNo))
  85. {
  86. MessageBox.Show("请填写设备编号!", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
  87. return;
  88. }
  89. if (string.IsNullOrEmpty(ProjectName))
  90. {
  91. MessageBox.Show("请选择项目名称!", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
  92. return;
  93. }
  94. if (string.IsNullOrEmpty(DeviceKindName))
  95. {
  96. MessageBox.Show("请填写设备类型!", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
  97. return;
  98. }
  99. BasDeviceDto basDevice = new BasDeviceDto();
  100. basDevice.DeviceName = DeviceName;
  101. basDevice.DeviceNo = DeviceNo;
  102. basDevice.ProjectId= (long)(_iBasicProjectService.FindByProjectName(ProjectName)?.project_id);
  103. basDevice.DeviceKindId= (long)(_iBasicDeviceKindService.FindByDeviceKindName(DeviceKindName)?.devicekind_id);
  104. basDevice.Remark = Remark;
  105. DialogParameters parm = new DialogParameters();
  106. parm.Add("ReturnValue", basDevice);
  107. RequestClose?.Invoke(new DialogResult(ButtonResult.OK, parm));
  108. }
  109. /// <summary>
  110. /// 重置
  111. /// </summary>
  112. /// <param name="obj"></param>
  113. private void ResetMethod(string obj)
  114. {
  115. }
  116. #endregion
  117. #region 命令绑定
  118. public DelegateCommand CloseCommand { set; get; }
  119. public DelegateCommand<string> SureCommand { set; get; }
  120. public DelegateCommand CancelCommand { set; get; }
  121. public DelegateCommand TxtLostFocusCommand { set; get; }
  122. public DelegateCommand TxtPLCValueLostFocusCommand { set; get; }
  123. #endregion
  124. #region 变量绑定
  125. private string deviceName;
  126. public string DeviceName
  127. {
  128. get { return deviceName; }
  129. set { deviceName = value; RaisePropertyChanged(); }
  130. }
  131. private string deviceNo;
  132. public string DeviceNo
  133. {
  134. get { return deviceNo; }
  135. set { deviceNo = value; RaisePropertyChanged(); }
  136. }
  137. private string projectName;
  138. public string ProjectName
  139. {
  140. get { return projectName; }
  141. set { projectName = value; RaisePropertyChanged(); }
  142. }
  143. private string deviceKindName;
  144. public string DeviceKindName
  145. {
  146. get { return deviceKindName; }
  147. set { deviceKindName = value; RaisePropertyChanged(); }
  148. }
  149. private List<string> projectNameList;
  150. public List<string> ProjectNameList
  151. {
  152. get { return projectNameList; }
  153. set { projectNameList = value; RaisePropertyChanged(); }
  154. }
  155. private List<string> deviceKindNameList;
  156. public List<string> DeviceKindNameList
  157. {
  158. get { return deviceKindNameList; }
  159. set { deviceKindNameList = value; RaisePropertyChanged(); }
  160. }
  161. private string remark;
  162. public string Remark
  163. {
  164. get { return remark; }
  165. set { remark = value; RaisePropertyChanged(); }
  166. }
  167. #endregion
  168. }
  169. }