using DevExpress.XtraEditors; using IStation.Untity; using System.Collections.Generic; using System.Linq; using System.Windows.Forms; namespace IStation.WinFrmUI.Basic { public partial class EquipmentMonitorMappingMgrPage : DocumentPage { public EquipmentMonitorMappingMgrPage() { InitializeComponent(); this.PageTitle.Caption = "测点关联"; this.treeList1.InitialMultiColMultiSelectSettings(); this.treeList1.SelectImageList = ImageLib.Lib; this.equipmentExTreeListCtrl1.StationChangedEvent += EquipmentExTreeListCtrl1_StationChangedEvent; this.equipmentExTreeListCtrl1.FocusedChangedEvent += EquipmentExTreeListCtrl1_FocusedChangedEvent; } public class CurrentViewModel { public CurrentViewModel() { } public CurrentViewModel(Model.MonitorPointGroup rhs) { this.ID = rhs.ID; this.ParentIds = rhs.ParentIds; this.ParentID = TreeParentIdsHelper.GetLastParentID(rhs.ParentIds); this.Name = rhs.Name; this.SortCode = rhs.SortCode; this.Description = rhs.Description; this.ImageIndex = ImageLib.Group; this.Model = rhs; this.IsGroup = true; if (this.ParentIds == null) { this.ParentIds = new List(); } } public CurrentViewModel(Model.MonitorPoint rhs) { this.ID = rhs.ID; this.ParentID = rhs.GroupID; this.Name = rhs.Name; this.SortCode = rhs.SortCode; this.Description = rhs.Description; this.ImageIndex = ImageLib.MonitorPoint; this.Model = rhs; this.IsGroup = false; this.ParentIds = new List(); } public long ID { get; set; } public List ParentIds { get; set; } public long ParentID { get; set; } public string Name { get; set; } public int SortCode { get; set; } public string Description { get; set; } public int ImageIndex { get; set; } public object Model { get; set; } public bool IsGroup { get; set; } public bool? Checked { get; set; } = false; public long MappingID { get; set; } } private BLL.EquipmentMonitorMapping _bll = new BLL.EquipmentMonitorMapping(); private List _allBindingList = null; private Model.Equipment _model = null; /// /// 初始化数据 /// public override void InitialDataSource() { this.equipmentExTreeListCtrl1.SetBindingData(); } //泵站变换 private void EquipmentExTreeListCtrl1_StationChangedEvent(Model.Station model) { _allBindingList = new List(); if (model != null) { if (model != null) { var groups = new BLL.MonitorPointGroup().GetByBelongTypeAndBelongID(IStation.ObjectType.Station, model.ID); if (groups != null && groups.Any()) { foreach (var item in groups) { var vm = new CurrentViewModel(item); _allBindingList.Add(vm); } } var monitorPoints = new BLL.MonitorPoint().GetByBelongTypeAndBelongID(IStation.ObjectType.Station, model.ID); if (monitorPoints != null && monitorPoints.Any()) { foreach (var item in monitorPoints) { var vm = new CurrentViewModel(item); _allBindingList.Add(vm); } } } } this.currentViewModelBindingSource.DataSource = _allBindingList; this.currentViewModelBindingSource.ResetBindings(false); } //设备变换 private void EquipmentExTreeListCtrl1_FocusedChangedEvent(Model.Equipment obj) { _model = obj; var mappings = _bll.GetByEquipmentID(obj.ID); if (_allBindingList != null && _allBindingList.Any()) { _allBindingList.ForEach(x => x.Checked = false); if (mappings != null && mappings.Any()) { var ids = mappings.ToDictionary(x => x.MonitorPointID, x => x.ID); var groups = _allBindingList.Where(x => x.IsGroup).OrderByDescending(x => x.ParentIds.Count).ToList(); foreach (var group in groups) { var children = _allBindingList.Where(x => x.ParentID == group.ID || x.ParentIds.Contains(group.ID)); if (children == null || !children.Any()) { group.Checked = false; continue; } var children_monitorPoints = children.Where(x => !x.IsGroup).ToList(); int children_monitorPoints_cekNum = 0; children_monitorPoints?.ForEach(x => { if (ids.Keys.Contains(x.ID)) { x.Checked = true; x.MappingID = ids[x.ID]; children_monitorPoints_cekNum++; } }); bool? cekBol = null; if (children_monitorPoints.Count == children_monitorPoints_cekNum) { cekBol = true; } else if (children_monitorPoints_cekNum == 0) { cekBol = false; } if (cekBol != null) { var children_groups = children.Where(x => x.IsGroup).ToList(); if (children_groups != null && children_groups.Any()) { bool? children_groups_cekBol = false; var children_groups_cekNum = children_groups.Count(x => x.Checked.HasValue && x.Checked.Value); if (children_groups_cekNum == 0) { children_groups_cekBol = false; } else if (children_groups_cekNum == children_groups.Count) { children_groups_cekBol = true; } if (children_groups_cekBol == null || cekBol.Value != children_groups_cekBol.Value) { cekBol = null; } } } group.Checked = cekBol; } } } this.currentViewModelBindingSource.ResetBindings(false); } //全部展开 private void barBtnExpandAll_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e) { this.treeList1.ExpandAll(); } //全部折叠 private void barBtnCollapseAll_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e) { this.treeList1.CollapseAll(); } //检索 private void barCekSearch_CheckedChanged(object sender, DevExpress.XtraBars.ItemClickEventArgs e) { if (this.barCekSearch.Checked) this.treeList1.ShowFindPanel(); else this.treeList1.HideFindPanel(); } // 刷新数据 private void barBtnRefresh_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e) { InitialDataSource(); } // 保存 private void barBtnSave_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e) { if (_model == null) return; this.treeList1.CloseEditor(); var mappings = _allBindingList?.Where(x => !x.IsGroup && x.Checked.HasValue && x.Checked.Value) .Select(x => new Model.EquipmentMonitorMapping() { //ID = x.MappingID, EquipmentID = _model.ID, MonitorPointID = x.ID }).ToList(); var bol = _bll.SetOfMapping(_model.ID, mappings); if (!bol) { XtraMessageBox.Show("保存失败!"); return; } XtraMessageBox.Show("保存成功!"); } } }