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 DataSourcesMapperCtrl : XtraUserControl { public DataSourcesMapperCtrl() { InitializeComponent(); 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; } 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 Model.MonitorDataSources _model = null; private BLL.SignalType _bllSignalType = new BLL.SignalType(); private List _allBindingList = null; /// /// 初始化数据 /// public void Clear() { _allBindingList = new List(); this.currentViewModelBindingSource.DataSource = _allBindingList; } /// /// 初始化数据 /// public void SetBindingData(Model.MonitorDataSources model) { WaitFrmHelper.ShowWaitForm(); _model = model; _allBindingList = new List(); 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(); } //自定义组名 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.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; } } } } /// /// 展开 /// public void Expand() { this.gridView1.ExpandGroupRow(this.gridView1.FocusedRowHandle, true); } /// /// 折叠 /// public void Collapse() { this.gridView1.CollapseGroupRow(this.gridView1.FocusedRowHandle, true); } /// /// 全部展开 /// public void ExpandAll() { this.gridView1.ExpandAllGroups(); } /// /// 全部折叠 /// public void CollapseAll() { this.gridView1.CollapseAllGroups(); } /// /// 检索 /// public void Search(bool bol) { if (bol) this.gridView1.ShowFindPanel(); else this.gridView1.HideFindPanel(); } /// /// 验证 /// private bool Valid() { this.gridView1.ClearColumnErrors(); this.gridView1.CloseEditor(); this.gridView1.UpdateCurrentRow(); if (_allBindingList.Count > 0) { 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, "必填项"); //XtraMessageBox.Show("请将值映射信息设置完整!"); return false; } } } } return true; } /// /// 保存 /// public void Save() { if (_model == null) return; if (!_allBindingList.Any()) return; if (!Valid()) return; this.gridView1.CloseEditor(); this.gridView1.UpdateCurrentRow(); 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(); 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); } } } }