终端一体化运控平台
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 

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