using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; using System.Text; using System.Threading.Tasks; using BPA.Helper; using BPASmartClient.SmallBatchingSystem.Views; using Microsoft.Toolkit.Mvvm.ComponentModel; using Microsoft.Toolkit.Mvvm.Input; namespace BPASmartClient.SmallBatchingSystem.ViewModels { public class NewOutletViewModel : BaseModel { public NewOutletViewModel() { Json.Data.SiloInfoModels.ToList()?.ForEach(item => { SileName.Add($"{item.SiloName}"); }); 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 RelayCommand(() => { SiloInfos.Add(new RawMaterialInfo()); }); CancelCommand = new RelayCommand(() => { ActionManage.GetInstance.Send("NewOutletViewClose"); }); SaveCommand = new RelayCommand(() => { 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 RelayCommand((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 : ObservableObject { 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; } }