using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; using System.Text; using System.Threading.Tasks; using BPA.Helper; using BPA.Helper; namespace BPASmartClient.DosingSystem.ViewModel { public class NewOutletViewModel : ViewModelBase { public NewOutletViewModel() { DeviceInquire.GetInstance.devices.ToList()?.ForEach(item => { SileName.Add($"{item.DeviceName}"); }); ActionManage.GetInstance.Register(new Action((o) => { if (o != null && o is OutletInfoModel tempOutletInfo) { OutletName = tempOutletInfo.OutletName; OutletLoc = tempOutletInfo.OutletLoc; tempOutletInfo.SiloInfos?.ToList()?.ForEach((item => { int tempIndex = Array.FindIndex(SileName.ToArray(), p => p == item); SiloInfos.Add(new RawMaterialInfo() { RawMaterialName = item, SelectIndex = tempIndex }); })); Index = Array.FindIndex(Json.Data.OutletInfoModels.ToArray(), p => p.OutletName == tempOutletInfo.OutletName); } }), "OpenNewOutlet", true); AddCommand = new BPARelayCommand(() => { SiloInfos.Add(new RawMaterialInfo()); }); CancelCommand = new BPARelayCommand(() => { ActionManage.GetInstance.Send("NewOutletViewClose"); }); SaveCommand = new BPARelayCommand(() => { ObservableCollection SileNames = new ObservableCollection(); SiloInfos.ToList()?.ForEach(item => { SileNames.Add(item.RawMaterialName); }); if (Index >= 0 && Index < Json.Data.OutletInfoModels.Count) { var array = Json.Data.OutletInfoModels.ToArray(); var res = Array.FindIndex(array, p => p.OutletName == OutletName); if (res >= 0 && res != Index) { ErrorInfo = "出料口名称已经存在!"; return; } var temp = Array.FindIndex(array, p => p.OutletLoc == OutletLoc); if (temp >= 0 && temp != Index) { ErrorInfo = "出料口位置已存在!"; return; } Json.Data.OutletInfoModels.ElementAt(Index).OutletName = OutletName; Json.Data.OutletInfoModels.ElementAt(Index).OutletLoc = OutletLoc; Json.Data.OutletInfoModels.ElementAt(Index).SiloInfos.Clear(); Json.Data.OutletInfoModels.ElementAt(Index).SiloInfos = SileNames; //Control.GetInstance.OperationLog($"{OutletName} 编辑完成"); } else { var res = Json.Data.OutletInfoModels.FirstOrDefault(p => p.OutletName == OutletName); if (res != null) { ErrorInfo = "出料口名称已经存在!"; return; } var temp = Json.Data.OutletInfoModels.FirstOrDefault(p => p.OutletLoc == OutletLoc); if (temp != null) { ErrorInfo = "出料口位置已存在!"; return; } Json.Data.OutletInfoModels.Add(new OutletInfoModel() { OutletLoc = OutletLoc, OutletName = OutletName, SiloInfos = SileNames }); //Control.GetInstance.OperationLog($"{OutletName} 添加成功"); } ActionManage.GetInstance.Send("NewOutletViewClose"); }); RemoveCommand = new BPARelayCommand((o) => { if (!string.IsNullOrEmpty(o?.ToString())) { var res = SiloInfos.FirstOrDefault(p => p.RawMaterialName == o.ToString()); if (res != null) SiloInfos.Remove(res); } }); } public string OutletName { get { return _mOutletName; } set { _mOutletName = value; OnPropertyChanged(); } } private string _mOutletName; public int OutletLoc { get { return _mOutletLoc; } set { _mOutletLoc = value; OnPropertyChanged(); } } private int _mOutletLoc; public ObservableCollection SiloInfos { get; set; } = new ObservableCollection(); public ObservableCollection SileName { get; set; } = new ObservableCollection(); } public class RawMaterialInfo : NotifyBase { public string RawMaterialName { get { return _mRawMaterialName; } set { _mRawMaterialName = value; OnPropertyChanged(); } } private string _mRawMaterialName; public int SelectIndex { get { return _mSelectIndex; } set { _mSelectIndex = value; OnPropertyChanged(); } } private int _mSelectIndex; } }