using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Linq; using System.Threading.Tasks; using System.Windows.Forms; using DevExpress.XtraEditors; using ProjectBase.Controls; using SIMDP.BLL; using SIMDP.Model; using ProjectBase.Data.BaseDAL; namespace SIMDP { public partial class FormLogOperation : DevExpress.XtraEditors.XtraForm { BlAuthoryGroup blGroup = new BlAuthoryGroup(); BlLogOperation blOperation = new BlLogOperation(); string condition = null; public FormLogOperation() { InitializeComponent(); } private void FormLogOperation_Load(object sender, EventArgs e) { InitControl(); InitGrid(); InitTree(); } /// /// 初始化控件 /// private void InitControl() { List tableList = BLLFactory.Instance.GetTableNames(); tableList.Insert(0, "全部表"); this.lookUp_TableName.Properties.DataSource = tableList; this.lookUp_TableName.Properties.NullText = null; this.lookUp_TableName.EditValue = tableList[0]; List list = new List() { "全部", "增加", "修改", "删除" }; this.lookUp_Type.Properties.DataSource = list; this.lookUp_Type.Properties.NullText = null; this.lookUp_Type.EditValue = list[0]; } /// /// 初始化树 /// private void InitTree() { this.tree_Operation.BeginUpdate(); this.tree_Operation.Nodes.Clear(); List list = BLLFactory.Instance.GetAll(); List rootList = new List(); foreach (MoAuthoryGroup item in list) { if (item.ParentGroupId == "") { rootList.Add(item); } } foreach (MoAuthoryGroup root in rootList) { if (root != null) { TreeNode rootNode = new TreeNode(); //根节点 rootNode.Text = root.GroupName; rootNode.Name = root.GroupId; rootNode.ImageIndex = 0; rootNode.SelectedImageIndex = 0; rootNode.Tag = string.Format(" operation_groupId = '{0}'", root.GroupId); this.tree_Operation.Nodes.Add(rootNode); List subList = BLLFactory.Instance.Find(string.Format(" parent_group_id = '{0}'", root.GroupId)); AddNode(subList, rootNode); } } TreeNode tableNode = new TreeNode("数据库表", 5, 5); this.tree_Operation.Nodes.Add(tableNode); List tableList = BLLFactory.Instance.GetFieldList("operation_tableName"); foreach (string tablename in tableList) { TreeNode subNode = new TreeNode(tablename); subNode.ImageIndex = 6; subNode.SelectedImageIndex = 6; subNode.Tag = string.Format(" operation_tableName = '{0}'", tablename); tableNode.Nodes.Add(subNode); List operationList = new List() { "增加", "修改", "删除" }; foreach (string operationType in operationList) { TreeNode operationNode = new TreeNode(operationType); if (operationType == "增加") { operationNode.ImageIndex = 7; operationNode.SelectedImageIndex = 7; } else if (operationType == "修改") { operationNode.ImageIndex = 8; operationNode.SelectedImageIndex = 8; } else if (operationType == "删除") { operationNode.ImageIndex = 9; operationNode.SelectedImageIndex = 9; } operationNode.Tag = string.Format(" operation_tableName = '{0}' AND operation_type = '{1}'",tablename, operationType); subNode.Nodes.Add(operationNode); } } this.tree_Operation.ExpandAll(); this.tree_Operation.EndUpdate(); this.tree_Operation.SelectedNode = null; this.tree_Operation.TabStop = false; } /// /// 添加树节点 /// /// /// private void AddNode(List list, TreeNode parentNode) { if (list == null) { return; } foreach (MoAuthoryGroup item in list) { TreeNode node = new TreeNode(); //根节点 node.Text = item.GroupName; node.Name = item.GroupId; string index = item.GroupId.Substring(0, 1); node.ImageIndex = Convert.ToInt32(index) - 1; node.SelectedImageIndex = Convert.ToInt32(index) - 1; node.Tag = string.Format(" operation_groupId = '{0}'", item.GroupId); parentNode.Nodes.Add(node); List subList = BLLFactory.Instance.Find(string.Format(" parent_group_id = '{0}'", item.GroupId)); AddNode(subList, node); } } /// /// 选中节点后显示 /// /// /// private void tree_Operation_AfterSelect(object sender, TreeViewEventArgs e) { if (e.Node != null && e.Node.Tag != null) { condition = e.Node.Tag.ToString(); BindData(condition); } else { condition = " 1=1"; BindData(condition); } } /// /// 初始化表格控件 /// private void InitGrid() { //entity this.winGridViewPager1.OnPageChanged += new EventHandler(winGridViewPager1_OnPageChanged); this.winGridViewPager1.OnStartExport += new EventHandler(winGridViewPager1_OnStartExport); this.winGridViewPager1.OnRefresh += new EventHandler(winGridViewPager1_OnRefresh); this.winGridViewPager1.gridView1.CustomColumnDisplayText += new DevExpress.XtraGrid.Views.Base.CustomColumnDisplayTextEventHandler(gridView1_CustomColumnDisplayText); this.winGridViewPager1.ShowLineNumber = true; this.winGridViewPager1.BestFitColumnWith = true; this.winGridViewPager1.DisplayColumns = "Account,Name,GroupId,TableName,Type,Ip,Mac,Time"; #region 添加别名解析 this.winGridViewPager1.AddColumnAlias("ID", ""); this.winGridViewPager1.AddColumnAlias("Account", "登录用户账号"); this.winGridViewPager1.AddColumnAlias("Name", "登录用户名称"); this.winGridViewPager1.AddColumnAlias("GroupId", "所属公司"); this.winGridViewPager1.AddColumnAlias("TableName", "操作表名称"); this.winGridViewPager1.AddColumnAlias("Type", "操作类型"); this.winGridViewPager1.AddColumnAlias("Ip", "IP地址"); this.winGridViewPager1.AddColumnAlias("Mac", "Mac地址"); this.winGridViewPager1.AddColumnAlias("Time", "日志更新时间"); #endregion this.winGridViewPager1.PrintTitle = "用户操作日志表"; this.winGridViewPager1.gridView1.Appearance.HeaderPanel.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center; condition = " 1=1"; BindData(condition); } /// /// 绑定数据源 /// /// private void BindData(string condition) { this.winGridViewPager1.DataSource = blOperation.FindWithPager(condition, this.winGridViewPager1.PagerInfo); for (int i = 0; i < this.winGridViewPager1.gridView1.Columns.Count; i++) //设置每列内容居中显示 { this.winGridViewPager1.gridView1.Columns[i].AppearanceCell.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center; } } /// /// 分页控件翻页的操作 /// private void winGridViewPager1_OnPageChanged(object sender, EventArgs e) { BindData(condition); } /// /// 分页控件全部导出操作前的操作 /// private void winGridViewPager1_OnStartExport(object sender, EventArgs e) { this.winGridViewPager1.AllToExport = blOperation.Find(condition); } /// /// 分页控件刷新操作 /// private void winGridViewPager1_OnRefresh(object sender, EventArgs e) { BindData(condition); } void gridView1_CustomColumnDisplayText(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDisplayTextEventArgs e) { if (e.Column.ColumnType == typeof(DateTime)) { string columnName = e.Column.FieldName; if (e.Value != null) { if (Convert.ToDateTime(e.Value) <= Convert.ToDateTime("1900-1-1")) { e.DisplayText = ""; } else { e.DisplayText = Convert.ToDateTime(e.Value).ToString("yyyy-MM-dd HH:mm");//yyyy-MM-dd } } } Dictionary dic = blOperation.GetGroupName(); if (e.Column.FieldName == "GroupId") { e.DisplayText = dic.FirstOrDefault(p => p.Key == e.Value.ToString().Trim()).Value; } } private void btn_Query_Click(object sender, EventArgs e) { if (string.IsNullOrEmpty(txt_Account.Text) && lookUp_TableName.Text == "全部表" && lookUp_Type.Text == "全部" && string.IsNullOrEmpty(date_Start.Text) && string.IsNullOrEmpty(date_End.Text)) { BindData(" 1=1"); condition = " 1=1"; return; } string sql = " 1=1"; if (!string.IsNullOrEmpty(txt_Account.Text)) { sql += string.Format(" AND operation_account = '{0}'",txt_Account.Text); } if (lookUp_TableName.Text != "全部表") { sql += string.Format(" AND operation_tableName = '{0}'", lookUp_TableName.Text); } if (lookUp_Type.Text != "全部") { sql += string.Format(" AND operation_type = '{0}'", lookUp_Type.Text); } if (!string.IsNullOrEmpty(date_Start.Text)) { sql += string.Format(" AND operation_time >= '{0}'", date_Start.DateTime.ToString("yyyy-MM-dd")); } if (!string.IsNullOrEmpty(date_End.Text)) { sql += string.Format(" AND operation_time < '{0}'", date_End.DateTime.AddDays(1).ToString("yyyy-MM-dd")); } BindData(sql); condition = sql; } private void menu_Expand_Click(object sender, EventArgs e) { this.tree_Operation.ExpandAll(); } private void menu_Collapse_Click(object sender, EventArgs e) { this.tree_Operation.CollapseAll(); } private void menu_Refresh_Click(object sender, EventArgs e) { InitTree(); } } }