using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data; using QMFrameWork.Data; using QMFrameWork.Data.Attributes; using QMAPP.Entity.Sys; using System.Reflection; namespace QMAPP.DAL.Sys { /// /// 数据变更痕迹管理 /// 创建者:李炳海 /// 创建日期:2013.3.10 /// public class DataChangeManageDAL { /// /// 数据会话 /// public IDataSession Session { get; set; } #region 获取数据变更列表 /// /// 获取数据变更列表 /// /// 查询条件 /// 分页设置 /// 数据页 public DataPage GetList(DataMark condition, DataPage page) { string sql = null; List parameters = new List(); try { //获取查询语句 //sql = this.GetQuerySqlByOracle(condition, ref parameters); sql = this.GetQuerySqlBySqlServer(condition, ref parameters); //分页关键字段及排序 page.KeyName = "USERID"; if (string.IsNullOrEmpty(page.SortExpression)) { page.SortExpression = "OPERATETIME DESC"; } using (IDataSession session = AppDataFactory.CreateMainSession()) { page = session.GetDataPage(sql, parameters.ToArray(), page); } return page; } catch (Exception ex) { throw; } } #endregion #region 获取查询条件 /// /// 获取查询语句 /// /// 查询条件 /// 参数 /// 查询语句 private string GetQuerySqlByOracle(DataMark condition, ref List parameters) { StringBuilder sqlBuilder = new StringBuilder(); StringBuilder whereBuilder = new StringBuilder(); try { sqlBuilder.Append("SELECT T1.MARKID,T1.OPERATETIME,T2.USERNAME AS OPERATEUSER,T1.OPERATETYPE,T3.COMMENTS AS DATAKIND "); sqlBuilder.Append("FROM T_BD_DATAMARK T1 "); sqlBuilder.Append("LEFT JOIN T_QM_USER T2 ON T1.OPERATEUSER=T2.USERID "); sqlBuilder.Append("LEFT JOIN ALL_TAB_COMMENTS T3 ON T3.OWNER='KANBAN' AND T1.DATAKIND=T3.TABLE_NAME "); if (string.IsNullOrEmpty(condition.STARTOPERATETIME)==false) { whereBuilder.Append(" AND T1.OPERATETIME >= :DATE1"); parameters.Add(new DataParameter("DATE1", DateTime.Parse(condition.STARTOPERATETIME))); } if (string.IsNullOrEmpty(condition.ENDOPERATETIME)==false) { whereBuilder.Append(" AND T1.OPERATETIME <= :DATE2"); parameters.Add(new DataParameter("DATE2", DateTime.Parse(condition.ENDOPERATETIME).AddDays(1).AddSeconds(-1))); } if (string.IsNullOrEmpty(condition.OPERATEUSER) == false) { whereBuilder.Append(" AND (T2.USERNAME LIKE :OPERATEUSER OR T2.LOGINUSERID LIKE :OPERATEUSER)"); parameters.Add(new DataParameter("OPERATEUSER", "%" + condition.OPERATEUSER + "%")); } if (string.IsNullOrEmpty(condition.DATAKIND) == false) { whereBuilder.Append(" AND T1.DATAKIND=:DATAKIND"); parameters.Add(new DataParameter("DATAKIND", condition.DATAKIND)); } if (whereBuilder.Length > 0) { sqlBuilder.Append(" WHERE " + whereBuilder.ToString().Substring(4)); } return sqlBuilder.ToString(); } catch (Exception ex) { throw; } } /// /// 获取查询语句 /// /// 查询条件 /// 参数 /// 查询语句 private string GetQuerySqlBySqlServer(DataMark condition, ref List parameters) { StringBuilder sqlBuilder = new StringBuilder(); StringBuilder whereBuilder = new StringBuilder(); try { sqlBuilder.Append("SELECT T1.MARKID,T1.OPERATETIME,T2.USERNAME AS OPERATEUSER,T1.OPERATETYPE "); sqlBuilder.Append(@",(SELECT VALUE FROM SYS.EXTENDED_PROPERTIES WHERE MAJOR_ID=Object_Id(T1.DATAKIND) AND MINOR_ID = 0 ) AS DATAKIND "); sqlBuilder.Append("FROM T_BD_DATAMARK T1 "); sqlBuilder.Append("LEFT JOIN T_QM_USER T2 ON T1.OPERATEUSER=T2.USERID "); if (string.IsNullOrEmpty(condition.STARTOPERATETIME) == false) { whereBuilder.Append(" AND T1.OPERATETIME >= @DATE1"); parameters.Add(new DataParameter("DATE1", DateTime.Parse(condition.STARTOPERATETIME))); } if (string.IsNullOrEmpty(condition.ENDOPERATETIME) == false) { whereBuilder.Append(" AND T1.OPERATETIME <= @DATE2"); parameters.Add(new DataParameter("DATE2", DateTime.Parse(condition.ENDOPERATETIME).AddDays(1).AddSeconds(-1))); } if (string.IsNullOrEmpty(condition.OPERATEUSER) == false) { whereBuilder.Append(" AND (T2.USERNAME LIKE @OPERATEUSER OR T2.LOGINUSERID LIKE @OPERATEUSER)"); parameters.Add(new DataParameter("OPERATEUSER", "%" + condition.OPERATEUSER + "%")); } if (string.IsNullOrEmpty(condition.DATAKIND) == false) { whereBuilder.Append(" AND T1.DATAKIND = @DATAKIND"); parameters.Add(new DataParameter("DATAKIND", condition.DATAKIND)); } if (whereBuilder.Length > 0) { sqlBuilder.Append(" WHERE " + whereBuilder.ToString().Substring(4)); } return sqlBuilder.ToString(); } catch (Exception ex) { throw; } } #endregion #region 记录数据变更新信息 /// /// 记录数据变更新信息 /// /// 数据类型 /// 操作类型 /// 操作者 /// 原数据 /// 新数据 public void RecordDataChangeMark(DataOprType oprType,string OperateUser,T oldInfo,T newInfo) where T:new() { string tableName = ""; DataMark mark = new DataMark(); try { Type type = typeof(T); //获取表名 object[] attrsClassAtt = type.GetCustomAttributes(typeof(DBTableAttribute), true); DBTableAttribute tableAtt = (DBTableAttribute)attrsClassAtt[0]; tableName = tableAtt.TableName; //创建痕迹信息 mark.MARKID = Guid.NewGuid().ToString(); mark.OPERATEUSER = OperateUser; mark.DATAKIND = tableName; mark.OPERATETIME = DateTime.Now; mark.OPERATETYPE = oprType.ToString(); mark.DATAID = ""; mark.ORIGINALDATA = QMFrameWork.Common.Serialization.JsonConvertHelper.GetSerializes(oldInfo); if (oprType == DataOprType.Update||oprType==DataOprType.Insert) { mark.CHANGEDDATA = QMFrameWork.Common.Serialization.JsonConvertHelper.GetSerializes(newInfo); } //保存痕迹信息 Session.Insert(mark); } catch (Exception ex) { throw; } } #endregion #region 获取数据变更痕迹数据 /// /// 获取数据变更痕迹数据 /// /// 变更痕迹 /// 变更痕迹 public DataMark GetInfo(DataMark info,ref DataTable columnDt) { StringBuilder sqlBuilder = new StringBuilder(); List parameters = new List(); try { //using (IDataSession session = AppDataFactory.CreateMainSession()) //{ // sqlBuilder.Append("SELECT T1.MARKID,T1.OPERATETIME,T2.USERDES AS OPERATEUSER,T1.OPERATETYPE,T1.DATAKIND,T3.COMMENTS AS DATAKINDDES,T1.OriginalData,T1.ChangedData "); // sqlBuilder.Append("FROM T_BD_DATAMARK T1 "); // sqlBuilder.Append("left JOIN T_QM_USER T2 ON t1.OPERATEUSER=T2.USERID "); // sqlBuilder.Append("left JOIN ALL_TAB_COMMENTS T3 ON T3.OWNER='PLP' AND T1.DATAKIND=T3.TABLE_NAME "); // sqlBuilder.Append("WHERE T1.MARKID=:MARKID"); // //获取变更痕迹信息 // info = session.Get(sqlBuilder.ToString(), new DataParameter("MARKID", info.MARKID)); // //获取字段描述信息 // if (columnDt != null) // { // sqlBuilder.Clear(); // sqlBuilder.Append("SELECT T1.COLUMN_NAME AS COLUMNNAME, T1.COMMENTS AS COLUMNDES "); // sqlBuilder.Append("FROM USER_COL_COMMENTS T1, ALL_TAB_COMMENTS T2 "); // sqlBuilder.Append("WHERE T1.TABLE_NAME = T2.TABLE_NAME AND T1.TABLE_NAME = :TABLENAME"); // parameters.Add(new DataParameter("TABLENAME", info.DATAKIND)); // session.FillTable(columnDt, sqlBuilder.ToString(), parameters.ToArray()); // } //} using (IDataSession session = AppDataFactory.CreateMainSession()) { sqlBuilder.Append("SELECT T1.MARKID,T1.OPERATETIME,T2.USERNAME AS OPERATEUSER,T1.OPERATETYPE,T1.DATAKIND,T1.ORIGINALDATA,T1.CHANGEDDATA "); sqlBuilder.Append("FROM T_BD_DATAMARK T1 "); sqlBuilder.Append("left JOIN T_QM_USER T2 ON t1.OPERATEUSER=T2.USERID "); sqlBuilder.Append("WHERE T1.MARKID=@MARKID"); //获取变更痕迹信息 info = session.Get(sqlBuilder.ToString(), new DataParameter("MARKID", info.MARKID)); //获取字段描述信息 if (columnDt != null) { string sql = @"SELECT sys.columns.name as COLUMNNAME,(SELECT value FROM sys.extended_properties WHERE sys.extended_properties.major_id = sys.columns.object_id AND sys.extended_properties.minor_id = sys.columns.column_id) AS COLUMNDES FROM sys.columns, sys.tables, sys.types WHERE sys.columns.object_id = sys.tables.object_id AND sys.columns.system_type_id=sys.types.system_type_id AND sys.tables.name=@TABLENAME AND sys.types.name<>'sysname' ORDER BY sys.columns.column_id"; parameters.Add(new DataParameter("TABLENAME", info.DATAKIND)); session.FillTable(columnDt, sql, parameters.ToArray()); } } return info; } catch (Exception ex) { throw; } } #endregion } }