终端一体化运控平台
25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 

275 satır
14 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using BPA.Helper;
  7. using System.Collections.ObjectModel;
  8. using BPASmartClient.JXJFoodSmallStation.Model;
  9. using BPA.Helper;
  10. using BPASmartClient.CustomResource.UserControls.MessageShow;
  11. using BPASmartClient.CustomResource.UserControls;
  12. using System.Diagnostics;
  13. using BPASmartClient.JXJFoodSmallStation.Model.Siemens;
  14. using Google.Protobuf.WellKnownTypes;
  15. using Newtonsoft.Json.Linq;
  16. using BPASmartClient.CustomResource.Pages.Model;
  17. namespace BPASmartClient.JXJFoodSmallStation.ViewModel
  18. {
  19. public class NewLocalRecipeViewModel : NotifyBase
  20. {
  21. public NewLocalRecipeViewModel()
  22. {
  23. //ActionManage.GetInstance.CancelRegister("LocalRecipeEdit");
  24. ActionManage.GetInstance.Register(new Action<object>((o) =>
  25. {
  26. if (o != null && o is RecipeData rm)
  27. {
  28. RecipeName = rm.RecipeName;
  29. RecipeCode = rm.RecipeCode;
  30. TrayNum = rm.TrayCode;
  31. foreach (RemoteRecipeRawMaterial item in rm.RawMaterial)
  32. {
  33. var res = Json<DevicePar>.Data.rawMaterialStockBin.FirstOrDefault(p => p.RawMaterialLocation == item.RawMaterialLocation);
  34. if (res != null && !string.IsNullOrEmpty(res.RawMaterialName))
  35. {
  36. var temp = Json<DevicePar>.Data.BomMaterial.FirstOrDefault(p => p.MaterialCode == res.RawMaterialName);
  37. if (temp != null && !string.IsNullOrEmpty(temp.MaterialName))
  38. {
  39. item.RawMaterialName = temp.MaterialName;
  40. }
  41. }
  42. //AllRawMaterial.Add(item);
  43. AllRawMaterial.Add(new RemoteRecipeRawMaterial()
  44. {
  45. RawMaterialType = item.RawMaterialType,
  46. RawMaterialName = item.RawMaterialName,
  47. RawMaterialWeight = item.RawMaterialWeight,
  48. RawMaterialLocation = item.RawMaterialLocation,
  49. RawMaterialBarrelNum = item.RawMaterialBarrelNum,
  50. });
  51. }
  52. foreach (var item1 in rm.WindSend)
  53. {
  54. var res = Json<DevicePar>.Data.windSendRawMaterial.FirstOrDefault(p => p.Location == item1.Location);
  55. if (res != null)
  56. {
  57. item1.RawMaterialName = res.RawMaterialChineseName;
  58. }
  59. AllRawMaterial.Add(new RemoteRecipeRawMaterial()
  60. {
  61. RawMaterialType = item1.RawMaterialType,
  62. RawMaterialName = item1.RawMaterialName,
  63. RawMaterialWeight = item1.RawMaterialWeight,
  64. RawMaterialLocation = item1.Location,
  65. RawMaterialBarrelNum = item1.RawMaterialBarrelNum,
  66. });
  67. }
  68. }
  69. }), "LocalRecipeEdit", true);
  70. RemoveCommand = new BPARelayCommand<object>((o) =>
  71. {
  72. var res = AllRawMaterial.FirstOrDefault(p => p.RawMaterialName == o.ToString());
  73. if (res != null) AllRawMaterial.Remove(res);
  74. //NoticeDemoViewModel.OpenMsg(EnumPromptType.Success, App.MainWindow, "提示", $"{o.ToString()}:原料删除成功!");
  75. });
  76. AddCommand = new BPARelayCommand(() =>
  77. {
  78. int MaxRawMaterial = 15;
  79. var res = AllRawMaterial.Where(p => p.RawMaterialType == "小料").ToList();
  80. if (res.Count < MaxRawMaterial)
  81. {
  82. AllRawMaterial.Add(new RemoteRecipeRawMaterial()
  83. {
  84. RawMaterialType = "小料",
  85. RawMaterialName = "小料" + (res.Count + 1),
  86. RawMaterialLocation = res.Count + 1,
  87. RawMaterialBarrelNum = (short)new Random().Next(6, 9),
  88. RawMaterialWeight = (float)(new Random().Next(100, 300) * 0.001),
  89. });
  90. }
  91. else
  92. {
  93. NoticeDemoViewModel.OpenMsg(EnumPromptType.Success, App.MainWindow, "提示", $"原料最多添加{MaxRawMaterial}种");
  94. }
  95. });
  96. AddFLCommand = new BPARelayCommand(() =>
  97. {
  98. int MaxRawMaterial = 5;
  99. var res = AllRawMaterial.Where(p => p.RawMaterialType == "粉料").ToList();
  100. if (res.Count < MaxRawMaterial)
  101. {
  102. AllRawMaterial.Add(new RemoteRecipeRawMaterial()
  103. {
  104. RawMaterialType = "粉料",
  105. RawMaterialName = "粉料" + (res.Count + 1),
  106. RawMaterialLocation = res.Count + 1,
  107. RawMaterialWeight = new Random().Next(1, 5),
  108. RawMaterialBarrelNum = 3,
  109. });
  110. }
  111. else
  112. {
  113. NoticeDemoViewModel.OpenMsg(EnumPromptType.Success, App.MainWindow, "提示", $"粉料最多添加{MaxRawMaterial}种");
  114. }
  115. });
  116. SaveCommand = new BPARelayCommand(() =>
  117. {
  118. if (!MessageNotify.GetInstance.ShowDialog("请确认,是否保存该配方配置?"))
  119. {
  120. return;
  121. }
  122. if (RecipeName.Length < 0 || RecipeName == null || RecipeName == String.Empty || RecipeName == "")
  123. {
  124. NoticeDemoViewModel.OpenMsg(EnumPromptType.Error, App.MainWindow, "错误", $"配方名称不能为空");
  125. return;
  126. }
  127. if (RecipeCode.Length < 0 || RecipeCode == null || RecipeCode == String.Empty || RecipeCode == "")
  128. {
  129. NoticeDemoViewModel.OpenMsg(EnumPromptType.Error, App.MainWindow, "错误", $"配方编号不能为空");
  130. return;
  131. }
  132. if (TrayNum != 1 && TrayNum != 2)
  133. {
  134. TrayNum = 1;
  135. }
  136. ObservableCollection<RemoteRecipeRawMaterial> RawMaterials = new ObservableCollection<RemoteRecipeRawMaterial>();
  137. ObservableCollection<WindSendRawMaterial> WindSendRawMaterials = new ObservableCollection<WindSendRawMaterial>();
  138. if (AllRawMaterial == null || AllRawMaterial.Count <= 0)
  139. {
  140. NoticeDemoViewModel.OpenMsg(EnumPromptType.Warn, App.MainWindow, "警告", $"没有可保存的参数!");
  141. return;
  142. }
  143. var res = AllRawMaterial.Where(p => p.RawMaterialType == "小料").ToList();
  144. for (int i = 0; i < res.Count; i++)
  145. {
  146. if (res.Where(p => p.RawMaterialLocation == res.ElementAt(i).RawMaterialLocation)?.ToList()?.Count >= 2)
  147. {
  148. NoticeDemoViewModel.OpenMsg(EnumPromptType.Error, App.MainWindow, "错误", $"原料位置冲突,请检查后重试!");
  149. return;
  150. }
  151. }
  152. var res1 = AllRawMaterial.Where(p => p.RawMaterialType == "粉料").ToList();
  153. for (int i = 0; i < res1.Count; i++)
  154. {
  155. if (res1.Where(p => p.RawMaterialLocation == res1.ElementAt(i).RawMaterialLocation)?.ToList()?.Count >= 2)
  156. {
  157. NoticeDemoViewModel.OpenMsg(EnumPromptType.Error, App.MainWindow, "错误", $"粉料位置冲突,请检查后重试!");
  158. return;
  159. }
  160. }
  161. int index = Array.FindIndex(Json<LocalRecipeDataColl>.Data.RemoteRecipes.ToArray(), p => p.RecipeCode == RecipeCode);
  162. if (index >= 0)
  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. if (item.RawMaterialType == "粉料")
  178. {
  179. WindSendRawMaterials.Add(new WindSendRawMaterial()
  180. {
  181. RawMaterialName = item.RawMaterialName,
  182. RawMaterialWeight = item.RawMaterialWeight,
  183. Location = item.RawMaterialLocation,
  184. RawMaterialType = item.RawMaterialType,
  185. RawMaterialBarrelNum = item.RawMaterialBarrelNum,
  186. });
  187. }
  188. }
  189. Json<LocalRecipeDataColl>.Data.RemoteRecipes.ElementAt(index).RecipeName = RecipeName;
  190. Json<LocalRecipeDataColl>.Data.RemoteRecipes.ElementAt(index).TrayCode = TrayNum;
  191. Json<LocalRecipeDataColl>.Data.RemoteRecipes.ElementAt(index).RecipeCode = RecipeCode;
  192. Json<LocalRecipeDataColl>.Data.RemoteRecipes.ElementAt(index).RawMaterial = RawMaterials;
  193. Json<LocalRecipeDataColl>.Data.RemoteRecipes.ElementAt(index).WindSend = WindSendRawMaterials;
  194. NoticeDemoViewModel.OpenMsg(EnumPromptType.Success, App.MainWindow, "提示", $"修改配方完成!");
  195. MessageNotify.GetInstance.ShowUserLog($"修改配方名称:【{RecipeName}】,配方编号:【{RecipeCode}】。");
  196. }
  197. else
  198. {
  199. foreach (var item in AllRawMaterial)
  200. {
  201. if (item.RawMaterialType == "小料")
  202. {
  203. RawMaterials.Add(new RemoteRecipeRawMaterial()
  204. {
  205. RawMaterialName = item.RawMaterialName,
  206. RawMaterialType = item.RawMaterialType,
  207. RawMaterialLocation = item.RawMaterialLocation,
  208. RawMaterialBarrelNum = item.RawMaterialBarrelNum,
  209. RawMaterialWeight = item.RawMaterialWeight,
  210. });
  211. }
  212. if (item.RawMaterialType == "粉料")
  213. {
  214. WindSendRawMaterials.Add(new WindSendRawMaterial()
  215. {
  216. RawMaterialName = item.RawMaterialName,
  217. RawMaterialWeight = item.RawMaterialWeight,
  218. Location = item.RawMaterialLocation,
  219. RawMaterialType = item.RawMaterialType,
  220. RawMaterialBarrelNum = item.RawMaterialBarrelNum,
  221. });
  222. }
  223. }
  224. var newRemoteRecipeData = new RecipeData()
  225. {
  226. RecipeName = RecipeName,
  227. RecipeCode = RecipeCode,
  228. TrayCode = TrayNum,
  229. RawMaterial = RawMaterials,
  230. WindSend = WindSendRawMaterials,
  231. };
  232. Json<LocalRecipeDataColl>.Data.RemoteRecipes.Add(newRemoteRecipeData);
  233. //新增一条配方应该默认显示出来。
  234. Json<LocalRecipeDataColl>.Data.SelectedRecipes.Add(newRemoteRecipeData);
  235. NoticeDemoViewModel.OpenMsg(EnumPromptType.Success, App.MainWindow, "提示", $"自定义配方添加完成!");
  236. MessageNotify.GetInstance.ShowUserLog($"添加一个自定义配方,配方名称:【{RecipeName}】,配方编号:【{RecipeCode}】。");
  237. }
  238. ActionManage.GetInstance.Send("CloseNewRemoteRecipeView");
  239. });
  240. }
  241. public string RecipeCode { get { return _mRecipeCode; } set { _mRecipeCode = value; OnPropertyChanged(); } }
  242. private string _mRecipeCode = string.Empty;
  243. public string RecipeName { get { return _mRecipeName; } set { _mRecipeName = value; OnPropertyChanged(); } }
  244. private string _mRecipeName = string.Empty;
  245. public int TrayNum { get { return _mTrayNum; } set { _mTrayNum = value; OnPropertyChanged(); } }
  246. private int _mTrayNum = 1;
  247. public string ErrorInfo { get { return _mErrorInfo; } set { _mErrorInfo = value; OnPropertyChanged(); } }
  248. private string _mErrorInfo;
  249. public ObservableCollection<RemoteRecipeRawMaterial> AllRawMaterial { get; set; } = new ObservableCollection<RemoteRecipeRawMaterial>();
  250. //public ObservableCollection<NewRemoteRecipeModel> AllRawMaterial { get; set; } = new ObservableCollection<NewRemoteRecipeModel>();
  251. public BPARelayCommand<object> RemoveCommand { get; set; }
  252. public BPARelayCommand AddCommand { get; set; }
  253. public BPARelayCommand AddFLCommand { get; set; }
  254. public BPARelayCommand SaveCommand { get; set; }
  255. }
  256. }