|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144 |
- 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 ObservableCollection<Property> currentProperty { get; set; } = new ObservableCollection<Property>();
-
- 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; }
-
-
- private void AddMaterials()
- {
- Json<LocalMaterails>.Data.locaMaterails.Add(new RecipeMaterials
- {
- ID = Guid.NewGuid().ToString(),
- Name = MaterialName,
- MaterialType = MaterialType,
- MaterialPosion = MaterialPosion,
- PropertyCollections = currentProperty,
- });
- }
-
- public MaterialConfigureViewModel()
- {
- foreach (Property item in Json<LocalMaterails>.Data.PorpertyCollections)
- {
- currentProperty.Add(new Property
- {
- PropertyName = item.PropertyName,
- PropertyId = item.PropertyId,
- _propertyDsecription = item._propertyDsecription
- });
- }
- if (GlobleData.ChangeMaterail != null)
- {
- MaterialName = GlobleData.ChangeMaterail.Name;
- MaterialType = GlobleData.ChangeMaterail.MaterialType;
- MaterialPosion = GlobleData.ChangeMaterail.MaterialPosion;
- foreach (var item in currentProperty)
- {
- var res = GlobleData.ChangeMaterail.PropertyCollections.FirstOrDefault(p => p.PropertyId == item.PropertyId);
- if (res != null)
- {
- item.PropertyValue = res.PropertyValue;
- }
-
- }
- }
-
- 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)
- {
- if (MaterialName != res.Name)//修改了原料名称
- {
- if (Json<LocalMaterails>.Data.locaMaterails.FirstOrDefault(p => p.Name == MaterialName) != null)
- {
- ErrorMessage = "原料名称已存在";
- return;
- }
- }
- if (MaterialPosion != res.MaterialPosion)//修改了原料位置
- {
- if (Json<LocalMaterails>.Data.locaMaterails.FirstOrDefault(p => p.MaterialPosion == MaterialPosion) != null)
- {
- ErrorMessage = "原料位置重复";
- return;
- }
- }
- res.Name = MaterialName;
- res.MaterialType = MaterialType;
- res.MaterialPosion = MaterialPosion;
- res.PropertyCollections = currentProperty;
- }
- }
- else //添加新原料
- {
- if (Json<LocalMaterails>.Data.locaMaterails != null)
- {
- var res = Json<LocalMaterails>.Data.locaMaterails.FirstOrDefault(p => p.Name == MaterialName);
- if (res != null)
- {
- ErrorMessage = "原料名称已存在";
- return;
- }
- var item = Json<LocalMaterails>.Data.locaMaterails.FirstOrDefault(p => p.MaterialPosion == MaterialPosion);
- if (item != null)
- {
- ErrorMessage = "原料位置重复";
- }
- AddMaterials();
- }
- else
- {
- AddMaterials();
- }
- }
- Json<LocalMaterails>.Save();
- ActionManage.GetInstance.Send("CloseMaterialConfigureView");
- });
- }
- }
- }
|