| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147 |
- 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<BindRecord> _dgBindRecords = new ObservableCollection<BindRecord>();
- public ObservableCollection<BindRecord> 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<BindRecord>(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);
- }
- }
- }
- }
- }
- }
|