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

228 lines
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<RecipeManage>.Read();
  42. Json<MaterialNames>.Read();
  43. MaterialNames.GetInstance.Names = Json<MaterialNames>.Data.Names;
  44. materialNames = Json<MaterialNames>.Data.Names;
  45. ActionManage.GetInstance.Register(new Action<object>(Id =>
  46. {
  47. if (Id != null && Id is string strId)
  48. {
  49. var res = Json<RecipeManage>.Data.Recipes.FirstOrDefault(p => p.RecipeId == strId);
  50. if (res != null && res is NewRecipeModel rom)
  51. {
  52. RecipeName = rom.RecipeName;
  53. RecipeRollerNum = rom.materialCollection.Count;
  54. foreach (var item in rom.materialCollection)
  55. {
  56. materials.Add(item);
  57. }
  58. recipeId = strId;
  59. }
  60. }
  61. }), "EditRecipe");
  62. AddRecipe = new RelayCommand(() =>
  63. {
  64. for (int i = 0; i < RecipeRollerNum; i++)
  65. {
  66. pr1:
  67. string materialCode = Guid.NewGuid().ToString();//原料唯一ID ,后期需要根据实际要求更改
  68. var res = materials.FirstOrDefault(p => p.MaterialCode == materialCode);
  69. if (res == null)
  70. {
  71. materials.Add(new MaterialType() { MaterialCode = materialCode });
  72. }
  73. else
  74. {
  75. goto pr1;
  76. }
  77. }
  78. });
  79. RemoveRecipe = new RelayCommand<string>(code =>
  80. {
  81. var res = materials.FirstOrDefault(m => m.MaterialCode == code);
  82. if (res != null)
  83. materials.Remove(res);
  84. });
  85. Comfirm = new RelayCommand(() =>
  86. {
  87. var bom = Json<RecipeManage>.Data.Recipes.FirstOrDefault(p => p.RecipeId == recipeId);
  88. if (bom == null)//新配方
  89. {
  90. var name = Json<RecipeManage>.Data.Recipes.FirstOrDefault(p => p.RecipeName == RecipeName);
  91. if (name == null)
  92. {
  93. if (materials.Count<=8&& materials.Count> 0)
  94. {
  95. //int lineNum= materials.ElementAt(0).MaterialLoc / 100;
  96. // //桶号正确性验证
  97. // for (int i = 0; i < materials.Count; i++)
  98. // {
  99. // if (materials.ElementAt(i).MaterialLoc / 100 != lineNum|| materials.ElementAt(i).MaterialLoc % 100!=i+1||lineNum<=0||lineNum>5)
  100. // {
  101. // MessageNotify.GetInstance.ShowUserLog($"新建配方【{RecipeName}】无效:【配方中原料桶号异常】");
  102. // NoticeDemoViewModel.OpenMsg(EnumPromptType.Error, App.MainWindow, "提示", $"新建配方【{RecipeName}】无效");
  103. // ActionManage.GetInstance.Send("CloseNewRecipeView");
  104. // return;
  105. // }
  106. // }
  107. prop: string recipeID = Guid.NewGuid().ToString();//配方唯一ID,后期根据实际要求更改
  108. var res = Json<RecipeManage>.Data.Recipes.FirstOrDefault(p => p.RecipeId == recipeID);
  109. if (res == null)
  110. {
  111. Json<RecipeManage>.Data.Recipes.Add(new NewRecipeModel { RecipeId = recipeID, RecipeName = RecipeName, FlowProcess=new FlowProcessManage(), materialCollection = materials,DataTime=DateTime.Now.ToShortDateString()});//配方添加
  112. }
  113. else
  114. {
  115. goto prop;
  116. }
  117. MessageNotify.GetInstance.ShowUserLog($"配方【{RecipeName}】新建成功");
  118. NoticeDemoViewModel.OpenMsg(EnumPromptType.Success, App.MainWindow, "提示", $"新建配方【{RecipeName}】成功");
  119. }
  120. else
  121. {
  122. MessageNotify.GetInstance.ShowUserLog($"新建配方【{RecipeName}】无效:【配方中原料桶数异常】");
  123. NoticeDemoViewModel.OpenMsg(EnumPromptType.Error, App.MainWindow, "提示", $"新建配方【{RecipeName}】无效");
  124. }
  125. ActionManage.GetInstance.Send("CloseNewRecipeView");
  126. }
  127. else
  128. {
  129. MessageBox.Show("配方名称重复或为空,请重命名!!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  130. }
  131. }
  132. else //已有配方,用于编辑
  133. {
  134. if (materials.Count > 0 && materials.Count <= 8)
  135. {
  136. //int lineNum = materials.ElementAt(0).MaterialLoc / 100;
  137. ////桶号正确性验证
  138. //for (int i = 0; i < materials.Count; i++)
  139. //{
  140. // if (materials.ElementAt(i).MaterialLoc / 100 != lineNum || materials.ElementAt(i).MaterialLoc % 100 != i + 1 || lineNum <= 0 || lineNum > 5)
  141. // {
  142. // MessageNotify.GetInstance.ShowUserLog($"另存配方【{RecipeName}】无效:【配方中原料桶号异常】");
  143. // NoticeDemoViewModel.OpenMsg(EnumPromptType.Error, App.MainWindow, "提示", $"另存配方【{RecipeName}】无效");
  144. // ActionManage.GetInstance.Send("CloseNewRecipeView");
  145. // return;
  146. // }
  147. //}
  148. bom.materialCollection = materials;
  149. bom.RecipeName = RecipeName;
  150. bom.UpdateTime = DateTime.Now.ToShortDateString();
  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 RelayCommand(() =>
  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, RecipeName = RecipeName, materialCollection = materials });//配方添加
  188. }
  189. else
  190. {
  191. goto prop;
  192. }
  193. MessageNotify.GetInstance.ShowUserLog("另存配方成功");
  194. NoticeDemoViewModel.OpenMsg(EnumPromptType.Success, App.MainWindow, "提示", $"另存配方【{RecipeName}】成功");
  195. }
  196. else
  197. {
  198. MessageNotify.GetInstance.ShowUserLog($"另存配方【{rec.RecipeName}】无效:【配方中原料桶数异常】");
  199. NoticeDemoViewModel.OpenMsg(EnumPromptType.Error, App.MainWindow, "提示", $"另存配方【{rec.RecipeName}】无效");
  200. }
  201. ActionManage.GetInstance.Send("CloseNewRecipeView");
  202. }
  203. else
  204. {
  205. MessageBox.Show("另存配方失败","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
  206. }
  207. ActionManage.GetInstance.Send("CloseNewRecipeView");
  208. });
  209. }
  210. }
  211. }