using LocalhostMES.DataBase; using LocalhostMES.Core; using LocalhostMES.Models; using Prism.Commands; using Prism.Mvvm; using System; using System.Collections.ObjectModel; using System.Windows; namespace LocalhostMES.ViewModels.Tabs { public class PartCatalogViewModel : BindableBase { public PartCatalogViewModel() { LoadedCommand = new DelegateCommand(OnLoaded); AddCommand = new DelegateCommand(AddParkingLot); UpdateCommand = new DelegateCommand(UpdateParkingLot, CanUpdate).ObservesProperty(() => IsEditMode); DeleteCommand = new DelegateCommand(DeleteParkingLot); EditCommand = new DelegateCommand(EditParkingLot); ClearFormCommand = new DelegateCommand(ClearForm); CancelEditCommand = new DelegateCommand(CancelEdit); MesDataChangedNotifier.Changed += OnDataChanged; } public DelegateCommand LoadedCommand { get; } public DelegateCommand AddCommand { get; } public DelegateCommand UpdateCommand { get; } public DelegateCommand DeleteCommand { get; } public DelegateCommand EditCommand { get; } public DelegateCommand ClearFormCommand { get; } public DelegateCommand CancelEditCommand { get; } private ObservableCollection _parkingLots = new ObservableCollection(); public ObservableCollection ParkingLots { get => _parkingLots; set => SetProperty(ref _parkingLots, value); } private ParkingLot _currentParkingLot = new ParkingLot(); public ParkingLot CurrentParkingLot { get => _currentParkingLot; set => SetProperty(ref _currentParkingLot, value); } private ParkingLot _selectedParkingLot; public ParkingLot SelectedParkingLot { get => _selectedParkingLot; set => SetProperty(ref _selectedParkingLot, value); } private bool _isEditMode; public bool IsEditMode { get => _isEditMode; set => SetProperty(ref _isEditMode, value); } private void OnLoaded() { ReloadParkingLots(); } private void ReloadParkingLots() { ParkingLots = new ObservableCollection(DatabaseHelper.SelectParkingLot()); } private void AddParkingLot() { if (!ValidateParkingLot()) { return; } var row = new ParkingLot { PartName = CurrentParkingLot.PartName?.Trim(), Quantity = CurrentParkingLot.Quantity }; if (DatabaseHelper.InsertParkingLot(row)) { MessageBox.Show("添加成功!", "提示", MessageBoxButton.OK, MessageBoxImage.Information); ReloadParkingLots(); ClearForm(); } else { MessageBox.Show("添加失败(可能已存在相同记录)。", "提示", MessageBoxButton.OK, MessageBoxImage.Warning); } } private void EditParkingLot(ParkingLot parkingLot) { if (parkingLot == null) { return; } CurrentParkingLot = new ParkingLot { Id = parkingLot.Id, PartName = parkingLot.PartName, Quantity = parkingLot.Quantity }; IsEditMode = true; SelectedParkingLot = parkingLot; } private void UpdateParkingLot() { if (!ValidateParkingLot() || SelectedParkingLot == null) { return; } SelectedParkingLot.PartName = CurrentParkingLot.PartName?.Trim(); SelectedParkingLot.Quantity = CurrentParkingLot.Quantity; if (DatabaseHelper.UpdateParkingLot(SelectedParkingLot)) { MessageBox.Show("更新成功!", "提示", MessageBoxButton.OK, MessageBoxImage.Information); CancelEdit(); ReloadParkingLots(); } else { MessageBox.Show("更新失败。", "提示", MessageBoxButton.OK, MessageBoxImage.Warning); } } private bool CanUpdate() => IsEditMode; private void DeleteParkingLot(ParkingLot parkingLot) { if (parkingLot == null) { return; } var result = MessageBox.Show($"确定要删除零件「{parkingLot.PartName}」吗?", "确认删除", MessageBoxButton.YesNo, MessageBoxImage.Warning); if (result != MessageBoxResult.Yes) { return; } if (DatabaseHelper.DeleteParkingLot(parkingLot.Id)) { if (IsEditMode && SelectedParkingLot == parkingLot) { CancelEdit(); } ReloadParkingLots(); } else { MessageBox.Show("删除失败。", "提示", MessageBoxButton.OK, MessageBoxImage.Warning); } } private void ClearForm() { CurrentParkingLot = new ParkingLot(); } private void CancelEdit() { IsEditMode = false; ClearForm(); SelectedParkingLot = null; } private bool ValidateParkingLot() { if (string.IsNullOrWhiteSpace(CurrentParkingLot.PartName)) { MessageBox.Show("名称不能为空。", "验证", MessageBoxButton.OK, MessageBoxImage.Warning); return false; } if (CurrentParkingLot.Quantity < 0) { MessageBox.Show("数量不能为负数。", "验证", MessageBoxButton.OK, MessageBoxImage.Warning); return false; } return true; } private void OnDataChanged(object sender, MesDataChangedEventArgs e) { if (!e.Has(MesDataScope.ParkingLot)) { return; } Application.Current?.Dispatcher.Invoke(ReloadParkingLots); } } }