终端一体化运控平台
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.
 
 
 

133 rivejä
6.0 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.DosingHKProject.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.DosingHKProject.Model.Siemens;
  15. using BPASmartClient.DosingProject;
  16. namespace BPASmartClient.DosingHKProject.ViewModel
  17. {
  18. public class NewLocalRecipeViewModel : ObservableObject
  19. {
  20. public NewLocalRecipeViewModel()
  21. {
  22. ActionManage.GetInstance.CancelRegister("LocalRecipeEdit");
  23. ActionManage.GetInstance.Register(new Action<object>((o) =>
  24. {
  25. if (o != null && o is RemoteRecipeData rm)
  26. {
  27. RecipeName = rm.RecipeName;
  28. RecipeCode = rm.RecipeCode;
  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. RawMaterial.Add(new RemoteRecipeRawMaterial()
  44. {
  45. RawMaterialName = "原料" + (RawMaterial.Count + 1),
  46. RawMaterialLocation = RawMaterial.Count + 1,
  47. RawMaterialBarrelNum = 1,
  48. RawMaterialWeight = 100
  49. });
  50. });
  51. SaveCommand = new RelayCommand(() =>
  52. {
  53. if (RecipeName.Length < 0 || RecipeName == null)
  54. {
  55. NoticeDemoViewModel.OpenMsg(EnumPromptType.Error, App.MainWindow, "错误", $"配方名称不能为空");
  56. return;
  57. }
  58. ObservableCollection<RemoteRecipeRawMaterial> RawMaterials = new ObservableCollection<RemoteRecipeRawMaterial>();
  59. if (RawMaterial == null || RawMaterial.Count <= 0)
  60. {
  61. NoticeDemoViewModel.OpenMsg(EnumPromptType.Warn, App.MainWindow, "警告", $"没有可保存的参数!");
  62. return;
  63. }
  64. for (int i = 0; i < RawMaterial.Count; i++)
  65. {
  66. if (RawMaterial.Where(p => p.RawMaterialLocation == RawMaterial.ElementAt(i).RawMaterialLocation)?.ToList()?.Count >= 2)
  67. {
  68. NoticeDemoViewModel.OpenMsg(EnumPromptType.Error, App.MainWindow, "错误", $"原料位置冲突,请检查后重试!");
  69. return;
  70. }
  71. }
  72. int index = Array.FindIndex(Json<LocalRecipeDataColl>.Data.Recipes.ToArray(), p => p.RecipeCode == RecipeCode);
  73. if (index >= 0)
  74. {
  75. foreach (var item in RawMaterial)
  76. {
  77. RawMaterials.Add(new RemoteRecipeRawMaterial()
  78. {
  79. RawMaterialLocation = item.RawMaterialLocation,
  80. RawMaterialBarrelNum = item.RawMaterialBarrelNum,
  81. RawMaterialWeight = item.RawMaterialWeight,
  82. });
  83. }
  84. Json<LocalRecipeDataColl>.Data.Recipes.ElementAt(index).RecipeName= RecipeName;
  85. Json<LocalRecipeDataColl>.Data.Recipes.ElementAt(index).RecipeCode = RecipeCode;
  86. Json<LocalRecipeDataColl>.Data.Recipes.ElementAt(index).RawMaterial = RawMaterial;
  87. NoticeDemoViewModel.OpenMsg(EnumPromptType.Success, App.MainWindow, "提示", $"修改配方完成!");
  88. }
  89. else
  90. {
  91. foreach (var item in RawMaterial)
  92. {
  93. RawMaterials.Add(new RemoteRecipeRawMaterial()
  94. {
  95. RawMaterialLocation = item.RawMaterialLocation,
  96. RawMaterialBarrelNum = item.RawMaterialBarrelNum,
  97. RawMaterialWeight = item.RawMaterialWeight,
  98. });
  99. }
  100. Json<LocalRecipeDataColl>.Data.Recipes.Add(new RemoteRecipeData()
  101. {
  102. RecipeName = RecipeName,
  103. RecipeCode = RecipeCode,
  104. RawMaterial = RawMaterials,
  105. });
  106. NoticeDemoViewModel.OpenMsg(EnumPromptType.Success, App.MainWindow, "提示", $"自定义配方添加完成!");
  107. }
  108. ActionManage.GetInstance.Send("CloseNewRemoteRecipeView");
  109. });
  110. }
  111. public string RecipeCode { get { return _mRecipeCode; } set { _mRecipeCode = value; OnPropertyChanged(); } }
  112. private string _mRecipeCode = string.Empty;
  113. public string RecipeName { get { return _mRecipeName; } set { _mRecipeName = value; OnPropertyChanged(); } }
  114. private string _mRecipeName = string.Empty;
  115. public string ErrorInfo { get { return _mErrorInfo; } set { _mErrorInfo = value; OnPropertyChanged(); } }
  116. private string _mErrorInfo;
  117. public ObservableCollection<RemoteRecipeRawMaterial> RawMaterial { get; set; } = new ObservableCollection<RemoteRecipeRawMaterial>();
  118. public RelayCommand<object> RemoveCommand { get; set; }
  119. public RelayCommand AddCommand { get; set; }
  120. public RelayCommand SaveCommand { get; set; }
  121. }
  122. }