首页 > 代码库 > 如何实现系统记录变更的记录功能

如何实现系统记录变更的记录功能

在程序操作中,我们需要把记录变更操作记录下来,通用权限管理系统正好有这个表,下面参照这个表创建自己的修改记录表,表结构如下

 

 

用代码生成器的截图如下

 

 

实体类

  1 //-----------------------------------------------------------------------  2 // <copyright file="BASEMODIFYRECORDEntity.cs" company="ZTO">  3 //     Copyright (c) 2014 , All rights reserved.  4 // </copyright>  5 //-----------------------------------------------------------------------  6   7 using System;  8 using System.Collections.Generic;  9 using System.Linq; 10 using System.Data; 11  12 namespace DotNet.Business 13 { 14     using DotNet.Utilities; 15  16     /// <summary> 17     /// BASEMODIFYRECORDEntity 18     ///  19     ///  20     /// 修改纪录 21     ///  22     /// 2014-11-04 版本:1.0 SongBiao 创建文件。 23     ///  24     /// <author> 25     ///     <name>SongBiao</name> 26     ///     <date>2014-11-04</date> 27     /// </author> 28     /// </summary> 29     public partial class BASEMODIFYRECORDEntity : BaseEntity 30     { 31         private string nEWKEY = string.Empty; 32         /// <summary> 33         /// NEWKEY 34         /// </summary> 35         public string NEWKEY 36         { 37             get 38             { 39                 return nEWKEY; 40             } 41             set 42             { 43                 nEWKEY = value; 44             } 45         } 46  47         private string nEWVALUE = http://www.mamicode.com/string.Empty; 48         /// <summary> 49         /// NEWVALUE 50         /// </summary> 51         public string NEWVALUE 52         { 53             get 54             { 55                 return nEWVALUE; 56             } 57             set 58             { 59                 nEWVALUE =http://www.mamicode.com/ value; 60             } 61         } 62  63         private string cOLUMNDESCRIPTION = string.Empty; 64         /// <summary> 65         /// COLUMNDESCRIPTION 66         /// </summary> 67         public string COLUMNDESCRIPTION 68         { 69             get 70             { 71                 return cOLUMNDESCRIPTION; 72             } 73             set 74             { 75                 cOLUMNDESCRIPTION = value; 76             } 77         } 78  79         private string createBy = string.Empty; 80         /// <summary> 81         /// CREATEBY 82         /// </summary> 83         public string CreateBy 84         { 85             get 86             { 87                 return createBy; 88             } 89             set 90             { 91                 createBy = value; 92             } 93         } 94  95         private string oLDKEY = string.Empty; 96         /// <summary> 97         /// OLDKEY 98         /// </summary> 99         public string OLDKEY100         {101             get102             {103                 return oLDKEY;104             }105             set106             {107                 oLDKEY = value;108             }109         }110 111         private DateTime? createOn = null;112         /// <summary>113         /// CREATEON114         /// </summary>115         public DateTime? CreateOn116         {117             get118             {119                 return createOn;120             }121             set122             {123                 createOn = value;124             }125         }126 127         private string oLDVALUE = http://www.mamicode.com/string.Empty;128         /// <summary>129         /// OLDVALUE130         /// </summary>131         public string OLDVALUE132         {133             get134             {135                 return oLDVALUE;136             }137             set138             {139                 oLDVALUE =http://www.mamicode.com/ value;140             }141         }142 143         private string createUserId = string.Empty;144         /// <summary>145         /// CREATEUSERID146         /// </summary>147         public string CreateUserId148         {149             get150             {151                 return createUserId;152             }153             set154             {155                 createUserId = value;156             }157         }158 159         private string rECORDKEY = string.Empty;160         /// <summary>161         /// RECORDKEY162         /// </summary>163         public string RECORDKEY164         {165             get166             {167                 return rECORDKEY;168             }169             set170             {171                 rECORDKEY = value;172             }173         }174 175         private string cOLUMNCODE = string.Empty;176         /// <summary>177         /// COLUMNCODE178         /// </summary>179         public string COLUMNCODE180         {181             get182             {183                 return cOLUMNCODE;184             }185             set186             {187                 cOLUMNCODE = value;188             }189         }190 191         private Decimal? id = null;192         /// <summary>193         /// ID194         /// </summary>195         public Decimal? Id196         {197             get198             {199                 return id;200             }201             set202             {203                 id = value;204             }205         }206 207         private string tABLEDESCRIPTION = string.Empty;208         /// <summary>209         /// TABLEDESCRIPTION210         /// </summary>211         public string TABLEDESCRIPTION212         {213             get214             {215                 return tABLEDESCRIPTION;216             }217             set218             {219                 tABLEDESCRIPTION = value;220             }221         }222 223         private string tABLECODE = string.Empty;224         /// <summary>225         /// TABLECODE226         /// </summary>227         public string TABLECODE228         {229             get230             {231                 return tABLECODE;232             }233             set234             {235                 tABLECODE = value;236             }237         }238 239         /// <summary>240         /// 从数据行读取241         /// </summary>242         /// <param name="dr">数据行</param>243         protected override BaseEntity GetFrom(IDataRow dr)244         {245             GetFromExpand(dr);246             NEWKEY = BaseBusinessLogic.ConvertToString(dr[BASEMODIFYRECORDEntity.FieldNEWKEY]);247             NEWVALUE =http://www.mamicode.com/ BaseBusinessLogic.ConvertToString(dr[BASEMODIFYRECORDEntity.FieldNEWVALUE]);248             COLUMNDESCRIPTION = BaseBusinessLogic.ConvertToString(dr[BASEMODIFYRECORDEntity.FieldCOLUMNDESCRIPTION]);249             CreateBy = BaseBusinessLogic.ConvertToString(dr[BASEMODIFYRECORDEntity.FieldCreateBy]);250             OLDKEY = BaseBusinessLogic.ConvertToString(dr[BASEMODIFYRECORDEntity.FieldOLDKEY]);251             CreateOn = BaseBusinessLogic.ConvertToNullableDateTime(dr[BASEMODIFYRECORDEntity.FieldCreateOn]);252             OLDVALUE =http://www.mamicode.com/ BaseBusinessLogic.ConvertToString(dr[BASEMODIFYRECORDEntity.FieldOLDVALUE]);253             CreateUserId = BaseBusinessLogic.ConvertToString(dr[BASEMODIFYRECORDEntity.FieldCreateUserId]);254             RECORDKEY = BaseBusinessLogic.ConvertToString(dr[BASEMODIFYRECORDEntity.FieldRECORDKEY]);255             COLUMNCODE = BaseBusinessLogic.ConvertToString(dr[BASEMODIFYRECORDEntity.FieldCOLUMNCODE]);256             Id = BaseBusinessLogic.ConvertToNullableDecimal(dr[BASEMODIFYRECORDEntity.FieldId]);257             TABLEDESCRIPTION = BaseBusinessLogic.ConvertToString(dr[BASEMODIFYRECORDEntity.FieldTABLEDESCRIPTION]);258             TABLECODE = BaseBusinessLogic.ConvertToString(dr[BASEMODIFYRECORDEntity.FieldTABLECODE]);259             return this;260         }261 262         ///<summary>263         /// 264         ///</summary>265         public static string TableName = "BASEMODIFYRECORD";266 267         ///<summary>268         /// NEWKEY269         ///</summary>270         public static string FieldNEWKEY = "NEWKEY";271 272         ///<summary>273         /// NEWVALUE274         ///</summary>275         public static string FieldNEWVALUE = http://www.mamicode.com/"NEWVALUE";276 277         ///<summary>278         /// COLUMNDESCRIPTION279         ///</summary>280         public static string FieldCOLUMNDESCRIPTION = "COLUMNDESCRIPTION";281 282         ///<summary>283         /// CREATEBY284         ///</summary>285         public static string FieldCreateBy = "CreateBy";286 287         ///<summary>288         /// OLDKEY289         ///</summary>290         public static string FieldOLDKEY = "OLDKEY";291 292         ///<summary>293         /// CREATEON294         ///</summary>295         public static string FieldCreateOn = "CreateOn";296 297         ///<summary>298         /// OLDVALUE299         ///</summary>300         public static string FieldOLDVALUE = http://www.mamicode.com/"OLDVALUE";301 302         ///<summary>303         /// CREATEUSERID304         ///</summary>305         public static string FieldCreateUserId = "CreateUserId";306 307         ///<summary>308         /// RECORDKEY309         ///</summary>310         public static string FieldRECORDKEY = "RECORDKEY";311 312         ///<summary>313         /// COLUMNCODE314         ///</summary>315         public static string FieldCOLUMNCODE = "COLUMNCODE";316 317         ///<summary>318         /// ID319         ///</summary>320         public static string FieldId = "Id";321 322         ///<summary>323         /// TABLEDESCRIPTION324         ///</summary>325         public static string FieldTABLEDESCRIPTION = "TABLEDESCRIPTION";326 327         ///<summary>328         /// TABLECODE329         ///</summary>330         public static string FieldTABLECODE = "TABLECODE";331     }332 }

 

管理类

  1 //-----------------------------------------------------------------------  2 // <copyright file="BASEMODIFYRECORDManager.Auto.cs" company="ZTO">  3 //     Copyright (c) 2014 , All rights reserved.  4 // </copyright>  5 //-----------------------------------------------------------------------  6   7 using System;  8 using System.Collections.Generic;  9 using System.Linq; 10 using System.Data; 11  12 namespace DotNet.Business 13 { 14     using DotNet.Business; 15     using DotNet.Utilities; 16  17     /// <summary> 18     /// BASEMODIFYRECORDManager 19     ///  20     ///  21     /// 修改纪录 22     ///  23     /// 2014-11-04 版本:1.0 SongBiao 创建文件。 24     ///  25     /// <author> 26     ///     <name>SongBiao</name> 27     ///     <date>2014-11-04</date> 28     /// </author> 29     /// </summary> 30     public partial class BASEMODIFYRECORDManager : BaseManager, IBaseManager 31     { 32         /// <summary> 33         /// 构造函数 34         /// </summary> 35         public BASEMODIFYRECORDManager() 36         { 37             if (base.dbHelper == null) 38             { 39                 base.dbHelper = DbHelperFactory.GetHelper(BaseSystemInfo.UserCenterDbType, BaseSystemInfo.UserCenterDbConnection); 40             } 41             if (string.IsNullOrEmpty(base.CurrentTableName)) 42             { 43                 base.CurrentTableName = BASEMODIFYRECORDEntity.TableName; 44             } 45             base.PrimaryKey = "Id"; 46         } 47  48         /// <summary> 49         /// 构造函数 50         /// <param name="tableName">指定表名</param> 51         /// </summary> 52         public BASEMODIFYRECORDManager(string tableName) 53         { 54             base.CurrentTableName = tableName; 55         } 56  57         /// <summary> 58         /// 构造函数 59         /// </summary> 60         /// <param name="dbHelper">数据库连接</param> 61         public BASEMODIFYRECORDManager(IDbHelper dbHelper): this() 62         { 63             DbHelper = dbHelper; 64         } 65  66         /// <summary> 67         /// 构造函数 68         /// </summary> 69         /// <param name="userInfo">用户信息</param> 70         public BASEMODIFYRECORDManager(BaseUserInfo userInfo) : this() 71         { 72             UserInfo = userInfo; 73         } 74  75         /// <summary> 76         /// 构造函数 77         /// </summary> 78         /// <param name="userInfo">用户信息</param> 79         /// <param name="tableName">指定表名</param> 80         public BASEMODIFYRECORDManager(BaseUserInfo userInfo, string tableName) : this(userInfo) 81         { 82             base.CurrentTableName = tableName; 83         } 84  85         /// <summary> 86         /// 构造函数 87         /// </summary> 88         /// <param name="dbHelper">数据库连接</param> 89         /// <param name="userInfo">用户信息</param> 90         public BASEMODIFYRECORDManager(IDbHelper dbHelper, BaseUserInfo userInfo) : this(dbHelper) 91         { 92             UserInfo = userInfo; 93         } 94  95         /// <summary> 96         /// 构造函数 97         /// </summary> 98         /// <param name="dbHelper">数据库连接</param> 99         /// <param name="userInfo">用户信息</param>100         /// <param name="tableName">指定表名</param>101         public BASEMODIFYRECORDManager(IDbHelper dbHelper, BaseUserInfo userInfo, string tableName) : this(dbHelper, userInfo)102         {103             base.CurrentTableName = tableName;104         }105 106         /// <summary>107         /// 添加, 这里可以人工干预,提高程序的性能108         /// </summary>109         /// <param name="entity">实体</param>110         /// <param name="identity">自增量方式,表主键是否采用自增的策略</param>111         /// <param name="returnId">返回主键,不返回程序允许速度会快,主要是为了主细表批量插入数据优化用的</param>112         /// <returns>主键</returns>113         public string Add(BASEMODIFYRECORDEntity entity, bool identity = true, bool returnId = true)114         {115             this.Identity = identity;116             this.ReturnId = returnId;117             entity.Id = int.Parse(this.AddObject(entity));118             return entity.Id.ToString();119         }120 121         /// <summary>122         /// 更新123         /// </summary>124         /// <param name="entity">实体</param>125         public int Update(BASEMODIFYRECORDEntity entity)126         {127             return this.UpdateObject(entity);128         }129 130         /// <summary>131         /// 获取实体132         /// </summary>133         /// <param name="id">主键</param>134         public BASEMODIFYRECORDEntity GetObject(string id)135         {136             return GetObject(int.Parse(id));137         }138 139         public BASEMODIFYRECORDEntity GetObject(int id)140         {141             return BaseEntity.Create<BASEMODIFYRECORDEntity>(this.GetDataTable(new KeyValuePair<string, object>(this.PrimaryKey, id)));142         }143 144         /// <summary>145         /// 添加实体146         /// </summary>147         /// <param name="entity">实体</param>148         public string AddObject(BASEMODIFYRECORDEntity entity)149         {150             string key = string.Empty;151             SQLBuilder sqlBuilder = new SQLBuilder(DbHelper, this.Identity, this.ReturnId);152             sqlBuilder.BeginInsert(this.CurrentTableName, this.PrimaryKey);153             if (!this.Identity) 154             {155                 // 这里已经是指定了主键了,所以不需要返回主键了156                 sqlBuilder.ReturnId = false;157                 sqlBuilder.SetValue(this.PrimaryKey, entity.Id);158             }159             else160             {161                 if (!this.ReturnId && (DbHelper.CurrentDbType == CurrentDbType.Oracle || DbHelper.CurrentDbType == CurrentDbType.DB2))162                 {163                     if (DbHelper.CurrentDbType == CurrentDbType.Oracle)164                     {165                         sqlBuilder.SetFormula(this.PrimaryKey, "SEQ_" + this.CurrentTableName.ToUpper() + ".NEXTVAL ");166                     }167                     if (DbHelper.CurrentDbType == CurrentDbType.DB2)168                     {169                         sqlBuilder.SetFormula(this.PrimaryKey, "NEXT VALUE FOR SEQ_" + this.CurrentTableName.ToUpper());170                     }171                 }172                 else173                 {174                     if (this.Identity && (DbHelper.CurrentDbType == CurrentDbType.Oracle || DbHelper.CurrentDbType == CurrentDbType.DB2))175                     {176                         BaseSequenceManager sequenceManager = new BaseSequenceManager(DbHelper);177                         entity.Id = int.Parse(sequenceManager.Increment(this.CurrentTableName));178                         sqlBuilder.SetValue(this.PrimaryKey, entity.Id);179                     }180                 }181             }182             this.SetObject(sqlBuilder, entity);183             if (UserInfo != null) 184             { 185                 sqlBuilder.SetValue(BASEMODIFYRECORDEntity.FieldCreateUserId, UserInfo.Id);186                 sqlBuilder.SetValue(BASEMODIFYRECORDEntity.FieldCreateBy, UserInfo.Realname);187             } 188             sqlBuilder.SetDBNow(BASEMODIFYRECORDEntity.FieldCreateOn);189             if (this.Identity && (DbHelper.CurrentDbType == CurrentDbType.SqlServer || DbHelper.CurrentDbType == CurrentDbType.Access))190             {191                 key = sqlBuilder.EndInsert().ToString();192             }193             else194             {195                 sqlBuilder.EndInsert();196             }197             if (this.Identity && (DbHelper.CurrentDbType == CurrentDbType.Oracle || DbHelper.CurrentDbType == CurrentDbType.DB2))198             {199                 return entity.Id.ToString();200             }201             return key;202         }203 204         /// <summary>205         /// 更新实体206         /// </summary>207         /// <param name="entity">实体</param>208         public int UpdateObject(BASEMODIFYRECORDEntity entity)209         {210             SQLBuilder sqlBuilder = new SQLBuilder(DbHelper);211             sqlBuilder.BeginUpdate(this.CurrentTableName);212             this.SetObject(sqlBuilder, entity);213             sqlBuilder.SetWhere(this.PrimaryKey, entity.Id);214             return sqlBuilder.EndUpdate();215         }216 217         // 这个是声明扩展方法218         partial void SetObjectExpand(SQLBuilder sqlBuilder, BASEMODIFYRECORDEntity entity);219         220         /// <summary>221         /// 设置实体222         /// </summary>223         /// <param name="entity">实体</param>224         private void SetObject(SQLBuilder sqlBuilder, BASEMODIFYRECORDEntity entity)225         {226             SetObjectExpand(sqlBuilder, entity);227             sqlBuilder.SetValue(BASEMODIFYRECORDEntity.FieldNEWKEY, entity.NEWKEY);228             sqlBuilder.SetValue(BASEMODIFYRECORDEntity.FieldNEWVALUE, entity.NEWVALUE);229             sqlBuilder.SetValue(BASEMODIFYRECORDEntity.FieldCOLUMNDESCRIPTION, entity.COLUMNDESCRIPTION);230             sqlBuilder.SetValue(BASEMODIFYRECORDEntity.FieldOLDKEY, entity.OLDKEY);231             sqlBuilder.SetValue(BASEMODIFYRECORDEntity.FieldOLDVALUE, entity.OLDVALUE);232             sqlBuilder.SetValue(BASEMODIFYRECORDEntity.FieldRECORDKEY, entity.RECORDKEY);233             sqlBuilder.SetValue(BASEMODIFYRECORDEntity.FieldCOLUMNCODE, entity.COLUMNCODE);234             sqlBuilder.SetValue(BASEMODIFYRECORDEntity.FieldTABLEDESCRIPTION, entity.TABLEDESCRIPTION);235             sqlBuilder.SetValue(BASEMODIFYRECORDEntity.FieldTABLECODE, entity.TABLECODE);236         }237 238         /// <summary>239         /// 删除实体240         /// </summary>241         /// <param name="id">主键</param>242         /// <returns>影响行数</returns>243         public int Delete(int id)244         {245             return this.Delete(new KeyValuePair<string, object>(this.PrimaryKey, id));246         }247     }248 }

 

参照上面,在操作时调用对应方法,即可将记录变更的数据记录下来。这样数据出现问题就可以及时找到。

 

用通用权限管理的代码系统生成器生成代码确实非常棒,与其权限功能配合很好用,我们只需考虑业务功能即可,大大节省了开发时间。

 

如何实现系统记录变更的记录功能