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 Admin.Core.Service; using Admin.Core.Model.Model_New; using Admin.Core.Common; using CommunityToolkit.Mvvm.Messaging; using static System.Windows.Forms.VisualStyles.VisualStyleElement.StartPanel; namespace Aucma.Core.SheetMetal.ViewModels { public partial class SplitPlanViewModel : ObservableObject { private static readonly log4net.ILog log = LogManager.GetLogger(typeof(IndexPageViewModel)); protected readonly IProductPlanInfoServices? _productPlanInfoServices; //protected readonly ISmTaskExecutionServices? _smTaskExecutionServices; protected readonly IExecutePlanInfoServices? _executePlanInfoServices; public SplitPlanViewModel() { _productPlanInfoServices = App.ServiceProvider.GetService(); _executePlanInfoServices = App.ServiceProvider.GetService(); LoadData(); //WeakReferenceMessenger.Default.Register; } #region 加载DataGrid数据 private async void LoadData() { int i = 1; string station = "1001"; var planlist = await _productPlanInfoServices.QueryAsync(d=>d.ProductLineCode.Equals(station)); 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 PlanMaintenanceModel() { 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++; } } private async void LoadData(string obj) { int i = 1; string station = "1001"; var planlist = await _productPlanInfoServices.QueryAsync(d => d.ProductLineCode.Equals(station)); var queryList = planlist.Where(d=>d.OrderCode.Contains(obj)|| d.MaterialCode.Contains(obj) || d.MaterialName.Contains(obj)); var execList = await _executePlanInfoServices.QueryAsync(d => d.ProductLineCode.Equals(station)); 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 PlanMaintenanceModel() { 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 初始化datagrid private ObservableCollection materialDataGrid = new ObservableCollection(); public ObservableCollection MaterialDataGrid { get { return materialDataGrid; } set { materialDataGrid = value; OnPropertyChanged();//属性通知 } } #endregion #region 查询 /// /// 查询 /// [RelayCommand] private void QueryPlan(string obj) { if (obj.IsNullOrEmpty()) { System.Windows.Application.Current.Dispatcher.Invoke((Action)(() => { MaterialDataGrid.Clear(); LoadData(); })); return; } MaterialDataGrid.Clear(); 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 = "1001";//计划工位 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 PlanMaintenanceModel selectedCells; public PlanMaintenanceModel SelectedCells { get { return selectedCells; } set { selectedCells = value; SetProperty(ref selectedCells, value); } } #endregion #region 关闭当前窗口 /// /// 关闭当前窗口 /// public void CloseWindow() { WeakReferenceMessenger.Default.Send("close"); } #endregion } }