refactor: 重构项目基础架构与实体体系
- 替换原有自定义生命周期接口为通用基础服务体系 - 将所有实体基类替换为带雪花ID的SqlSugarBaseEntity - 迁移DTO到Models项目并统一管理 - 移除冗余的Repository层实现,改用通用基础服务 - 添加Yitter.IdGenerator雪花ID生成支持 - 重构SqlSugar数据库上下文与依赖注入配置 - 新增微信用户、用户勋章、背包等实体与配套服务 - 整理分页查询与结果封装类 - 优化WebApi控制器结构
This commit is contained in:
160
QYZH.InteractiveMagazine.Repository/Core/SqlSugarExtension.cs
Normal file
160
QYZH.InteractiveMagazine.Repository/Core/SqlSugarExtension.cs
Normal file
@ -0,0 +1,160 @@
|
||||
using QYZH.InteractiveMagazine.Models.Entity;
|
||||
using SqlSugar;
|
||||
using System.Linq.Expressions;
|
||||
using System.Reflection;
|
||||
|
||||
|
||||
namespace QYZH.InteractiveMagazine.Repository.Core
|
||||
{
|
||||
/// <summary>
|
||||
/// sql sugar 管理器
|
||||
/// </summary>
|
||||
public static class SqlSugarExtension
|
||||
{
|
||||
/// <summary>
|
||||
/// 生成类文件
|
||||
/// </summary>
|
||||
/// <param name="db"></param>
|
||||
/// <param name="directoryPath">生成路径</param>
|
||||
/// <param name="nameSpace">名称空间</param>
|
||||
public static ISqlSugarClient CreateClassFile(this ISqlSugarClient db, string directoryPath, string nameSpace = "Models")
|
||||
{
|
||||
foreach (var item in db.DbMaintenance.GetTableInfoList())
|
||||
{
|
||||
string[] entityNameArray = item.Name.Split('_', StringSplitOptions.RemoveEmptyEntries).ToArray();
|
||||
string entityName = string.Join("", entityNameArray.Select(c => c.ToCamelCase()));
|
||||
db.MappingTables.Add(entityName, item.Name);
|
||||
foreach (var col in db.DbMaintenance.GetColumnInfosByTableName(item.Name))
|
||||
{
|
||||
var columnNames = col.DbColumnName.ToLower().Split('_', StringSplitOptions.RemoveEmptyEntries).ToArray();
|
||||
string columnName = string.Join("", columnNames.Select(c => c.ToCamelCase()));
|
||||
db.MappingColumns.Add(columnName, col.DbColumnName, entityName);
|
||||
}
|
||||
}
|
||||
db.DbFirst.IsCreateAttribute().CreateClassFile(directoryPath, nameSpace);
|
||||
return db;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 添加全局过滤器
|
||||
/// IsDeleted
|
||||
/// </summary>
|
||||
/// <param name="db">数据库Client</param>
|
||||
/// <param name="assemblyName">程序集名称</param>
|
||||
/// <returns></returns>
|
||||
public static ISqlSugarClient SetQueryFilter(this ISqlSugarClient db, string assemblyName)
|
||||
{
|
||||
var assembly = Assembly.Load(assemblyName);
|
||||
|
||||
var modelBaseType = typeof(SqlSugarBaseEntity);
|
||||
var repoBaseType = typeof(SqlSugarBaseEntity);
|
||||
|
||||
var types = assembly.ExportedTypes
|
||||
.Where(t => (modelBaseType.IsAssignableFrom(t) || repoBaseType.IsAssignableFrom(t))
|
||||
&& t != modelBaseType && t != repoBaseType
|
||||
&& !t.IsGenericTypeDefinition)
|
||||
.ToList();
|
||||
|
||||
foreach (var entityType in types)
|
||||
{
|
||||
var property = entityType.GetProperty("IsDeleted", System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.DeclaredOnly);
|
||||
if (property == null)
|
||||
{
|
||||
var baseType = entityType.BaseType;
|
||||
while (baseType != null && baseType != typeof(object))
|
||||
{
|
||||
property = baseType.GetProperty("IsDeleted", System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.DeclaredOnly);
|
||||
if (property != null) break;
|
||||
baseType = baseType.BaseType;
|
||||
}
|
||||
}
|
||||
|
||||
if (property == null) continue;
|
||||
|
||||
var propertyType = property.PropertyType;
|
||||
|
||||
Expression filterExpression;
|
||||
var param = Expression.Parameter(entityType, "it");
|
||||
var propertyAccess = Expression.Property(param, property);
|
||||
|
||||
if (propertyType == typeof(bool))
|
||||
{
|
||||
filterExpression = Expression.Equal(propertyAccess, Expression.Constant(false, typeof(bool)));
|
||||
}
|
||||
else if (propertyType == typeof(bool?))
|
||||
{
|
||||
filterExpression = Expression.Equal(propertyAccess, Expression.Constant(false, typeof(bool?)));
|
||||
}
|
||||
else if (propertyType == typeof(byte))
|
||||
{
|
||||
filterExpression = Expression.Equal(propertyAccess, Expression.Constant((byte)0, typeof(byte)));
|
||||
}
|
||||
else if (propertyType == typeof(byte?))
|
||||
{
|
||||
filterExpression = Expression.Equal(propertyAccess, Expression.Constant((byte)0, typeof(byte?)));
|
||||
}
|
||||
else
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
var lambda = Expression.Lambda(filterExpression, param);
|
||||
db.QueryFilter.AddTableFilter(entityType, lambda);
|
||||
}
|
||||
|
||||
return db;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 添加创建人 创建时间 更新人 更新时间 默认值
|
||||
/// </summary>
|
||||
/// <param name="db"></param>
|
||||
public static ISqlSugarClient SetDefaultValue(this ISqlSugarClient db)
|
||||
{
|
||||
#region 创建人 创建时间 更新人 更新时间 默认值
|
||||
db.Aop.DataExecuting = (oldValue, entityInfo) =>
|
||||
{
|
||||
var entityValue = entityInfo.EntityColumnInfo.PropertyInfo.GetValue(entityInfo.EntityValue)?.ToString();
|
||||
var currnetUserName = "";
|
||||
/*** inset生效 ***/
|
||||
if (entityInfo.OperationType == DataFilterType.InsertByObject)
|
||||
{
|
||||
if (entityInfo.PropertyName == "CreatedAt" && (string.IsNullOrWhiteSpace(entityValue) || entityValue == DateTime.MinValue.ToString()))
|
||||
entityInfo.SetValue(DateTime.Now);//修改CreateTime字段
|
||||
else if (entityInfo.PropertyName == "CreatedBy" && (string.IsNullOrWhiteSpace(entityValue)))
|
||||
entityInfo.SetValue(currnetUserName);//修改创建人字段
|
||||
else if (entityInfo.PropertyName == "IsDeleted" && string.IsNullOrWhiteSpace(entityValue))
|
||||
entityInfo.SetValue("0");//修改CreateTime字段
|
||||
|
||||
}
|
||||
|
||||
/*** update生效 ***/
|
||||
if (entityInfo.OperationType == DataFilterType.UpdateByObject)
|
||||
{
|
||||
if (entityInfo.PropertyName == "UpdatedAt" && (string.IsNullOrWhiteSpace(entityValue) || entityValue == DateTime.MinValue.ToString()))
|
||||
entityInfo.SetValue(DateTime.Now);//修改UpdatedTime字段
|
||||
else if (entityInfo.PropertyName == "UpdatedBy" && (string.IsNullOrWhiteSpace(entityValue) || entityValue == "0"))
|
||||
entityInfo.SetValue(currnetUserName);//修改更新人字段
|
||||
}
|
||||
};
|
||||
return db;
|
||||
#endregion
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 把一个字符串转成驼峰规则的字符串
|
||||
/// </summary>
|
||||
/// <param name="str"></param>
|
||||
/// <returns></returns>
|
||||
public static string ToCamelCase(this string str)
|
||||
{
|
||||
if (!string.IsNullOrEmpty(str) && str.Length > 1)
|
||||
{
|
||||
return char.ToUpperInvariant(str[0]) + str.Substring(1);
|
||||
}
|
||||
return str;
|
||||
}
|
||||
}
|
||||
}
|
||||
51
QYZH.InteractiveMagazine.Repository/Core/SqlSugarManager.cs
Normal file
51
QYZH.InteractiveMagazine.Repository/Core/SqlSugarManager.cs
Normal file
@ -0,0 +1,51 @@
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using QYZH.InteractiveMagazine.Repository.Core;
|
||||
using Serilog;
|
||||
using SqlSugar;
|
||||
using SqlSugar.IOC;
|
||||
|
||||
namespace QYZH.InteractiveMagazine.Repository.Core
|
||||
{
|
||||
/// <summary>
|
||||
/// sql sugar 管理器
|
||||
/// </summary>
|
||||
public static class SqlSugarManager
|
||||
{
|
||||
/// <summary>
|
||||
/// 获基础信息库对象 (用IOC这块代码不能写到IOC里面)
|
||||
/// </summary>
|
||||
public static void InitSqlSugarDb(this WebApplicationBuilder builder, IocConfig config)
|
||||
{
|
||||
builder.Services.AddSqlSugar(config);
|
||||
//配置参数
|
||||
SugarIocServices.ConfigurationSugar(db =>
|
||||
{
|
||||
db.Aop.OnLogExecuting = (sql, pars) =>
|
||||
{
|
||||
//var param = db.GetConnectionScope(0).Utilities.SerializeObject(pars.ToDictionary(it => it.ParameterName, it => it.Value));
|
||||
Log.Information($"【sql语句】{UtilMethods.GetSqlString((DbType)config.DbType, sql, pars)}\n");
|
||||
};
|
||||
|
||||
db.Aop.OnError = (ex) =>
|
||||
{
|
||||
string sql = $"【错误SQL】{UtilMethods.GetSqlString((DbType)config.DbType, ex.Sql, (SugarParameter[])ex.Parametres)}\r\n";
|
||||
Log.Error(ex, $"{sql}\r\n{ex.Message}\r\n{ex.StackTrace}");
|
||||
};
|
||||
//SQL执行完
|
||||
db.Aop.OnLogExecuted = (sql, pars) =>
|
||||
{
|
||||
//执行完了可以输出SQL执行时间 (OnLogExecutedDelegate)
|
||||
};
|
||||
db.SetDefaultValue().SetQueryFilter(GetAssemblyNames());//.CreateClassFile("C:\\Model");
|
||||
});
|
||||
}
|
||||
|
||||
private static string GetAssemblyNames()
|
||||
{
|
||||
string friendlyName = AppDomain.CurrentDomain.FriendlyName;
|
||||
string[] source = friendlyName.Split('.');
|
||||
string assemblyNames = string.Join(".", source.Take(source.Count() - 1)) + ".Models";
|
||||
return assemblyNames;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user