终端一体化运控平台
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

145 line
5.7 KiB

  1. using BPASmart.Model;
  2. using BPASmart.RecipeManagement.Globle;
  3. using BPASmartClient.Helper;
  4. using Microsoft.Toolkit.Mvvm.ComponentModel;
  5. using Microsoft.Toolkit.Mvvm.Input;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Collections.ObjectModel;
  9. using System.Linq;
  10. using System.Text;
  11. using System.Threading.Tasks;
  12. namespace BPASmart.RecipeManagement.ViewModel
  13. {
  14. public class MaterialConfigureViewModel : ObservableObject
  15. {
  16. public ObservableCollection<Property> currentProperty { get; set; } = new ObservableCollection<Property>();
  17. public string MaterialName { get { return _materialName; } set { _materialName = value; OnPropertyChanged(); } }
  18. private string _materialName;
  19. public ObservableCollection<MaterialType> MaterialTypes { get; set; } = new ObservableCollection<MaterialType>();
  20. public MaterialType MaterialType { get { return _materialType; } set { _materialType = value; OnPropertyChanged(); } }
  21. private MaterialType _materialType = 0;
  22. public string MaterialPosion { get { return _materialPosion; } set { _materialPosion = value; OnPropertyChanged(); } }
  23. private string _materialPosion;
  24. public string ErrorMessage { get { return _errorMessage; } set { _errorMessage = value; OnPropertyChanged(); } }
  25. private string _errorMessage;
  26. public RelayCommand SaveCommand { get; set; }
  27. private void AddMaterials()
  28. {
  29. Json<LocalMaterails>.Data.locaMaterails.Add(new RecipeMaterials
  30. {
  31. ID = Guid.NewGuid().ToString(),
  32. Name = MaterialName,
  33. MaterialType = MaterialType,
  34. MaterialPosion = MaterialPosion,
  35. PropertyCollections = currentProperty,
  36. });
  37. }
  38. public MaterialConfigureViewModel()
  39. {
  40. foreach (Property item in Json<LocalMaterails>.Data.PorpertyCollections)
  41. {
  42. currentProperty.Add(new Property
  43. {
  44. PropertyName = item.PropertyName,
  45. PropertyId = item.PropertyId,
  46. _propertyDsecription = item._propertyDsecription
  47. });
  48. }
  49. if (GlobleData.ChangeMaterail != null)
  50. {
  51. MaterialName = GlobleData.ChangeMaterail.Name;
  52. MaterialType = GlobleData.ChangeMaterail.MaterialType;
  53. MaterialPosion = GlobleData.ChangeMaterail.MaterialPosion;
  54. foreach (var item in currentProperty)
  55. {
  56. var res = GlobleData.ChangeMaterail.PropertyCollections.FirstOrDefault(p => p.PropertyId == item.PropertyId);
  57. if (res != null)
  58. {
  59. item.PropertyValue = res.PropertyValue;
  60. }
  61. }
  62. }
  63. foreach (MaterialType item in Enum.GetValues(typeof(MaterialType)))
  64. {
  65. MaterialTypes.Add(item);
  66. }
  67. SaveCommand = new RelayCommand(() =>
  68. {
  69. if (MaterialName == null)
  70. {
  71. ErrorMessage = "原料名称不能为空";
  72. return;
  73. }
  74. if (GlobleData.ChangeMaterail != null)//编辑原料
  75. {
  76. var res = Json<LocalMaterails>.Data.locaMaterails.FirstOrDefault(p => p.ID == GlobleData.ChangeMaterail.ID);
  77. if (res != null)
  78. {
  79. if (MaterialName != res.Name)//修改了原料名称
  80. {
  81. if (Json<LocalMaterails>.Data.locaMaterails.FirstOrDefault(p => p.Name == MaterialName) != null)
  82. {
  83. ErrorMessage = "原料名称已存在";
  84. return;
  85. }
  86. }
  87. if (MaterialPosion != res.MaterialPosion)//修改了原料位置
  88. {
  89. if (Json<LocalMaterails>.Data.locaMaterails.FirstOrDefault(p => p.MaterialPosion == MaterialPosion) != null)
  90. {
  91. ErrorMessage = "原料位置重复";
  92. return;
  93. }
  94. }
  95. res.Name = MaterialName;
  96. res.MaterialType = MaterialType;
  97. res.MaterialPosion = MaterialPosion;
  98. res.PropertyCollections = currentProperty;
  99. }
  100. }
  101. else //添加新原料
  102. {
  103. if (Json<LocalMaterails>.Data.locaMaterails != null)
  104. {
  105. var res = Json<LocalMaterails>.Data.locaMaterails.FirstOrDefault(p => p.Name == MaterialName);
  106. if (res != null)
  107. {
  108. ErrorMessage = "原料名称已存在";
  109. return;
  110. }
  111. var item = Json<LocalMaterails>.Data.locaMaterails.FirstOrDefault(p => p.MaterialPosion == MaterialPosion);
  112. if (item != null)
  113. {
  114. ErrorMessage = "原料位置重复";
  115. }
  116. AddMaterials();
  117. }
  118. else
  119. {
  120. AddMaterials();
  121. }
  122. }
  123. Json<LocalMaterails>.Save();
  124. ActionManage.GetInstance.Send("CloseMaterialConfigureView");
  125. });
  126. }
  127. }
  128. }