|
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using BPA.Helper;
-
- using System.Collections.ObjectModel;
- using BPA.Helper;
-
- namespace BPASmartClient.SmallBatchingSystem.ViewModels
- {
- public class NewRecipeViewModel : BaseModel
- {
- public NewRecipeViewModel()
- {
- Json<ConfigInfoModel>.Data.SiloInfoModels.ToList()?.ForEach(item => { SileName.Add(item.SiloName); });
- ActionManage.GetInstance.Register(new Action<object>((o) =>
- {
- if (o != null && o is RecipeInfo tempRecipeInfo)
- {
- RecipeName = tempRecipeInfo.RecipeName;
- tempRecipeInfo.SiloInfoModels.ToList()?.ForEach(item =>
- {
- int tempIndex = Array.FindIndex(SileName.ToArray(), p => p == item.SiloName);
- SiloInfos.Add(new RecipeRawMaterialInfo() { SiloName = item.SiloName, SiloWeight = item.SiloWeight, SelectIndex = tempIndex });
- });
- Index = Array.FindIndex(Json<ConfigInfoModel>.Data.Recipes.ToArray(), p => p.RecipeName == tempRecipeInfo.RecipeName);
- }
- }), "OpenNewRecipe", true);
- AddCommand = new BPARelayCommand(() => { SiloInfos.Add(new RecipeRawMaterialInfo()); });
- CancelCommand = new BPARelayCommand(() => { ActionManage.GetInstance.Send("NewRecipeViewModelClose"); });
- SaveCommand = new BPARelayCommand(() =>
- {
- if (Index >= 0 && Index < Json<ConfigInfoModel>.Data.OutletInfoModels.Count)
- {
- var res = Array.FindIndex(Json<ConfigInfoModel>.Data.Recipes.ToArray(), p => p.RecipeName == RecipeName);
- if (res >= 0 && res != Index)
- {
- ErrorInfo = "配方名称已经存在!";
- return;
- }
-
- Json<ConfigInfoModel>.Data.Recipes.ElementAt(Index).RecipeName = RecipeName;
- Json<ConfigInfoModel>.Data.Recipes.ElementAt(Index).SiloInfoModels.Clear();
- SiloInfos.ToList()?.ForEach(item =>
- {
- Json<ConfigInfoModel>.Data.Recipes.ElementAt(Index).SiloInfoModels.Add(item);
- });
- Control.GetInstance.OperationLog($"{RecipeName} 编辑完成");
- }
- else
- {
- var res = Json<ConfigInfoModel>.Data.Recipes.FirstOrDefault(p => p.RecipeName == RecipeName);
- if (res != null)
- {
- ErrorInfo = "配方名称已经存在!";
- return;
- }
-
- Json<ConfigInfoModel>.Data.Recipes.Add(new RecipeInfo()
- {
- RecipeName = RecipeName,
- SiloInfoModels = SiloInfos
- });
- Control.GetInstance.OperationLog($"{RecipeName} 添加成功");
- }
- ActionManage.GetInstance.Send("NewRecipeViewModelClose");
- });
-
- RemoveCommand = new BPARelayCommand<object>((o) =>
- {
- if (!string.IsNullOrEmpty(o?.ToString()))
- {
- var res = SiloInfos.FirstOrDefault(p => p.SiloName == o.ToString());
- if (res != null) SiloInfos.Remove(res);
- }
- });
- }
-
- public string RecipeName { get { return _mRecipeName; } set { _mRecipeName = value; OnPropertyChanged(); } }
- private string _mRecipeName;
-
- public ObservableCollection<RecipeRawMaterialInfo> SiloInfos { get; set; } = new ObservableCollection<RecipeRawMaterialInfo>();
-
- public ObservableCollection<string> SileName { get; set; } = new ObservableCollection<string>();
-
- }
- }
|