using LocalhostMES.DataBase; using LocalhostMES.Enums; using LocalhostMES.Core; using LocalhostMES.Models; using LocalhostMES.ViewModels.Services; using Prism.Commands; using Prism.Mvvm; using System.Collections.ObjectModel; using System.Windows; namespace LocalhostMES.ViewModels.Tabs { public class KeyPartManagementViewModel : BindableBase { private readonly IMesWorkspace _workspace; public KeyPartManagementViewModel(IMesWorkspace workspace) { _workspace = workspace; _workspace.Initialize(); MesDataChangedNotifier.Changed += OnDataChanged; } private ObservableCollection _partInfos = new ObservableCollection(); public ObservableCollection PartInfos { get => _partInfos; set => SetProperty(ref _partInfos, value); } private LocalhostPartInfo _newPart = new LocalhostPartInfo(); public LocalhostPartInfo NewPart { get => _newPart; set => SetProperty(ref _newPart, value); } private LocalhostPartInfo _selectPart; public LocalhostPartInfo SelectPart { get => _selectPart; set => SetProperty(ref _selectPart, value); } private string _statusMessage = string.Empty; public string StatusMessage { get => _statusMessage; set => SetProperty(ref _statusMessage, value); } private DelegateCommand _addPartInfoCommand; public DelegateCommand AddPartInfoCommand => _addPartInfoCommand ?? (_addPartInfoCommand = new DelegateCommand(AddPartInfo)); private DelegateCommand _refPartInfoCommand; public DelegateCommand RefPartInfoCommand => _refPartInfoCommand ?? (_refPartInfoCommand = new DelegateCommand(RefPartInfo)); private DelegateCommand _deletePartInfoCommand; public DelegateCommand DeletePartInfoCommand => _deletePartInfoCommand ?? (_deletePartInfoCommand = new DelegateCommand(DeletePartInfo)); private DelegateCommand _deletePartRowCommand; public DelegateCommand DeletePartRowCommand => _deletePartRowCommand ?? (_deletePartRowCommand = new DelegateCommand(DeletePartRow, p => p != null)); private void AddPartInfo() { var pn = (NewPart.PartNum ?? string.Empty).Trim(); if (string.IsNullOrEmpty(pn)) { _workspace.ShowStatus("请填写关键件号", true); return; } if (NewPart.PartQty < 0) { _workspace.ShowStatus("数量不能为负数", true); return; } var st = NewPart.StationType; var toSave = new LocalhostPartInfo { StationType = st, PartNum = pn, PartQty = NewPart.PartQty }; if (DatabaseHelper.InsertPartInfo(toSave)) { NewPart = new LocalhostPartInfo { StationType = st }; _workspace.ShowStatus("关键件已保存", false); RefPartInfo(); } else { _workspace.ShowStatus("保存失败(可能已存在相同工站类型+关键件号)", true); } } private void RefPartInfo() { var st = NewPart.StationType; PartInfos = new ObservableCollection(DatabaseHelper.SelectPartInfo(st)); StatusMessage = $"工站 {st},共 {PartInfos.Count} 条"; } private void DeletePartInfo() { if (SelectPart == null) { _workspace.ShowStatus("请先选择要删除的关键件行", true); return; } if (DatabaseHelper.DeletePartInfo(SelectPart.StationType, SelectPart.PartNum)) { RefPartInfo(); _workspace.ShowStatus("已删除", false); } else { _workspace.ShowStatus("删除失败", true); } } private void DeletePartRow(LocalhostPartInfo part) { if (part == null) { return; } if (DatabaseHelper.DeletePartInfo(part.StationType, part.PartNum)) { PartInfos.Remove(part); _workspace.ShowStatus("已删除", false); } else { _workspace.ShowStatus("删除失败", true); } } private void OnDataChanged(object sender, MesDataChangedEventArgs e) { if (!e.Has(MesDataScope.KeyPart)) { return; } Application.Current?.Dispatcher.Invoke(RefPartInfo); } } }