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

116 lines
3.7 KiB

  1. using BPASmart.Model.配方;
  2. using BPASmartClient.Helper;
  3. using Microsoft.Toolkit.Mvvm.ComponentModel;
  4. using Microsoft.Toolkit.Mvvm.Input;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Collections.ObjectModel;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. namespace BPASmart.RecipeManagement.ViewModel
  12. {
  13. public class RecipesConfigureViewModel:ObservableObject
  14. {
  15. public ObservableCollection<RecipeMaterials> recipeMaterials { get; set; } = new ObservableCollection<RecipeMaterials>();
  16. /// <summary>
  17. /// 原料名称集合
  18. /// </summary>
  19. public ObservableCollection<string> materialsName { get; set; } = new ObservableCollection<string>();
  20. /// <summary>
  21. /// 配方名称
  22. /// </summary>
  23. public string RecipeName { get { return _repiceName; } set { _repiceName = value; OnPropertyChanged(); } }
  24. private string _repiceName;
  25. public string ErrorMessage { get { return _errorMessage; } set { _errorMessage = value; OnPropertyChanged(); } }
  26. private string _errorMessage;
  27. public RelayCommand AddMaterailsCommand { get; set; }
  28. public RelayCommand SaveCommand { get; set; }
  29. public RelayCommand<object> DeleteCommand { get; set; }
  30. private void Delete(object o)
  31. {
  32. if (o == null) return;
  33. if(o is string id)
  34. {
  35. var res = recipeMaterials.FirstOrDefault(p=>p.ID == id);
  36. if (res != null) recipeMaterials.Remove(res);
  37. }
  38. }
  39. public RecipesConfigureViewModel()
  40. {
  41. AddMaterailsCommand = new RelayCommand(() =>
  42. {
  43. recipeMaterials.Add(new RecipeMaterials()
  44. {
  45. ID = Guid.NewGuid().ToString()
  46. });
  47. });
  48. SaveCommand = new RelayCommand(() =>
  49. {
  50. if(RecipeName == null)
  51. {
  52. ErrorMessage = "配方名称为空";
  53. return;
  54. }
  55. if (Globle.GlobleData.ChangeRecipes!=null)
  56. {
  57. var res = Json<LocalRecipes>.Data.locaRecipes.FirstOrDefault(p => p.ID == Globle.GlobleData.ChangeRecipes.ID);
  58. res.recipeMaterials = recipeMaterials;
  59. res.Name = RecipeName;
  60. }
  61. else
  62. {
  63. var res = Json<LocalRecipes>.Data.locaRecipes.FirstOrDefault(p => p.Name == RecipeName);
  64. if (res != null)
  65. {
  66. ErrorMessage = "配方名称已存在";
  67. return;
  68. }
  69. Json<LocalRecipes>.Data.locaRecipes.Add(new Recipes
  70. {
  71. ID = Guid.NewGuid().ToString() ,
  72. Name = RecipeName,
  73. recipeMaterials = recipeMaterials
  74. });
  75. }
  76. Json<LocalRecipes>.Save();
  77. });
  78. DeleteCommand = new RelayCommand<object>(Delete);
  79. if (Json<LocalMaterails>.Data.locaMaterails.Count > 0)
  80. {
  81. foreach (var materail in Json<LocalMaterails>.Data.locaMaterails)
  82. {
  83. materialsName.Add(materail.Name);
  84. }
  85. }
  86. if(Globle.GlobleData.ChangeRecipes != null)
  87. {
  88. RecipeName = Globle.GlobleData.ChangeRecipes.Name.ToString();
  89. recipeMaterials = Globle.GlobleData.ChangeRecipes.recipeMaterials;
  90. }
  91. }
  92. }
  93. }