using BPA.Helper; using BPASmartClient.CustomResource.Pages.Model; using BPASmartClient.CustomResource.UserControls; using BPASmartClient.CustomResource.UserControls.MessageShow; using BPASmartClient.Academy.Model; using BPASmartClient.Model; using System; using System.Collections.ObjectModel; using System.Linq; using System.Text; namespace BPASmartClient.Academy.ViewModel { public class NewRecipeViewModel : ViewModelBase { public NewRecipeViewModel() { ActionManage.GetInstance.Register(new Action((o) => { if (o != null && o is RecipeModel rm) { RecipeName = rm.RecipeName; foreach (var item in rm.RawMaterials) { item.SelectIndex = Array.FindIndex(RawMaterialNames.ToArray(), p => p == item.RawMaterialName); RawMaterials.Add(item); } RecipCode = rm.RecipCode; Index = Array.FindIndex(Json.Data.Recipes.ToArray(), p => p.RecipeName == RecipeName); } }), "Details", true); AddCommand = new BPARelayCommand(() => { p: string guid = Guid.NewGuid().ToString(); if (RawMaterials.FirstOrDefault(p => p.RawMaterialId == guid) == null) { int index = RawMaterials.Count; RawMaterials.Add(new RawMaterialModel() { RawMaterialId = guid, SelectIndex = index }); } else goto p; }); RemoveCommand = new BPARelayCommand((obj) => { if (obj is string rm) { var res = RawMaterials.FirstOrDefault(p => p.RawMaterialId == rm); if (res != null) RawMaterials.Remove(res); } }); SaveCommand = new BPARelayCommand(() => { if (string.IsNullOrEmpty(RecipeName)) { MessageNotify.GetInstance.ShowDialog("请输入配方名称!", DialogType.Warning); ErrorInfo = "请输入配方名称"; return; } var tempRes = RawMaterials.GroupBy(p => p.RawMaterialName); //编辑配方 if (Index >= 0 && Index < Json.Data.Recipes.Count) { var res = Array.FindIndex(Json.Data.Recipes.ToArray(), p => p.RecipeName == RecipeName); if (res >= 0 && res != Index) { ErrorInfo = "配方名称已经存在!"; MessageNotify.GetInstance.ShowDialog("配方名称已经存在!", DialogType.Warning); return; } Json.Data.Recipes.ElementAt(Index).RecipeName = RecipeName; Json.Data.Recipes.ElementAt(Index).RawMaterials.Clear(); RawMaterials.ToList()?.ForEach(item => { Json.Data.Recipes.ElementAt(Index).RawMaterials.Add(item); }); Json.Save(); NoticeDemoViewModel.OpenMsg(EnumPromptType.Success, App.MainWindow, "提示", $"{RecipeName} 配方编辑完成"); } else //新建配方 { if (Json.Data.Recipes.FirstOrDefault(p => p.RecipeName == RecipeName) != null) { ErrorInfo = "配方名称已存在!"; MessageNotify.GetInstance.ShowDialog("配方名称已经存在!", DialogType.Warning); return; } ObservableCollection TempRawMaterials = new ObservableCollection(); RawMaterials.ToList()?.ForEach(item => { TempRawMaterials.Add(item); }); Json.Data.Recipes.Add(new RecipeModel() { RecipeName = RecipeName, RawMaterials = TempRawMaterials, }); Json.Save(); NoticeDemoViewModel.OpenMsg(EnumPromptType.Success, App.MainWindow, "提示", $"新建配方成功!"); } ActionManage.GetInstance.Send("CloseNewRecipeView"); }); if (Global.userInfo.permission == CustomResource.Pages.Enums.Permission.管理员) { foreach (var item in Global.DeviceRawMaterials) { RawMaterialNames.Add(item.RawMaterialName); } foreach (var item in Json.Data.LocalMaterails) { RawMaterialNames.Add(item.RawMaterialName); } } else { foreach (var item in Global.DeviceRawMaterials) { if (Global.userInfo.devRawMaterials.FirstOrDefault(p => p.RawMaterialName == item.RawMaterialName) != null) RawMaterialNames.Add(item.RawMaterialName); } foreach (var item in Json.Data.LocalMaterails) { if (Global.userInfo.locaRawMaterials.FirstOrDefault(p => p.RawMaterialName == item.RawMaterialName) != null) RawMaterialNames.Add(item.RawMaterialName); } } } private void AddRecipes() { string date = DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss"); var dates = date.Split("-").ToList(); StringBuilder sb = new StringBuilder(); dates?.ForEach((item) => { sb.Append(item); }); Json.Data.Recipes.Add(new RecipeModel() { SerialNum = Json.Data.Recipes.Count + 1, RawMaterials = RawMaterials, RecipCode = sb.ToString(), RecipeName = RecipeName, }); } private string RecipCode = string.Empty; public string RecipeName { get { return _mRecipeName; } set { _mRecipeName = value; OnPropertyChanged(); } } private string _mRecipeName = string.Empty; public ObservableCollection RawMaterials { get; set; } = new ObservableCollection(); public ObservableCollection RawMaterialNames { get; set; } = new ObservableCollection(); } }