|  | @@ -15,11 +15,20 @@ using Admin.Core.Common.BaseModel;
 | 
											
												
													
														|  |  using Admin.Core.Service.Admin.Api.Output;
 |  |  using Admin.Core.Service.Admin.Api.Output;
 | 
											
												
													
														|  |  using Admin.Core.Service.Admin.View.Output;
 |  |  using Admin.Core.Service.Admin.View.Output;
 | 
											
												
													
														|  |  using Admin.Core.Service.Admin.Permission.Output;
 |  |  using Admin.Core.Service.Admin.Permission.Output;
 | 
											
												
													
														|  | 
 |  | +using FreeSql.Aop;
 | 
											
												
													
														|  | 
 |  | +using Admin.Core.Common.Attributes;
 | 
											
												
													
														|  | 
 |  | +using Admin.Core.Common.Auth;
 | 
											
												
													
														|  | 
 |  | +using Yitter.IdGenerator;
 | 
											
												
													
														|  |  
 |  |  
 | 
											
												
													
														|  |  namespace Admin.Core.Db
 |  |  namespace Admin.Core.Db
 | 
											
												
													
														|  |  {
 |  |  {
 | 
											
												
													
														|  |      public class DbHelper
 |  |      public class DbHelper
 | 
											
												
													
														|  |      {
 |  |      {
 | 
											
												
													
														|  | 
 |  | +        /// <summary>
 | 
											
												
													
														|  | 
 |  | +        /// 偏移时间
 | 
											
												
													
														|  | 
 |  | +        /// </summary>
 | 
											
												
													
														|  | 
 |  | +        public static TimeSpan TimeOffset;
 | 
											
												
													
														|  | 
 |  | +
 | 
											
												
													
														|  |          /// <summary>
 |  |          /// <summary>
 | 
											
												
													
														|  |          /// 创建数据库
 |  |          /// 创建数据库
 | 
											
												
													
														|  |          /// </summary>
 |  |          /// </summary>
 | 
											
										
											
												
													
														|  | @@ -108,6 +117,72 @@ namespace Admin.Core.Db
 | 
											
												
													
														|  |              }
 |  |              }
 | 
											
												
													
														|  |          }
 |  |          }
 | 
											
												
													
														|  |  
 |  |  
 | 
											
												
													
														|  | 
 |  | +        /// <summary>
 | 
											
												
													
														|  | 
 |  | +        /// 审计数据
 | 
											
												
													
														|  | 
 |  | +        /// </summary>
 | 
											
												
													
														|  | 
 |  | +        /// <param name="e"></param>
 | 
											
												
													
														|  | 
 |  | +        /// <param name="timeOffset"></param>
 | 
											
												
													
														|  | 
 |  | +        /// <param name="user"></param>
 | 
											
												
													
														|  | 
 |  | +        public static void AuditValue(AuditValueEventArgs e, TimeSpan timeOffset, IUser user)
 | 
											
												
													
														|  | 
 |  | +        {
 | 
											
												
													
														|  | 
 |  | +            if (e.Property.GetCustomAttribute<ServerTimeAttribute>(false) != null
 | 
											
												
													
														|  | 
 |  | +                   && (e.Column.CsType == typeof(DateTime) || e.Column.CsType == typeof(DateTime?))
 | 
											
												
													
														|  | 
 |  | +                   && (e.Value == null || (DateTime)e.Value == default || (DateTime?)e.Value == default))
 | 
											
												
													
														|  | 
 |  | +            {
 | 
											
												
													
														|  | 
 |  | +                e.Value = DateTime.Now.Subtract(timeOffset);
 | 
											
												
													
														|  | 
 |  | +            }
 | 
											
												
													
														|  | 
 |  | +
 | 
											
												
													
														|  | 
 |  | +            if (e.Column.CsType == typeof(long)
 | 
											
												
													
														|  | 
 |  | +            && e.Property.GetCustomAttribute<SnowflakeAttribute>(false) != null
 | 
											
												
													
														|  | 
 |  | +            && (e.Value == null || (long)e.Value == default || (long?)e.Value == default))
 | 
											
												
													
														|  | 
 |  | +            {
 | 
											
												
													
														|  | 
 |  | +                e.Value = YitIdHelper.NextId();
 | 
											
												
													
														|  | 
 |  | +            }
 | 
											
												
													
														|  | 
 |  | +
 | 
											
												
													
														|  | 
 |  | +            if (user == null || user.Id <= 0)
 | 
											
												
													
														|  | 
 |  | +            {
 | 
											
												
													
														|  | 
 |  | +                return;
 | 
											
												
													
														|  | 
 |  | +            }
 | 
											
												
													
														|  | 
 |  | +
 | 
											
												
													
														|  | 
 |  | +            if (e.AuditValueType == FreeSql.Aop.AuditValueType.Insert)
 | 
											
												
													
														|  | 
 |  | +            {
 | 
											
												
													
														|  | 
 |  | +                switch (e.Property.Name)
 | 
											
												
													
														|  | 
 |  | +                {
 | 
											
												
													
														|  | 
 |  | +                    case "CreatedUserId":
 | 
											
												
													
														|  | 
 |  | +                        if (e.Value == null || (long)e.Value == default || (long?)e.Value == default)
 | 
											
												
													
														|  | 
 |  | +                        {
 | 
											
												
													
														|  | 
 |  | +                            e.Value = user.Id;
 | 
											
												
													
														|  | 
 |  | +                        }
 | 
											
												
													
														|  | 
 |  | +                        break;
 | 
											
												
													
														|  | 
 |  | +                    case "CreatedUserName":
 | 
											
												
													
														|  | 
 |  | +                        if (e.Value == null || ((string)e.Value).IsNull())
 | 
											
												
													
														|  | 
 |  | +                        {
 | 
											
												
													
														|  | 
 |  | +                            e.Value = user.Name;
 | 
											
												
													
														|  | 
 |  | +                        }
 | 
											
												
													
														|  | 
 |  | +                        break;
 | 
											
												
													
														|  | 
 |  | +                    case "TenantId":
 | 
											
												
													
														|  | 
 |  | +                        if (e.Value == null || (long)e.Value == default || (long?)e.Value == default)
 | 
											
												
													
														|  | 
 |  | +                        {
 | 
											
												
													
														|  | 
 |  | +                            e.Value = user.TenantId;
 | 
											
												
													
														|  | 
 |  | +                        }
 | 
											
												
													
														|  | 
 |  | +                        break;
 | 
											
												
													
														|  | 
 |  | +                }
 | 
											
												
													
														|  | 
 |  | +            }
 | 
											
												
													
														|  | 
 |  | +            else if (e.AuditValueType == FreeSql.Aop.AuditValueType.Update)
 | 
											
												
													
														|  | 
 |  | +            {
 | 
											
												
													
														|  | 
 |  | +                switch (e.Property.Name)
 | 
											
												
													
														|  | 
 |  | +                {
 | 
											
												
													
														|  | 
 |  | +                    case "ModifiedUserId":
 | 
											
												
													
														|  | 
 |  | +                        e.Value = user.Id;
 | 
											
												
													
														|  | 
 |  | +                        break;
 | 
											
												
													
														|  | 
 |  | +                    case "ModifiedUserName":
 | 
											
												
													
														|  | 
 |  | +                        e.Value = user.Name;
 | 
											
												
													
														|  | 
 |  | +                        break;
 | 
											
												
													
														|  | 
 |  | +                }
 | 
											
												
													
														|  | 
 |  | +
 | 
											
												
													
														|  | 
 |  | +            }
 | 
											
												
													
														|  | 
 |  | +        }
 | 
											
												
													
														|  | 
 |  | +
 | 
											
												
													
														|  |          /// <summary>
 |  |          /// <summary>
 | 
											
												
													
														|  |          /// 同步结构
 |  |          /// 同步结构
 | 
											
												
													
														|  |          /// </summary>
 |  |          /// </summary>
 | 
											
										
											
												
													
														|  | @@ -168,6 +243,7 @@ namespace Admin.Core.Db
 | 
											
												
													
														|  |          /// <typeparam name="T"></typeparam>
 |  |          /// <typeparam name="T"></typeparam>
 | 
											
												
													
														|  |          /// <param name="db"></param>
 |  |          /// <param name="db"></param>
 | 
											
												
													
														|  |          /// <param name="unitOfWork"></param>
 |  |          /// <param name="unitOfWork"></param>
 | 
											
												
													
														|  | 
 |  | +        /// <param name="tran"></param>
 | 
											
												
													
														|  |          /// <param name="data"></param>
 |  |          /// <param name="data"></param>
 | 
											
												
													
														|  |          /// <param name="dbConfig"></param>
 |  |          /// <param name="dbConfig"></param>
 | 
											
												
													
														|  |          /// <returns></returns>
 |  |          /// <returns></returns>
 | 
											
										
											
												
													
														|  | @@ -233,6 +309,73 @@ namespace Admin.Core.Db
 | 
											
												
													
														|  |              }
 |  |              }
 | 
											
												
													
														|  |          }
 |  |          }
 | 
											
												
													
														|  |  
 |  |  
 | 
											
												
													
														|  | 
 |  | +        /// <summary>
 | 
											
												
													
														|  | 
 |  | +        /// 同步数据审计方法
 | 
											
												
													
														|  | 
 |  | +        /// </summary>
 | 
											
												
													
														|  | 
 |  | +        /// <param name="s"></param>
 | 
											
												
													
														|  | 
 |  | +        /// <param name="e"></param>
 | 
											
												
													
														|  | 
 |  | +        private static void SyncDataAuditValue(object s, AuditValueEventArgs e)
 | 
											
												
													
														|  | 
 |  | +        {
 | 
											
												
													
														|  | 
 |  | +            var user = new { Id = 161223411986501, Name = "平台管理员", TenantId = 161223412138053 };
 | 
											
												
													
														|  | 
 |  | +
 | 
											
												
													
														|  | 
 |  | +            if (e.Property.GetCustomAttribute<ServerTimeAttribute>(false) != null
 | 
											
												
													
														|  | 
 |  | +                   && (e.Column.CsType == typeof(DateTime) || e.Column.CsType == typeof(DateTime?))
 | 
											
												
													
														|  | 
 |  | +                   && (e.Value == null || (DateTime)e.Value == default || (DateTime?)e.Value == default))
 | 
											
												
													
														|  | 
 |  | +            {
 | 
											
												
													
														|  | 
 |  | +                e.Value = DateTime.Now.Subtract(TimeOffset);
 | 
											
												
													
														|  | 
 |  | +            }
 | 
											
												
													
														|  | 
 |  | +
 | 
											
												
													
														|  | 
 |  | +            if (e.Column.CsType == typeof(long)
 | 
											
												
													
														|  | 
 |  | +            && e.Property.GetCustomAttribute<SnowflakeAttribute>(false) != null
 | 
											
												
													
														|  | 
 |  | +            && (e.Value == null || (long)e.Value == default || (long?)e.Value == default))
 | 
											
												
													
														|  | 
 |  | +            {
 | 
											
												
													
														|  | 
 |  | +                e.Value = YitIdHelper.NextId();
 | 
											
												
													
														|  | 
 |  | +            }
 | 
											
												
													
														|  | 
 |  | +
 | 
											
												
													
														|  | 
 |  | +            if (user == null || user.Id <= 0)
 | 
											
												
													
														|  | 
 |  | +            {
 | 
											
												
													
														|  | 
 |  | +                return;
 | 
											
												
													
														|  | 
 |  | +            }
 | 
											
												
													
														|  | 
 |  | +
 | 
											
												
													
														|  | 
 |  | +            if (e.AuditValueType == AuditValueType.Insert)
 | 
											
												
													
														|  | 
 |  | +            {
 | 
											
												
													
														|  | 
 |  | +                switch (e.Property.Name)
 | 
											
												
													
														|  | 
 |  | +                {
 | 
											
												
													
														|  | 
 |  | +                    case "CreatedUserId":
 | 
											
												
													
														|  | 
 |  | +                        if (e.Value == null || (long)e.Value == default || (long?)e.Value == default)
 | 
											
												
													
														|  | 
 |  | +                        {
 | 
											
												
													
														|  | 
 |  | +                            e.Value = user.Id;
 | 
											
												
													
														|  | 
 |  | +                        }
 | 
											
												
													
														|  | 
 |  | +                        break;
 | 
											
												
													
														|  | 
 |  | +                    case "CreatedUserName":
 | 
											
												
													
														|  | 
 |  | +                        if (e.Value == null || ((string)e.Value).IsNull())
 | 
											
												
													
														|  | 
 |  | +                        {
 | 
											
												
													
														|  | 
 |  | +                            e.Value = user.Name;
 | 
											
												
													
														|  | 
 |  | +                        }
 | 
											
												
													
														|  | 
 |  | +                        break;
 | 
											
												
													
														|  | 
 |  | +                    case "TenantId":
 | 
											
												
													
														|  | 
 |  | +                        if (e.Value == null || (long)e.Value == default || (long?)e.Value == default)
 | 
											
												
													
														|  | 
 |  | +                        {
 | 
											
												
													
														|  | 
 |  | +                            e.Value = user.TenantId;
 | 
											
												
													
														|  | 
 |  | +                        }
 | 
											
												
													
														|  | 
 |  | +                        break;
 | 
											
												
													
														|  | 
 |  | +                }
 | 
											
												
													
														|  | 
 |  | +            }
 | 
											
												
													
														|  | 
 |  | +            else if (e.AuditValueType == AuditValueType.Update)
 | 
											
												
													
														|  | 
 |  | +            {
 | 
											
												
													
														|  | 
 |  | +                switch (e.Property.Name)
 | 
											
												
													
														|  | 
 |  | +                {
 | 
											
												
													
														|  | 
 |  | +                    case "ModifiedUserId":
 | 
											
												
													
														|  | 
 |  | +                        e.Value = user.Id;
 | 
											
												
													
														|  | 
 |  | +                        break;
 | 
											
												
													
														|  | 
 |  | +                    case "ModifiedUserName":
 | 
											
												
													
														|  | 
 |  | +                        e.Value = user.Name;
 | 
											
												
													
														|  | 
 |  | +                        break;
 | 
											
												
													
														|  | 
 |  | +                }
 | 
											
												
													
														|  | 
 |  | +
 | 
											
												
													
														|  | 
 |  | +            }
 | 
											
												
													
														|  | 
 |  | +        }
 | 
											
												
													
														|  | 
 |  | +
 | 
											
												
													
														|  |          /// <summary>
 |  |          /// <summary>
 | 
											
												
													
														|  |          /// 同步数据
 |  |          /// 同步数据
 | 
											
												
													
														|  |          /// </summary>
 |  |          /// </summary>
 | 
											
										
											
												
													
														|  | @@ -248,6 +391,8 @@ namespace Admin.Core.Db
 | 
											
												
													
														|  |  
 |  |  
 | 
											
												
													
														|  |                  Console.WriteLine("\r\n sync data started");
 |  |                  Console.WriteLine("\r\n sync data started");
 | 
											
												
													
														|  |  
 |  |  
 | 
											
												
													
														|  | 
 |  | +                db.Aop.AuditValue += SyncDataAuditValue;
 | 
											
												
													
														|  | 
 |  | +
 | 
											
												
													
														|  |                  var filePath = Path.Combine(AppContext.BaseDirectory, "Db/Data/data.json").ToPath();
 |  |                  var filePath = Path.Combine(AppContext.BaseDirectory, "Db/Data/data.json").ToPath();
 | 
											
												
													
														|  |                  var jsonData = FileHelper.ReadFile(filePath);
 |  |                  var jsonData = FileHelper.ReadFile(filePath);
 | 
											
												
													
														|  |                  var data = JsonConvert.DeserializeObject<Data>(jsonData);
 |  |                  var data = JsonConvert.DeserializeObject<Data>(jsonData);
 | 
											
										
											
												
													
														|  | @@ -275,6 +420,8 @@ namespace Admin.Core.Db
 | 
											
												
													
														|  |                      uow.Commit();
 |  |                      uow.Commit();
 | 
											
												
													
														|  |                  }
 |  |                  }
 | 
											
												
													
														|  |  
 |  |  
 | 
											
												
													
														|  | 
 |  | +                db.Aop.AuditValue -= SyncDataAuditValue;
 | 
											
												
													
														|  | 
 |  | +
 | 
											
												
													
														|  |                  Console.WriteLine(" sync data succeed\r\n");
 |  |                  Console.WriteLine(" sync data succeed\r\n");
 | 
											
												
													
														|  |              }
 |  |              }
 | 
											
												
													
														|  |              catch (Exception ex)
 |  |              catch (Exception ex)
 |