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

233 rivejä
12 KiB

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