using DevExpress.XtraEditors; using System; using System.ComponentModel; using System.Linq; namespace IStation.WinFrmUI.Basic { /// /// /// public partial class PumpCurveMappingListCtrl : XtraUserControl { public PumpCurveMappingListCtrl() { InitializeComponent(); this.treeList1.InitialDefaultSettings(); this.treeList1.SelectImageList = ImageLib.Lib; this.layoutControl1.SetupLayoutControl(); } public class CurrentViewModel { public long ID { get; set; } public long ParentID { get; set; } public string Name { get; set; } public string ObjectType { get; set; } public object Model { get; set; } public bool IsGroup { get; set; } public int ImageIndex { get; set; } public bool IsWorkCurve { get; set; } public void Reset(bool isWorking) { if (this.ObjectType == IStation.ObjectType.PumpCurveMapping) { this.IsWorkCurve = isWorking; this.ImageIndex = this.IsWorkCurve ? ImageLib.WorkPumpCurve : ImageLib.PumpCurve; } } } /// /// 聚焦改变事件 /// public event Action FocusedChangedEvent; private BindingList _allBindingList = null;//所有绑定列表 /// /// 绑定数据 /// public void Clear() { _allBindingList = new BindingList(); this.treeList1.DataSource = _allBindingList; this.FocusedChangedEvent?.Invoke(null); } /// /// 绑定数据 /// public void SetBindingData() { WaitFrmHelper.ShowWaitForm("正在加载数据..."); _allBindingList = new BindingList(); var stations = new BLL.Station().GetAll(); if (stations != null && stations.Any()) { var imgIndex = ImageLib.Station; foreach (var station in stations) { var vm = new CurrentViewModel() { ID = station.ID, Name = station.Name, ObjectType = IStation.ObjectType.Station, Model = station, IsGroup = true, ImageIndex = imgIndex, IsWorkCurve = false }; //_allBindingList.Add(vm); } } long? key = null; var pumps = new BLL.Product().GetAllPump(); if (pumps != null && pumps.Any()) { var imgIndex = ImageLib.Pump; foreach (var pump in pumps) { var vm = new CurrentViewModel() { ID = pump.ID, Name = pump.Name, ParentID = pump.BelongID, ObjectType = IStation.ObjectType.Product, Model = pump, IsGroup = true, ImageIndex = imgIndex }; _allBindingList.Add(vm); } var pumpIds = pumps.Select(x => x.ID).ToList(); var pumpCurveMappings = new BLL.PumpCurveMapping().GetByPumpIds(pumpIds); if (pumpCurveMappings != null && pumpCurveMappings.Any()) { var first = pumpCurveMappings.First(); key = first.ID; var pumpCurveIds = pumpCurveMappings.Select(x => x.CurveID).Distinct().ToList(); foreach (var mapping in pumpCurveMappings) { var vm = new CurrentViewModel() { ID = mapping.ID, Name = mapping.OtherName, ParentID = mapping.PumpID, ObjectType = IStation.ObjectType.PumpCurveMapping, Model = mapping, IsGroup = false }; vm.Reset(mapping.IsWorking); _allBindingList.Add(vm); } } } this.treeList1.DataSource = _allBindingList; this.treeList1.ForceInitialize(); if (key.HasValue) { this.treeList1.FocusedNode = this.treeList1.FindNodeByKeyID(key.Value); } WaitFrmHelper.HideWaitForm(); } /// /// 修改曲线映射别名 /// /// public void UpdateOtherName(string name) { var row = this.treeList1.GetCurrentViewModel(_allBindingList); if (row == null) return; row.Name = name; this.treeList1.RefreshNode(this.treeList1.FocusedNode); } /// /// 修改工作曲线 /// public void UpdateWorkingCurve() { var row = this.treeList1.GetCurrentViewModel(_allBindingList); if (row == null) return; var existWorkPumpCurve = _allBindingList.Where(x => x.ParentID == row.ParentID && x.ID != row.ID && x.IsWorkCurve).FirstOrDefault(); if (existWorkPumpCurve != null) { existWorkPumpCurve.Reset(false); } row.Reset(true); this.treeList1.RefreshDataSource(); } /// /// 删除 /// /// public void DeletePumpMapping(long id) { var node = this.treeList1.FindNodeByKeyID(id); if (node == null) return; if (this.treeList1.GetDataRecordByNode(node) is CurrentViewModel vm) { _allBindingList.Remove(vm); } } /// /// 添加泵曲线 /// public void AddPumpCurveMapping(Model.PumpCurveMapping mapping) { if (mapping == null) return; var vm = new CurrentViewModel() { ID = mapping.ID, Name = mapping.OtherName, ParentID = mapping.PumpID, ObjectType = IStation.ObjectType.PumpCurveMapping, Model = mapping, IsGroup = false }; vm.Reset(mapping.IsWorking); _allBindingList.Add(vm); this.treeList1.RefreshDataSource(); } //全部展开 private void barBtnExpandAll_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e) { this.treeList1.ExpandAll(); } //全部折叠 private void barBtnCollpseAll_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e) { this.treeList1.CollapseAll(); } //检索 private void barCkSearch_CheckedChanged(object sender, DevExpress.XtraBars.ItemClickEventArgs e) { if (this.barCkSearch.Checked) this.treeList1.ShowFindPanel(); else this.treeList1.HideFindPanel(); } //详细信息 private void barBtnDetail_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e) { XtraMessageBox.Show("待补充!"); } //泵信息 private void barBtnPumpInfo_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e) { var vm = this.treeList1.GetCurrentViewModel(_allBindingList); if (vm == null) return; var parentVm = _allBindingList?.Where(x => x.ID == vm.ParentID).FirstOrDefault(); if (parentVm == null) return; var pump = parentVm.Model as Model.Product; WaitFrmHelper.ShowWaitForm(); var dlg = new PumpInfoDlg(); dlg.Shown += delegate { WaitFrmHelper.HideWaitForm(); }; dlg.SetBindingData(pump); dlg.ShowDialog(); } //聚焦前 private void treeList1_BeforeFocusNode(object sender, DevExpress.XtraTreeList.BeforeFocusNodeEventArgs e) { if (this.treeList1.GetDataRecordByNode(e.Node) is CurrentViewModel row) { if (row.IsGroup) { e.CanFocus = false; } } } //聚焦改变 private void treeList1_FocusedNodeChanged(object sender, DevExpress.XtraTreeList.FocusedNodeChangedEventArgs e) { var vm = this.treeList1.GetCurrentViewModel(_allBindingList); if (vm == null) return; if (vm.IsGroup) return; var mapping = vm.Model as Model.PumpCurveMapping; this.FocusedChangedEvent?.Invoke(mapping); } } }