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

123 lines
4.9 KiB

  1. using BPASmartClient.CustomResource.Pages.Model;
  2. using BPASmartClient.Helper;
  3. using FryPot_DosingSystem.Model;
  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 FryPot_DosingSystem.ViewModel
  13. {
  14. internal class NewRecipeViewModel : ObservableObject
  15. {
  16. /// <summary>
  17. /// 配方唯一编码,用于编辑配方
  18. /// </summary>
  19. public string recipeId { get; set; }
  20. /// <summary>
  21. /// 配方名称
  22. /// </summary>
  23. private string _recipeName;
  24. public string RecipeName { get { return _recipeName; } set { _recipeName = value; OnPropertyChanged(); } }
  25. private int _recipeRollerNum;
  26. /// <summary>
  27. /// 配方中桶数
  28. /// </summary>
  29. public int RecipeRollerNum { get { return _recipeRollerNum; } set { _recipeRollerNum = value; OnPropertyChanged(); } }
  30. public ObservableCollection<MaterialType> materials { get; set; } = new ObservableCollection<MaterialType>();
  31. public ObservableCollection<string> materialNames { get; set; } = new ObservableCollection<string>();
  32. public RelayCommand AddRecipe { get; set; }
  33. public RelayCommand<string> RemoveRecipe { get; set; }
  34. public RelayCommand Comfirm { get; set; }
  35. public NewRecipeViewModel()
  36. {
  37. Json<MaterialNames>.Read();
  38. MaterialNames.GetInstance.Names = Json<MaterialNames>.Data.Names;
  39. materialNames = Json<MaterialNames>.Data.Names;
  40. ActionManage.GetInstance.Register(new Action<object>(Id =>
  41. {
  42. if (Id != null && Id is string strId)
  43. {
  44. var res = Json<RecipeManage>.Data.Recipes.FirstOrDefault(p => p.RecipeId == strId);
  45. if (res != null && res is NewRecipeModel rom)
  46. {
  47. RecipeName = rom.RecipeName;
  48. RecipeRollerNum = rom.materialCollection.Count;
  49. foreach (var item in rom.materialCollection)
  50. {
  51. materials.Add(item);
  52. }
  53. recipeId = strId;
  54. }
  55. }
  56. }), "EditRecipe");
  57. AddRecipe = new RelayCommand(() =>
  58. {
  59. for (int i = 0; i < RecipeRollerNum; i++)
  60. {
  61. pr1:
  62. string materialCode = Guid.NewGuid().ToString();//原料唯一ID ,后期需要根据实际要求更改
  63. var res = materials.FirstOrDefault(p => p.MaterialCode == materialCode);
  64. if (res == null)
  65. {
  66. materials.Add(new MaterialType() { MaterialCode = materialCode });
  67. }
  68. else
  69. {
  70. goto pr1;
  71. }
  72. }
  73. });
  74. RemoveRecipe = new RelayCommand<string>(code =>
  75. {
  76. var res = materials.FirstOrDefault(m => m.MaterialCode == code);
  77. if (res != null)
  78. materials.Remove(res);
  79. });
  80. Comfirm = new RelayCommand(() =>
  81. {
  82. var bom = Json<RecipeManage>.Data.Recipes.FirstOrDefault(p => p.RecipeId == recipeId);
  83. if (bom == null)//新配方
  84. {
  85. if (RecipeRollerNum != 0)
  86. {
  87. prop: string recipeID = Guid.NewGuid().ToString();//配方唯一ID,后期根据实际要求更改
  88. var res = Json<RecipeManage>.Data.Recipes.FirstOrDefault(p => p.RecipeId == recipeID);
  89. if (res == null)
  90. {
  91. Json<RecipeManage>.Data.Recipes.Add(new NewRecipeModel { RecipeId = recipeID, RecipeName = RecipeName, materialCollection = materials });//配方添加
  92. }
  93. else
  94. {
  95. goto prop;
  96. }
  97. MessageLog.GetInstance.ShowUserLog("新建配方成功");
  98. }
  99. else
  100. {
  101. MessageLog.GetInstance.ShowUserLog("新建配方无效【配方中没有添加原料】");
  102. }
  103. ActionManage.GetInstance.Send("CloseNewRecipeView");
  104. }
  105. else //已有配方,用于编辑
  106. {
  107. bom.materialCollection = materials;
  108. bom.RecipeName = RecipeName;
  109. ActionManage.GetInstance.Send("CloseNewRecipeView");
  110. }
  111. });
  112. }
  113. }
  114. }