PartCatalogViewModel.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. using LocalhostMES.DataBase;
  2. using LocalhostMES.Core;
  3. using LocalhostMES.Models;
  4. using Prism.Commands;
  5. using Prism.Mvvm;
  6. using System;
  7. using System.Collections.ObjectModel;
  8. using System.Windows;
  9. namespace LocalhostMES.ViewModels.Tabs
  10. {
  11. public class PartCatalogViewModel : BindableBase
  12. {
  13. public PartCatalogViewModel()
  14. {
  15. LoadedCommand = new DelegateCommand(OnLoaded);
  16. AddCommand = new DelegateCommand(AddParkingLot);
  17. UpdateCommand = new DelegateCommand(UpdateParkingLot, CanUpdate).ObservesProperty(() => IsEditMode);
  18. DeleteCommand = new DelegateCommand<ParkingLot>(DeleteParkingLot);
  19. EditCommand = new DelegateCommand<ParkingLot>(EditParkingLot);
  20. ClearFormCommand = new DelegateCommand(ClearForm);
  21. CancelEditCommand = new DelegateCommand(CancelEdit);
  22. MesDataChangedNotifier.Changed += OnDataChanged;
  23. }
  24. public DelegateCommand LoadedCommand { get; }
  25. public DelegateCommand AddCommand { get; }
  26. public DelegateCommand UpdateCommand { get; }
  27. public DelegateCommand<ParkingLot> DeleteCommand { get; }
  28. public DelegateCommand<ParkingLot> EditCommand { get; }
  29. public DelegateCommand ClearFormCommand { get; }
  30. public DelegateCommand CancelEditCommand { get; }
  31. private ObservableCollection<ParkingLot> _parkingLots = new ObservableCollection<ParkingLot>();
  32. public ObservableCollection<ParkingLot> ParkingLots
  33. {
  34. get => _parkingLots;
  35. set => SetProperty(ref _parkingLots, value);
  36. }
  37. private ParkingLot _currentParkingLot = new ParkingLot();
  38. public ParkingLot CurrentParkingLot
  39. {
  40. get => _currentParkingLot;
  41. set => SetProperty(ref _currentParkingLot, value);
  42. }
  43. private ParkingLot _selectedParkingLot;
  44. public ParkingLot SelectedParkingLot
  45. {
  46. get => _selectedParkingLot;
  47. set => SetProperty(ref _selectedParkingLot, value);
  48. }
  49. private bool _isEditMode;
  50. public bool IsEditMode
  51. {
  52. get => _isEditMode;
  53. set => SetProperty(ref _isEditMode, value);
  54. }
  55. private void OnLoaded()
  56. {
  57. ReloadParkingLots();
  58. }
  59. private void ReloadParkingLots()
  60. {
  61. ParkingLots = new ObservableCollection<ParkingLot>(DatabaseHelper.SelectParkingLot());
  62. }
  63. private void AddParkingLot()
  64. {
  65. if (!ValidateParkingLot())
  66. {
  67. return;
  68. }
  69. var row = new ParkingLot
  70. {
  71. PartName = CurrentParkingLot.PartName?.Trim(),
  72. Quantity = CurrentParkingLot.Quantity
  73. };
  74. if (DatabaseHelper.InsertParkingLot(row))
  75. {
  76. MessageBox.Show("添加成功!", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
  77. ReloadParkingLots();
  78. ClearForm();
  79. }
  80. else
  81. {
  82. MessageBox.Show("添加失败(可能已存在相同记录)。", "提示", MessageBoxButton.OK, MessageBoxImage.Warning);
  83. }
  84. }
  85. private void EditParkingLot(ParkingLot parkingLot)
  86. {
  87. if (parkingLot == null)
  88. {
  89. return;
  90. }
  91. CurrentParkingLot = new ParkingLot
  92. {
  93. Id = parkingLot.Id,
  94. PartName = parkingLot.PartName,
  95. Quantity = parkingLot.Quantity
  96. };
  97. IsEditMode = true;
  98. SelectedParkingLot = parkingLot;
  99. }
  100. private void UpdateParkingLot()
  101. {
  102. if (!ValidateParkingLot() || SelectedParkingLot == null)
  103. {
  104. return;
  105. }
  106. SelectedParkingLot.PartName = CurrentParkingLot.PartName?.Trim();
  107. SelectedParkingLot.Quantity = CurrentParkingLot.Quantity;
  108. if (DatabaseHelper.UpdateParkingLot(SelectedParkingLot))
  109. {
  110. MessageBox.Show("更新成功!", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
  111. CancelEdit();
  112. ReloadParkingLots();
  113. }
  114. else
  115. {
  116. MessageBox.Show("更新失败。", "提示", MessageBoxButton.OK, MessageBoxImage.Warning);
  117. }
  118. }
  119. private bool CanUpdate() => IsEditMode;
  120. private void DeleteParkingLot(ParkingLot parkingLot)
  121. {
  122. if (parkingLot == null)
  123. {
  124. return;
  125. }
  126. var result = MessageBox.Show($"确定要删除零件「{parkingLot.PartName}」吗?",
  127. "确认删除", MessageBoxButton.YesNo, MessageBoxImage.Warning);
  128. if (result != MessageBoxResult.Yes)
  129. {
  130. return;
  131. }
  132. if (DatabaseHelper.DeleteParkingLot(parkingLot.Id))
  133. {
  134. if (IsEditMode && SelectedParkingLot == parkingLot)
  135. {
  136. CancelEdit();
  137. }
  138. ReloadParkingLots();
  139. }
  140. else
  141. {
  142. MessageBox.Show("删除失败。", "提示", MessageBoxButton.OK, MessageBoxImage.Warning);
  143. }
  144. }
  145. private void ClearForm()
  146. {
  147. CurrentParkingLot = new ParkingLot();
  148. }
  149. private void CancelEdit()
  150. {
  151. IsEditMode = false;
  152. ClearForm();
  153. SelectedParkingLot = null;
  154. }
  155. private bool ValidateParkingLot()
  156. {
  157. if (string.IsNullOrWhiteSpace(CurrentParkingLot.PartName))
  158. {
  159. MessageBox.Show("名称不能为空。", "验证", MessageBoxButton.OK, MessageBoxImage.Warning);
  160. return false;
  161. }
  162. if (CurrentParkingLot.Quantity < 0)
  163. {
  164. MessageBox.Show("数量不能为负数。", "验证", MessageBoxButton.OK, MessageBoxImage.Warning);
  165. return false;
  166. }
  167. return true;
  168. }
  169. private void OnDataChanged(object sender, MesDataChangedEventArgs e)
  170. {
  171. if (!e.Has(MesDataScope.ParkingLot))
  172. {
  173. return;
  174. }
  175. Application.Current?.Dispatcher.Invoke(ReloadParkingLots);
  176. }
  177. }
  178. }