using CommunityToolkit.Mvvm.ComponentModel; using CommunityToolkit.Mvvm.Input; using Aucma.Core.SheetMetal.Models; using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using Admin.Core.IService; using log4net; using Microsoft.Extensions.DependencyInjection; using Admin.Core.Model; using Castle.Core.Internal; using CommunityToolkit.Mvvm.Messaging; using Aucma.Core.SheetMetal.Views; using Aucma.Core.SheetMetal.Common; using Admin.Core.Common; using Admin.Core.Service; using NPOI.SS.Formula.Functions; namespace Aucma.Core.SheetMetal.ViewModels { public partial class CorrespondingModelPageViewModel : ObservableObject { #region 刷新创建计划 /// /// 刷新创建计划 /// public delegate void RefreshSetBomDataGrid(); public static event RefreshSetBomDataGrid RefreshSetBomDataGridEvent; #endregion private static readonly log4net.ILog log = LogManager.GetLogger(typeof(SplitPlanViewModel)); protected readonly IProductPlanInfoServices? _productPlanInfoServices; protected readonly IExecutePlanInfoServices? _executePlanInfoServices; private readonly ISmSyncModelSubServices _smSyncModelSubServices; private readonly IBaseMaterialInfoServices _baseMaterialInfoServices; private readonly ISmSyncModelServices _smSyncModelServices; private AppConfigHelper appConfig = new AppConfigHelper();//读取配置文件保存的查询内容 #region 构造函数 public CorrespondingModelPageViewModel() { } public CorrespondingModelPageViewModel(string productId) { _productPlanInfoServices = App.ServiceProvider.GetService(); _executePlanInfoServices = App.ServiceProvider.GetService(); _baseMaterialInfoServices = App.ServiceProvider.GetService(); _smSyncModelServices = App.ServiceProvider.GetService(); Task.WaitAll(LoadData()); //加载快捷方式 SaveSearchCriteria(); //WeakReferenceMessenger.Default.Register(this, SaveSearchCriteria); _radioButtonStatus = 1; ProductId = productId; } #endregion #region 加载DataGrid数据 private async Task LoadData() { try { MaterialDataGrid.Clear(); int i = 1; string station = Appsettings.app("StationInfo", "StationCode"); var materialList = _baseMaterialInfoServices.QueryAsync().Result; var modelList = from d in materialList select new { d.MaterialCode, d.MaterialName, d.MaterialSpecifications }; if (modelList == null) return; foreach (var item in modelList) { MaterialDataGrid.Add(new BaseMaterialInfoModel() { No = i, MaterialCode = item.MaterialCode, MaterialName = item.MaterialName, MaterialSpecificatons = item.MaterialSpecifications }); i++; } } catch (Exception ex) { log.Error($"初始化查询:{ex.Message}"); } await Task.CompletedTask; } private async Task LoadData(string obj) { try { string station = Appsettings.app("StationInfo", "StationCode"); MaterialDataGrid.Clear(); int i = 1; var materialList = _baseMaterialInfoServices.QueryAsync(d=>d.MaterialSpecifications.Contains(obj)).Result; var modelList =( from d in materialList select new BaseMaterialInfoModel { MaterialCode= d.MaterialCode, MaterialName= d.MaterialName, MaterialSpecificatons = d.MaterialSpecifications }).ToList(); if (modelList == null) return; foreach (var item in modelList) { MaterialDataGrid.Add(new BaseMaterialInfoModel() { No = i, MaterialCode = item.MaterialCode, MaterialName = item.MaterialName, MaterialSpecificatons = item.MaterialSpecificatons }); i++; } } catch (Exception ex) { log.Error($"根据搜索条件查询:{ex.Message}"); } await Task.CompletedTask; } #endregion #region 初始化datagrid private ObservableCollection materialDataGrid = new ObservableCollection(); public ObservableCollection MaterialDataGrid { get => materialDataGrid; set => SetProperty(ref materialDataGrid, value); } #endregion #region 查询 /// /// 查询 /// [RelayCommand] private async Task QueryPlan(string obj) { if (obj.IsNullOrEmpty()) { System.Windows.Application.Current.Dispatcher.Invoke((Action)(async () => { MaterialDataGrid.Clear(); await LoadData(); })); return; } MaterialDataGrid.Clear(); await LoadData(obj); } #endregion #region 绑定BOM /// /// 绑定BOM /// [RelayCommand] private async void AddBom(string materialSpecificatons) { if (!materialSpecificatons.IsNullOrEmpty()) { //添加BOM对应 var smSyncModel=_smSyncModelServices.FirstAsync(d=>d.ObjId==int.Parse(ProductId)).Result; smSyncModel.MaterialSpecifications = materialSpecificatons; var result = await _smSyncModelServices.UpdateAsync(smSyncModel); if (result) { MessageBox.Show("设置成功!","系统提醒",MessageBoxButton.OK); RefreshSetBomDataGridEvent?.Invoke(); } return; } } #endregion #region 获取当前行数据 赋值到textbox private ProductPlanInfoModel selectedCells; public ProductPlanInfoModel SelectedCells { get { return selectedCells; } set { selectedCells = value; SetProperty(ref selectedCells, value); } } #endregion #region 关闭当前窗口 /// /// 关闭当前窗口 /// public void CloseWindow() { WeakReferenceMessenger.Default.Send("close"); } #endregion #region 参数定义 private string _productId = string.Empty; public string ProductId { get => _productId; set => SetProperty(ref _productId, value); } private string _search = string.Empty; public string Search { get => _search; set => SetProperty(ref _search, value); } /// /// 下拉框 /// public string _materialTypeCombox; public string MaterialTypeCombox { get => _materialTypeCombox; set => SetProperty(ref _materialTypeCombox, value); } #region 多选按钮 /// /// 多选按钮 /// public int _radioButtonStatus; public int RadioButtonStatus { get => _radioButtonStatus; set => SetProperty(ref _radioButtonStatus, value); } #endregion private ObservableCollection _configurations = new ObservableCollection(); public ObservableCollection Configurations { get => _configurations; set => SetProperty(ref _configurations, value); } private ProductPlanInfoModel selectedDataItem; public ProductPlanInfoModel SelectedDataItem { get => selectedDataItem; set => SetProperty(ref selectedDataItem, value); } #endregion #region 重置 /// /// 重置 /// [RelayCommand] public async Task Reset() { Search = string.Empty; MaterialTypeCombox = string.Empty; await this.LoadData(); } #endregion #region 搜索条件设置 /// /// 搜索条件设置 /// [RelayCommand] public async Task SearchCriteriaSet() { SearchCriteriaView searchCriteriaWindow = new SearchCriteriaView(); bool? dialogResult = searchCriteriaWindow.ShowDialog(); if (dialogResult == false) // 用户点击了“取消”按钮或关闭窗口 { await LoadData(); } } #endregion #region 查询快捷查询方式 private void SaveSearchCriteria(object recipient, string message) { if (message == "RefreshSearchItems") { Configurations = new ObservableCollection(); var searchItems = appConfig.searchItems; var split = searchItems.Split('%'); foreach (var item in split) { if (!string.IsNullOrEmpty(item)) { Configurations.Add(item); } } } } private void SaveSearchCriteria() { Configurations = new ObservableCollection(); var searchItems = appConfig.searchItems; var split = searchItems.Split('%'); foreach (var item in split) { if (!string.IsNullOrEmpty(item)) { Configurations.Add(item); } } } #endregion #region 双击事件 public void MouseClick(object obj) { var info = SelectedDataItem as ProductPlanInfoModel; if (info != null) { info.PlanType = _radioButtonStatus; QuantityIssuedView quantityIssuedWindow = new QuantityIssuedView(info); quantityIssuedWindow.ShowDialog(); } } /// /// 鼠标双击事件 /// [RelayCommand] public void DoubleMouseClick() { MessageBox.Show("双击事件"); } #endregion #region 快捷查询 /// /// 快捷查询 /// /// /// [RelayCommand] public async Task RadioButton(string selectedOption) { string productLineCode = Appsettings.app("StationInfo", "StationCode"); MaterialDataGrid.Clear(); if (!string.IsNullOrEmpty(selectedOption)) { await LoadData(selectedOption); } else { await LoadData(); } } #endregion #region 按钮 /// /// 按钮 /// /// [RelayCommand] public void UpdateRadioButtonStatus(string status) { if (status == "status1") { _radioButtonStatus = 1; } if (status == "status2") { _radioButtonStatus = 2; } if (status == "status3") { _radioButtonStatus = 3; } } #endregion #region 获取最大值 /// /// 获取最大值 /// /// MES编码 /// public string GetMaxNum(string mesId) { try { List tempList = new List(); var list = _executePlanInfoServices.QueryAsync(d => d.TaskCode.Contains(mesId)).Result; if (list.Count() == 0) { return mesId + "0001"; } foreach (var item in list) { string code = item.TaskCode.Substring(item.TaskCode.Length - 4); int num = Convert.ToInt32(code); tempList.Add(num); } string maxStr = (tempList.Max() + 1).ToString(); string taskCode = maxStr.PadLeft(4, '0'); return taskCode; } catch { return mesId + "0001"; } } #endregion } public class BaseMaterialInfoModel { public int No { get; set; } public string MaterialCode { get; set; } public string MaterialName { get; set; } public string MaterialSpecificatons { get; set; } } }