|
- using BPASmart.Model.配方;
- using BPASmart.RecipeManagement.Globle;
- 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 MaterialConfigureViewModel:ObservableObject
- {
-
-
- public string MaterialName { get { return _materialName; } set { _materialName = value; OnPropertyChanged(); } }
- private string _materialName;
-
- public ObservableCollection<MaterialType> MaterialTypes { get; set; } = new ObservableCollection<MaterialType>();
-
- public MaterialType MaterialType { get{ return _materialType; } set { _materialType = value; OnPropertyChanged(); } }
- private MaterialType _materialType = 0;
-
- public string MaterialPosion { get { return _materialPosion; } set { _materialPosion = value; OnPropertyChanged(); } }
- private string _materialPosion;
-
- public string ErrorMessage { get { return _errorMessage; } set { _errorMessage = value; OnPropertyChanged(); } }
- private string _errorMessage;
-
- public RelayCommand SaveCommand { get; set; }
-
- public MaterialConfigureViewModel()
- {
- if(GlobleData.ChangeMaterail != null)
- {
- MaterialName = GlobleData.ChangeMaterail.Name;
- MaterialType = GlobleData.ChangeMaterail.MaterialType;
- MaterialPosion = GlobleData.ChangeMaterail.MaterialPosion;
- }
-
- foreach(MaterialType item in Enum.GetValues(typeof(MaterialType)))
- {
- MaterialTypes.Add(item);
- }
-
-
- SaveCommand = new RelayCommand(() =>
- {
- if(MaterialName == null)
- {
- ErrorMessage = "原料名称不能为空";
- return;
- }
- if(GlobleData.ChangeMaterail!=null)//编辑原料
- {
- var res = Json<LocalMaterails>.Data.locaMaterails.FirstOrDefault(p=>p.ID == GlobleData.ChangeMaterail.ID);
- if (res != null)
- {
- res.Name = MaterialName;
- res.MaterialType = MaterialType;
- res.MaterialPosion = MaterialPosion;
- Json<LocalMaterails>.Save();
- ErrorMessage = "保存成功";
- }
- }
- else //添加新原料
- {
- if(Json<LocalMaterails>.Data.locaMaterails != null)
- {
- var res = Json<LocalMaterails>.Data.locaMaterails.FirstOrDefault(p => p.Name == MaterialName);
- if (res != null)
- {
- ErrorMessage = "原料名称已存在";
- return;
- }
- Json<LocalMaterails>.Data.locaMaterails.Add(new RecipeMaterials
- {
- ID = Guid.NewGuid().ToString(),
- Name = MaterialName,
- MaterialType = MaterialType,
- MaterialPosion = MaterialPosion
- });
- Json<LocalMaterails>.Save();
- ErrorMessage = "保存成功";
- }
- else
- {
- Json<LocalMaterails>.Data.locaMaterails.Add(new RecipeMaterials
- {
- ID = Guid.NewGuid().ToString(),
- Name = MaterialName,
- MaterialType = MaterialType,
- MaterialPosion = MaterialPosion
- });
- Json<LocalMaterails>.Save();
- ErrorMessage = "保存成功";
- }
-
- }
- });
- }
- }
- }
|