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 CurrentViewModel() { }
public CurrentViewModel(Model.Station station)
{
this.ID = station.ID;
this.Name = station.Name;
this.ObjectType = IStation.ObjectType.Station;
this.Model = station;
this.IsGroup = true;
this.ImageIndex = ImageLib.Station;
this.IsWorkCurve = false;
}
public CurrentViewModel(Model.Equipment pump)
{
this.ID = pump.ID;
this.Name = pump.Name;
this.ParentID = pump.BelongID;
this.ObjectType = IStation.ObjectType.Equipment;
this.Model = pump;
this.IsGroup = false;
this.ImageIndex = ImageLib.Pump;
}
public CurrentViewModel(Model.PumpCurveMapping mapping)
{
this.ID = mapping.CurveID;
this.Name = mapping.OtherName;
this.ParentID = mapping.PumpID;
this.ObjectType = IStation.ObjectType.PumpCurveMapping;
this.Model = mapping;
this.IsGroup = false;
}
public CurrentViewModel(Model.PumpSpeedCurve curve)
{
this.ID = curve.ID;
this.Name = curve.HZ.ToString();
this.ParentID = curve.PumpCurveID;
this.ObjectType = IStation.ObjectType.PumpSpeedCurve;
this.Model = curve;
this.IsGroup = false;
this.ImageIndex = ImageLib.PumpSpeedCurve;
}
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 BLL.PumpCurve _bll = new BLL.PumpCurve();
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();
stations?.ForEach(x =>
{
_allBindingList.Add(new CurrentViewModel(x));
});
long? key = null;
var pumps = new BLL.Equipment().GetAllPump();
if (pumps != null && pumps.Any())
{
pumps.ForEach(pump =>
{
_allBindingList.Add(new CurrentViewModel(pump));
});
var bllSpeedCurve = new BLL.PumpSpeedCurve();
var pumpIds = pumps?.Select(x => x.ID).ToList();
var pumpCurveMappings = new BLL.PumpCurveMapping().GetByPumpIds(pumpIds);
if (pumps != null && pumps.Any())
{
var first = pumpCurveMappings.First();
key = first.ID;
foreach (var mapping in pumpCurveMappings)
{
var vm = new CurrentViewModel(mapping);
vm.Reset(mapping.IsWorking);
_allBindingList.Add(vm);
var speedCurves = bllSpeedCurve.GetByPumpCurveID(mapping.CurveID);
speedCurves?.ForEach(x =>
{
_allBindingList.Add(new CurrentViewModel(x));
});
}
}
}
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;
if (_allBindingList == 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();
this.treeList1.FocusedNode = this.treeList1.FocusedNode;
}
//全部展开
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 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.Equipment;
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;
if (vm.ObjectType == IStation.ObjectType.Equipment)
{
this.FocusedChangedEvent?.Invoke(vm.ObjectType, vm.Model);
}
else if (vm.ObjectType == IStation.ObjectType.PumpCurveMapping)
{
var pumpMapping = vm.Model as Model.PumpCurveMapping;
var pumpCurve = _bll.GetByID(pumpMapping.CurveID);
var pumpCurveMapping = new Model.PumpCurveExMapping(pumpCurve, pumpMapping);
this.FocusedChangedEvent?.Invoke(IStation.ObjectType.PumpCurveExMapping, pumpCurveMapping);
}
else if (vm.ObjectType == IStation.ObjectType.PumpSpeedCurve)
{
this.FocusedChangedEvent?.Invoke(vm.ObjectType, vm.Model);
}
}
}
}