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

119 lines
5.4 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.ObjectModel;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using BPA.Helper;
  8. using BPASmartClient.SmallBatchingSystem.Views;
  9. using Microsoft.Toolkit.Mvvm.ComponentModel;
  10. using Microsoft.Toolkit.Mvvm.Input;
  11. namespace BPASmartClient.SmallBatchingSystem.ViewModels
  12. {
  13. public class NewOutletViewModel : BaseModel
  14. {
  15. public NewOutletViewModel()
  16. {
  17. Json<ConfigInfoModel>.Data.SiloInfoModels.ToList()?.ForEach(item => { SileName.Add($"{item.SiloName}"); });
  18. ActionManage.GetInstance.Register(new Action<object>((o) =>
  19. {
  20. if (o != null && o is OutletInfoModel tempOutletInfo)
  21. {
  22. OutletName = tempOutletInfo.OutletName;
  23. OutletLoc = tempOutletInfo.OutletLoc;
  24. tempOutletInfo.SiloInfos?.ToList()?.ForEach((item =>
  25. {
  26. int tempIndex = Array.FindIndex(SileName.ToArray(), p => p == item);
  27. SiloInfos.Add(new RawMaterialInfo() { RawMaterialName = item, SelectIndex = tempIndex });
  28. }));
  29. Index = Array.FindIndex(Json<ConfigInfoModel>.Data.OutletInfoModels.ToArray(), p => p.OutletName == tempOutletInfo.OutletName);
  30. }
  31. }), "OpenNewOutlet", true);
  32. AddCommand = new RelayCommand(() => { SiloInfos.Add(new RawMaterialInfo()); });
  33. CancelCommand = new RelayCommand(() => { ActionManage.GetInstance.Send("NewOutletViewClose"); });
  34. SaveCommand = new RelayCommand(() =>
  35. {
  36. ObservableCollection<string> SileNames = new ObservableCollection<string>();
  37. SiloInfos.ToList()?.ForEach(item => { SileNames.Add(item.RawMaterialName); });
  38. if (Index >= 0 && Index < Json<ConfigInfoModel>.Data.OutletInfoModels.Count)
  39. {
  40. var array = Json<ConfigInfoModel>.Data.OutletInfoModels.ToArray();
  41. var res = Array.FindIndex(array, p => p.OutletName == OutletName);
  42. if (res >= 0 && res != Index)
  43. {
  44. ErrorInfo = "出料口名称已经存在!";
  45. return;
  46. }
  47. var temp = Array.FindIndex(array, p => p.OutletLoc == OutletLoc);
  48. if (temp >= 0 && temp != Index)
  49. {
  50. ErrorInfo = "出料口位置已存在!";
  51. return;
  52. }
  53. Json<ConfigInfoModel>.Data.OutletInfoModels.ElementAt(Index).OutletName = OutletName;
  54. Json<ConfigInfoModel>.Data.OutletInfoModels.ElementAt(Index).OutletLoc = OutletLoc;
  55. Json<ConfigInfoModel>.Data.OutletInfoModels.ElementAt(Index).SiloInfos.Clear();
  56. Json<ConfigInfoModel>.Data.OutletInfoModels.ElementAt(Index).SiloInfos = SileNames;
  57. Control.GetInstance.OperationLog($"{OutletName} 编辑完成");
  58. }
  59. else
  60. {
  61. var res = Json<ConfigInfoModel>.Data.OutletInfoModels.FirstOrDefault(p => p.OutletName == OutletName);
  62. if (res != null)
  63. {
  64. ErrorInfo = "出料口名称已经存在!";
  65. return;
  66. }
  67. var temp = Json<ConfigInfoModel>.Data.OutletInfoModels.FirstOrDefault(p => p.OutletLoc == OutletLoc);
  68. if (temp != null)
  69. {
  70. ErrorInfo = "出料口位置已存在!";
  71. return;
  72. }
  73. Json<ConfigInfoModel>.Data.OutletInfoModels.Add(new OutletInfoModel()
  74. {
  75. OutletLoc = OutletLoc,
  76. OutletName = OutletName,
  77. SiloInfos = SileNames
  78. });
  79. Control.GetInstance.OperationLog($"{OutletName} 添加成功");
  80. }
  81. ActionManage.GetInstance.Send("NewOutletViewClose");
  82. });
  83. RemoveCommand = new RelayCommand<object>((o) =>
  84. {
  85. if (!string.IsNullOrEmpty(o?.ToString()))
  86. {
  87. var res = SiloInfos.FirstOrDefault(p => p.RawMaterialName == o.ToString());
  88. if (res != null) SiloInfos.Remove(res);
  89. }
  90. });
  91. }
  92. public string OutletName { get { return _mOutletName; } set { _mOutletName = value; OnPropertyChanged(); } }
  93. private string _mOutletName;
  94. public int OutletLoc { get { return _mOutletLoc; } set { _mOutletLoc = value; OnPropertyChanged(); } }
  95. private int _mOutletLoc;
  96. public ObservableCollection<RawMaterialInfo> SiloInfos { get; set; } = new ObservableCollection<RawMaterialInfo>();
  97. public ObservableCollection<string> SileName { get; set; } = new ObservableCollection<string>();
  98. }
  99. public class RawMaterialInfo : ObservableObject
  100. {
  101. public string RawMaterialName { get { return _mRawMaterialName; } set { _mRawMaterialName = value; OnPropertyChanged(); } }
  102. private string _mRawMaterialName;
  103. public int SelectIndex { get { return _mSelectIndex; } set { _mSelectIndex = value; OnPropertyChanged(); } }
  104. private int _mSelectIndex;
  105. }
  106. }