using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; using System.Windows; using SWS.CAD.ViewModels.myViewModelBase; using System.Windows.Input; using Telerik.Windows.Controls; using Prism.Ioc; using Prism.Services.Dialogs; using SWS.Commons; using SWS.Model; using SWS.WPF.Views; namespace SWS.CAD.ViewModels { public class DialogBusbarManagementViewModel : DialogBase, IDialogAware { #region 字段 private ObservableCollection _SignalInfos = new ObservableCollection(); /// /// 端子排参数信息列表 /// public ObservableCollection SignalInfos { get { return _SignalInfos; } set { _SignalInfos = value; RaisePropertyChanged(nameof(SignalInfos)); } } private SignalInfo _SelectedSignalInfo; /// /// 当前选中的信号类型 /// public SignalInfo SelectedSignalInfo { get { return _SelectedSignalInfo; } set { _SelectedSignalInfo = value; RaisePropertyChanged(nameof(SelectedSignalInfo)); } } private bool _IsReadOnly; /// /// 是否可编辑 /// public bool IsReadOnly { get { return _IsReadOnly; } set { _IsReadOnly = value; RaisePropertyChanged(nameof(IsReadOnly)); } } #region 表格下拉列表 private ObservableCollection _BusbarDropDownList; /// /// 母线下拉列表 /// public ObservableCollection BusbarDropDownList { get { return _BusbarDropDownList; } set { _BusbarDropDownList = value; } } private ec_Cable _SelectBusbarValue; /// /// 下拉列表选择的母线值 /// public ec_Cable SelectBusbarValue { get { return _SelectBusbarValue; } set { _SelectBusbarValue = value; RaisePropertyChanged(nameof(SelectBusbarValue)); } } #endregion private List _UpdateCableLs = new List(); /// /// 存储要修改的Cable /// public List UpdateCableLs { get { return _UpdateCableLs; } set { _UpdateCableLs = value; RaisePropertyChanged(nameof(UpdateCableLs)); } } #endregion public string Title => ""; public event Action RequestClose; public bool CanCloseDialog() { return true; } public void OnDialogClosed() { } private List Cables; public async void OnDialogOpened(IDialogParameters parameters) { //title = parameters.GetValue(GlobalObject.dialogPar.title.ToString()); title = "母线管理其下信号"; IsReadOnly = true; Cables = parameters.GetValue>(GlobalObject.dialogPar.para1.ToString()); BusbarDropDownList = new ObservableCollection(Cables); var BusbarName= parameters.GetValue(GlobalObject.dialogPar.info.ToString()); SelectBusbarValue = Cables.Where(c => c.TagNumber.Equals(BusbarName)).FirstOrDefault(); if (SelectBusbarValue.WireGroups != null) { int index = 0; foreach (var WireGroup in SelectBusbarValue.WireGroups) { index++; SignalInfos.Add(new SignalInfo(WireGroup){Index = index,}); } } } public override void ExecuteOKCommandAsync(object para) { IDialogParameters res = new Prism.Services.Dialogs.DialogParameters(); //res.Add(GlobalObject.dialogPar.para1.ToString(), Cables.ToList()); res.Add(GlobalObject.dialogPar.para1.ToString(), UpdateCableLs.ToList()); RequestClose.Invoke(new DialogResult(ButtonResult.Yes, res)); } public override void ExecuteCloseCommand(object parameter) { if (parameter as string == "ClickNo") { DialogResult res = new DialogResult(ButtonResult.No); RequestClose.Invoke(res); } else { RequestClose.Invoke(new DialogResult(ButtonResult.Cancel)); } this.Dispose(); } #region 方法 public ICommand Busbarcmd => new DelegateCommand(BusbarSelectionChanged); /// /// 下拉框选择项改变时 /// /// public void BusbarSelectionChanged(object parameter) { SignalInfos.Clear(); if (SelectBusbarValue.WireGroups != null) { int index = 0; foreach (var WireGroup in SelectBusbarValue.WireGroups) { index++; SignalInfos.Add(new SignalInfo(WireGroup){Index = index,}); } } } public ICommand EditEndCmd => new DelegateCommand(EditEnd); /// /// 编辑结束事件 /// /// public virtual void EditEnd(object parameter) { //做个标记表示该项修改过 } public ICommand NewCom => new DelegateCommand(NewSignal); /// /// 新增母线关联信号 /// /// public void NewSignal(object parameter) { //打开信号管理页面 IDialogParameters para = new Prism.Services.Dialogs.DialogParameters(); para.Add(GlobalObject.dialogPar.para1.ToString(), "关联母线"); var _dialogService = GlobalObject._prismContainer.Resolve(); _dialogService.ShowDialog(nameof(DialogSignalManagement), para, (RES) => { if (RES.Result == ButtonResult.Yes) { //返回的信号集合 var ResWireGroupIds = RES.Parameters.GetValue>(GlobalObject.dialogPar.para1.ToString()); //返回的信号添加到表格源数据中 int index = SignalInfos.Count(); foreach (var ResSignalManagementInfo in ResWireGroupIds) { index++; SignalInfos.Add(new SignalInfo(ResSignalManagementInfo) { Index=index}); //源数据中也需要添加 if (!SelectBusbarValue.WireGroupIds.Contains(ResSignalManagementInfo.Wire_Group_ID)) { SelectBusbarValue.WireGroupIds.Add(ResSignalManagementInfo.Wire_Group_ID); List WireGrouplist = SelectBusbarValue.WireGroups.ToList(); WireGrouplist.Add(new ec_Wire_Group() { Group_Name = ResSignalManagementInfo.Group_Name, Signal_Group = ResSignalManagementInfo.GroupOther, Signal_SeqNo = ResSignalManagementInfo.Signal_SeqNo, Group_Desc = ResSignalManagementInfo.Group_Desc, Group_Desc_EN = ResSignalManagementInfo.Group_Desc_EN, IO_Type = ResSignalManagementInfo.IO_Type, Wire_Group_ID = ResSignalManagementInfo.Wire_Group_ID, }); SelectBusbarValue.WireGroups = WireGrouplist; } //添加到要返回的修改列表 if (UpdateCableLs.Contains(SelectBusbarValue))//存在就覆盖 { UpdateCableLs[UpdateCableLs.IndexOf(SelectBusbarValue)] = SelectBusbarValue; } else { UpdateCableLs.Add(SelectBusbarValue); } } } else if (RES.Result == ButtonResult.No) { } }); } public ICommand DeleteCom => new DelegateCommand(DeleteSignal); /// /// 删除 /// /// public void DeleteSignal(object parameter) { if (SelectedSignalInfo==null) { System.Windows.MessageBox.Show("请选择要删除的信号", "KunHengCAD", MessageBoxButton.OK, MessageBoxImage.Warning); return; } else { MessageBoxResult result = System.Windows.MessageBox.Show($"确定删除选择的1行数据?", "KunHengCAD", MessageBoxButton.OKCancel, MessageBoxImage.Question); if (result == MessageBoxResult.OK) { //删除数据源的信号实例 SelectBusbarValue.WireGroups.ToList().RemoveAll(s => s.Wire_Group_ID.Equals(SelectedSignalInfo.Wire_Group_ID)); List WireGrouplist = SelectBusbarValue.WireGroups.ToList(); WireGrouplist.RemoveAll(s => s.Wire_Group_ID.Equals(SelectedSignalInfo.Wire_Group_ID)); SelectBusbarValue.WireGroups = WireGrouplist; //删除源中的信号id SelectBusbarValue.WireGroupIds.Remove(SelectedSignalInfo.Wire_Group_ID); //刷新页面重新赋值 int index = 0; SignalInfos.Clear(); foreach (var WireGroup in SelectBusbarValue.WireGroups) { index++; SignalInfos.Add(new SignalInfo(WireGroup) { Index = index, }); } //有修改的Cable保存到UpdateCableLs集合返回给IO页面保存 if (UpdateCableLs.Contains(SelectBusbarValue))//存在就覆盖 { UpdateCableLs[UpdateCableLs.IndexOf(SelectBusbarValue)] = SelectBusbarValue; } else { UpdateCableLs.Add(SelectBusbarValue); } } else { return; } } } #endregion } public class SignalInfo : DialogBase { #region 页面渲染字段 private int _Index; /// /// 序号 /// public int Index { get { return _Index; } set { _Index = value; RaisePropertyChanged(nameof(Index)); } } private string _Group_Name; /// /// 信号名称 /// public string Group_Name { get { return _Group_Name; } set { _Group_Name = value; RaisePropertyChanged(nameof(Group_Name)); } } private string _GroupOther; /// /// 组别 /// public string GroupOther { get { return _GroupOther; } set { _GroupOther = value; RaisePropertyChanged(nameof(GroupOther)); } } private string _Signal_SeqNo; /// /// 信号编号 /// public string Signal_SeqNo { get { return _Signal_SeqNo; } set { _Signal_SeqNo = value; RaisePropertyChanged(nameof(Signal_SeqNo)); } } private string _Group_Desc_EN; /// /// 英文描述 /// public string Group_Desc_EN { get { return _Group_Desc_EN; } set { _Group_Desc_EN = value; RaisePropertyChanged(nameof(Group_Desc_EN)); } } private string _Group_Desc; /// /// 中文描述 /// public string Group_Desc { get { return _Group_Desc; } set { _Group_Desc = value; RaisePropertyChanged(nameof(Group_Desc)); } } private string _IoType; /// /// 信号类型 /// public string IoType { get { return _IoType; } set { _IoType = value; RaisePropertyChanged(nameof(Index)); } } #endregion #region 扩展字段 private string _Wire_Group_ID; /// /// ID /// public string Wire_Group_ID { get { return _Wire_Group_ID; } set { _Wire_Group_ID = value; RaisePropertyChanged(nameof(Wire_Group_ID)); } } private bool _IsModified; /// /// 表示是否修改过 /// public bool IsModified { get { return _IsModified; } set { _IsModified = value; RaisePropertyChanged(nameof(IsModified)); } } #endregion public SignalInfo() { } public SignalInfo(ec_Wire_Group WireGroup) { Group_Name = WireGroup.Group_Name; GroupOther = WireGroup.Signal_Group; Signal_SeqNo = WireGroup.Signal_SeqNo; Group_Desc = WireGroup.Group_Desc; Group_Desc_EN = WireGroup.Group_Desc_EN; IoType = WireGroup.IO_Type; Wire_Group_ID = WireGroup.Wire_Group_ID; } public SignalInfo(SignalManagementInfo SignalInfo) { Group_Name = SignalInfo.Group_Name; GroupOther = SignalInfo.GroupOther; Signal_SeqNo = SignalInfo.Signal_SeqNo; Group_Desc = SignalInfo.Group_Desc; Group_Desc_EN = SignalInfo.Group_Desc_EN; IoType = SignalInfo.IO_Type; Wire_Group_ID = SignalInfo.Wire_Group_ID; } } }