using LocalhostMES.DataBase; using LocalhostMES.Core; using LocalhostMES.Models; using LocalhostMES.ViewModels.Services; using Prism.Commands; using Prism.Mvvm; using System; using System.Collections.ObjectModel; using System.ComponentModel; using System.Windows; namespace LocalhostMES.ViewModels.Tabs { public class BindRecordsViewModel : BindableBase { private readonly IMesWorkspace _workspace; public BindRecordsViewModel(IMesWorkspace workspace) { _workspace = workspace; _workspace.Initialize(); RefreshBindRecords(); MesDataChangedNotifier.Changed += OnDataChanged; if (_workspace is INotifyPropertyChanged npc) { npc.PropertyChanged += OnWorkspacePropertyChanged; } } private ObservableCollection _dgBindRecords = new ObservableCollection(); public ObservableCollection DgBindRecords { get => _dgBindRecords; set => SetProperty(ref _dgBindRecords, value); } private BindRecord _selectBindRecord; public BindRecord SelectBindRecord { get => _selectBindRecord; set { if (SetProperty(ref _selectBindRecord, value) && value != null) { SearchSn = value.Sn; } } } private DelegateCommand _refreshBindRecordsCommand; public DelegateCommand RefreshBindRecordsCommand => _refreshBindRecordsCommand ?? (_refreshBindRecordsCommand = new DelegateCommand(RefreshBindRecords)); private DelegateCommand _clearBindRecordsCommand; public DelegateCommand ClearBindRecordsCommand => _clearBindRecordsCommand ?? (_clearBindRecordsCommand = new DelegateCommand(ClearBindRecords)); private string _searchSn; public string SearchSn { get => _searchSn; set => SetProperty(ref _searchSn, value); } private DelegateCommand _searchBySnCommand; public DelegateCommand SearchBySnCommand => _searchBySnCommand ?? (_searchBySnCommand = new DelegateCommand(SearchBySn)); private void RefreshBindRecords() { try { var sn = (SearchSn ?? string.Empty).Trim(); var response = string.IsNullOrEmpty(sn) ? DatabaseHelper.SelectBindRecord() : DatabaseHelper.SelectBindRecord(sn); if (response.Count != 0) { DgBindRecords = new ObservableCollection(response); _workspace.ShowStatus(string.IsNullOrEmpty(sn) ? $"已加载 {response.Count} 条绑定记录" : $"按SN查询到 {response.Count} 条绑定记录", false); } else { _workspace.ShowStatus(string.IsNullOrEmpty(sn) ? "暂无绑定记录" : $"未查询到SN={sn}的绑定记录", true); } } catch (Exception ex) { _workspace.ShowStatus($"刷新失败: {ex.Message}", true); } } private void SearchBySn() { RefreshBindRecords(); } private void ClearBindRecords() { var result = MessageBox.Show("确定要清空所有绑定记录吗?", "确认清空", MessageBoxButton.YesNo, MessageBoxImage.Warning); if (result == MessageBoxResult.Yes) { try { DatabaseHelper.DeleteBindRecord(); DgBindRecords.Clear(); _workspace.ShowStatus("绑定记录已清空", false); } catch (Exception ex) { _workspace.ShowStatus($"清空失败: {ex.Message}", true); } } } private void OnDataChanged(object sender, MesDataChangedEventArgs e) { if (!e.Has(MesDataScope.BindRecord)) { return; } Application.Current?.Dispatcher.Invoke(RefreshBindRecords); } private void OnWorkspacePropertyChanged(object sender, PropertyChangedEventArgs e) { if (e.PropertyName == nameof(IMesWorkspace.CrossPageSn) || e.PropertyName == nameof(IMesWorkspace.SelectedTabIndex)) { if (_workspace.SelectedTabIndex == 5) { var sn = (_workspace.CrossPageSn ?? string.Empty).Trim(); if (!string.IsNullOrEmpty(sn)) { SearchSn = sn; Application.Current?.Dispatcher.Invoke(RefreshBindRecords); } } } } } }