终端一体化运控平台
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 

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