|
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using Microsoft.Toolkit.Mvvm.ComponentModel;
- using Microsoft.Toolkit.Mvvm.Input;
- 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 RelayCommand(() => { SiloInfos.Add(new RecipeRawMaterialInfo()); });
- CancelCommand = new RelayCommand(() => { ActionManage.GetInstance.Send("NewRecipeViewModelClose"); });
- SaveCommand = new RelayCommand(() =>
- {
- 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 RelayCommand<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>();
-
- }
- }
|