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

90 lines
3.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 Microsoft.Toolkit.Mvvm.Input;
  8. using System.Collections.ObjectModel;
  9. using BPA.Helper;
  10. namespace BPASmartClient.SmallBatchingSystem.ViewModels
  11. {
  12. public class NewRecipeViewModel : BaseModel
  13. {
  14. public NewRecipeViewModel()
  15. {
  16. Json<ConfigInfoModel>.Data.SiloInfoModels.ToList()?.ForEach(item => { SileName.Add(item.SiloName); });
  17. ActionManage.GetInstance.Register(new Action<object>((o) =>
  18. {
  19. if (o != null && o is RecipeInfo tempRecipeInfo)
  20. {
  21. RecipeName = tempRecipeInfo.RecipeName;
  22. tempRecipeInfo.SiloInfoModels.ToList()?.ForEach(item =>
  23. {
  24. int tempIndex = Array.FindIndex(SileName.ToArray(), p => p == item.SiloName);
  25. SiloInfos.Add(new RecipeRawMaterialInfo() { SiloName = item.SiloName, SiloWeight = item.SiloWeight, SelectIndex = tempIndex });
  26. });
  27. Index = Array.FindIndex(Json<ConfigInfoModel>.Data.Recipes.ToArray(), p => p.RecipeName == tempRecipeInfo.RecipeName);
  28. }
  29. }), "OpenNewRecipe", true);
  30. AddCommand = new RelayCommand(() => { SiloInfos.Add(new RecipeRawMaterialInfo()); });
  31. CancelCommand = new RelayCommand(() => { ActionManage.GetInstance.Send("NewRecipeViewModelClose"); });
  32. SaveCommand = new RelayCommand(() =>
  33. {
  34. if (Index >= 0 && Index < Json<ConfigInfoModel>.Data.OutletInfoModels.Count)
  35. {
  36. var res = Array.FindIndex(Json<ConfigInfoModel>.Data.Recipes.ToArray(), p => p.RecipeName == RecipeName);
  37. if (res >= 0 && res != Index)
  38. {
  39. ErrorInfo = "配方名称已经存在!";
  40. return;
  41. }
  42. Json<ConfigInfoModel>.Data.Recipes.ElementAt(Index).RecipeName = RecipeName;
  43. Json<ConfigInfoModel>.Data.Recipes.ElementAt(Index).SiloInfoModels.Clear();
  44. SiloInfos.ToList()?.ForEach(item =>
  45. {
  46. Json<ConfigInfoModel>.Data.Recipes.ElementAt(Index).SiloInfoModels.Add(item);
  47. });
  48. Control.GetInstance.OperationLog($"{RecipeName} 编辑完成");
  49. }
  50. else
  51. {
  52. var res = Json<ConfigInfoModel>.Data.Recipes.FirstOrDefault(p => p.RecipeName == RecipeName);
  53. if (res != null)
  54. {
  55. ErrorInfo = "配方名称已经存在!";
  56. return;
  57. }
  58. Json<ConfigInfoModel>.Data.Recipes.Add(new RecipeInfo()
  59. {
  60. RecipeName = RecipeName,
  61. SiloInfoModels = SiloInfos
  62. });
  63. Control.GetInstance.OperationLog($"{RecipeName} 添加成功");
  64. }
  65. ActionManage.GetInstance.Send("NewRecipeViewModelClose");
  66. });
  67. RemoveCommand = new RelayCommand<object>((o) =>
  68. {
  69. if (!string.IsNullOrEmpty(o?.ToString()))
  70. {
  71. var res = SiloInfos.FirstOrDefault(p => p.SiloName == o.ToString());
  72. if (res != null) SiloInfos.Remove(res);
  73. }
  74. });
  75. }
  76. public string RecipeName { get { return _mRecipeName; } set { _mRecipeName = value; OnPropertyChanged(); } }
  77. private string _mRecipeName;
  78. public ObservableCollection<RecipeRawMaterialInfo> SiloInfos { get; set; } = new ObservableCollection<RecipeRawMaterialInfo>();
  79. public ObservableCollection<string> SileName { get; set; } = new ObservableCollection<string>();
  80. }
  81. }