|
|
|
@ -0,0 +1,79 @@
|
|
|
|
|
using Infrastructure.Options;
|
|
|
|
|
using Infrastructure.Repository;
|
|
|
|
|
using Microsoft.AspNetCore.Hosting;
|
|
|
|
|
using Serilog;
|
|
|
|
|
using SqlSugar;
|
|
|
|
|
using SqlSugar.Extensions;
|
|
|
|
|
|
|
|
|
|
namespace Infrastructure.Extensions;
|
|
|
|
|
|
|
|
|
|
public static class SqlSugarSetup
|
|
|
|
|
{
|
|
|
|
|
public static IServiceCollection AddDefaultSqlSugarSetup(
|
|
|
|
|
this IServiceCollection services,
|
|
|
|
|
IConfiguration configuration,
|
|
|
|
|
IWebHostEnvironment hostEnvironment)
|
|
|
|
|
{
|
|
|
|
|
ArgumentNullException.ThrowIfNull(services);
|
|
|
|
|
|
|
|
|
|
ArgumentNullException.ThrowIfNull(configuration);
|
|
|
|
|
|
|
|
|
|
ArgumentNullException.ThrowIfNull(hostEnvironment);
|
|
|
|
|
|
|
|
|
|
var sqlSugarOptions = configuration.GetSection(SqlSugarOptions.Name).Get<SqlSugarOptions>();
|
|
|
|
|
if (sqlSugarOptions is null || !sqlSugarOptions.IsEnable)
|
|
|
|
|
{
|
|
|
|
|
return services;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
services.TryAddScoped(typeof(IRepositoryBase<>), typeof(RepositoryBase<>));
|
|
|
|
|
services.TryAddScoped<IUnitOfWork, UnitOfWork>();
|
|
|
|
|
if (sqlSugarOptions.SnowFlake?.IsEnable ?? false)
|
|
|
|
|
{
|
|
|
|
|
var workerId = configuration["SNOWFLAKES_WORKERID"]?.ObjToInt() ?? sqlSugarOptions.SnowFlake?.WorkerId;
|
|
|
|
|
ArgumentNullException.ThrowIfNull(workerId);
|
|
|
|
|
SnowFlakeSingle.WorkId = (int)workerId;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var server = configuration["DB_HOST"] ?? sqlSugarOptions.Server;
|
|
|
|
|
ArgumentException.ThrowIfNullOrEmpty(server);
|
|
|
|
|
|
|
|
|
|
var port = configuration["DB_PORT"] ?? sqlSugarOptions.Port.ToString();
|
|
|
|
|
ArgumentException.ThrowIfNullOrEmpty(port);
|
|
|
|
|
|
|
|
|
|
var database = configuration["DB_DATABASE"] ?? sqlSugarOptions.Database;
|
|
|
|
|
ArgumentException.ThrowIfNullOrEmpty(database);
|
|
|
|
|
|
|
|
|
|
var user = configuration["DB_USER"] ?? sqlSugarOptions.User;
|
|
|
|
|
ArgumentException.ThrowIfNullOrEmpty(user);
|
|
|
|
|
|
|
|
|
|
var password = configuration["DB_PASSWORD"] ?? sqlSugarOptions.Password;
|
|
|
|
|
ArgumentException.ThrowIfNullOrEmpty(password);
|
|
|
|
|
|
|
|
|
|
var connectionString = $"server={server};port={port};database={database};userid={user};password={password};";
|
|
|
|
|
|
|
|
|
|
var connectionConfig = new ConnectionConfig()
|
|
|
|
|
{
|
|
|
|
|
DbType = DbType.PostgreSQL,
|
|
|
|
|
ConnectionString = connectionString,
|
|
|
|
|
InitKeyType = InitKeyType.Attribute,
|
|
|
|
|
IsAutoCloseConnection = true,
|
|
|
|
|
MoreSettings = new ConnMoreSettings()
|
|
|
|
|
{
|
|
|
|
|
PgSqlIsAutoToLower = false,
|
|
|
|
|
PgSqlIsAutoToLowerCodeFirst = false,
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var sugarScope = new SqlSugarScope(connectionConfig, config =>
|
|
|
|
|
{
|
|
|
|
|
config.QueryFilter.AddTableFilter<IDeletable>(d => !d.IsDeleted);
|
|
|
|
|
if (hostEnvironment.IsDevelopment() || hostEnvironment.IsStaging())
|
|
|
|
|
{
|
|
|
|
|
config.Aop.OnLogExecuting = (sql, parameters) => { Log.Logger.Information(sql); };
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
services.AddSingleton<ISqlSugarClient>(sugarScope);
|
|
|
|
|
return services;
|
|
|
|
|
}
|
|
|
|
|
}
|