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

149 lines
5.6 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. using BPASmartClient.CustomResource.Pages.Model;
  12. namespace BPASmartClient.DosingSystem.ViewModel
  13. {
  14. public class NewRecipeViewModel : ObservableObject
  15. {
  16. public NewRecipeViewModel()
  17. {
  18. ActionManage.GetInstance.Register(new Action<object>((o) =>
  19. {
  20. if (o != null && o is RecipeModel rm)
  21. {
  22. RecipeName = rm.RecipeName;
  23. foreach (var item in rm.RawMaterials)
  24. {
  25. RawMaterials.Add(item);
  26. }
  27. RecipCode = rm.RecipCode;
  28. }
  29. }), "Details");
  30. AddCommand = new RelayCommand(() =>
  31. {
  32. p:
  33. string guid = Guid.NewGuid().ToString();
  34. if (RawMaterials.FirstOrDefault(p => p.RawMaterialId == guid) == null)
  35. {
  36. RawMaterials.Add(new RawMaterialModel()
  37. {
  38. RawMaterialId = guid
  39. });
  40. }
  41. else goto p;
  42. });
  43. RemoveCommand = new RelayCommand<object>((obj) =>
  44. {
  45. if (obj is string rm)
  46. {
  47. var res = RawMaterials.FirstOrDefault(p => p.RawMaterialId == rm);
  48. if (res != null) RawMaterials.Remove(res);
  49. }
  50. });
  51. SaveCommand = new RelayCommand(() =>
  52. {
  53. for (int i = 0; i < RawMaterials.Count; i++)
  54. {
  55. var res = DeviceListViewModel.devices.FirstOrDefault(p => p.DeviceName == RawMaterials.ElementAt(i).RawMaterialName);
  56. if (res != null) RawMaterials.ElementAt(i).DeviceIp = res.IpAddress;//根据设备名称和原料名称的唯一匹配关系,给原料配置IP
  57. }
  58. if (RecipCode.Length <= 0)//新建配方
  59. {
  60. var res = Array.FindIndex(Json<LocaPar>.Data.Recipes.ToArray(), p => p.RecipeName == RecipeName);
  61. if (res >= 0 && res < Json<LocaPar>.Data.Recipes.Count)
  62. {
  63. ErrorInfo = "该配方已存在,请重新输入";
  64. }
  65. else
  66. {
  67. AddRecipes();
  68. }
  69. }
  70. else//修改配方
  71. {
  72. var res = Array.FindIndex(Json<LocaPar>.Data.Recipes.ToArray(), p => p.RecipCode == RecipCode);
  73. if (res >= 0 && res < Json<LocaPar>.Data.Recipes.Count)
  74. {
  75. Json<LocaPar>.Data.Recipes.ElementAt(res).RecipeName = RecipeName;
  76. Json<LocaPar>.Data.Recipes.ElementAt(res).RawMaterials.Clear();
  77. foreach (var item in RawMaterials)
  78. {
  79. Json<LocaPar>.Data.Recipes.ElementAt(res).RawMaterials.Add(item);
  80. }
  81. }
  82. }
  83. ActionManage.GetInstance.Send("CloseNewRecipeView");
  84. });
  85. }
  86. private void AddRecipes()
  87. {
  88. string date = DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss");
  89. var dates = date.Split("-").ToList();
  90. StringBuilder sb = new StringBuilder();
  91. dates?.ForEach((item) => { sb.Append(item); });
  92. Json<LocaPar>.Data.Recipes.Add(new RecipeModel()
  93. {
  94. SerialNum = Json<LocaPar>.Data.Recipes.Count + 1,
  95. RawMaterials = RawMaterials,
  96. RecipCode = sb.ToString(),
  97. RecipeName = RecipeName,
  98. });
  99. }
  100. static NewRecipeViewModel()
  101. {
  102. RawMaterialNames.Clear();
  103. //RawMaterialNames.Add("乳化剂");
  104. //RawMaterialNames.Add("酶制剂");
  105. //RawMaterialNames.Add("增味剂");
  106. RawMaterialNames.Add("营养强化剂");
  107. //RawMaterialNames.Add("抗结剂");
  108. //RawMaterialNames.Add("消泡剂");
  109. //RawMaterialNames.Add("膨松剂");
  110. //RawMaterialNames.Add("防腐剂");
  111. //RawMaterialNames.Add("着色剂");
  112. RawMaterialNames.Add("甜味剂");
  113. //RawMaterialNames.Add("酸味剂");
  114. //RawMaterialNames.Add("增白剂");
  115. //RawMaterialNames.Add("香料");
  116. RawMaterialNames.Add("抗氧化剂");
  117. RawMaterialNames.Add("食用香料");
  118. //RawMaterialNames.Add("增稠剂");
  119. }
  120. private string RecipCode = string.Empty;
  121. public string RecipeName { get { return _mRecipeName; } set { _mRecipeName = value; OnPropertyChanged(); } }
  122. private string _mRecipeName;
  123. public string ErrorInfo { get { return _mErrorInfo; } set { _mErrorInfo = value; OnPropertyChanged(); } }
  124. private string _mErrorInfo;
  125. public RelayCommand AddCommand { get; set; }
  126. public RelayCommand<object> RemoveCommand { get; set; }
  127. public RelayCommand SaveCommand { get; set; }
  128. public ObservableCollection<RawMaterialModel> RawMaterials { get; set; } = new ObservableCollection<RawMaterialModel>();
  129. public static ObservableCollection<string> RawMaterialNames { get; set; } = new ObservableCollection<string>();
  130. }
  131. }