|
- using BPASmart.Model;
- using BPASmartClient.Helper;
- using Microsoft.Toolkit.Mvvm.ComponentModel;
- using Microsoft.Toolkit.Mvvm.Input;
- using System;
- using System.Collections.Generic;
- using System.Collections.ObjectModel;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
-
- namespace BPASmart.RecipeManagement.ViewModel
- {
- public class RecipesConfigureViewModel : ObservableObject
- {
- public ObservableCollection<RecipeMaterials> recipeMaterials { get; set; } = new ObservableCollection<RecipeMaterials>();
-
- /// <summary>
- /// 原料名称集合
- /// </summary>
- public Dictionary<string, string> materialsName { get; set; } = new Dictionary<string, string>();
-
- /// <summary>
- /// 配方名称
- /// </summary>
- public string RecipeName { get { return _repiceName; } set { _repiceName = value; OnPropertyChanged(); } }
- private string _repiceName;
-
-
-
-
- public string ErrorMessage { get { return _errorMessage; } set { _errorMessage = value; OnPropertyChanged(); } }
- private string _errorMessage;
-
- public RelayCommand AddMaterailsCommand { get; set; }
-
- public RelayCommand SaveCommand { get; set; }
-
- public RelayCommand<object> DeleteCommand { get; set; }
-
- private void Delete(object o)
- {
- if (o == null) return;
- if (o is string id)
- {
- var res = recipeMaterials.FirstOrDefault(p => p.ID == id);
- if (res != null) recipeMaterials.Remove(res);
- }
- }
-
- public RecipesConfigureViewModel()
- {
- AddMaterailsCommand = new RelayCommand(() =>
- {
- recipeMaterials.Add(new RecipeMaterials()
- {
- //ID = Guid.NewGuid().ToString()
- });
- });
-
-
-
- SaveCommand = new RelayCommand(() =>
- {
- if (RecipeName == null)
- {
- ErrorMessage = "配方名称为空";
- return;
- }
-
- recipeMaterials?.ToList().ForEach(p => {
-
- if (materialsName.ContainsKey(p.ID)) p.Name = materialsName[p.ID];
- });
-
- if (Globle.GlobleData.ChangeRecipes != null)
- {
- var res = Json<LocalRecipes>.Data.locaRecipes.FirstOrDefault(p => p.ID == Globle.GlobleData.ChangeRecipes.ID);
- res.recipeMaterials = recipeMaterials;
- res.Name = RecipeName;
- }
- else
- {
- var res = Json<LocalRecipes>.Data.locaRecipes.FirstOrDefault(p => p.Name == RecipeName);
- if (res != null)
- {
- ErrorMessage = "配方名称已存在";
- return;
- }
- Json<LocalRecipes>.Data.locaRecipes.Add(new Recipes
- {
- ID = Guid.NewGuid().ToString(),
- Name = RecipeName,
- recipeMaterials = recipeMaterials
- });
- }
- Json<LocalRecipes>.Save();
- ActionManage.GetInstance.Send("CloseRecipesConfigureView");
- });
-
- DeleteCommand = new RelayCommand<object>(Delete);
-
- if (Json<LocalMaterails>.Data.locaMaterails.Count > 0)
- {
- foreach (var materail in Json<LocalMaterails>.Data.locaMaterails)
- {
- materialsName.Add(materail.ID,materail.Name);
- }
- }
- if (Globle.GlobleData.ChangeRecipes != null)
- {
- RecipeName = Globle.GlobleData.ChangeRecipes.Name.ToString();
-
- recipeMaterials = Globle.GlobleData.ChangeRecipes.recipeMaterials;
-
- }
- }
- }
- }
|