终端一体化运控平台
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 

232 rader
11 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using Microsoft.Toolkit.Mvvm.ComponentModel;
  7. using System.Collections.ObjectModel;
  8. using BPASmartClient.JXJFoodSmallStation.Model;
  9. using BPASmartClient.Helper;
  10. using Microsoft.Toolkit.Mvvm.Input;
  11. using BPASmartClient.CustomResource.UserControls.MessageShow;
  12. using BPASmartClient.CustomResource.UserControls;
  13. using System.Diagnostics;
  14. using BPASmartClient.JXJFoodSmallStation.Model.Siemens;
  15. namespace BPASmartClient.JXJFoodSmallStation.ViewModel
  16. {
  17. public class NewLocalRecipeViewModel : ObservableObject
  18. {
  19. public NewLocalRecipeViewModel()
  20. {
  21. ActionManage.GetInstance.CancelRegister("LocalRecipeEdit");
  22. ActionManage.GetInstance.Register(new Action<object>((o) =>
  23. {
  24. if (o != null && o is RemoteRecipeData rm)
  25. {
  26. RecipeName = rm.RecipeName;
  27. RecipeCode = rm.RecipeCode;
  28. TrayNum = rm.TrayCode;
  29. foreach (var item in rm.RawMaterial)
  30. {
  31. AllRawMaterial.Add(item);
  32. }
  33. foreach (var item1 in rm.WindSend)
  34. {
  35. AllRawMaterial.Add(new RemoteRecipeRawMaterial()
  36. {
  37. RawMaterialType = "粉料",
  38. RawMaterialName = item1.RawMaterialName,
  39. RawMaterialWeight = item1.RawMaterialWeight,
  40. RawMaterialBarrelNum = 3,
  41. });
  42. }
  43. }
  44. }), "LocalRecipeEdit");
  45. RemoveCommand = new RelayCommand<object>((o) =>
  46. {
  47. var res = AllRawMaterial.FirstOrDefault(p => p.RawMaterialLocation == (int)o);
  48. if (res != null) AllRawMaterial.Remove(res);
  49. //NoticeDemoViewModel.OpenMsg(EnumPromptType.Success, App.MainWindow, "提示", $"{o.ToString()}:原料删除成功!");
  50. });
  51. AddCommand = new RelayCommand(() =>
  52. {
  53. int MaxRawMaterial = 15;
  54. var res = AllRawMaterial.Where(p => p.RawMaterialType == "小料").ToList();
  55. if (res.Count < MaxRawMaterial)
  56. {
  57. AllRawMaterial.Add(new RemoteRecipeRawMaterial()
  58. {
  59. RawMaterialType = "小料",
  60. RawMaterialName = "小料" + (res.Count + 1),
  61. RawMaterialLocation = res.Count + 1,
  62. RawMaterialBarrelNum = (short)new Random().Next(6,9),
  63. RawMaterialWeight = (float)(new Random().Next(100, 300)*0.001),
  64. });
  65. }
  66. else
  67. {
  68. NoticeDemoViewModel.OpenMsg(EnumPromptType.Success, App.MainWindow, "提示", $"原料最多添加{MaxRawMaterial}种");
  69. }
  70. });
  71. AddFLCommand = new RelayCommand(() =>
  72. {
  73. int MaxRawMaterial = 5;
  74. var res = AllRawMaterial.Where(p => p.RawMaterialType == "粉料").ToList();
  75. if (res.Count < MaxRawMaterial)
  76. {
  77. AllRawMaterial.Add(new RemoteRecipeRawMaterial()
  78. {
  79. RawMaterialType = "粉料",
  80. RawMaterialName = "粉料" + (res.Count + 1),
  81. RawMaterialLocation = res.Count + 1,
  82. RawMaterialWeight = new Random().Next(1, 5),
  83. RawMaterialBarrelNum = 3,
  84. });
  85. }
  86. else
  87. {
  88. NoticeDemoViewModel.OpenMsg(EnumPromptType.Success, App.MainWindow, "提示", $"粉料最多添加{MaxRawMaterial}种");
  89. }
  90. });
  91. SaveCommand = new RelayCommand(() =>
  92. {
  93. if (RecipeName.Length < 0 || RecipeName == null|| RecipeName==String.Empty || RecipeName == "")
  94. {
  95. NoticeDemoViewModel.OpenMsg(EnumPromptType.Error, App.MainWindow, "错误", $"配方名称不能为空");
  96. return;
  97. }
  98. if (RecipeCode.Length < 0 || RecipeCode == null || RecipeCode == String.Empty || RecipeCode == "")
  99. {
  100. NoticeDemoViewModel.OpenMsg(EnumPromptType.Error, App.MainWindow, "错误", $"配方编号不能为空");
  101. return;
  102. }
  103. if (TrayNum != 1 && TrayNum!=2)
  104. {
  105. TrayNum = 1;
  106. }
  107. ObservableCollection<RemoteRecipeRawMaterial> RawMaterials = new ObservableCollection<RemoteRecipeRawMaterial>();
  108. ObservableCollection<WindSendRawMaterial> WindSendRawMaterials = new ObservableCollection<WindSendRawMaterial>();
  109. if (AllRawMaterial == null || AllRawMaterial.Count <= 0)
  110. {
  111. NoticeDemoViewModel.OpenMsg(EnumPromptType.Warn, App.MainWindow, "警告", $"没有可保存的参数!");
  112. return;
  113. }
  114. var res = AllRawMaterial.Where(p => p.RawMaterialType == "小料").ToList();
  115. for (int i = 0; i < res.Count; i++)
  116. {
  117. if (res.Where(p => p.RawMaterialLocation == res.ElementAt(i).RawMaterialLocation)?.ToList()?.Count >= 2)
  118. {
  119. NoticeDemoViewModel.OpenMsg(EnumPromptType.Error, App.MainWindow, "错误", $"原料位置冲突,请检查后重试!");
  120. return;
  121. }
  122. }
  123. var res1 = AllRawMaterial.Where(p => p.RawMaterialType == "粉料").ToList();
  124. for (int i = 0; i < res1.Count; i++)
  125. {
  126. if (res1.Where(p => p.RawMaterialLocation == res1.ElementAt(i).RawMaterialLocation)?.ToList()?.Count >= 2)
  127. {
  128. NoticeDemoViewModel.OpenMsg(EnumPromptType.Error, App.MainWindow, "错误", $"粉料位置冲突,请检查后重试!");
  129. return;
  130. }
  131. }
  132. int index = Array.FindIndex(Json<LocalRecipeDataColl>.Data.Recipes.ToArray(), p => p.RecipeCode == RecipeCode);
  133. if (index >= 0)
  134. {
  135. foreach (var item in AllRawMaterial)
  136. {
  137. if (item.RawMaterialType == "小料")
  138. {
  139. RawMaterials.Add(new RemoteRecipeRawMaterial()
  140. {
  141. RawMaterialName = item.RawMaterialName,
  142. RawMaterialType = item.RawMaterialType,
  143. RawMaterialLocation = item.RawMaterialLocation,
  144. RawMaterialBarrelNum = item.RawMaterialBarrelNum,
  145. RawMaterialWeight = item.RawMaterialWeight,
  146. });
  147. }
  148. }
  149. foreach (var item in AllRawMaterial)
  150. {
  151. if (item.RawMaterialType == "粉料")
  152. {
  153. WindSendRawMaterials.Add(new WindSendRawMaterial()
  154. {
  155. RawMaterialName = item.RawMaterialName,
  156. RawMaterialWeight = item.RawMaterialWeight,
  157. });
  158. }
  159. }
  160. Json<LocalRecipeDataColl>.Data.Recipes.ElementAt(index).RecipeName= RecipeName;
  161. Json<LocalRecipeDataColl>.Data.Recipes.ElementAt(index).TrayCode = TrayNum;
  162. Json<LocalRecipeDataColl>.Data.Recipes.ElementAt(index).RecipeCode = RecipeCode;
  163. Json<LocalRecipeDataColl>.Data.Recipes.ElementAt(index).RawMaterial = RawMaterials;
  164. Json<LocalRecipeDataColl>.Data.Recipes.ElementAt(index).WindSend = WindSendRawMaterials;
  165. NoticeDemoViewModel.OpenMsg(EnumPromptType.Success, App.MainWindow, "提示", $"修改配方完成!");
  166. }
  167. else
  168. {
  169. foreach (var item in AllRawMaterial)
  170. {
  171. if (item.RawMaterialType == "小料")
  172. {
  173. RawMaterials.Add(new RemoteRecipeRawMaterial()
  174. {
  175. RawMaterialName = item.RawMaterialName,
  176. RawMaterialType = item.RawMaterialType,
  177. RawMaterialLocation = item.RawMaterialLocation,
  178. RawMaterialBarrelNum = item.RawMaterialBarrelNum,
  179. RawMaterialWeight = item.RawMaterialWeight,
  180. });
  181. }
  182. }
  183. foreach (var item in AllRawMaterial)
  184. {
  185. if (item.RawMaterialType == "粉料")
  186. {
  187. WindSendRawMaterials.Add(new WindSendRawMaterial()
  188. {
  189. RawMaterialName = item.RawMaterialName,
  190. RawMaterialWeight = item.RawMaterialWeight,
  191. });
  192. }
  193. }
  194. Json<LocalRecipeDataColl>.Data.Recipes.Add(new RemoteRecipeData()
  195. {
  196. RecipeName = RecipeName,
  197. RecipeCode = RecipeCode,
  198. TrayCode = TrayNum,
  199. RawMaterial = RawMaterials,
  200. WindSend = WindSendRawMaterials,
  201. });
  202. NoticeDemoViewModel.OpenMsg(EnumPromptType.Success, App.MainWindow, "提示", $"自定义配方添加完成!");
  203. }
  204. ActionManage.GetInstance.Send("CloseNewRemoteRecipeView");
  205. });
  206. }
  207. public string RecipeCode { get { return _mRecipeCode; } set { _mRecipeCode = value; OnPropertyChanged(); } }
  208. private string _mRecipeCode = string.Empty;
  209. public string RecipeName { get { return _mRecipeName; } set { _mRecipeName = value; OnPropertyChanged(); } }
  210. private string _mRecipeName = string.Empty;
  211. public int TrayNum { get { return _mTrayNum; } set { _mTrayNum = value; OnPropertyChanged(); } }
  212. private int _mTrayNum = 1;
  213. public string ErrorInfo { get { return _mErrorInfo; } set { _mErrorInfo = value; OnPropertyChanged(); } }
  214. private string _mErrorInfo;
  215. public ObservableCollection<RemoteRecipeRawMaterial> AllRawMaterial { get; set; } = new ObservableCollection<RemoteRecipeRawMaterial>();
  216. public RelayCommand<object> RemoveCommand { get; set; }
  217. public RelayCommand AddCommand { get; set; }
  218. public RelayCommand AddFLCommand { get; set; }
  219. public RelayCommand SaveCommand { get; set; }
  220. }
  221. }