|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115 |
- 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 ObservableCollection<string> materialsName { get; set; } = new ObservableCollection<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;
- }
-
- 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();
- });
-
- DeleteCommand = new RelayCommand<object>(Delete);
-
- if (Json<LocalMaterails>.Data.locaMaterails.Count > 0)
- {
- foreach (var materail in Json<LocalMaterails>.Data.locaMaterails)
- {
- materialsName.Add(materail.Name);
- }
- }
- if(Globle.GlobleData.ChangeRecipes != null)
- {
- RecipeName = Globle.GlobleData.ChangeRecipes.Name.ToString();
-
- recipeMaterials = Globle.GlobleData.ChangeRecipes.recipeMaterials;
-
- }
- }
- }
- }
|