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

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