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;
namespace Aucma.Core.SheetMetal.ViewModels
{
public partial class CorrespondingModelPageViewModel : ObservableObject
{
#region 刷新创建计划
///
/// 刷新创建计划
///
public delegate void RefreshSetBomDataGrid(string smProductId);
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 AppConfigHelper appConfig = new AppConfigHelper();//读取配置文件保存的查询内容
#region 构造函数
public CorrespondingModelPageViewModel() { }
public CorrespondingModelPageViewModel(string productId)
{
_productPlanInfoServices = App.ServiceProvider.GetService();
_executePlanInfoServices = App.ServiceProvider.GetService();
_smSyncModelSubServices = 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 execList = _executePlanInfoServices.QuerySheetMetalPlanData(station);
if (execList == null) return;
foreach (var item in execList)
{
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,
OrderCode = item.OrderCode,
MaterialCode = item.MaterialCode,
MaterialName = item.MaterialName,
PlanAmount = item.PlanAmount,
ResidueAmount = item.PlanAmount - item.CompleteAmount,
SpliteResidueAmount = item.PlanAmount - residue,
MaterialSpecificatons = item.MaterialSpecificatons,
CompleteAmount = item.CompleteAmount,
StartDate = item.BeginTime
});
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 execList = _executePlanInfoServices.QuerySheetMetalPlanData(station);
if (execList == null) return;
execList = execList.Where(d => d.MaterialName.Contains(obj) || d.OrderCode.Contains(obj) || d.PlanCode.Contains(obj)).ToList();
foreach (var item in execList)
{
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,
MaterialSpecificatons = item.MaterialSpecificatons,
StartDate = item.BeginTime
});
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 orderCode)
{
if (!orderCode.IsNullOrEmpty())
{
//添加BOM对应
//MessageBox.Show(ProductId);
//MessageBox.Show(orderCode);
string station = Appsettings.app("StationInfo", "StationCode");
var execList = _executePlanInfoServices.QuerySheetMetalPlanData(station);
var result= execList.First(d=>d.OrderCode.Equals(orderCode));
SmSyncModelSub sub=new SmSyncModelSub();
sub.SmProductId = ProductId;
sub.OrderCode = result.OrderCode;
sub.PlanCode = result.PlanCode;
sub.MaterialCode = result.MaterialCode;
sub.MaterialName = result.MaterialName;
sub.MaterialModel = result.MaterialSpecificatons;
sub.CreateTime = DateTime.Now;
var backResult= await _smSyncModelSubServices.AddAsync(sub);
if (backResult>0)
{
MessageBox.Show("设置成功!","系统提醒",MessageBoxButton.OK);
RefreshSetBomDataGridEvent?.Invoke(ProductId);
}
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