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

132 regels
4.7 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 Microsoft.Toolkit.Mvvm.Input;
  9. using BPASmartClient.Helper;
  10. using BPASmartClient.DosingSystem.Model;
  11. namespace BPASmartClient.DosingSystem.ViewModel
  12. {
  13. public class NewRecipeViewModel : ObservableObject
  14. {
  15. public NewRecipeViewModel()
  16. {
  17. ActionManage.GetInstance.Register(new Action<object>((o) =>
  18. {
  19. if (o != null && o is RecipeModel rm)
  20. {
  21. RecipeName = rm.RecipeName;
  22. foreach (var item in rm.RawMaterials)
  23. {
  24. RawMaterials.Add(item);
  25. }
  26. RecipCode = rm.RecipCode;
  27. }
  28. }), "Details");
  29. AddCommand = new RelayCommand(() =>
  30. {
  31. p:
  32. string guid = Guid.NewGuid().ToString();
  33. if (RawMaterials.FirstOrDefault(p => p.RawMaterialId == guid) == null)
  34. {
  35. RawMaterials.Add(new RawMaterialModel()
  36. {
  37. RawMaterialId = guid
  38. });
  39. }
  40. else goto p;
  41. });
  42. RemoveCommand = new RelayCommand<object>((obj) =>
  43. {
  44. if (obj is string rm)
  45. {
  46. var res = RawMaterials.FirstOrDefault(p => p.RawMaterialId == rm);
  47. if (res != null) RawMaterials.Remove(res);
  48. }
  49. });
  50. SaveCommand = new RelayCommand(() =>
  51. {
  52. for (int i = 0; i < RawMaterials.Count; i++)
  53. {
  54. var res = DeviceListViewModel.devices.FirstOrDefault(p => p.DeviceName == RawMaterials.ElementAt(i).RawMaterialName);
  55. if (res != null) RawMaterials.ElementAt(i).DeviceIp = res.IpAddress;
  56. }
  57. if (RecipCode.Length <= 0)
  58. {
  59. var res = Array.FindIndex(Json<LocaPar>.Data.Recipes.ToArray(), p => p.RecipeName == RecipeName);
  60. if (res >= 0 && res < Json<LocaPar>.Data.Recipes.Count)
  61. {
  62. ErrorInfo = "该配方已存在,请重新输入";
  63. }
  64. else
  65. {
  66. AddRecipes();
  67. }
  68. }
  69. else
  70. {
  71. var res = Array.FindIndex(Json<LocaPar>.Data.Recipes.ToArray(), p => p.RecipCode == RecipCode);
  72. if (res >= 0 && res < Json<LocaPar>.Data.Recipes.Count)
  73. {
  74. Json<LocaPar>.Data.Recipes.ElementAt(res).RecipeName = RecipeName;
  75. Json<LocaPar>.Data.Recipes.ElementAt(res).RawMaterials.Clear();
  76. foreach (var item in RawMaterials)
  77. {
  78. Json<LocaPar>.Data.Recipes.ElementAt(res).RawMaterials.Add(item);
  79. }
  80. }
  81. }
  82. ActionManage.GetInstance.Send("CloseNewRecipeView");
  83. });
  84. }
  85. private void AddRecipes()
  86. {
  87. string date = DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss");
  88. var dates = date.Split("-").ToList();
  89. StringBuilder sb = new StringBuilder();
  90. dates?.ForEach((item) => { sb.Append(item); });
  91. Json<LocaPar>.Data.Recipes.Add(new RecipeModel()
  92. {
  93. SerialNum = Json<LocaPar>.Data.Recipes.Count + 1,
  94. RawMaterials = RawMaterials,
  95. RecipCode = sb.ToString(),
  96. RecipeName = RecipeName,
  97. });
  98. }
  99. static NewRecipeViewModel()
  100. {
  101. RawMaterialNames.Clear();
  102. }
  103. private string RecipCode = string.Empty;
  104. public string RecipeName { get { return _mRecipeName; } set { _mRecipeName = value; OnPropertyChanged(); } }
  105. private string _mRecipeName;
  106. public string ErrorInfo { get { return _mErrorInfo; } set { _mErrorInfo = value; OnPropertyChanged(); } }
  107. private string _mErrorInfo;
  108. public RelayCommand AddCommand { get; set; }
  109. public RelayCommand<object> RemoveCommand { get; set; }
  110. public RelayCommand SaveCommand { get; set; }
  111. public ObservableCollection<RawMaterialModel> RawMaterials { get; set; } = new ObservableCollection<RawMaterialModel>();
  112. public static ObservableCollection<string> RawMaterialNames { get; set; } = new ObservableCollection<string>();
  113. }
  114. }