终端一体化运控平台
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.
 
 
 

107 lines
4.1 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 string MaterialName { get { return _materialName; } set { _materialName = value; OnPropertyChanged(); } }
  17. private string _materialName;
  18. public ObservableCollection<MaterialType> MaterialTypes { get; set; } = new ObservableCollection<MaterialType>();
  19. public MaterialType MaterialType { get{ return _materialType; } set { _materialType = value; OnPropertyChanged(); } }
  20. private MaterialType _materialType = 0;
  21. public string MaterialPosion { get { return _materialPosion; } set { _materialPosion = value; OnPropertyChanged(); } }
  22. private string _materialPosion;
  23. public string ErrorMessage { get { return _errorMessage; } set { _errorMessage = value; OnPropertyChanged(); } }
  24. private string _errorMessage;
  25. public RelayCommand SaveCommand { get; set; }
  26. public MaterialConfigureViewModel()
  27. {
  28. if(GlobleData.ChangeMaterail != null)
  29. {
  30. MaterialName = GlobleData.ChangeMaterail.Name;
  31. MaterialType = GlobleData.ChangeMaterail.MaterialType;
  32. MaterialPosion = GlobleData.ChangeMaterail.MaterialPosion;
  33. }
  34. foreach(MaterialType item in Enum.GetValues(typeof(MaterialType)))
  35. {
  36. MaterialTypes.Add(item);
  37. }
  38. SaveCommand = new RelayCommand(() =>
  39. {
  40. if(MaterialName == null)
  41. {
  42. ErrorMessage = "原料名称不能为空";
  43. return;
  44. }
  45. if(GlobleData.ChangeMaterail!=null)//编辑原料
  46. {
  47. var res = Json<LocalMaterails>.Data.locaMaterails.FirstOrDefault(p=>p.ID == GlobleData.ChangeMaterail.ID);
  48. if (res != null)
  49. {
  50. res.Name = MaterialName;
  51. res.MaterialType = MaterialType;
  52. res.MaterialPosion = MaterialPosion;
  53. Json<LocalMaterails>.Save();
  54. ErrorMessage = "保存成功";
  55. }
  56. }
  57. else //添加新原料
  58. {
  59. if(Json<LocalMaterails>.Data.locaMaterails != null)
  60. {
  61. var res = Json<LocalMaterails>.Data.locaMaterails.FirstOrDefault(p => p.Name == MaterialName);
  62. if (res != null)
  63. {
  64. ErrorMessage = "原料名称已存在";
  65. return;
  66. }
  67. Json<LocalMaterails>.Data.locaMaterails.Add(new RecipeMaterials
  68. {
  69. ID = Guid.NewGuid().ToString(),
  70. Name = MaterialName,
  71. MaterialType = MaterialType,
  72. MaterialPosion = MaterialPosion
  73. });
  74. Json<LocalMaterails>.Save();
  75. ErrorMessage = "保存成功";
  76. }
  77. else
  78. {
  79. Json<LocalMaterails>.Data.locaMaterails.Add(new RecipeMaterials
  80. {
  81. ID = Guid.NewGuid().ToString(),
  82. Name = MaterialName,
  83. MaterialType = MaterialType,
  84. MaterialPosion = MaterialPosion
  85. });
  86. Json<LocalMaterails>.Save();
  87. ErrorMessage = "保存成功";
  88. }
  89. }
  90. });
  91. }
  92. }
  93. }