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

188 lines
8.9 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. prop: string recipeID = Guid.NewGuid().ToString();//配方唯一ID,后期根据实际要求更改
  95. var res = Json<RecipeManage>.Data.Recipes.FirstOrDefault(p => p.RecipeId == recipeID);
  96. if (res == null)
  97. {
  98. Json<RecipeManage>.Data.Recipes.Add(new NewRecipeModel { RecipeId = recipeID, RecipeName = RecipeName, FlowProcess=new FlowProcessManage(), materialCollection = materials,DataTime=DateTime.Now.ToShortDateString()});//配方添加
  99. }
  100. else
  101. {
  102. goto prop;
  103. }
  104. MessageLog.GetInstance.ShowUserLog($"配方【{RecipeName}】新建成功");
  105. NoticeDemoViewModel.OpenMsg(EnumPromptType.Success, App.MainWindow, "提示", $"新建配方【{RecipeName}】成功");
  106. }
  107. else
  108. {
  109. MessageLog.GetInstance.ShowUserLog($"新建配方【{RecipeName}】无效:【配方中原料桶数异常】");
  110. NoticeDemoViewModel.OpenMsg(EnumPromptType.Error, App.MainWindow, "提示", $"新建配方【{RecipeName}】无效");
  111. }
  112. ActionManage.GetInstance.Send("CloseNewRecipeView");
  113. }
  114. else
  115. {
  116. MessageBox.Show("配方名称重复或为空,请重命名!!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  117. }
  118. }
  119. else //已有配方,用于编辑
  120. {
  121. if (materials.Count > 0 && materials.Count <= 8)
  122. {
  123. bom.materialCollection = materials;
  124. bom.RecipeName = RecipeName;
  125. bom.UpdateTime = DateTime.Now.ToShortDateString();
  126. Json<RecipeManage>.Save();
  127. MessageLog.GetInstance.ShowUserLog($"配方【{RecipeName}】修改成功");
  128. NoticeDemoViewModel.OpenMsg(EnumPromptType.Success, App.MainWindow, "提示", $"修改配方【{RecipeName}】成功");
  129. }
  130. else
  131. {
  132. MessageLog.GetInstance.ShowUserLog($"修改配方【{RecipeName}】无效:【配方中原料桶数异常】");
  133. NoticeDemoViewModel.OpenMsg(EnumPromptType.Error, App.MainWindow, "提示", $"修改配方【{RecipeName}】无效");
  134. }
  135. ActionManage.GetInstance.Send("CloseNewRecipeView");
  136. }
  137. });
  138. SaveAs = new RelayCommand(() =>
  139. {
  140. var bom = Json<RecipeManage>.Data.Recipes.FirstOrDefault(p => p.RecipeName == RecipeName);
  141. var rec = Json<RecipeManage>.Data.Recipes.FirstOrDefault(p => p.RecipeId == recipeId);
  142. if (bom == null && rec != null)//配方名称更改
  143. {
  144. if (materials.Count>0&& materials.Count<=8)//验证配方中原料桶数
  145. {
  146. prop: string recipeID = Guid.NewGuid().ToString();//配方唯一ID,后期根据实际要求更改
  147. var res = Json<RecipeManage>.Data.Recipes.FirstOrDefault(p => p.RecipeId == recipeID);
  148. if (res == null)
  149. {
  150. Json<RecipeManage>.Data.Recipes.Add(new NewRecipeModel { RecipeId = recipeID, RecipeName = RecipeName, materialCollection = materials });//配方添加
  151. }
  152. else
  153. {
  154. goto prop;
  155. }
  156. MessageLog.GetInstance.ShowUserLog("另存配方成功");
  157. NoticeDemoViewModel.OpenMsg(EnumPromptType.Success, App.MainWindow, "提示", $"另存配方【{RecipeName}】成功");
  158. }
  159. else
  160. {
  161. MessageLog.GetInstance.ShowUserLog($"另存配方【{rec.RecipeName}】无效:【配方中原料桶数异常】");
  162. NoticeDemoViewModel.OpenMsg(EnumPromptType.Error, App.MainWindow, "提示", $"另存配方【{rec.RecipeName}】无效");
  163. }
  164. ActionManage.GetInstance.Send("CloseNewRecipeView");
  165. }
  166. else
  167. {
  168. MessageBox.Show("另存配方失败","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
  169. }
  170. ActionManage.GetInstance.Send("CloseNewRecipeView");
  171. });
  172. }
  173. }
  174. }