using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using BPA.Helper; using BPA.Helper; namespace BPASmartClient.SmallBatchingSystem.ViewModels { public class NewSiloViewModel : BaseModel { public NewSiloViewModel() { CancelCommand = new BPARelayCommand(() => { ActionManage.GetInstance.Send("SiloClose"); }); SaveCommand = new BPARelayCommand(() => { if (Index >= 0 && Index < Json.Data.SiloInfoModels.Count) { var array = Json.Data.SiloInfoModels.ToArray(); var res = Array.FindIndex(array, p => p.SiloName == SiloInfoModelObj.SiloName); if (res >= 0 && res != Index) { ErrorInfo = "料仓名称已经存在!"; return; } var temp = Array.FindIndex(array, p => p.SiloLoc == SiloInfoModelObj.SiloLoc); if (temp >= 0 && temp != Index) { ErrorInfo = "料仓位置已存在!"; return; } Json.Data.SiloInfoModels.ElementAt(Index).SiloName = SiloInfoModelObj.SiloName; Json.Data.SiloInfoModels.ElementAt(Index).SiloLoc = SiloInfoModelObj.SiloLoc; Control.GetInstance.OperationLog($"{SiloInfoModelObj.SiloName} 编辑完成"); } else { var res = Json.Data.SiloInfoModels.FirstOrDefault(p => p.SiloName == SiloInfoModelObj.SiloName); if (res != null) { ErrorInfo = "料仓名称已经存在!"; return; } var temp = Json.Data.SiloInfoModels.FirstOrDefault(p => p.SiloLoc == SiloInfoModelObj.SiloLoc); if (temp != null) { ErrorInfo = "料仓位置已存在!"; return; } Json.Data.SiloInfoModels.Add(new SiloInfoModel() { SiloLoc = SiloInfoModelObj.SiloLoc, SiloName = SiloInfoModelObj.SiloName, }); Control.GetInstance.OperationLog($"{SiloInfoModelObj.SiloName} 添加成功"); } ActionManage.GetInstance.Send("SiloClose"); }); ActionManage.GetInstance.Register(new Action((o) => { if (o != null && o is SiloInfoModel tempSilo) { SiloInfoModelObj.SiloLoc = tempSilo.SiloLoc; SiloInfoModelObj.SiloName = tempSilo.SiloName; Index = Array.FindIndex(Json.Data.SiloInfoModels.ToArray(), p => p.SiloName == tempSilo.SiloName); } }), "OpenNewSilo", true); } public SiloInfoModel SiloInfoModelObj { get { return _mSiloInfoModelObj; } set { _mSiloInfoModelObj = value; OnPropertyChanged(); } } private SiloInfoModel _mSiloInfoModelObj = new SiloInfoModel(); } }