终端一体化运控平台
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

223 lines
9.8 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 FryPot_DosingSystem.View;
  7. using Microsoft.Toolkit.Mvvm.ComponentModel;
  8. using Microsoft.Toolkit.Mvvm.Input;
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Collections.ObjectModel;
  12. using System.Linq;
  13. using System.Text;
  14. using System.Threading.Tasks;
  15. using System.Windows;
  16. using System.Windows.Media.Media3D;
  17. namespace FryPot_DosingSystem.ViewModel
  18. {
  19. internal class RecipeSetViewModel : ObservableObject
  20. {
  21. ///// <summary>
  22. ///// 配方编号
  23. ///// </summary>
  24. //private int _serialNumber;
  25. //public int SerialNumber { get { return _serialNumber; } set { _serialNumber = value;OnPropertyChanged(); } }
  26. ///// <summary>
  27. ///// 配方名称
  28. ///// </summary>
  29. //private string _recipeName;
  30. //public string RecipeName { get { return _recipeName; } set { _recipeName = value; OnPropertyChanged(); } }
  31. /// <summary>
  32. /// 复制配方的Id
  33. /// </summary>
  34. public static string CopyRecipeId { get; set; }
  35. public ObservableCollection<NewRecipeModel> recipeModels { get; set; } = new ObservableCollection<NewRecipeModel>();
  36. public RelayCommand NewRecipe { get; set; }
  37. public RelayCommand SaveRecipe { get; set; }
  38. /// <summary>
  39. /// 工艺操作
  40. /// </summary>
  41. public RelayCommand<string> OperateFlowProcess { get; set; }
  42. /// <summary>
  43. /// 编辑配方
  44. /// </summary>
  45. public RelayCommand<object> EditRecipeCommand { get; set; }
  46. /// <summary>
  47. /// 复制配方
  48. /// </summary>
  49. public RelayCommand<object> CopyRecipeCommand { get; set; }
  50. /// <summary>
  51. /// 删除配方
  52. /// </summary>
  53. public RelayCommand<object> DeleteRecipeCommand { get; set; }
  54. public RecipeSetViewModel()
  55. {
  56. // Json<RecipeManage>.Read();
  57. recipeModels =Json<RecipeManage>.Data.Recipes;
  58. int count = recipeModels.Count;
  59. bool sign = false;
  60. ActionManage.GetInstance.CancelRegister("RecipeIsChange");
  61. ActionManage.GetInstance.RegisterAsync(new Action(() =>
  62. {
  63. if (!sign)
  64. {
  65. bool b = Json<RecipeManage>.Data.Recipes.Count == count ? true : false;
  66. if (!b)
  67. {
  68. MessageBoxResult result = MessageBox.Show("配方数据未保存,是否保存", "提示", MessageBoxButton.OKCancel, MessageBoxImage.Information);
  69. if (result == MessageBoxResult.OK)
  70. {
  71. Json<RecipeManage>.Save();//保存配方
  72. count = recipeModels.Count;
  73. }
  74. else
  75. {
  76. sign = true;
  77. Json<RecipeManage>.Data.Recipes.RemoveAt(Json<RecipeManage>.Data.Recipes.Count - 1);
  78. }
  79. }
  80. }
  81. }), "RecipeIsChange");
  82. //复制指定炒锅配方
  83. ActionManage.GetInstance.Register<object>(new Action<object>((Num) => {
  84. if (Num != null&& CopyRecipeId!=null)
  85. {
  86. var res= Json<RecipeManage>.Data.Recipes.FirstOrDefault(p=>p.RecipeId==CopyRecipeId);
  87. if (res != null)
  88. {
  89. prop: string recipeID = Guid.NewGuid().ToString();//配方唯一ID,后期根据实际要求更改
  90. var res1 = Json<RecipeManage>.Data.Recipes.FirstOrDefault(p => p.RecipeId == recipeID);
  91. if (res1 == null)
  92. {
  93. string recipeNamec = string.Empty;
  94. if (res.RecipeName.Contains('('))
  95. {
  96. recipeNamec = res.RecipeName.Split('(')[0] + $"({Num}号锅)";
  97. }
  98. else
  99. {
  100. recipeNamec= res.RecipeName+$"({Num}号锅)";
  101. }
  102. //复制料筒配方
  103. ObservableCollection<MaterialType> collect = new ObservableCollection<MaterialType>();
  104. //浅拷贝
  105. foreach (var item in res.materialCollection)
  106. {
  107. var clone=(MaterialType)item.Clone();
  108. collect.Add(clone);
  109. }
  110. foreach (var item in collect)
  111. {
  112. if (ushort.TryParse((Num.ToString() + item.MaterialLoc.ToString().Substring(1, 2)), out ushort loc))
  113. {
  114. item.MaterialLoc = loc;
  115. }
  116. else
  117. {
  118. NoticeDemoViewModel.OpenMsg(EnumPromptType.Success, App.MainWindow, "提示", $"复制配方失败");
  119. return;
  120. }
  121. }
  122. //复制炒锅工艺配方
  123. FlowProcessManage FryFlow = new FlowProcessManage();
  124. FryFlow.StirTime=res.FlowProcess.StirTime;
  125. FryFlow.RecipeName = recipeNamec;
  126. FryFlow.targetWeightOffset = res.FlowProcess.targetWeightOffset;
  127. ObservableCollection<FlowProcessModel> flowCollect = new ObservableCollection<FlowProcessModel>();
  128. //浅拷贝
  129. foreach (var item in res.FlowProcess.fpModels)
  130. {
  131. var clone = (FlowProcessModel)item.Clone();
  132. flowCollect.Add(clone);
  133. }
  134. foreach (var item in flowCollect)
  135. {
  136. if (ushort.TryParse(item.FryMaterialNum, out ushort rollerNum))
  137. {
  138. item.FryMaterialNum = Num.ToString() + item.FryMaterialNum.ToString().Substring(1, 2);
  139. }
  140. }
  141. FryFlow.fpModels = flowCollect;
  142. Json<RecipeManage>.Data.Recipes.Add(new NewRecipeModel { RecipeId = recipeID, DataTime = DateTime.Now.ToShortDateString(), RecipeName = recipeNamec, materialCollection = collect, FlowProcess = FryFlow });//配方添加
  143. //Json<RecipeManage>.Save();
  144. }
  145. else
  146. {
  147. goto prop;
  148. }
  149. MessageNotify.GetInstance.ShowUserLog("复制配方成功");
  150. NoticeDemoViewModel.OpenMsg(EnumPromptType.Success, App.MainWindow, "提示", $"复制配方成功");
  151. }
  152. }
  153. }), "CopyPotNum");
  154. NewRecipe =new RelayCommand( new Action(() =>
  155. {
  156. NewRecipeView nrv = new NewRecipeView();
  157. nrv.ShowDialog();
  158. //MessageLog.GetInstance.ShowUserLog("新建配方");
  159. }));
  160. SaveRecipe =new RelayCommand( new Action(() =>
  161. {
  162. Json<RecipeManage>.Save();
  163. count = recipeModels.Count;
  164. NoticeDemoViewModel.OpenMsg(EnumPromptType.Success, App.MainWindow, "提示", $"配方保存成功!");
  165. }));
  166. EditRecipeCommand = new RelayCommand<object>((Id) =>
  167. {
  168. if (Id != null)
  169. {
  170. ActionManage.GetInstance.CancelRegister("EditRecipe");
  171. NewRecipeView nrv = new NewRecipeView();
  172. ActionManage.GetInstance.Send("EditRecipe", Id);
  173. nrv.ShowDialog();
  174. }
  175. });
  176. DeleteRecipeCommand = new RelayCommand<object>((Id) =>
  177. {
  178. if (Id != null && Id is String strId)
  179. {
  180. var res = recipeModels.FirstOrDefault(p => p.RecipeId == strId);
  181. if (res != null && res is NewRecipeModel nes)
  182. {
  183. recipeModels.Remove(nes);//删除配方
  184. Json<RecipeManage>.Save();//保存配方
  185. count = recipeModels.Count;
  186. MessageNotify.GetInstance.ShowUserLog($"成功删除配方【{nes.RecipeName}】");
  187. }
  188. }
  189. });
  190. //工艺流程操作
  191. OperateFlowProcess = new RelayCommand<string>((recipeName) =>
  192. {
  193. if (recipeName != null && recipeName != string.Empty && recipeName != "")
  194. {
  195. ActionManage.GetInstance.CancelRegister("EditFlowProcess");
  196. FlowProcessView fps = new FlowProcessView();
  197. ActionManage.GetInstance.Send("EditFlowProcess", recipeName);
  198. fps.ShowDialog();
  199. }
  200. });
  201. //复制配方
  202. CopyRecipeCommand = new RelayCommand<object>((Id) => {
  203. CopyInfoView copyInfoView = new CopyInfoView();
  204. CopyRecipeId = Id.ToString() ;
  205. copyInfoView.Show();
  206. });
  207. }
  208. }
  209. }