using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Microsoft.Toolkit.Mvvm.ComponentModel; using System.Collections.ObjectModel; using Microsoft.Toolkit.Mvvm.Input; using BPASmartClient.Helper; using BPASmartClient.JXJFoodBigStation.Model; using BPASmartClient.CustomResource.Pages.Model; using BPASmartClient.JXJFoodBigStation.Model.Siemens; using System.Windows.Forms; using BPASmartClient.CustomResource.UserControls; using BPASmartClient.CustomResource.UserControls.MessageShow; namespace BPASmartClient.JXJFoodBigStation.ViewModel { public class RecipeInfosViewModel : ObservableObject { public RecipeInfosViewModel() { foreach (var item in ProcessControl.GetInstance.RawMaterialsInfo) { RawMaterialLocation.Add(item.RawMaterialLocation); } ActionManage.GetInstance.Register(new Action((o) => { if (o != null && o is RecipeData rm) { RecipeName = rm.RecipeName; RecipeCode = rm.RecipeCode; TrayCode = rm.TrayCode; IsWashingBarrel = rm.IsWashingBarrel; foreach (var item in rm.RawMaterial) { //item.RawMaterialCount = Array.FindIndex(ProcessControl.GetInstance.RawMaterialsInfo.ToArray(), p => p.RawMaterialName == item.RawMaterialName); RawMaterialsInfo.Add(item); } } }), "RecipeInfo"); ActionManage.GetInstance.Register(new Action(() => { foreach (var item in RawMaterialsInfo) { if (RawMaterialLocation.Contains(item.RawMaterialLocation)) { int index = Array.FindIndex(ProcessControl.GetInstance.RawMaterialsInfo.ToArray(), p => p.RawMaterialLocation == item.RawMaterialLocation); item.RawMaterialName = ProcessControl.GetInstance.RawMaterialsInfo.ElementAt(index).RawMaterialName; item.RawMaterialChineseName = ProcessControl.GetInstance.RawMaterialsInfo.ElementAt(index).RawMaterialChineseName; } } }), "RawMaterialNames",true); AddRecipe = new RelayCommand(() => { RawMaterialsInfo.Add(new RawMaterial()); }); Comfirm = new RelayCommand(() => { var bom= Json.Data.Recipes.FirstOrDefault(p => p.RecipeCode == RecipeCode); if (bom == null)//新配方 { var name= Json.Data.Recipes.FirstOrDefault(p => p.RecipeName == RecipeName); if (name == null) { go: string recipeCode = new Random().Next(10000, 32767).ToString(); var res = Json.Data.Recipes.FirstOrDefault(p => p.RecipeCode == recipeCode); if (res == null) { if (IsWashingBarrel) { Json.Data.Recipes.Add(new RecipeData { RecipeCode = recipeCode,RecipeName = RecipeName, TrayCode = TrayCode, IsWashingBarrel = IsWashingBarrel, OrderType="洗桶" }); if (RawMaterialsInfo.Count > 0) NoticeDemoViewModel.OpenMsg(EnumPromptType.Info, App.MainWindow, "提示", $"订单类型为洗桶,不保存原料信息"); } else { Json.Data.Recipes.Add(new RecipeData { RecipeCode = recipeCode, RawMaterial = RawMaterialsInfo, RecipeName = RecipeName, TrayCode = TrayCode, IsWashingBarrel = IsWashingBarrel, OrderType = "配料" }); } Json.Save(); NoticeDemoViewModel.OpenMsg(EnumPromptType.Success, App.MainWindow, "成功", $"配方保存完成"); } else { goto go; } ActionManage.GetInstance.Send("CloseRecipeInfosView"); } else { MessageBox.Show("配方名称重复,请重命名!!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); } } else//编辑已有配方 { bom.RawMaterial.Clear(); foreach (var item in RawMaterialsInfo) { bom.RawMaterial.Add(item); } bom.RecipeName = RecipeName; bom.TrayCode = TrayCode; bom.IsWashingBarrel = IsWashingBarrel; if (IsWashingBarrel) { bom.RawMaterial.Clear(); } Json.Save(); ActionManage.GetInstance.Send("CloseRecipeInfosView"); } }); RemoveRecipe = new RelayCommand((index) => { var res = RawMaterialsInfo.FirstOrDefault(p => p.RawMaterialLocation == index); if (res != null) RawMaterialsInfo.Remove(res); }); } public string RecipeName { get { return _mRecipeName; } set { _mRecipeName = value; OnPropertyChanged(); } } private string _mRecipeName; public string RecipeCode { get { return _mRecipeCode; } set { _mRecipeCode = value; OnPropertyChanged(); } } private string _mRecipeCode; public int TrayCode { get { return _mTrayCode; } set { _mTrayCode = value; OnPropertyChanged(); } } private int _mTrayCode; public int SelectIndex { get { return _mSelectIndex; } set { _mSelectIndex = value; OnPropertyChanged(); } } private int _mSelectIndex; public bool IsWashingBarrel { get { return _mIsWashingBarrel; } set { _mIsWashingBarrel = value; OnPropertyChanged(); } } private bool _mIsWashingBarrel; public RelayCommand ReturnPage { get; set; } public RelayCommand AddRecipe { get; set; } public RelayCommand Comfirm { get; set; } public RelayCommand RemoveRecipe { get; set; } public ObservableCollection RawMaterialsInfo { get; set; } = new ObservableCollection() ; public ObservableCollection RawMaterialLocation { get; set; } = new ObservableCollection(); } }