终端一体化运控平台
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 

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