终端一体化运控平台
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 

233 рядки
12 KiB

  1. using BPASmartClient.CustomResource.Pages.Model;
  2. using BPASmartClient.CustomResource.UserControls;
  3. using BPASmartClient.CustomResource.UserControls.MessageShow;
  4. using BPASmartClient.Helper;
  5. using FryPot_DosingSystem.Model;
  6. using Microsoft.Toolkit.Mvvm.ComponentModel;
  7. using Microsoft.Toolkit.Mvvm.Input;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Collections.ObjectModel;
  11. using System.Linq;
  12. using System.Text;
  13. using System.Threading.Tasks;
  14. using System.Windows.Forms;
  15. namespace FryPot_DosingSystem.ViewModel
  16. {
  17. internal class NewRecipeViewModel : ObservableObject
  18. {
  19. /// <summary>
  20. /// 配方唯一编码,用于编辑配方
  21. /// </summary>
  22. public string recipeId { get; set; }
  23. /// <summary>
  24. /// 配方名称
  25. /// </summary>
  26. private string _recipeName;
  27. public string RecipeName { get { return _recipeName; } set { _recipeName = value; OnPropertyChanged(); } }
  28. private int _recipeRollerNum;
  29. /// <summary>
  30. /// 配方中桶数
  31. /// </summary>
  32. public int RecipeRollerNum { get { return _recipeRollerNum; } set { _recipeRollerNum = value; OnPropertyChanged(); } }
  33. public ObservableCollection<MaterialType> materials { get; set; } = new ObservableCollection<MaterialType>();
  34. public ObservableCollection<string> materialNames { get; set; } = new ObservableCollection<string>();
  35. public RelayCommand AddRecipe { get; set; }
  36. public RelayCommand<string> RemoveRecipe { get; set; }
  37. public RelayCommand Comfirm { get; set; }
  38. public RelayCommand SaveAs { get; set; }
  39. public NewRecipeViewModel()
  40. {
  41. Json<MaterialNames>.Read();
  42. MaterialNames.GetInstance.Names = Json<MaterialNames>.Data.Names;
  43. materialNames = Json<MaterialNames>.Data.Names;
  44. ActionManage.GetInstance.Register(new Action<object>(Id =>
  45. {
  46. if (Id != null && Id is string strId)
  47. {
  48. var res = Json<RecipeManage>.Data.Recipes.FirstOrDefault(p => p.RecipeId == strId);
  49. if (res != null && res is NewRecipeModel rom)
  50. {
  51. RecipeName = rom.RecipeName;
  52. RecipeRollerNum = rom.materialCollection.Count;
  53. foreach (var item in rom.materialCollection)
  54. {
  55. materials.Add(item);
  56. }
  57. recipeId = strId;
  58. }
  59. }
  60. }), "EditRecipe");
  61. AddRecipe = new RelayCommand(() =>
  62. {
  63. for (int i = 0; i < RecipeRollerNum; i++)
  64. {
  65. pr1:
  66. string materialCode = Guid.NewGuid().ToString();//原料唯一ID ,后期需要根据实际要求更改
  67. var res = materials.FirstOrDefault(p => p.MaterialCode == materialCode);
  68. if (res == null)
  69. {
  70. materials.Add(new MaterialType() { MaterialCode = materialCode });
  71. }
  72. else
  73. {
  74. goto pr1;
  75. }
  76. }
  77. });
  78. RemoveRecipe = new RelayCommand<string>(code =>
  79. {
  80. var res = materials.FirstOrDefault(m => m.MaterialCode == code);
  81. if (res != null)
  82. materials.Remove(res);
  83. });
  84. Comfirm = new RelayCommand(() =>
  85. {
  86. var bom = Json<RecipeManage>.Data.Recipes.FirstOrDefault(p => p.RecipeId == recipeId);
  87. if (bom == null)//新配方
  88. {
  89. var name = Json<RecipeManage>.Data.Recipes.FirstOrDefault(p => p.RecipeName == RecipeName);
  90. if (name == null)
  91. {
  92. if (materials.Count<=8&& materials.Count> 0)
  93. {
  94. int lineNum = materials.ElementAt(0).MaterialLoc / 100;
  95. //桶号正确性验证
  96. for (int i = 0; i < materials.Count; i++)
  97. {
  98. if (materials.ElementAt(i).MaterialLoc / 100 != lineNum || materials.ElementAt(i).MaterialLoc % 100 != i + 1 || lineNum <= 0 || lineNum > 5)
  99. {
  100. MessageNotify.GetInstance.ShowUserLog($"新建配方【{RecipeName}】无效:【配方中原料桶号异常】");
  101. NoticeDemoViewModel.OpenMsg(EnumPromptType.Warn, App.MainWindow, "提示", $"新建配方【{RecipeName}】无效");
  102. ActionManage.GetInstance.Send("CloseNewRecipeView");
  103. return;
  104. }
  105. }
  106. prop: string recipeID = Guid.NewGuid().ToString();//配方唯一ID,后期根据实际要求更改
  107. var res = Json<RecipeManage>.Data.Recipes.FirstOrDefault(p => p.RecipeId == recipeID);
  108. if (res == null)
  109. {
  110. Json<RecipeManage>.Data.Recipes.Add(new NewRecipeModel { RecipeId = recipeID, RecipeName = RecipeName, FlowProcess=new FlowProcessManage(), materialCollection = materials,DataTime=DateTime.Now.ToShortDateString()});//配方添加
  111. }
  112. else
  113. {
  114. goto prop;
  115. }
  116. MessageNotify.GetInstance.ShowUserLog($"配方【{RecipeName}】新建成功");
  117. NoticeDemoViewModel.OpenMsg(EnumPromptType.Success, App.MainWindow, "提示", $"新建配方【{RecipeName}】成功");
  118. }
  119. else
  120. {
  121. MessageNotify.GetInstance.ShowUserLog($"新建配方【{RecipeName}】无效:【配方中原料桶数异常】");
  122. NoticeDemoViewModel.OpenMsg(EnumPromptType.Error, App.MainWindow, "提示", $"新建配方【{RecipeName}】无效");
  123. }
  124. ActionManage.GetInstance.Send("CloseNewRecipeView");
  125. }
  126. else
  127. {
  128. MessageBox.Show("配方名称重复或为空,请重命名!!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  129. }
  130. }
  131. else //已有配方,用于编辑
  132. {
  133. if (materials.Count > 0 && materials.Count <= 8)
  134. {
  135. int lineNum = materials.ElementAt(0).MaterialLoc / 100;
  136. //桶号正确性验证
  137. for (int i = 0; i < materials.Count; i++)
  138. {
  139. if (materials.ElementAt(i).MaterialLoc / 100 != lineNum || materials.ElementAt(i).MaterialLoc % 100 != i + 1 || lineNum <= 0 || lineNum > 5)
  140. {
  141. MessageNotify.GetInstance.ShowUserLog($"另存配方【{RecipeName}】无效:【配方中原料桶号异常】");
  142. NoticeDemoViewModel.OpenMsg(EnumPromptType.Error, App.MainWindow, "提示", $"另存配方【{RecipeName}】无效");
  143. ActionManage.GetInstance.Send("CloseNewRecipeView");
  144. return;
  145. }
  146. }
  147. bom.materialCollection = materials;
  148. bom.RecipeName = RecipeName;
  149. bom.UpdateTime = DateTime.Now.ToShortDateString();
  150. if (bom.FlowProcess != null)
  151. bom.FlowProcess.RecipeName = RecipeName;
  152. Json<RecipeManage>.Save();
  153. MessageNotify.GetInstance.ShowUserLog($"配方【{RecipeName}】修改成功");
  154. NoticeDemoViewModel.OpenMsg(EnumPromptType.Success, App.MainWindow, "提示", $"修改配方【{RecipeName}】成功");
  155. }
  156. else
  157. {
  158. MessageNotify.GetInstance.ShowUserLog($"修改配方【{RecipeName}】无效:【配方中原料桶数异常】");
  159. NoticeDemoViewModel.OpenMsg(EnumPromptType.Error, App.MainWindow, "提示", $"修改配方【{RecipeName}】无效");
  160. }
  161. ActionManage.GetInstance.Send("CloseNewRecipeView");
  162. }
  163. });
  164. SaveAs = new RelayCommand(() =>
  165. {
  166. var bom = Json<RecipeManage>.Data.Recipes.FirstOrDefault(p => p.RecipeName == RecipeName);
  167. var rec = Json<RecipeManage>.Data.Recipes.FirstOrDefault(p => p.RecipeId == recipeId);
  168. if (bom == null && rec != null)//配方名称更改
  169. {
  170. if (materials.Count>0&& materials.Count<=8)//验证配方中原料桶数
  171. {
  172. int lineNum = materials.ElementAt(0).MaterialLoc / 100;
  173. //桶号正确性验证
  174. for (int i = 0; i < materials.Count; i++)
  175. {
  176. if (materials.ElementAt(i).MaterialLoc / 100 != lineNum || materials.ElementAt(i).MaterialLoc % 100 != i + 1 || lineNum <= 0 || lineNum > 5)
  177. {
  178. MessageNotify.GetInstance.ShowUserLog($"另存配方【{RecipeName}】无效:【配方中原料桶号异常】");
  179. NoticeDemoViewModel.OpenMsg(EnumPromptType.Error, App.MainWindow, "提示", $"另存配方【{RecipeName}】无效");
  180. ActionManage.GetInstance.Send("CloseNewRecipeView");
  181. return;
  182. }
  183. }
  184. prop: string recipeID = Guid.NewGuid().ToString();//配方唯一ID,后期根据实际要求更改
  185. var res = Json<RecipeManage>.Data.Recipes.FirstOrDefault(p => p.RecipeId == recipeID);
  186. if (res == null)
  187. {
  188. Json<RecipeManage>.Data.Recipes.Add(new NewRecipeModel { RecipeId = recipeID, DataTime = DateTime.Now.ToShortDateString(), RecipeName = RecipeName, materialCollection = materials,FlowProcess=new FlowProcessManage()});//配方添加
  189. //Json<RecipeManage>.Save();
  190. }
  191. else
  192. {
  193. goto prop;
  194. }
  195. MessageNotify.GetInstance.ShowUserLog("另存配方成功");
  196. NoticeDemoViewModel.OpenMsg(EnumPromptType.Success, App.MainWindow, "提示", $"另存配方【{RecipeName}】成功");
  197. }
  198. else
  199. {
  200. MessageNotify.GetInstance.ShowUserLog($"另存配方【{rec.RecipeName}】无效:【配方中原料桶数异常】");
  201. NoticeDemoViewModel.OpenMsg(EnumPromptType.Error, App.MainWindow, "提示", $"另存配方【{rec.RecipeName}】无效");
  202. }
  203. ActionManage.GetInstance.Send("CloseNewRecipeView");
  204. }
  205. else
  206. {
  207. MessageBox.Show("另存配方失败","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
  208. }
  209. ActionManage.GetInstance.Send("CloseNewRecipeView");
  210. });
  211. }
  212. }
  213. }