using GalaSoft.MvvmLight; using GalaSoft.MvvmLight.Command; using Microsoft.Extensions.DependencyInjection; using SlnMesnac.Model.domain; using SlnMesnac.Repository.service; using SlnMesnac.WPF.Models; using SqlSugar; using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Documents; using System.Xml.Linq; namespace SlnMesnac.WPF.ViewModel { public partial class ConfigPageViewModel : ObservableObject { private readonly ILogoConfigService? logoConfigService; public ConfigPageViewModel() { logoConfigService = App.ServiceProvider.GetService(); SyncCommand = new RelayCommand(SynchronizeLocal); QueryCommand = new RelayCommand(Query); UpdateCommand = new RelayCommand(t=> Update(t)); // MainWindowViewModel.RefreDataGridEvent += LoadDataAsync; LoadDataAsync(); } #region 加载DataGrid数据 private async void LoadDataAsync() { List list = await logoConfigService.GetLocalAllRecordAsync(); if (list == null || list.Count == 0) return; await App.Current.Dispatcher.BeginInvoke((Action)(() => { LogoConfigDataGrid.Clear(); foreach (LogoConfig verify in list) { LogoConfigDataGrid.Add(verify); } })); } #endregion //修改 private void Update(object parameter) { string materialType = parameter as string; if (!string.IsNullOrEmpty(materialType)) { LogoConfig record = logoConfigService.GetByMaterialType(materialType); if (record != null) { record.IsChecked = record.IsChecked == 1 ? 0 : 1; logoConfigService.updateByMaterialType(record); //LoadDataAsync(); } } } /// /// 从MES获取数据,如果本地没有某个型号,加入本地 /// public async void SynchronizeLocal() { await Task.Run(async() => { List mesList = logoConfigService.GetMesAllRecord(); if (mesList == null || mesList.Count == 0) return; List localList = await logoConfigService.GetLocalAllRecordAsync(); // 过滤后需要添加的型号 mesList = mesList.Where(m => !localList.Any(y => y.MaterialType == m.MaterialCode)).ToList(); if (mesList == null || mesList.Count == 0) return; List newList = new List(); foreach (ProductModel item in mesList) { newList.Add(new LogoConfig() { MaterialType = item.MaterialCode, MaterialName = item.MaterialName, IsChecked = 0 }); } bool result = await logoConfigService.InsertListAsync(newList); Console.WriteLine(result); LoadDataAsync(); }); } /// /// 查询方法 /// private async void Query() { if(QueryCode == null && QueryIsCheck == null) { LoadDataAsync(); return; } var aa = QueryCode; if(!string.IsNullOrEmpty(QueryCode)) { List list = await logoConfigService.GetLikeByCode(QueryCode); if (list == null || list.Count == 0) return; await App.Current.Dispatcher.BeginInvoke((Action)(() => { LogoConfigDataGrid.Clear(); foreach (LogoConfig verify in list) { LogoConfigDataGrid.Add(verify); } })); return; } if (QueryIsCheck != null) { if (QueryIsCheck.Content.Equals("")) { LoadDataAsync(); } else { int check = QueryIsCheck.Content.ToString() == "是" ? 1 : 0; List list = await logoConfigService.GetLocalAllRecordAsync(); if (list == null || list.Count == 0) return; list = list.Where(x => x.IsChecked == check).ToList(); await App.Current.Dispatcher.BeginInvoke((Action)(() => { LogoConfigDataGrid.Clear(); foreach (LogoConfig verify in list) { LogoConfigDataGrid.Add(verify); } })); } } } /// /// 查询条件 /// private string _QueryCode; public string QueryCode { get { return _QueryCode; } set { _QueryCode = value; RaisePropertyChanged();} } private ComboBoxItem _QueryIsCheck; public ComboBoxItem QueryIsCheck { get { return _QueryIsCheck; } set { _QueryIsCheck = value; RaisePropertyChanged(); } } #region 初始化datagrid private ObservableCollection _LogoConfigDataGrid = new ObservableCollection(); public ObservableCollection LogoConfigDataGrid { get { return _LogoConfigDataGrid; } set { _LogoConfigDataGrid = value; RaisePropertyChanged();//属性通知 } } #endregion /// /// 同步事件 /// public RelayCommand SyncCommand { get; set; } /// /// 查询事件 /// public RelayCommand QueryCommand { get; set; } /// /// 更新事件 /// public RelayCommand UpdateCommand { get; set; } } }