终端一体化运控平台
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 

221 рядки
11 KiB

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