using System; using System.Collections.Generic; using System.Linq; using System.Data; using System.Text; using System.Reflection; using QMFrameWork.Data; using QMFrameWork.Data.Attributes; using QMFrameWork.Common.Util; using QMFrameWork.Common.Serialization; using QMAPP.Entity.Sys; using QMAPP.DAL.Sys; namespace QMAPP.BLL.Sys { /// /// 数据变更痕迹管理 /// public class DataChangeManageBLL:BaseBLL { #region 获取数据变更信息 public DataMark GetInfo(DataMark info) { DataTable columnDt = new DataTable(); object oldInfo=null; object newInfo=null; Type entityType; try { //获取数据 info=new DataChangeManageDAL().GetInfo(info,ref columnDt); //获取实体类型 entityType = this.GetEntityTypeByTable(info.DATAKIND); info.DATAKIND = info.DATAKINDDES; //json格式数据转换为实体 if (string.IsNullOrEmpty(info.ORIGINALDATA) == false) { oldInfo = JsonConvertHelper.GetDeserialize(entityType, info.ORIGINALDATA); } if (string.IsNullOrEmpty(info.CHANGEDDATA) == false) { newInfo = JsonConvertHelper.GetDeserialize(entityType, info.CHANGEDDATA); } //生成字段明细数据 PropertyInfo[] pArray = entityType.GetProperties(); info.Details = new List(); //获取字段信息 foreach (var item in pArray) { object[] attrs = item.GetCustomAttributes(typeof(DBColumnAttribute), true); if (attrs.Count() == 0) { continue; } DBColumnAttribute ca = (DBColumnAttribute)attrs[0];//字段属性 DataMarkDetail detail = new DataMarkDetail(); //原值 object oldValue=null; if(oldInfo!=null) oldValue=BindHelper.GetPropertyValue(oldInfo, item.Name); detail.OldValue = oldValue==null?"":oldValue.ToString(); //变更后值 object newValue = null; if (newInfo != null) newValue = BindHelper.GetPropertyValue(newInfo, item.Name); detail.NewValue = newValue == null ? "" : newValue.ToString(); DataRow[] rows=columnDt.Select("COLUMNNAME='"+ca.ColumnName+"'"); if(rows.Length>0) detail.ColumnDes = rows[0]["COLUMNDES"].ToString(); if (string.IsNullOrEmpty(detail.ColumnDes) == true) { detail.ColumnDes = ca.ColumnName; } info.Details.Add(detail); } return info; } catch (Exception ex) { throw ex; } } #endregion #region 获取数据变更列表 /// /// 获取数据变更列表 /// /// 查询条件 /// 分页设置 /// 数据页 public DataPage GetList(DataMark condition, DataPage page) { try { //if (string.IsNullOrEmpty(condition.DATAKIND)==false) // condition.DATAKIND = this.GetTableName(condition.DATAKIND); return new DataChangeManageDAL().GetList(condition, page); } catch (Exception ex) { throw ex; } } #endregion #region 获取实体对应数据表名称 /// /// 获取实体对应数据表名称 /// /// 实体名 /// 数据表名称 private string GetTableName(string entityName) { string tableName = ""; try { Type entityType = this.GetEntityType(entityName); //获取表信息 object[] attrsClassAtt = entityType.GetCustomAttributes(typeof(DBTableAttribute), true); if (attrsClassAtt == null) { throw new Exception("当前实体没有添加属性DBTableAttribute!"); } DBTableAttribute tableAtt = (DBTableAttribute)attrsClassAtt[0]; tableName = tableAtt.TableName; return tableName; } catch (Exception ex) { throw ex; } } #endregion #region 获取实体类型 /// /// 获取实体类型 /// /// 实体名 /// 实体类型 private Type GetEntityType(string entityName) { List assemblyFiles = new List(); string path = ""; try { //主数据实体类库 assemblyFiles.Add("QMAPP.MD.Entity.dll"); //系统管理实体类库 assemblyFiles.Add("QMAPP.Entity.dll"); path = System.AppDomain.CurrentDomain.BaseDirectory; foreach (string assemblyFile in assemblyFiles) { Assembly asse = Assembly.LoadFile(path+"\\"+assemblyFile); Type[] types = asse.GetTypes(); foreach (Type type in types) { if (type.Name.EndsWith(entityName)) { return type; } } } return null; } catch (Exception ex) { throw new Exception("获取实体类型失败"); } } /// /// 获取实体类型 /// /// 表名 /// 实体类型 private Type GetEntityTypeByTable(string tableName) { Type t=null; try { //Assembly asse = Assembly.GetAssembly(typeof(QMAPP.Entity.BaseEntity)); Assembly asse = Assembly.LoadFile(@"E:\大众生产看板\项目文件\trunk\plm\ServicesCenter\WCF\QMFrameWork.WebServiceHost\bin\QMAPP.MD.Entity.dll"); Type[] types = asse.GetExportedTypes(); foreach (Type entityType in types) { //获取表信息 object[] attrsClassAtt = entityType.GetCustomAttributes(typeof(DBTableAttribute), true); if (attrsClassAtt != null && attrsClassAtt.Length>0) { DBTableAttribute tableAtt = (DBTableAttribute)attrsClassAtt[0]; if (tableAtt.TableName == tableName) { t = entityType; break; } } } if (t == null) { throw new Exception("通过表名获取实体类型失败"); } return t; } catch (Exception ex) { throw ex; } } #endregion } }