You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

206 lines
6.5 KiB
C#

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<ILogoConfigService>();
SyncCommand = new RelayCommand(SynchronizeLocal);
QueryCommand = new RelayCommand(Query);
UpdateCommand = new RelayCommand<object>(t=> Update(t));
// MainWindowViewModel.RefreDataGridEvent += LoadDataAsync;
LoadDataAsync();
}
#region 加载DataGrid数据
private async void LoadDataAsync()
{
List<LogoConfig> 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();
}
}
}
/// <summary>
/// 从MES获取数据如果本地没有某个型号加入本地
/// </summary>
public async void SynchronizeLocal()
{
await Task.Run(async() =>
{
List<ProductModel> mesList = logoConfigService.GetMesAllRecord();
if (mesList == null || mesList.Count == 0) return;
List<LogoConfig> localList = await logoConfigService.GetLocalAllRecordAsync();
// 过滤后需要添加的型号
mesList = mesList.Where(m => !localList.Any(y => y.MaterialType == m.MaterialCode)).ToList();
if (mesList == null || mesList.Count == 0) return;
List<LogoConfig> newList = new List<LogoConfig>();
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();
});
}
/// <summary>
/// 查询方法
/// </summary>
private async void Query()
{
if(QueryCode == null && QueryIsCheck == null)
{
LoadDataAsync();
return;
}
var aa = QueryCode;
if(!string.IsNullOrEmpty(QueryCode))
{
List<LogoConfig> 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<LogoConfig> 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);
}
}));
}
}
}
/// <summary>
/// 查询条件
/// </summary>
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<LogoConfig> _LogoConfigDataGrid = new ObservableCollection<LogoConfig>();
public ObservableCollection<LogoConfig> LogoConfigDataGrid
{
get { return _LogoConfigDataGrid; }
set
{
_LogoConfigDataGrid = value;
RaisePropertyChanged();//属性通知
}
}
#endregion
/// <summary>
/// 同步事件
/// </summary>
public RelayCommand SyncCommand { get; set; }
/// <summary>
/// 查询事件
/// </summary>
public RelayCommand QueryCommand { get; set; }
/// <summary>
/// 更新事件
/// </summary>
public RelayCommand<object> UpdateCommand { get; set; }
}
}