| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205 |
- 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<ParkingLot>(DeleteParkingLot);
- EditCommand = new DelegateCommand<ParkingLot>(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<ParkingLot> DeleteCommand { get; }
- public DelegateCommand<ParkingLot> EditCommand { get; }
- public DelegateCommand ClearFormCommand { get; }
- public DelegateCommand CancelEditCommand { get; }
- private ObservableCollection<ParkingLot> _parkingLots = new ObservableCollection<ParkingLot>();
- public ObservableCollection<ParkingLot> 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<ParkingLot>(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);
- }
- }
- }
|