添加项目文件。

This commit is contained in:
fuenmao
2025-09-29 13:48:36 +08:00
parent f61e922f0c
commit 5dc1780ef1
407 changed files with 63703 additions and 0 deletions
+216
View File
@@ -0,0 +1,216 @@
// Copyright (c) 2022-Now 少林寺驻北固山办事处大神父王喇嘛
//
// SimpleAdmin 基于 Apache License Version 2.0 协议发布,可用于商业项目,但必须遵守以下补充条款:
// 1.请不要删除和修改根目录下的LICENSE文件。
// 2.请不要删除和修改SimpleAdmin源码头部的版权声明。
// 3.分发源码时候,请注明软件出处 https://gitee.com/dotnetmoyu/SimpleAdmin
// 4.基于本软件的作品,只能使用 SimpleAdmin 作为后台服务,除外情况不可商用且不允许二次分发或开源。
// 5.请不得将本软件应用于危害国家安全、荣誉和利益的行为,不能以任何形式用于非法为目的的行为。
// 6.任何基于本软件而产生的一切法律纠纷和责任,均于我司无关。
using SimpleAdmin.SqlSugar.Filter;
using DbType = SqlSugar.DbType;
namespace SimpleAdmin.SqlSugar;
/// <summary>
/// 数据库上下文对象
/// </summary>
public static class DbContext
{
/// <summary>
/// 读取配置文件中的 ConnectionStrings:SqlSugar 配置节点
/// </summary>
public static readonly List<SqlSugarConfig> DB_CONFIGS = App.GetConfig<List<SqlSugarConfig>>("SqlSugarSettings:ConnectionStrings");
/// <summary>
/// SqlSugar 数据库实例
/// </summary>
public static readonly SqlSugarScope DB = new SqlSugarScope(DB_CONFIGS.Adapt<List<ConnectionConfig>>(), db =>
{
//遍历配置的数据库
DB_CONFIGS.ForEach(it =>
{
var sqlSugarScope = db.GetConnectionScope(it.ConfigId);//获取当前库
MoreSetting(sqlSugarScope, it.DbType);//更多设置
ExternalServicesSetting(sqlSugarScope, it);//实体拓展配置
AopSetting(sqlSugarScope);//aop配置
FilterSetting(sqlSugarScope);//过滤器配置
});
});
/// <summary>
/// 实体拓展配置,自定义类型多库兼容
/// </summary>
/// <param name="db"></param>
/// <param name="config"></param>
private static void ExternalServicesSetting(SqlSugarScopeProvider db, SqlSugarConfig config)
{
db.CurrentConnectionConfig.ConfigureExternalServices = new ConfigureExternalServices
{
// 处理表
EntityNameService = (type, entity) =>
{
if (config.IsUnderLine && !entity.DbTableName.Contains('_'))
entity.DbTableName = UtilMethods.ToUnderLine(entity.DbTableName);// 驼峰转下划线
},
//自定义类型多库兼容
EntityService = (c, p) =>
{
//如果是mysql并且是varchar(max) 已弃用
//if (config.DbType == SqlSugar.DbType.MySql && (p.DataType == SqlSugarConst.NVarCharMax))
//{
// p.DataType = SqlSugarConst.LongText;//转成mysql的longtext
//}
//else if (config.DbType == SqlSugar.DbType.Sqlite && (p.DataType == SqlSugarConst.NVarCharMax))
//{
// p.DataType = SqlSugarConst.Text;//转成sqlite的text
//}
//默认不写IsNullable为非必填
//if (new NullabilityInfoContext().Create(c).WriteState is NullabilityState.Nullable)
// p.IsNullable = true;
if (config.IsUnderLine && !p.IsIgnore && !p.DbColumnName.Contains('_') && p.DbColumnName != "ColumnName")
p.DbColumnName = UtilMethods.ToUnderLine(p.DbColumnName);// 驼峰转下划线
}
};
}
/// <summary>
/// Aop设置
/// </summary>
/// <param name="db"></param>
public static void AopSetting(SqlSugarScopeProvider db)
{
var config = db.CurrentConnectionConfig;
// 设置超时时间
db.Ado.CommandTimeOut = 30;
// 打印SQL语句
db.Aop.OnLogExecuting = (sql, pars) =>
{
//如果不是开发环境就打印sql
if (App.HostEnvironment.IsDevelopment())
{
if (sql.StartsWith("SELECT"))
{
Console.ForegroundColor = ConsoleColor.Green;
WriteSqlLog($"查询{config.ConfigId}库操作");
}
if (sql.StartsWith("UPDATE") || sql.StartsWith("INSERT"))
{
Console.ForegroundColor = ConsoleColor.Blue;
WriteSqlLog($"修改{config.ConfigId}库操作");
}
if (sql.StartsWith("DELETE"))
{
Console.ForegroundColor = ConsoleColor.Red;
WriteSqlLog($"删除{config.ConfigId}库操作");
}
Console.WriteLine(UtilMethods.GetSqlString(config.DbType, sql, pars));
WriteSqlLog($"{config.ConfigId}库操作结束");
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine();
}
};
//异常
db.Aop.OnError = ex =>
{
//如果不是开发环境就打印日志
if (App.WebHostEnvironment.IsDevelopment())
{
if (ex.Parametres == null) return;
Console.ForegroundColor = ConsoleColor.Red;
var pars = db.Utilities.SerializeObject(((SugarParameter[])ex.Parametres).ToDictionary(it => it.ParameterName, it => it.Value));
WriteSqlLog($"{config.ConfigId}库操作异常");
Console.WriteLine(UtilMethods.GetSqlString(config.DbType, ex.Sql, (SugarParameter[])ex.Parametres) + "\r\n");
Console.ForegroundColor = ConsoleColor.White;
}
};
//插入和更新过滤器
db.Aop.DataExecuting = (oldValue, entityInfo) =>
{
// 新增操作
if (entityInfo.OperationType == DataFilterType.InsertByObject)
{
// 主键(long类型)且没有值的---赋值雪花Id
if (entityInfo.EntityColumnInfo.IsPrimarykey && entityInfo.EntityColumnInfo.PropertyInfo.PropertyType == typeof(long))
{
var id = entityInfo.EntityColumnInfo.PropertyInfo.GetValue(entityInfo.EntityValue);
if (id == null || (long)id == 0)
entityInfo.SetValue(CommonUtils.GetSingleId());
}
if (entityInfo.PropertyName == nameof(BaseEntity.CreateTime))
entityInfo.SetValue(DateTime.Now);
if (App.User != null)
{
//创建人和创建机构ID
if (entityInfo.PropertyName == nameof(BaseEntity.CreateUserId))
entityInfo.SetValue(App.User.FindFirst(ClaimConst.USER_ID)?.Value);
if (entityInfo.PropertyName == nameof(BaseEntity.CreateUser))
entityInfo.SetValue(App.User.FindFirst(ClaimConst.ACCOUNT)?.Value);
if (entityInfo.PropertyName == nameof(DataEntityBase.CreateOrgId))
entityInfo.SetValue(App.User.FindFirst(ClaimConst.ORG_ID)?.Value);
}
}
// 更新操作
if (entityInfo.OperationType == DataFilterType.UpdateByObject)
{
//更新时间
if (entityInfo.PropertyName == nameof(BaseEntity.UpdateTime))
entityInfo.SetValue(DateTime.Now);
//更新人
if (App.User != null)
{
if (entityInfo.PropertyName == nameof(BaseEntity.UpdateUserId))
entityInfo.SetValue(App.User?.FindFirst(ClaimConst.USER_ID)?.Value);
if (entityInfo.PropertyName == nameof(BaseEntity.UpdateUser))
entityInfo.SetValue(App.User?.FindFirst(ClaimConst.ACCOUNT)?.Value);
}
}
};
//查询数据转换
db.Aop.DataExecuted = (value, entity) =>
{
};
}
/// <summary>
/// 实体更多配置
/// </summary>
/// <param name="db">db</param>
/// <param name="dbType">数据库类型</param>
private static void MoreSetting(SqlSugarScopeProvider db, DbType dbType)
{
db.CurrentConnectionConfig.MoreSettings = new ConnMoreSettings
{
SqlServerCodeFirstNvarchar = dbType == DbType.SqlServer//设置默认nvarchar
};
}
/// <summary>
/// 过滤器设置
/// </summary>
/// <param name="db"></param>
public static void FilterSetting(SqlSugarScopeProvider db)
{
// 假删除过滤器
LogicDeletedEntityFilter(db);
}
/// <summary>
/// 假删除过滤器
/// </summary>
/// <param name="db"></param>
private static void LogicDeletedEntityFilter(SqlSugarScopeProvider db)
{
// 对实现了IDeletedFilter接口的实体添加过滤条件:只查询未删除的数据
db.QueryFilter.AddTableFilter<IDeletedFilter>(u => u.IsDelete == false);
}
private static void WriteSqlLog(string msg)
{
Console.WriteLine($"=============={msg}==============");
}
}
+134
View File
@@ -0,0 +1,134 @@
// Copyright (c) 2022-Now 少林寺驻北固山办事处大神父王喇嘛
//
// SimpleAdmin 基于 Apache License Version 2.0 协议发布,可用于商业项目,但必须遵守以下补充条款:
// 1.请不要删除和修改根目录下的LICENSE文件。
// 2.请不要删除和修改SimpleAdmin源码头部的版权声明。
// 3.分发源码时候,请注明软件出处 https://gitee.com/dotnetmoyu/SimpleAdmin
// 4.基于本软件的作品,只能使用 SimpleAdmin 作为后台服务,除外情况不可商用且不允许二次分发或开源。
// 5.请不得将本软件应用于危害国家安全、荣誉和利益的行为,不能以任何形式用于非法为目的的行为。
// 6.任何基于本软件而产生的一切法律纠纷和责任,均于我司无关。
namespace SimpleAdmin.SqlSugar;
/// <summary>
/// 仓储模式对象
/// </summary>
[SuppressSniffer]
public class DbRepository<T> : SimpleClient<T> where T : class, new()
{
protected ITenant Tenant;//多租户事务、GetConnection、IsAnyConnection等功能
public DbRepository(ISqlSugarClient context = null) : base(context)//注意这里要有默认值等于null
{
Context = DbContext.DB.GetConnectionScopeWithAttr<T>();//ioc注入的对象
Tenant = DbContext.DB;
}
#region 仓储方法拓展
#region 插入
/// <summary>
/// 批量插入判断走普通导入还是大数据
/// </summary>
/// <param name="data">数据</param>
/// <param name="threshold">阈值</param>
/// <returns></returns>
public virtual async Task<int> InsertOrBulkCopy(List<T> data, int threshold = 10000)
{
if (data.Count > threshold)
return await Context.Fastest<T>().BulkCopyAsync(data);//大数据导入
return await Context.Insertable(data).ExecuteCommandAsync();//普通导入
}
#endregion 插入
#region 列表
/// <summary>
/// 获取列表指定多个字段
/// </summary>
/// <param name="whereExpression">查询条件</param>
/// <param name="selectExpression">查询字段</param>
/// <returns></returns>
public virtual Task<List<T>> GetListAsync(Expression<Func<T, bool>> whereExpression, Expression<Func<T, T>> selectExpression)
{
return Context.Queryable<T>().Where(whereExpression).Select(selectExpression).ToListAsync();
}
/// <summary>
/// 获取列表指定单个字段
/// </summary>
/// <param name="whereExpression">查询条件</param>
/// <param name="selectExpression">查询字段</param>
/// <returns></returns>
public virtual Task<List<string>> GetListAsync(Expression<Func<T, bool>> whereExpression, Expression<Func<T, string>> selectExpression)
{
return Context.Queryable<T>().Where(whereExpression).Select(selectExpression).ToListAsync();
}
/// <summary>
/// 获取列表指定单个字段
/// </summary>
/// <param name="whereExpression">查询条件</param>
/// <param name="selectExpression">查询字段</param>
/// <returns></returns>
public virtual Task<List<long>> GetListAsync(Expression<Func<T, bool>> whereExpression, Expression<Func<T, long>> selectExpression)
{
return Context.Queryable<T>().Where(whereExpression).Select(selectExpression).ToListAsync();
}
#endregion 列表
#region 单查
/// <summary>
/// 获取指定表的单个字段
/// </summary>
/// <param name="whereExpression">查询条件</param>
/// <param name="selectExpression">查询字段</param>
/// <returns></returns>
public virtual Task<string> GetFirstAsync(Expression<Func<T, bool>> whereExpression, Expression<Func<T, string>> selectExpression)
{
return Context.Queryable<T>().Where(whereExpression).Select(selectExpression).FirstAsync();
}
/// <summary>
/// 获取指定表的单个字段
/// </summary>
/// <param name="whereExpression">查询条件</param>
/// <param name="selectExpression">查询字段</param>
/// <returns></returns>
protected virtual Task<long> GetFirstAsync(Expression<Func<T, bool>> whereExpression, Expression<Func<T, long>> selectExpression)
{
return Context.Queryable<T>().Where(whereExpression).Select(selectExpression).FirstAsync();
}
/// <summary>
/// 根据条件查询获取自动分表的单个数据
/// </summary>
/// <param name="whereExpression">条件表达式</param>
/// <param name="startTime">开始时间</param>
/// <param name="endTime">结束时间</param>
/// <returns>实体</returns>
public virtual Task<T> GetFirstSplitTableAsync(Expression<Func<T, bool>> whereExpression, DateTime startTime, DateTime endTime)
{
return Context.Queryable<T>().Where(whereExpression).SplitTable(startTime, endTime).FirstAsync();
}
/// <summary>
/// 根据条件查询获取自动分表的单个数据
/// </summary>
/// <param name="whereExpression">条件表达式</param>
/// <param name="getTableNamesFunc">分表查询表达式</param>
/// <returns>实体</returns>
public virtual Task<T> GetFirstSplitTableAsync(Expression<Func<T, bool>> whereExpression,
Func<List<SplitTableInfo>, IEnumerable<SplitTableInfo>> getTableNamesFunc)
{
return Context.Queryable<T>().Where(whereExpression).SplitTable(getTableNamesFunc).FirstAsync();
}
#endregion 单查
#endregion 仓储方法拓展
}
+22
View File
@@ -0,0 +1,22 @@
// Copyright (c) 2022-Now 少林寺驻北固山办事处大神父王喇嘛
//
// SimpleAdmin 基于 Apache License Version 2.0 协议发布,可用于商业项目,但必须遵守以下补充条款:
// 1.请不要删除和修改根目录下的LICENSE文件。
// 2.请不要删除和修改SimpleAdmin源码头部的版权声明。
// 3.分发源码时候,请注明软件出处 https://gitee.com/dotnetmoyu/SimpleAdmin
// 4.基于本软件的作品,只能使用 SimpleAdmin 作为后台服务,除外情况不可商用且不允许二次分发或开源。
// 5.请不得将本软件应用于危害国家安全、荣誉和利益的行为,不能以任何形式用于非法为目的的行为。
// 6.任何基于本软件而产生的一切法律纠纷和责任,均于我司无关。
namespace SimpleAdmin.SqlSugar;
/// <summary>
/// SqlSugar数据库配置
/// </summary>
public sealed class SqlSugarConfig : ConnectionConfig
{
/// <summary>
/// 是否驼峰转下划线
/// </summary>
public bool IsUnderLine { get; set; }
}