终端一体化运控平台
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

176 lines
7.9 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. RawMaterial.Add(item);
  32. }
  33. }
  34. }), "LocalRecipeEdit");
  35. RemoveCommand = new RelayCommand<object>((o) =>
  36. {
  37. var res = RawMaterial.FirstOrDefault(p => p.RawMaterialLocation == (int)o);
  38. if (res != null) RawMaterial.Remove(res);
  39. //NoticeDemoViewModel.OpenMsg(EnumPromptType.Success, App.MainWindow, "提示", $"{o.ToString()}:原料删除成功!");
  40. });
  41. AddCommand = new RelayCommand(() =>
  42. {
  43. int MaxRawMaterial = 15;
  44. if (GVL_SmallStation.GetInstance.IsUseWindSend && TrayNum == 1)
  45. {
  46. MaxRawMaterial = 20;
  47. }
  48. else
  49. {
  50. MaxRawMaterial = 15;
  51. }
  52. if (RawMaterial.Count < MaxRawMaterial)
  53. {
  54. if (RawMaterial.Count < 15)
  55. {
  56. RawMaterial.Add(new RemoteRecipeRawMaterial()
  57. {
  58. RawMaterialName = "原料" + (RawMaterial.Count + 1),
  59. RawMaterialLocation = RawMaterial.Count + 1,
  60. RawMaterialBarrelNum = 1,
  61. RawMaterialWeight = 100
  62. });
  63. }
  64. else
  65. {
  66. RawMaterial.Add(new RemoteRecipeRawMaterial()
  67. {
  68. RawMaterialName = "粉料" + (RawMaterial.Count -14),
  69. RawMaterialLocation = (RawMaterial.Count - 14),
  70. RawMaterialBarrelNum = 7,
  71. RawMaterialWeight = 100
  72. });
  73. }
  74. }
  75. else
  76. {
  77. NoticeDemoViewModel.OpenMsg(EnumPromptType.Success, App.MainWindow, "提示", $"原料最多添加{MaxRawMaterial}种");
  78. }
  79. });
  80. SaveCommand = new RelayCommand(() =>
  81. {
  82. if (RecipeName.Length < 0 || RecipeName == null|| RecipeName==String.Empty || RecipeName == "")
  83. {
  84. NoticeDemoViewModel.OpenMsg(EnumPromptType.Error, App.MainWindow, "错误", $"配方名称不能为空");
  85. return;
  86. }
  87. if (RecipeCode.Length < 0 || RecipeCode == null || RecipeCode == String.Empty || RecipeCode == "")
  88. {
  89. NoticeDemoViewModel.OpenMsg(EnumPromptType.Error, App.MainWindow, "错误", $"配方编号不能为空");
  90. return;
  91. }
  92. if (TrayNum != 1 && TrayNum!=2)
  93. {
  94. TrayNum = 1;
  95. }
  96. ObservableCollection<RemoteRecipeRawMaterial> RawMaterials = new ObservableCollection<RemoteRecipeRawMaterial>();
  97. if (RawMaterial == null || RawMaterial.Count <= 0)
  98. {
  99. NoticeDemoViewModel.OpenMsg(EnumPromptType.Warn, App.MainWindow, "警告", $"没有可保存的参数!");
  100. return;
  101. }
  102. for (int i = 0; i < RawMaterial.Count; i++)
  103. {
  104. if (RawMaterial.Where(p => p.RawMaterialLocation == RawMaterial.ElementAt(i).RawMaterialLocation)?.ToList()?.Count >= 2)
  105. {
  106. NoticeDemoViewModel.OpenMsg(EnumPromptType.Error, App.MainWindow, "错误", $"原料位置冲突,请检查后重试!");
  107. return;
  108. }
  109. }
  110. int index = Array.FindIndex(Json<LocalRecipeDataColl>.Data.Recipes.ToArray(), p => p.RecipeCode == RecipeCode);
  111. if (index >= 0)
  112. {
  113. foreach (var item in RawMaterial)
  114. {
  115. RawMaterials.Add(new RemoteRecipeRawMaterial()
  116. {
  117. RawMaterialLocation = item.RawMaterialLocation,
  118. RawMaterialBarrelNum = item.RawMaterialBarrelNum,
  119. RawMaterialWeight = item.RawMaterialWeight,
  120. });
  121. }
  122. Json<LocalRecipeDataColl>.Data.Recipes.ElementAt(index).RecipeName= RecipeName;
  123. Json<LocalRecipeDataColl>.Data.Recipes.ElementAt(index).TrayCode = TrayNum;
  124. Json<LocalRecipeDataColl>.Data.Recipes.ElementAt(index).RecipeCode = RecipeCode;
  125. Json<LocalRecipeDataColl>.Data.Recipes.ElementAt(index).RawMaterial = RawMaterial;
  126. NoticeDemoViewModel.OpenMsg(EnumPromptType.Success, App.MainWindow, "提示", $"修改配方完成!");
  127. }
  128. else
  129. {
  130. foreach (var item in RawMaterial)
  131. {
  132. RawMaterials.Add(new RemoteRecipeRawMaterial()
  133. {
  134. RawMaterialLocation = item.RawMaterialLocation,
  135. RawMaterialBarrelNum = item.RawMaterialBarrelNum,
  136. RawMaterialWeight = item.RawMaterialWeight,
  137. });
  138. }
  139. Json<LocalRecipeDataColl>.Data.Recipes.Add(new RemoteRecipeData()
  140. {
  141. RecipeName = RecipeName,
  142. RecipeCode = RecipeCode,
  143. TrayCode = TrayNum,
  144. RawMaterial = RawMaterials,
  145. });
  146. NoticeDemoViewModel.OpenMsg(EnumPromptType.Success, App.MainWindow, "提示", $"自定义配方添加完成!");
  147. }
  148. ActionManage.GetInstance.Send("CloseNewRemoteRecipeView");
  149. });
  150. }
  151. public string RecipeCode { get { return _mRecipeCode; } set { _mRecipeCode = value; OnPropertyChanged(); } }
  152. private string _mRecipeCode = string.Empty;
  153. public string RecipeName { get { return _mRecipeName; } set { _mRecipeName = value; OnPropertyChanged(); } }
  154. private string _mRecipeName = string.Empty;
  155. public int TrayNum { get { return _mTrayNum; } set { _mTrayNum = value; OnPropertyChanged(); } }
  156. private int _mTrayNum = 1;
  157. public string ErrorInfo { get { return _mErrorInfo; } set { _mErrorInfo = value; OnPropertyChanged(); } }
  158. private string _mErrorInfo;
  159. public ObservableCollection<RemoteRecipeRawMaterial> RawMaterial { get; set; } = new ObservableCollection<RemoteRecipeRawMaterial>();
  160. public RelayCommand<object> RemoveCommand { get; set; }
  161. public RelayCommand AddCommand { get; set; }
  162. public RelayCommand SaveCommand { get; set; }
  163. }
  164. }