终端一体化运控平台
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

171 lines
7.3 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. using System.Windows.Forms;
  13. namespace FryPot_DosingSystem.ViewModel
  14. {
  15. internal class NewRecipeViewModel : ObservableObject
  16. {
  17. /// <summary>
  18. /// 配方唯一编码,用于编辑配方
  19. /// </summary>
  20. public string recipeId { get; set; }
  21. /// <summary>
  22. /// 配方名称
  23. /// </summary>
  24. private string _recipeName;
  25. public string RecipeName { get { return _recipeName; } set { _recipeName = value; OnPropertyChanged(); } }
  26. private int _recipeRollerNum;
  27. /// <summary>
  28. /// 配方中桶数
  29. /// </summary>
  30. public int RecipeRollerNum { get { return _recipeRollerNum; } set { _recipeRollerNum = value; OnPropertyChanged(); } }
  31. public ObservableCollection<MaterialType> materials { get; set; } = new ObservableCollection<MaterialType>();
  32. public ObservableCollection<string> materialNames { get; set; } = new ObservableCollection<string>();
  33. public RelayCommand AddRecipe { get; set; }
  34. public RelayCommand<string> RemoveRecipe { get; set; }
  35. public RelayCommand Comfirm { get; set; }
  36. public RelayCommand SaveAs { get; set; }
  37. public NewRecipeViewModel()
  38. {
  39. Json<MaterialNames>.Read();
  40. MaterialNames.GetInstance.Names = Json<MaterialNames>.Data.Names;
  41. materialNames = Json<MaterialNames>.Data.Names;
  42. ActionManage.GetInstance.Register(new Action<object>(Id =>
  43. {
  44. if (Id != null && Id is string strId)
  45. {
  46. var res = Json<RecipeManage>.Data.Recipes.FirstOrDefault(p => p.RecipeId == strId);
  47. if (res != null && res is NewRecipeModel rom)
  48. {
  49. RecipeName = rom.RecipeName;
  50. RecipeRollerNum = rom.materialCollection.Count;
  51. foreach (var item in rom.materialCollection)
  52. {
  53. materials.Add(item);
  54. }
  55. recipeId = strId;
  56. }
  57. }
  58. }), "EditRecipe");
  59. AddRecipe = new RelayCommand(() =>
  60. {
  61. for (int i = 0; i < RecipeRollerNum; i++)
  62. {
  63. pr1:
  64. string materialCode = Guid.NewGuid().ToString();//原料唯一ID ,后期需要根据实际要求更改
  65. var res = materials.FirstOrDefault(p => p.MaterialCode == materialCode);
  66. if (res == null)
  67. {
  68. materials.Add(new MaterialType() { MaterialCode = materialCode });
  69. }
  70. else
  71. {
  72. goto pr1;
  73. }
  74. }
  75. });
  76. RemoveRecipe = new RelayCommand<string>(code =>
  77. {
  78. var res = materials.FirstOrDefault(m => m.MaterialCode == code);
  79. if (res != null)
  80. materials.Remove(res);
  81. });
  82. Comfirm = new RelayCommand(() =>
  83. {
  84. var bom = Json<RecipeManage>.Data.Recipes.FirstOrDefault(p => p.RecipeId == recipeId);
  85. if (bom == null)//新配方
  86. {
  87. var name = Json<RecipeManage>.Data.Recipes.FirstOrDefault(p => p.RecipeName == RecipeName);
  88. if (name == null)
  89. {
  90. if (RecipeRollerNum != 0)
  91. {
  92. prop: string recipeID = Guid.NewGuid().ToString();//配方唯一ID,后期根据实际要求更改
  93. var res = Json<RecipeManage>.Data.Recipes.FirstOrDefault(p => p.RecipeId == recipeID);
  94. if (res == null)
  95. {
  96. Json<RecipeManage>.Data.Recipes.Add(new NewRecipeModel { RecipeId = recipeID, RecipeName = RecipeName, materialCollection = materials,DataTime=DateTime.Now.ToShortDateString()});//配方添加
  97. }
  98. else
  99. {
  100. goto prop;
  101. }
  102. MessageLog.GetInstance.ShowUserLog($"配方【{RecipeName}】新建成功");
  103. }
  104. else
  105. {
  106. MessageLog.GetInstance.ShowUserLog($"新建配方【{RecipeName}】无效【配方中没有添加原料】");
  107. }
  108. ActionManage.GetInstance.Send("CloseNewRecipeView");
  109. }
  110. else
  111. {
  112. MessageBox.Show("配方名称重复,请重命名!!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  113. }
  114. }
  115. else //已有配方,用于编辑
  116. {
  117. bom.materialCollection = materials;
  118. bom.RecipeName = RecipeName;
  119. bom.UpdateTime = DateTime.Now.ToShortDateString();
  120. Json<RecipeManage>.Save();
  121. ActionManage.GetInstance.Send("CloseNewRecipeView");
  122. }
  123. });
  124. SaveAs = new RelayCommand(() =>
  125. {
  126. var bom = Json<RecipeManage>.Data.Recipes.FirstOrDefault(p => p.RecipeName == RecipeName);
  127. var rec = Json<RecipeManage>.Data.Recipes.FirstOrDefault(p => p.RecipeId == recipeId);
  128. if (bom == null && rec != null)//配方名称更改
  129. {
  130. if (RecipeRollerNum != 0)
  131. {
  132. prop: string recipeID = Guid.NewGuid().ToString();//配方唯一ID,后期根据实际要求更改
  133. var res = Json<RecipeManage>.Data.Recipes.FirstOrDefault(p => p.RecipeId == recipeID);
  134. if (res == null)
  135. {
  136. Json<RecipeManage>.Data.Recipes.Add(new NewRecipeModel { RecipeId = recipeID, RecipeName = RecipeName, materialCollection = materials });//配方添加
  137. }
  138. else
  139. {
  140. goto prop;
  141. }
  142. MessageLog.GetInstance.ShowUserLog("新建配方成功");
  143. }
  144. else
  145. {
  146. MessageLog.GetInstance.ShowUserLog("新建配方无效【配方中没有添加原料】");
  147. }
  148. ActionManage.GetInstance.Send("CloseNewRecipeView");
  149. }
  150. else
  151. {
  152. MessageBox.Show("另存配方失败","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
  153. }
  154. ActionManage.GetInstance.Send("CloseNewRecipeView");
  155. });
  156. }
  157. }
  158. }