using Admin.Core.Common; using Admin.Core.IService; using Admin.Core.Model; using Admin.Core.Service; using Aucma.Core.SheetMetal.Business; using Aucma.Core.SheetMetal.Models; using Aucma.Core.SheetMetal.Views; using CommunityToolkit.Mvvm.ComponentModel; using CommunityToolkit.Mvvm.Input; using Microsoft.Extensions.DependencyInjection; using System; using System.Collections.ObjectModel; using System.Linq; using System.Threading.Tasks; using System.Windows; namespace Aucma.Core.SheetMetal.ViewModels { public partial class BomMaintenancePageViewModel : ObservableObject { private readonly ISmSyncModelServices _smSyncModelServices; private readonly ISmSyncModelSubServices _smSyncModelSubServices; SyncModelHandle handle = new SyncModelHandle(); public BomMaintenancePageViewModel() { _smSyncModelServices = App.ServiceProvider.GetService(); _smSyncModelSubServices = App.ServiceProvider.GetService(); CorrespondingModelPageViewModel.RefreshSetBomDataGridEvent += Refresh; OnLoadData(); } #region 刷新 [RelayCommand] public void Refresh() { OnLoadData(); } #endregion #region 初始化加载 public void OnLoadData() { SmSyncModelSideDataGrid.Clear(); var list = _smSyncModelServices.QueryAsync().Result; if (list == null || list.Count() == 0) return; var sideList= list.OrderBy(d => d.ObjId); foreach (var item in sideList.Where(d=>d.Category=="0")) { SmSyncModelSideDataGrid.Add(item); } SmSyncModelBackDataGrid.Clear(); var backList = list.OrderBy(d => d.ObjId); foreach (var item in backList.Where(d => d.Category == "1")) { SmSyncModelBackDataGrid.Add(item); } } #endregion #region 查询按钮 /// /// 查询按钮 /// /// [RelayCommand] public async Task QueryModel(string param) { var list=await _smSyncModelServices.QueryAsync(d=>d.SmModel.Contains(param)); foreach (var item in list) { SmSyncModelSideDataGrid.Add(item); } } #endregion #region 删除按钮 /// /// 删除按钮 /// /// [RelayCommand] public async Task DeleteBom(object obj) { int id = (int)obj; var smSyncModelSub = await _smSyncModelSubServices.FirstAsync(d=>d.ObjId==id); var result = await _smSyncModelSubServices.DeleteByIdAsync(id); if (result) { MessageBox.Show("删除成功!", "系统提醒", MessageBoxButton.OK); RefreshSetBom(smSyncModelSub.SmProductId); } } #endregion #region 同步钣金型号按钮 /// /// 同步钣金型号按钮 /// /// [RelayCommand] public async Task SyncSideModel() { Console.WriteLine("手动同步前板型号开始!"); await handle.QuerySidePanelMode(); OnLoadData(); MessageBox.Show("手动同步前板型号结束!"); Console.WriteLine("手动同步前板型号结束!"); } /// /// 同步钣金型号按钮 /// /// [RelayCommand] public async Task SyncBackModel() { Console.WriteLine("手动同步后板型号开始!"); await handle.QueryBackPanelMode(); OnLoadData(); MessageBox.Show("手动同步后板型号结束!"); Console.WriteLine("手动同步后板型号结束!"); } #endregion #region 设置钣金型号对应关系 /// /// 设置钣金型号对应关系 /// /// [RelayCommand] public Task SetProduct(object obj) { string id =Convert.ToInt32(obj).ToString(); //获取PLC设置好的点位 一共四个上料分垛 CorrespondingModelPageView view = new CorrespondingModelPageView(id); view.ShowDialog(); return Task.FromResult(0); } #endregion #region 初始化datagrid private ObservableCollection smSyncModelSideDataGrid = new ObservableCollection(); public ObservableCollection SmSyncModelSideDataGrid { get { return smSyncModelSideDataGrid; } set { smSyncModelSideDataGrid = value; OnPropertyChanged();//属性通知 } } #endregion #region 初始化datagrid private ObservableCollection smSyncModelBackDataGrid = new ObservableCollection(); public ObservableCollection SmSyncModelBackDataGrid { get { return smSyncModelBackDataGrid; } set { smSyncModelBackDataGrid = value; OnPropertyChanged();//属性通知 } } #endregion #region 刷新子控件 public void SelectRow(SmSyncModel sub) { RefreshSetBom(sub.SmProductId); } public void RefreshSetBom(string smProductId) { } #endregion } }