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; namespace BPASmartClient.JXJFoodBigStation.ViewModel { public class RecipeInfosViewModel : ObservableObject { public RecipeInfosViewModel() { ActionManage.GetInstance.Register(new Action((o) => { if (o != null && o is RecipeData rm) { RecipeName = rm.RecipeName; RecipeCode = rm.RecipeCode; TrayCode = rm.TrayCode; //RawMaterialsInfo = rm.RawMaterial; //var rest = RawMaterialsInfo.GetHashCode(); foreach (var item in rm.RawMaterial) { RawMaterialsInfo.Add(item); } } }), "RecipeInfo"); 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) { Json.Data.Recipes.Add(new RecipeData { RecipeCode = recipeCode, RawMaterial= RawMaterialsInfo,RecipeName=RecipeName,TrayCode=TrayCode}); Json.Save(); } 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; Json.Save(); ActionManage.GetInstance.Send("CloseRecipeInfosView"); } }); SaveAs = new RelayCommand(() => { var bom = Json.Data.Recipes.FirstOrDefault(p => p.RecipeName == RecipeName); var rec = Json.Data.Recipes.FirstOrDefault(p => p.RecipeCode == RecipeCode); if (bom == null && rec != null)//配方名称更改 { prop: string recipeCode = new Random().Next(10000, 99999).ToString();//配方唯一ID,后期根据实际要求更改 var res = Json.Data.Recipes.FirstOrDefault(p => p.RecipeCode == recipeCode); if (res == null) { Json.Data.Recipes.Add(new RecipeData { RecipeCode = recipeCode, RawMaterial = RawMaterialsInfo, RecipeName = RecipeName, TrayCode = TrayCode });//配方添加 Json.Save(); } else { goto prop; } ActionManage.GetInstance.Send("CloseRecipeInfosView"); } else { MessageBox.Show("另存配方失败", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); } ActionManage.GetInstance.Send("CloseNewRecipeView"); }); RemoveRecipe = new RelayCommand((materilaName) => { var res = RawMaterialsInfo.FirstOrDefault(p => p.RawMaterialName == materilaName); 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 RelayCommand ReturnPage { get; set; } public RelayCommand AddRecipe { get; set; } public RelayCommand Comfirm { get; set; } public RelayCommand SaveAs { get; set; } public RelayCommand RemoveRecipe { get; set; } public ObservableCollection RawMaterialsInfo { get; set; } = new ObservableCollection() ; } }