using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Data.Common; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using DevExpress.XtraGrid.Columns; using ProjectBase.Controls; using ProjectBase.Data.BaseDAL; using ProjectBase.Data.Logs; using SIMDP.BLL; using SIMDP.Model; using ProjectBase.Util; namespace SIMDP.View { public partial class FormNode : BaseDock { //BlPlcInfo blPlcInfo = new BlPlcInfo(); //BlDataGroup blDataGroup = new BlDataGroup(); //BlDataPoint bLDataPoint = new BlDataPoint(); private Dictionary columnNameAlias = new Dictionary();//GridView中字段别名字典集合 TreeNode choiceNode; // 选中节点信息 DbTransaction trans; public FormNode() { InitializeComponent(); InitTreeNode(); } /// /// 初始化树 /// private void InitTreeNode() { this.tree_Node.BeginUpdate(); this.tree_Node.Nodes.Clear(); this.tree_Node.ImageList = this.imageList1; List plcInfoList = BLLFactory.Instance.GetAll(); foreach (MoPlcInfo plcInfo in plcInfoList) { if (plcInfo != null) { TreeNode rootNode = new TreeNode(); //根节点 rootNode.Text = plcInfo.PlcName; rootNode.Name = plcInfo.PlcId.ToString(); rootNode.ImageIndex = 0; rootNode.SelectedImageIndex = 0; this.tree_Node.Nodes.Add(rootNode); List dataGroupList = BLLFactory.Instance.Find(string.Format(" data_group_plc_id = {0}", plcInfo.PlcId.ToString())); foreach (MoDataGroup dataGroup in dataGroupList) { TreeNode oneNode = new TreeNode(); //一级节点 oneNode.Text = dataGroup.DataGroupName; oneNode.Name = dataGroup.DataGroupId.ToString(); oneNode.ImageIndex = 1; oneNode.SelectedImageIndex = 1; rootNode.Nodes.Add(oneNode); List dataPointsList = BLLFactory.Instance.Find(string.Format(" data_point_plc_id = {0} And data_point_group_id = {1}", plcInfo.PlcId.ToString(), dataGroup.DataGroupId.ToString())); foreach (MoDataPoint dataPoint in dataPointsList) { TreeNode twoNode = new TreeNode(); //二级节点 twoNode.Text = dataPoint.DataPointName; twoNode.Name = dataPoint.DataPointId.ToString(); twoNode.ImageIndex = 2; twoNode.SelectedImageIndex = 2; oneNode.Nodes.Add(twoNode); } } } } this.tree_Node.ExpandAll(); this.tree_Node.EndUpdate(); } /// /// 选中节点后显示 /// /// /// private void tree_Node_AfterSelect(object sender, TreeViewEventArgs e) { choiceNode = e.Node; if (0 == e.Node.Level) { string plcId = e.Node.Name; if (!string.IsNullOrEmpty(plcId)) { this.gridView_Node.Columns.Clear(); #region 添加别名解析 columnNameAlias.Clear(); columnNameAlias.Add("DataGroupPlcId", "数据组所属PLC编码"); columnNameAlias.Add("DataGroupId", "数据组编码"); columnNameAlias.Add("DataGroupName", "数据组名称"); columnNameAlias.Add("DataGroupType", "数据组类型"); foreach (var item in columnNameAlias) { GridColumn col = new GridColumn(); col.Caption = item.Value; col.FieldName = item.Key; col.Visible = true; this.gridView_Node.Columns.Add(col); } #endregion #region 添加修改和删除操作 //DevExpress.XtraEditors.Repository.RepositoryItemHyperLinkEdit repositoryEditGroup = new DevExpress.XtraEditors.Repository.RepositoryItemHyperLinkEdit(); //repositoryEditGroup.Name = "repositoryEditGroup"; //repositoryEditGroup.NullText = "修改"; //repositoryEditGroup.Click += new System.EventHandler(RepositoryEditGroup_Click); //DevExpress.XtraGrid.Columns.GridColumn gridColumn_Edit = new GridColumn(); //gridColumn_Edit.Caption = "修改操作"; //gridColumn_Edit.Name = "gridColumn_Edit"; //gridColumn_Edit.Visible = true; //gridColumn_Edit.ColumnEdit = repositoryEditGroup; //this.gridView_Node.Columns.Add(gridColumn_Edit); //DevExpress.XtraEditors.Repository.RepositoryItemHyperLinkEdit repositoryDeleteGroup = new DevExpress.XtraEditors.Repository.RepositoryItemHyperLinkEdit(); //repositoryDeleteGroup.Name = "repositoryDeleteGroup"; //repositoryDeleteGroup.NullText = "删除"; //repositoryDeleteGroup.Click += new System.EventHandler(RepositoryDeleteGroup_Click); //DevExpress.XtraGrid.Columns.GridColumn gridColumn_Delete = new GridColumn(); //gridColumn_Delete.Caption = "删除操作"; //gridColumn_Delete.Name = "gridColumn_Delete"; //gridColumn_Delete.Visible = true; //gridColumn_Delete.ColumnEdit = repositoryDeleteGroup; //this.gridView_Node.Columns.Add(gridColumn_Delete); //this.gridControl_Node.RepositoryItems.AddRange(new DevExpress.XtraEditors.Repository.RepositoryItem[] { repositoryEditGroup, repositoryDeleteGroup }); #endregion for (int i = 0; i < this.gridView_Node.Columns.Count; i++) //设置每列内容居中显示 { this.gridView_Node.Columns[i].AppearanceCell.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center; } this.gridControl_Node.DataSource = BLLFactory.Instance.Find(string.Format(" data_group_plc_id = {0}", plcId)); this.gridControl_Node.RefreshDataSource(); } } else if (1 == e.Node.Level) { string plcId = e.Node.Parent.Name; string groupId = e.Node.Name; if (!string.IsNullOrEmpty(groupId)) { this.gridView_Node.Columns.Clear(); #region 添加别名解析 columnNameAlias.Clear(); columnNameAlias.Add("DataPointPlcId", "数据点PLC编码"); columnNameAlias.Add("DataPointGroupId", "数据点采集组编码"); columnNameAlias.Add("DataPointId", "数据点编码"); columnNameAlias.Add("DataPointName", "数据点名称"); columnNameAlias.Add("DataPointType", "数据点类型"); columnNameAlias.Add("DataPointSource", "数据点PLC变量名"); columnNameAlias.Add("DataProc", "数据点执行逻辑"); foreach (var item in columnNameAlias) { GridColumn col = new GridColumn(); col.Caption = item.Value; col.FieldName = item.Key; col.Visible = true; this.gridView_Node.Columns.Add(col); } #endregion for (int i = 0; i < this.gridView_Node.Columns.Count; i++) //设置每列内容居中显示 { this.gridView_Node.Columns[i].AppearanceCell.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center; } this.gridControl_Node.DataSource = BLLFactory.Instance.Find(string.Format(" data_point_plc_id = {0} And data_point_group_id = {1}", plcId, groupId)); this.gridControl_Node.RefreshDataSource(); } } else if (2 == e.Node.Level) { string plcId = e.Node.Parent.Parent.Name; string groupId = e.Node.Parent.Name; string pointId = e.Node.Name; if (!string.IsNullOrEmpty(pointId)) { this.gridView_Node.Columns.Clear(); #region 添加别名解析 columnNameAlias.Clear(); columnNameAlias.Add("DataPointPlcId", "数据点PLC编码"); columnNameAlias.Add("DataPointGroupId", "数据点采集组编码"); columnNameAlias.Add("DataPointId", "数据点编码"); columnNameAlias.Add("DataPointName", "数据点名称"); columnNameAlias.Add("DataPointType", "数据点类型"); columnNameAlias.Add("DataPointSource", "数据点PLC变量名"); columnNameAlias.Add("DataProc", "数据点执行逻辑"); foreach (var item in columnNameAlias) { GridColumn col = new GridColumn(); col.Caption = item.Value; col.FieldName = item.Key; col.Visible = true; this.gridView_Node.Columns.Add(col); } #endregion for (int i = 0; i < this.gridView_Node.Columns.Count; i++) //设置每列内容居中显示 { this.gridView_Node.Columns[i].AppearanceCell.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center; } this.gridControl_Node.DataSource = BLLFactory.Instance.Find(string.Format(" data_point_plc_id = {0} And data_point_group_id = {1} And data_point_id = {2}", plcId, groupId, pointId)); this.gridControl_Node.RefreshDataSource(); } } } private void menuTree_Expand_Click(object sender, EventArgs e) { this.tree_Node.ExpandAll(); } private void menuTree_Collapse_Click(object sender, EventArgs e) { this.tree_Node.CollapseAll(); } private void menuTree_Refresh_Click(object sender, EventArgs e) { InitTreeNode(); } private void menuTree_AddGroup_Click(object sender, EventArgs e) { if (BLLFactory.Instance.GetRecordCount()<=0) { DevExpress.XtraEditors.XtraMessageBox.Show("请先添加设备配置信息"); } if (choiceNode.Level == 0) { FormEditGroup editGroup = new FormEditGroup(); editGroup.saveData += new FormEditGroup.save(InitTreeNode); editGroup.plcId = choiceNode.Name; editGroup.ShowDialog(); } else { DevExpress.XtraEditors.XtraMessageBox.Show("请选中设备配置信息的一个点!"); } } private void menuTree_AddPoint_Click(object sender, EventArgs e) { if (BLLFactory.Instance.GetRecordCount() <= 0) { DevExpress.XtraEditors.XtraMessageBox.Show("请先添加组信息"); } if (choiceNode.Level == 1) { FormEditPoint editPoint = new FormEditPoint(); editPoint.groupId = choiceNode.Name; editPoint.plcId = choiceNode.Parent.Name; editPoint.saveData += new FormEditPoint.save(InitTreeNode); editPoint.ShowDialog(); } else { DevExpress.XtraEditors.XtraMessageBox.Show("请选中组!"); } } private void gridView_Node_CustomDrawRowIndicator(object sender, DevExpress.XtraGrid.Views.Grid.RowIndicatorCustomDrawEventArgs e) { if (e.Info.IsRowIndicator && e.RowHandle >= 0) { e.Info.DisplayText = (e.RowHandle + 1).ToString(); } } private void menuTree_Delete_Click(object sender, EventArgs e) { if (choiceNode == null) { DevExpress.XtraEditors.XtraMessageBox.Show("请选中要删除的记录。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } if (choiceNode.Level == 0) { DevExpress.XtraEditors.XtraMessageBox.Show("不能删除PLC,请重新选择。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } if (DevExpress.XtraEditors.XtraMessageBox.Show("删除此记录将影响规则配置,您确定删除选定的记录么?", "提示信息", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.No) { return; } bool flag = false; using (trans = BLLFactory.Instance.CreateTransaction()) { try { if (choiceNode.Level == 1) { if (choiceNode.Nodes.Count > 0) { flag = BLLFactory.Instance.DeleteByCondition(string.Format(" data_point_group_id = {0}", choiceNode.Name), trans); if (flag) { flag = BLLFactory.Instance.Delete(choiceNode.Name, SysEnvironment.CurrentLoginID, trans); } } else { flag = BLLFactory.Instance.Delete(choiceNode.Name, SysEnvironment.CurrentLoginID); } } else if (choiceNode.Level == 2) { flag = BLLFactory.Instance.Delete(choiceNode.Name, SysEnvironment.CurrentLoginID); } trans.Commit(); } catch (Exception ex) { trans.Rollback(); DevExpress.XtraEditors.XtraMessageBox.Show("删除失败。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); LogHelper.log.Error(string.Format("删除数据库出现错误:{0}", ex)); } } trans.Dispose(); if (flag) { DevExpress.XtraEditors.XtraMessageBox.Show("删除成功。请在规则配置中作出相应的修改", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); InitTreeNode(); } } private void gridView_Node_CustomColumnDisplayText(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDisplayTextEventArgs e) { if (e.Column.FieldName == "DataGroupType") { e.DisplayText = SysEnvironment.dirDataGroupType.FirstOrDefault(p => p.Key == Convert.ToInt32(e.Value)).Value; } if (e.Column.FieldName == "DataPointType") { e.DisplayText = SysEnvironment.dirType.FirstOrDefault(p => p.Key == Convert.ToInt32(e.Value)).Value; } } private void gridControl_Node_DoubleClick(object sender, EventArgs e) { try { object selectRow = this.gridView_Node.GetFocusedRow(); if (selectRow == null) { DevExpress.XtraEditors.XtraMessageBox.Show("请先选择一条记录!"); return; } if (selectRow.GetType() == typeof(MoDataGroup)) { MoDataGroup group = selectRow as MoDataGroup; FormEditGroup editGroup = new FormEditGroup(); editGroup.saveData += new FormEditGroup.save(InitTreeNode); editGroup.group = group; editGroup.ShowDialog(); } else if (selectRow.GetType() == typeof(MoDataPoint)) { MoDataPoint point = selectRow as MoDataPoint; FormEditPoint editPoint = new FormEditPoint(); editPoint.point = point; editPoint.saveData += new FormEditPoint.save(InitTreeNode); editPoint.ShowDialog(); } } catch (Exception ex) { DevExpress.XtraEditors.XtraMessageBox.Show("双击修改时出现错误,请先选择一条记录", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); LogHelper.log.Error(ex); } } } }