using DevExpress.XtraEditors;
|
using DevExpress.XtraGrid.Views.Grid.ViewInfo;
|
using System;
|
using System.Collections.Generic;
|
using System.Linq;
|
using System.Windows.Forms;
|
|
namespace IStation.WinFrmUI.Monitor
|
{
|
public partial class MonitorDataSourcesMgrPage : DocumentPage
|
{
|
public MonitorDataSourcesMgrPage()
|
{
|
InitializeComponent();
|
this.PageTitle.Caption = "数据来源";
|
this.gridView1.SetNormalEditView();
|
this.gridView1.BestFitColumns();
|
this.gridView1.OptionsView.ShowIndicator = false;
|
this.gridView1.OptionsView.GroupDrawMode = DevExpress.XtraGrid.Views.Grid.GroupDrawMode.Office;
|
this.gridView1.OptionsBehavior.AllowFixedGroups = DevExpress.Utils.DefaultBoolean.True;
|
this.gridView1.OptionsView.ShowFooter = true;
|
this.monitorDataSourcesListCtrl1.FocusedChangedEvent += MonitorDataSourcesListCtrl1_FocusedChangedEvent;
|
}
|
|
public class CurrentViewModel
|
{
|
public long StationID { get; set; }
|
public string StationName { get; set; }
|
public long GroupID { get; set; }
|
public string GroupName { get; set; }
|
public string SignalType { get; set; }
|
public string Name { get; set; }
|
public long SysId { get; set; }
|
public long SignalId { get; set; }
|
public string SignId { get; set; }
|
public Model.eFormatType FormatType { get; set; }
|
public Model.eFormulaType FormulaType { get; set; }
|
public string FormulaParas { get; set; }
|
public DateTime? MinTime { get; set; }
|
public DateTime? MaxTime { get; set; }
|
public int? RecordCount { get; set; }
|
}
|
|
private BLL.SignalType _bllSignalType = new BLL.SignalType();
|
|
private Model.MonitorDataSources _model = null;
|
private List<CurrentViewModel> _allBindingList = null;
|
|
/// <summary>
|
/// 清空数据
|
/// </summary>
|
public void Clear()
|
{
|
_allBindingList = new List<CurrentViewModel>();
|
this.currentViewModelBindingSource.DataSource = _allBindingList;
|
}
|
|
/// <summary>
|
/// 初始化数据
|
/// </summary>
|
public override void InitialDataSource()
|
{
|
this.monitorDataSourcesListCtrl1.SetBindingData();
|
}
|
|
//来源变换
|
private void MonitorDataSourcesListCtrl1_FocusedChangedEvent(Model.MonitorDataSources obj)
|
{
|
_model = obj;
|
SetBindingData(obj);
|
}
|
|
/// <summary>
|
/// 绑定数据
|
/// </summary>
|
public void SetBindingData(Model.MonitorDataSources model)
|
{
|
WaitFrmHelper.ShowWaitForm();
|
_allBindingList = new List<CurrentViewModel>();
|
if (model != null)
|
{
|
var bllMonitorPointGroup = new BLL.MonitorPointGroup();
|
var bllMonitorPoint = new BLL.MonitorPoint();
|
var bllMonitorDataSetSummary = new BLL.MonitorDataSetSummary();
|
var stations = new BLL.Station().GetAll();
|
if (stations != null && stations.Any())
|
{
|
foreach (var station in stations)
|
{
|
var monitorPointGroups = bllMonitorPointGroup.GetByBelongTypeAndBelongID(IStation.ObjectType.Station, station.ID);
|
if (monitorPointGroups == null || !monitorPointGroups.Any())
|
continue;
|
|
foreach (var group in monitorPointGroups)
|
{
|
var monitorPoints = bllMonitorPoint.GetExSignalWithSignalTypeByBelongTypeAndBelongIDAndGroupID(group.BelongType, group.BelongID, group.ID);
|
if (monitorPoints == null || !monitorPoints.Any())
|
continue;
|
foreach (var item in monitorPoints)
|
{
|
var vm = new CurrentViewModel();
|
vm.StationID = station.ID;
|
vm.StationName = station.Name;
|
vm.GroupID = group.ID;
|
vm.GroupName = group.Name;
|
vm.Name = item.Name;
|
vm.SysId = item.MonitorPointID;
|
vm.SignalId = item.SignalID;
|
vm.SignalType = item.SignalType;
|
vm.FormatType = item.FormatType;
|
var mapper = model.DataSourcesMappings?.Find(x => x.SysId == vm.SysId);
|
if (mapper != null)
|
{
|
vm.SignId = mapper.SignId;
|
vm.FormulaType = mapper.FormulaType;
|
vm.FormulaParas = mapper.FormulaParas;
|
|
var monitorDataSetSummaries = bllMonitorDataSetSummary.GetByMonitor(model.ID, item.MonitorPointID);
|
if (monitorDataSetSummaries != null && monitorDataSetSummaries.Any())
|
{
|
vm.MinTime = monitorDataSetSummaries.Min(x => x.MinTime);
|
vm.MaxTime = monitorDataSetSummaries.Max(x => x.MaxTime);
|
vm.RecordCount = monitorDataSetSummaries.Sum(x => x.Count);
|
}
|
}
|
_allBindingList.Add(vm);
|
}
|
}
|
}
|
}
|
}
|
|
this.currentViewModelBindingSource.DataSource = _allBindingList;
|
this.gridView1.ExpandAllGroups();
|
this.gridView1.BestFitColumns();
|
WaitFrmHelper.HideWaitForm();
|
}
|
|
#region GridView
|
|
//自定义组名
|
private void gridView1_CustomDrawGroupRow(object sender, DevExpress.XtraGrid.Views.Base.RowObjectCustomDrawEventArgs e)
|
{
|
var vm = this.gridView1.GetRow(e.RowHandle) as CurrentViewModel;
|
var grid = e.Info as GridGroupRowInfo;
|
if (grid.Level == 0)
|
{
|
grid.GroupText = vm.StationName;
|
}
|
else
|
{
|
grid.GroupText = vm.GroupName;
|
}
|
}
|
|
//自定义控件
|
private void gridView1_CustomRowCellEdit(object sender, DevExpress.XtraGrid.Views.Grid.CustomRowCellEditEventArgs e)
|
{
|
if (e.Column == this.colFormulaType)
|
{
|
var edit = new DevExpress.XtraEditors.Repository.RepositoryItemImageComboBox();
|
edit.Items.Add("无", Model.eFormulaType.None, -1);
|
|
var vm = this.gridView1.GetRow(e.RowHandle) as CurrentViewModel;
|
if (vm==null)
|
{
|
return;
|
}
|
if (vm.FormatType == Model.eFormatType.Numeric)
|
{
|
edit.Items.Add("单位换算", Model.eFormulaType.Unit, -1);
|
edit.Items.Add("一元系数", Model.eFormulaType.Once, -1);
|
edit.Items.Add("二元系数", Model.eFormulaType.Twice, -1);
|
}
|
else
|
{
|
edit.Items.Add("单一映射", Model.eFormulaType.Single, -1);
|
edit.Items.Add("比较映射", Model.eFormulaType.Compare, -1);
|
edit.Items.Add("区间映射", Model.eFormulaType.Range, -1);
|
}
|
edit.EditValueChanged += (s, args) =>
|
{
|
vm.FormulaParas = string.Empty;
|
};
|
e.RepositoryItem = edit;
|
}
|
}
|
|
//单元格点击
|
private void gridView1_RowCellClick(object sender, DevExpress.XtraGrid.Views.Grid.RowCellClickEventArgs e)
|
{
|
if (e.Column == this.colFormulaParas)
|
{
|
var vm = this.gridView1.GetRow(e.RowHandle) as CurrentViewModel;
|
var signalType = _bllSignalType.GetByIdentifier(vm.SignalType);
|
if (vm.FormatType == Model.eFormatType.Numeric)
|
{
|
switch (vm.FormulaType)
|
{
|
case Model.eFormulaType.Unit:
|
{
|
var dlg = new UnitRatioMatrixDlg();
|
dlg.Set(vm.FormulaParas, signalType);
|
dlg.ReloadDataEvent += (json) =>
|
{
|
vm.FormulaParas = json;
|
return true;
|
};
|
dlg.ShowDialog();
|
}
|
break;
|
case Model.eFormulaType.Once:
|
{
|
var dlg = new OnceRatioMatrixDlg();
|
dlg.Set(vm.FormulaParas);
|
dlg.ReloadDataEvent += (json) =>
|
{
|
vm.FormulaParas = json;
|
return true;
|
};
|
dlg.ShowDialog();
|
}
|
break;
|
case Model.eFormulaType.Twice:
|
{
|
var dlg = new TwiceRatioMatrixDlg();
|
dlg.Set(vm.FormulaParas);
|
dlg.ReloadDataEvent += (json) =>
|
{
|
vm.FormulaParas = json;
|
return true;
|
};
|
dlg.ShowDialog();
|
}
|
break;
|
}
|
}
|
else
|
{
|
switch (vm.FormulaType)
|
{
|
case Model.eFormulaType.Single:
|
{
|
var dlg = new SingleMappingMatrixDlg();
|
dlg.Set(vm.FormulaParas, signalType);
|
dlg.ReloadDataEvent += (json) =>
|
{
|
vm.FormulaParas = json;
|
return true;
|
};
|
dlg.ShowDialog();
|
}
|
break;
|
case Model.eFormulaType.Compare:
|
{
|
var dlg = new CompareMappingMatrixDlg();
|
dlg.Set(vm.FormulaParas, signalType);
|
dlg.ReloadDataEvent += (json) =>
|
{
|
vm.FormulaParas = json;
|
return true;
|
};
|
dlg.ShowDialog();
|
}
|
break;
|
case Model.eFormulaType.Range:
|
{
|
var dlg = new RangeMappingMatrixDlg();
|
dlg.Set(vm.FormulaParas);
|
dlg.ReloadDataEvent += (json) =>
|
{
|
vm.FormulaParas = json;
|
return true;
|
};
|
dlg.ShowDialog();
|
}
|
break;
|
}
|
}
|
}
|
}
|
|
#endregion
|
|
#region 菜单
|
|
//展开
|
private void barBtnExpand_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
|
{
|
this.gridView1.ExpandGroupRow(this.gridView1.FocusedRowHandle, true);
|
}
|
|
//折叠
|
private void barBtnCollapse_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
|
{
|
this.gridView1.CollapseGroupRow(this.gridView1.FocusedRowHandle, true);
|
}
|
|
//全部展开
|
private void barBtnExpandAll_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
|
{
|
this.gridView1.ExpandAllGroups();
|
}
|
|
//全部折叠
|
private void barBtnCollapseAll_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
|
{
|
this.gridView1.CollapseAllGroups();
|
}
|
|
//检索
|
private void barCekSearch_CheckedChanged(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
|
{
|
if (this.barCekSearch.Checked)
|
this.gridView1.ShowFindPanel();
|
else
|
this.gridView1.HideFindPanel();
|
}
|
|
// 刷新数据
|
private void barBtnRefresh_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
|
{
|
InitialDataSource();
|
}
|
|
|
#endregion
|
|
|
// 保存
|
private void barBtnSave_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
|
{
|
if (_allBindingList == null || !_allBindingList.Any())
|
return;
|
this.gridView1.ClearColumnErrors();
|
this.gridView1.CloseEditor();
|
this.gridView1.UpdateCurrentRow();
|
|
for (int i = 0; i < _allBindingList.Count; i++)
|
{
|
var vm = _allBindingList[i];
|
if (vm.FormulaType != Model.eFormulaType.None)
|
{
|
if (string.IsNullOrEmpty(vm.FormulaParas))
|
{
|
this.gridView1.FocusedRowHandle = i;
|
this.gridView1.SetColumnError(this.colFormulaParas, "必填项");
|
return;
|
}
|
}
|
}
|
|
var mappers = _allBindingList.Where(x => !string.IsNullOrEmpty(x.SignId)).
|
Select(x => new Model.DataSourcesMapping()
|
{
|
SysId = x.SysId,
|
SignId = x.SignId,
|
FormulaType = x.FormulaType,
|
FormulaParas = x.FormulaParas
|
}).ToList();
|
|
var operatingObjList = new List<CurrentViewModel>();
|
var oldMappings = _model.DataSourcesMappings?.ToList();
|
if (oldMappings != null)
|
{
|
foreach (var old in oldMappings)
|
{
|
var vm = _allBindingList.Find(x => x.SysId == old.SysId);
|
var exist = mappers.Find(x => x.SysId == old.SysId);
|
if (exist == null)
|
{
|
if (vm.RecordCount.HasValue)
|
{
|
operatingObjList.Add(vm);
|
}
|
}
|
else
|
{
|
if (exist.SignId != old.SignId ||
|
exist.FormulaType != old.FormulaType ||
|
exist.FormulaParas != old.FormulaParas)
|
{
|
if (vm.RecordCount.HasValue)
|
{
|
operatingObjList.Add(vm);
|
}
|
}
|
}
|
}
|
}
|
|
_model.DataSourcesMappings = mappers;
|
var bol = new BLL.MonitorDataSources().Update(_model);
|
if (!bol)
|
{
|
XtraMessageBox.Show("保存失败!");
|
return;
|
}
|
XtraMessageBox.Show("保存成功!");
|
|
if (operatingObjList.Any())
|
{
|
if (XtraMessageBox.Show($"某些配置项修改前存在历史数据,是否清空?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation) != DialogResult.OK)
|
return;
|
var bllMonitorDataSet = new BLL.MonitorDataSet();
|
foreach (var item in operatingObjList)
|
{
|
var clearBol = bllMonitorDataSet.Clear(_model.ID, item.SysId, item.SignalId);
|
}
|
SetBindingData(_model);
|
}
|
}
|
|
//导入数据
|
private void barBtnImport_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
|
{
|
if (_model == null)
|
return;
|
var bol = MonitorDataImportHelper.Import(_model.ID, _model.DataSourcesMappings);
|
if (!bol)
|
{
|
return;
|
}
|
XtraMessageBox.Show("导入成功!");
|
SetBindingData(_model);
|
}
|
|
|
|
|
}
|
}
|