using CommunityToolkit.Mvvm.ComponentModel; using CommunityToolkit.Mvvm.Input; 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 Admin.Core.Common; using Aucma.Core.OldBoxFoam.Common; using Aucma.Core.OldBoxFoam.Models; using Aucma.Core.OldBoxFoam.Views; using Admin.Core.Model.Model_New; using Admin.Core.Service; namespace Aucma.Core.OldBoxFoam.ViewModels { public partial class SplitPlanViewModel : ObservableObject { private static readonly log4net.ILog log = LogManager.GetLogger(typeof(SplitPlanViewModel)); protected readonly IProductPlanInfoServices? _productPlanInfoServices; //protected readonly ISmTaskExecutionServices? _smTaskExecutionServices; protected readonly IExecutePlanInfoServices? _executePlanInfoServices; protected readonly IOldBoxFoamTypeServices _oldBoxFoamTypeServices; protected readonly IBoxFoamPlanServices? _boxFoamPlanServices; private AppConfigHelper appConfig = new AppConfigHelper(); private string boxTypeSearchText = string.Empty; private string spaceCodes = string.Empty; public SplitPlanViewModel(string objId, int shiftType, string boxType,string spaceCode) { this.boxTypeSearchText = boxType; this.spaceCodes = spaceCode; _productPlanInfoServices = App.ServiceProvider.GetService(); _executePlanInfoServices = App.ServiceProvider.GetService(); _oldBoxFoamTypeServices = App.ServiceProvider.GetService(); _boxFoamPlanServices = App.ServiceProvider.GetService(); Task.WaitAll(LoadData()); //加载快捷方式 SaveSearchCriteria(); WeakReferenceMessenger.Default.Register(this, SaveSearchCriteria); _objId = objId; _shiftType = shiftType; } #region 加载DataGrid数据 private async Task LoadData() { MaterialDataGrid.Clear(); int i = 1; string station = Appsettings.app("StationInfo", "StationCode"); ; var planlist = await _productPlanInfoServices.QueryAsync(d => d.ProductLineCode.Equals(station)); if (!string.IsNullOrEmpty(boxTypeSearchText)) { planlist = planlist.Where(x=>x.MaterialName.Contains(boxTypeSearchText)).ToList(); } var execList = await _executePlanInfoServices.QueryAsync(d => d.ProductLineCode.Equals(station)); foreach (var item in planlist) { int residue = 0; if (execList == null) residue = 0; else residue = (execList.Where(d => d.MaterialCode.Equals(item.MaterialCode))).Sum(d => d.PlanAmount); MaterialDataGrid.Add(new ProductPlanInfoModel() { No = i, PlanCode = item.PlanCode, MaterialCode = item.MaterialCode, MaterialName = item.MaterialName, OrderCode = item.OrderCode, PlanAmount = item.PlanAmount, ResidueAmount = item.PlanAmount - item.CompleteAmount, SpliteResidueAmount = item.PlanAmount - residue, CompleteAmount = item.CompleteAmount, StartDate = item.BeginTime }); i++; } } private async Task LoadData(string obj) { try { int i = 1; string station = Appsettings.app("StationInfo", "StationCode"); var planlist = await _productPlanInfoServices.QueryAsync(d => d.ProductLineCode.Equals(station)); if (planlist == null) return; var queryList = planlist.Where(d =>d.MaterialName.Contains(obj)); var execList = await _executePlanInfoServices.QueryAsync(d => d.ProductLineCode.Equals(station)); if (execList == null) return; foreach (var item in queryList) { int residue = 0; if (execList == null) residue = 0; else residue = (execList.Where(d => d.MaterialCode.Equals(item.MaterialCode))).Sum(d => d.PlanAmount); MaterialDataGrid.Add(new ProductPlanInfoModel() { No = i, PlanCode = item.PlanCode, MaterialCode = item.MaterialCode, MaterialName = item.MaterialName, OrderCode = item.OrderCode, PlanAmount = item.PlanAmount, ResidueAmount = item.PlanAmount - item.CompleteAmount, SpliteResidueAmount = item.PlanAmount - residue, CompleteAmount = item.CompleteAmount, StartDate = item.BeginTime }); i++; } } catch (Exception ex) { log.Error("计划拆分异常:"+ex.Message); } } #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 创建任务 /// /// 创建任务 /// [RelayCommand] private async Task CreateTask(string obj) { if (string.IsNullOrEmpty(obj)) { MessageBox.Show("请选中需要拆分的计划!", "系统提醒"); return; } string plan_code = SelectedCells.PlanCode; string order_code = SelectedCells.OrderCode; string material_code = SelectedCells.MaterialCode; string material_name = SelectedCells.MaterialName; int plan_amount = SelectedCells.PlanAmount; var list = await _executePlanInfoServices.QueryAsync(d=>d.ProductLineCode.Equals("1001")); ExecutePlanInfo task=new ExecutePlanInfo(); task.ExecutePlanCode = Guid.NewGuid().ToString(); task.ProductPlanCode = plan_code; task.OrderCode = order_code; task.MaterialCode = material_code; task.MaterialName = material_name; task.ProductLineCode = "1003"; //计划工位 if (list.Count == 0) task.ExecuteOrder = 1; if (list.Count != 0) task.ExecuteOrder = list.Max(d => d.ExecuteOrder) + 1; task.ExecuteMethod = 1;//不做要求,系统自动确定 task.ExecuteStatus = 1; task.PlanAmount = SelectedCells.SpliteResidueAmount; task.CompleteAmount = 0; task.CreatedTime = DateTime.Now; var result = await _executePlanInfoServices.AddAsync(task); if (result>0) { MessageBox.Show("计划拆分成功!","系统提醒"); WeakReferenceMessenger.Default.Send("Refresh");//刷新窗口 CloseWindow(); } else { MessageBox.Show("计划拆分失败,请检查后重试!", "系统提醒"); } } #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 int _shiftType; public int ShiftType { get => _shiftType; set => SetProperty(ref _shiftType, value); } private string _objId = string.Empty; public string ObjId { get => _objId; set => SetProperty(ref _objId, 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 void 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, ObjId); //quantityIssuedWindow.ShowDialog(); PlanInfo = info; } } /// /// 鼠标双击事件 /// [RelayCommand] public void DoubleMouseClick() { MessageBox.Show("双击事件"); } #endregion #region 快捷查询 /// /// 快捷查询 /// /// /// [RelayCommand] public async Task RadioButton(string selectedOption) { string productLineCode = Appsettings.app("StoreInfo", "ProductLineCode"); List planInfos = await _productPlanInfoServices.QueryAsync(d => d.ProductLineCode.Equals(productLineCode) && d.MaterialName.Contains(selectedOption)); if (planInfos != null) { if (planInfos.Count > 0) { MaterialDataGrid.Clear(); int i = 1; var execList = await _executePlanInfoServices.QueryAsync(d => d.ProductLineCode.Equals(productLineCode)); foreach (var item in planInfos) { int residue = 0; if (execList == null) { residue = 0; } else { residue = (execList.Where(d => d.MaterialCode.Equals(item.MaterialCode))).Sum(d => d.PlanAmount); } MaterialDataGrid.Add(new ProductPlanInfoModel() { No = i, PlanCode = item.PlanCode, MaterialCode = item.MaterialCode, MaterialName = item.MaterialName, OrderCode = item.OrderCode, PlanAmount = item.PlanAmount, ResidueAmount = item.PlanAmount - item.CompleteAmount, SpliteResidueAmount = item.PlanAmount - residue, StartDate = item.BeginTime }); i++; } } } } #endregion #region 按钮 /// /// 按钮 /// /// [RelayCommand] public void UpdateRadioButtonStatus(string status) { if (status== "status1") { _radioButtonStatus = 1; } if (status == "status2") { _radioButtonStatus = 2; } if (status == "status3") { _radioButtonStatus = 3; } } #endregion private ProductPlanInfoModel _planInfo = new ProductPlanInfoModel(); public ProductPlanInfoModel PlanInfo { get => _planInfo; set => SetProperty(ref _planInfo, value); } private string _TransmitAmount = string.Empty; public string TransmitAmount { get => _TransmitAmount; set => SetProperty(ref _TransmitAmount, value); } [RelayCommand] private void KeypadButton(object obj) { var info = obj as string; TransmitAmount += info; } /// /// 清除 /// [RelayCommand] private void ClearTransmitAmount() { string amount = _TransmitAmount.ToString(); if (amount.Length > 0) { TransmitAmount = amount.Substring(0, amount.Length - 1); } } /// /// 设置货道物料型号 /// [RelayCommand] private async void SaveMaterialCodeBySpaceInfo() { if (string.IsNullOrEmpty(spaceCodes)) { MessageBox.Show("未关联到货道信息不进行型号设置!"); return; } // 查询货道信息 List spaceList = await _oldBoxFoamTypeServices.QueryAsync(); OldBoxFoamType space = spaceList.Where(x => x.ObjId == int.Parse(spaceCodes)).FirstOrDefault(); space.MaterialCode = PlanInfo.MaterialCode; space.MaterialName = PlanInfo.MaterialName; bool result = await _oldBoxFoamTypeServices.UpdateAsync(space); if (result) { MessageBox.Show("型号设置成功"); } } /// /// 保存数据 /// [RelayCommand] private async void AddPlanInfo() { try { if (string.IsNullOrEmpty(TransmitAmount)) { MessageBox.Show("请输入下达的计划数量,不允许为空!!!"); return; } var productPlanInfo = _planInfo; if (_planInfo != null) { BoxFoamPlan plan = new BoxFoamPlan(); plan.MaterialCode = _planInfo.MaterialCode; plan.MaterialName = _planInfo.MaterialName; plan.PlanAmount = Convert.ToInt32(TransmitAmount); plan.ProductLineCode = "CX_01"; plan.StationCode = "1105"; plan.UpdateTime = DateTime.Now; plan.CreateTime = DateTime.Now; plan.OrderCode = _planInfo.OrderCode; plan.PlanCode = _planInfo.PlanCode; plan.ShiftType = 1; var result = _boxFoamPlanServices.AddAsync(plan).Result; if (result >= 0) { MessageBox.Show("计划添加成功!", "系统提醒"); WeakReferenceMessenger.Default.Send("RefreshTask");//刷新任务界面 if (!string.IsNullOrEmpty(spaceCodes)) { // 查询货道信息 List spaceList = await _oldBoxFoamTypeServices.QueryAsync(); OldBoxFoamType space = spaceList.Where(x => x.ObjId == int.Parse(spaceCodes)).FirstOrDefault(); space.MaterialCode = PlanInfo.MaterialCode; space.MaterialName = PlanInfo.MaterialName; await _oldBoxFoamTypeServices.UpdateAsync(space); } } else { MessageBox.Show("任务添加失败!", "系统提醒"); } } }catch(Exception ex) { MessageBox.Show($"生产计划创建异常:{ex.Message}", "系统异常"); } } /// /// 关闭 /// /// [RelayCommand] private void CloseWindow(object parameter) { var window = parameter as Window; if (window != null) { window.Close(); } } } }