终端一体化运控平台
Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 

120 linhas
3.9 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 Dictionary<string, string> materialsName { get; set; } = new Dictionary<string, 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. recipeMaterials?.ToList().ForEach(p => {
  56. if (materialsName.ContainsKey(p.ID)) p.Name = materialsName[p.ID];
  57. });
  58. if (Globle.GlobleData.ChangeRecipes != null)
  59. {
  60. var res = Json<LocalRecipes>.Data.locaRecipes.FirstOrDefault(p => p.ID == Globle.GlobleData.ChangeRecipes.ID);
  61. res.recipeMaterials = recipeMaterials;
  62. res.Name = RecipeName;
  63. }
  64. else
  65. {
  66. var res = Json<LocalRecipes>.Data.locaRecipes.FirstOrDefault(p => p.Name == RecipeName);
  67. if (res != null)
  68. {
  69. ErrorMessage = "配方名称已存在";
  70. return;
  71. }
  72. Json<LocalRecipes>.Data.locaRecipes.Add(new Recipes
  73. {
  74. ID = Guid.NewGuid().ToString(),
  75. Name = RecipeName,
  76. recipeMaterials = recipeMaterials
  77. });
  78. }
  79. Json<LocalRecipes>.Save();
  80. ActionManage.GetInstance.Send("CloseRecipesConfigureView");
  81. });
  82. DeleteCommand = new RelayCommand<object>(Delete);
  83. if (Json<LocalMaterails>.Data.locaMaterails.Count > 0)
  84. {
  85. foreach (var materail in Json<LocalMaterails>.Data.locaMaterails)
  86. {
  87. materialsName.Add(materail.ID,materail.Name);
  88. }
  89. }
  90. if (Globle.GlobleData.ChangeRecipes != null)
  91. {
  92. RecipeName = Globle.GlobleData.ChangeRecipes.Name.ToString();
  93. recipeMaterials = Globle.GlobleData.ChangeRecipes.recipeMaterials;
  94. }
  95. }
  96. }
  97. }