2023-05-19 09:30:57 +00:00
|
|
|
|
using Infrastructure;
|
2022-07-01 22:39:39 +08:00
|
|
|
|
using Infrastructure.Helper;
|
2021-12-26 18:26:38 +08:00
|
|
|
|
using SqlSugar;
|
|
|
|
|
|
using SqlSugar.IOC;
|
2022-07-01 22:39:39 +08:00
|
|
|
|
using System.Reflection;
|
2021-12-26 18:26:38 +08:00
|
|
|
|
using ZR.Admin.WebApi.Framework;
|
|
|
|
|
|
using ZR.Model.System;
|
|
|
|
|
|
|
|
|
|
|
|
namespace ZR.Admin.WebApi.Extensions
|
|
|
|
|
|
{
|
|
|
|
|
|
public static class DbExtension
|
|
|
|
|
|
{
|
|
|
|
|
|
private static NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
|
2022-01-22 16:57:38 +08:00
|
|
|
|
//全部数据权限
|
2023-05-19 09:30:57 +00:00
|
|
|
|
public static long DATA_SCOPE_ALL = 1;
|
2022-01-22 16:57:38 +08:00
|
|
|
|
//自定数据权限
|
2023-05-19 09:30:57 +00:00
|
|
|
|
public static long DATA_SCOPE_CUSTOM = 2;
|
2022-01-22 16:57:38 +08:00
|
|
|
|
//部门数据权限
|
2023-05-19 09:30:57 +00:00
|
|
|
|
public static long DATA_SCOPE_DEPT = 3;
|
2022-01-22 16:57:38 +08:00
|
|
|
|
//部门及以下数据权限
|
2023-05-19 09:30:57 +00:00
|
|
|
|
public static long DATA_SCOPE_DEPT_AND_CHILD = 4;
|
2022-01-22 16:57:38 +08:00
|
|
|
|
//仅本人数据权限
|
2023-05-19 09:30:57 +00:00
|
|
|
|
public static long DATA_SCOPE_SELF = 5;
|
2022-01-22 16:57:38 +08:00
|
|
|
|
|
2021-12-26 18:26:38 +08:00
|
|
|
|
public static void AddDb(IConfiguration Configuration)
|
|
|
|
|
|
{
|
2022-05-12 21:28:27 +08:00
|
|
|
|
string connStr = Configuration.GetConnectionString("conn_db");
|
2022-06-01 17:36:06 +08:00
|
|
|
|
int dbType = Convert.ToInt32(Configuration.GetConnectionString("conn_db_type"));
|
2021-12-26 18:26:38 +08:00
|
|
|
|
|
2022-12-16 16:08:39 +08:00
|
|
|
|
var iocList = new List<IocConfig>() {
|
2022-04-09 19:11:16 +08:00
|
|
|
|
new IocConfig() {
|
2022-05-12 21:28:27 +08:00
|
|
|
|
ConfigId = "0",//默认db
|
2022-04-09 19:11:16 +08:00
|
|
|
|
ConnectionString = connStr,
|
|
|
|
|
|
DbType = (IocDbType)dbType,
|
|
|
|
|
|
IsAutoCloseConnection = true
|
2022-05-12 21:28:27 +08:00
|
|
|
|
},
|
|
|
|
|
|
new IocConfig() {
|
2022-04-09 19:11:16 +08:00
|
|
|
|
ConfigId = "1",
|
2022-12-16 16:08:39 +08:00
|
|
|
|
ConnectionString = connStr,
|
|
|
|
|
|
DbType = (IocDbType)dbType,
|
2022-04-09 19:11:16 +08:00
|
|
|
|
IsAutoCloseConnection = true
|
|
|
|
|
|
}
|
2022-12-16 16:08:39 +08:00
|
|
|
|
};
|
|
|
|
|
|
SugarIocServices.AddSqlSugar(iocList);
|
2023-02-15 22:13:01 +08:00
|
|
|
|
ICacheService cache = new SqlSugarCache();
|
2022-03-16 21:58:37 +08:00
|
|
|
|
SugarIocServices.ConfigurationSugar(db =>
|
2021-12-26 18:26:38 +08:00
|
|
|
|
{
|
2022-05-12 21:28:27 +08:00
|
|
|
|
//db0数据过滤
|
2022-04-09 21:50:37 +08:00
|
|
|
|
FilterData(0);
|
2022-10-25 08:12:15 +08:00
|
|
|
|
|
2022-12-16 16:08:39 +08:00
|
|
|
|
iocList.ForEach(iocConfig =>
|
2022-03-16 21:58:37 +08:00
|
|
|
|
{
|
2023-03-05 16:58:32 +08:00
|
|
|
|
SetSugarAop(db, iocConfig, cache);
|
2022-12-16 16:08:39 +08:00
|
|
|
|
});
|
2022-03-16 21:58:37 +08:00
|
|
|
|
});
|
2022-12-16 16:08:39 +08:00
|
|
|
|
}
|
2022-10-25 08:12:15 +08:00
|
|
|
|
|
2023-03-05 16:58:32 +08:00
|
|
|
|
private static void SetSugarAop(SqlSugarClient db, IocConfig iocConfig, ICacheService cache)
|
2022-12-16 16:08:39 +08:00
|
|
|
|
{
|
|
|
|
|
|
var config = db.GetConnection(iocConfig.ConfigId).CurrentConnectionConfig;
|
2023-04-14 08:46:59 +08:00
|
|
|
|
|
2022-12-16 16:08:39 +08:00
|
|
|
|
string configId = config.ConfigId;
|
2023-05-12 15:10:45 +08:00
|
|
|
|
db.GetConnectionScope(configId).Aop.OnLogExecuting = (sql, pars) =>
|
2022-12-16 16:08:39 +08:00
|
|
|
|
{
|
2022-12-20 16:06:59 +08:00
|
|
|
|
string log = $"【db{configId} SQL语句】{UtilMethods.GetSqlString(config.DbType, sql, pars)}\n";
|
2023-04-14 08:46:59 +08:00
|
|
|
|
if (sql.TrimStart().StartsWith("SELECT", StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
|
{
|
|
|
|
|
|
logger.Info(log);
|
|
|
|
|
|
}
|
|
|
|
|
|
else if (sql.StartsWith("UPDATE", StringComparison.OrdinalIgnoreCase) || sql.StartsWith("INSERT", StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
|
{
|
2022-12-16 16:08:39 +08:00
|
|
|
|
logger.Warn(log);
|
2023-04-14 08:46:59 +08:00
|
|
|
|
}
|
2023-03-01 17:30:13 +08:00
|
|
|
|
else if (sql.StartsWith("DELETE", StringComparison.OrdinalIgnoreCase) || sql.StartsWith("TRUNCATE", StringComparison.OrdinalIgnoreCase))
|
2023-04-14 08:46:59 +08:00
|
|
|
|
{
|
2022-12-16 16:08:39 +08:00
|
|
|
|
logger.Error(log);
|
2023-04-14 08:46:59 +08:00
|
|
|
|
}
|
2023-03-01 17:30:13 +08:00
|
|
|
|
else
|
2023-04-14 08:46:59 +08:00
|
|
|
|
{
|
|
|
|
|
|
log = $"【db{configId} SQL语句】dbo.{sql} {string.Join(", ", pars.Select(x => x.ParameterName + " = " + GetParsValue(x)))};\n";
|
2023-03-01 17:30:13 +08:00
|
|
|
|
logger.Info(log);
|
2023-04-14 08:46:59 +08:00
|
|
|
|
}
|
2022-12-16 16:08:39 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
2023-05-12 15:10:45 +08:00
|
|
|
|
db.GetConnectionScope(configId).Aop.OnError = (ex) =>
|
2022-12-16 16:08:39 +08:00
|
|
|
|
{
|
2023-05-12 15:10:45 +08:00
|
|
|
|
var pars = db.Utilities.SerializeObject(((SugarParameter[])ex.Parametres).ToDictionary(it => it.ParameterName, it => it.Value));
|
|
|
|
|
|
|
|
|
|
|
|
string sql = "【错误SQL】" + UtilMethods.GetSqlString(config.DbType, ex.Sql, (SugarParameter[])ex.Parametres) + "\r\n";
|
|
|
|
|
|
logger.Error(ex, $"{sql}\r\n{ex.Message}\r\n{ex.StackTrace}");
|
2022-12-16 16:08:39 +08:00
|
|
|
|
};
|
2023-03-05 16:58:32 +08:00
|
|
|
|
|
|
|
|
|
|
db.GetConnectionScope(configId).CurrentConnectionConfig.MoreSettings = new ConnMoreSettings()
|
|
|
|
|
|
{
|
|
|
|
|
|
IsAutoRemoveDataCache = true
|
|
|
|
|
|
};
|
|
|
|
|
|
db.GetConnectionScope(configId).CurrentConnectionConfig.ConfigureExternalServices = new ConfigureExternalServices()
|
|
|
|
|
|
{
|
2023-05-12 15:10:45 +08:00
|
|
|
|
DataInfoCacheService = cache,
|
|
|
|
|
|
EntityService = (c, p) =>
|
|
|
|
|
|
{
|
|
|
|
|
|
if (db.GetConnectionScope(configId).CurrentConnectionConfig.DbType == DbType.PostgreSQL && p.DataType != null && p.DataType.Contains("nvarchar"))
|
|
|
|
|
|
{
|
|
|
|
|
|
p.DataType = "text";
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2023-03-05 16:58:32 +08:00
|
|
|
|
};
|
2022-07-01 16:29:02 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2022-10-25 08:12:15 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 初始化db
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="service"></param>
|
2022-07-01 16:29:02 +08:00
|
|
|
|
public static void InitDb(this IServiceProvider service)
|
|
|
|
|
|
{
|
|
|
|
|
|
var db = DbScoped.SugarScope;
|
2023-05-15 19:52:54 +08:00
|
|
|
|
//建库:如果不存在创建数据库存在不会重复创建
|
|
|
|
|
|
db.DbMaintenance.CreateDatabase();// 注意 :Oracle和个别国产库需不支持该方法,需要手动建库
|
2022-10-25 08:12:15 +08:00
|
|
|
|
|
2022-07-01 16:29:02 +08:00
|
|
|
|
var baseType = typeof(SysBase);
|
2022-10-25 08:12:15 +08:00
|
|
|
|
var entityes = AssemblyUtils.GetAllTypes().Where(p => !p.IsAbstract && p != baseType && /*p.IsAssignableTo(baseType) && */p.GetCustomAttribute<SugarTable>() != null).ToArray();
|
2023-05-15 19:52:54 +08:00
|
|
|
|
db.CodeFirst.SetStringDefaultLength(200).InitTables(entityes);
|
2021-12-26 18:26:38 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-04-14 08:46:59 +08:00
|
|
|
|
private static object GetParsValue(SugarParameter x)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (x.DbType == System.Data.DbType.String || x.DbType == System.Data.DbType.DateTime || x.DbType == System.Data.DbType.String)
|
|
|
|
|
|
{
|
|
|
|
|
|
return "'" + x.Value + "'";
|
|
|
|
|
|
}
|
|
|
|
|
|
return x.Value;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-01-22 20:47:48 +08:00
|
|
|
|
/// <summary>
|
2022-05-12 21:28:27 +08:00
|
|
|
|
/// 数据过滤
|
2022-01-22 20:47:48 +08:00
|
|
|
|
/// </summary>
|
2022-04-09 19:11:16 +08:00
|
|
|
|
/// <param name="configId">多库id</param>
|
|
|
|
|
|
private static void FilterData(int configId)
|
2021-12-26 18:26:38 +08:00
|
|
|
|
{
|
|
|
|
|
|
var u = App.User;
|
2022-01-22 16:57:38 +08:00
|
|
|
|
if (u == null) return;
|
|
|
|
|
|
//获取当前用户的信息
|
|
|
|
|
|
var user = JwtUtil.GetLoginUser(App.HttpContext);
|
|
|
|
|
|
if (user == null) return;
|
|
|
|
|
|
//管理员不过滤
|
2022-10-20 18:04:25 +08:00
|
|
|
|
if (user.RoleIds.Any(f => f.Equals(GlobalConstant.AdminRole))) return;
|
2022-05-12 21:28:27 +08:00
|
|
|
|
var db = DbScoped.SugarScope.GetConnectionScope(configId);
|
2023-05-23 14:11:52 +08:00
|
|
|
|
var expUser = Expressionable.Create<SysUser>().Or(it => 1 == 1);
|
|
|
|
|
|
var expRole = Expressionable.Create<SysRole>().Or(it => 1 == 1);
|
|
|
|
|
|
var expLoginlog = Expressionable.Create<SysLogininfor>();
|
|
|
|
|
|
|
2022-04-09 19:11:16 +08:00
|
|
|
|
foreach (var role in user.Roles.OrderBy(f => f.DataScope))
|
2021-12-26 18:26:38 +08:00
|
|
|
|
{
|
2023-05-23 14:11:52 +08:00
|
|
|
|
long dataScope = role.DataScope;
|
2022-01-22 16:57:38 +08:00
|
|
|
|
if (DATA_SCOPE_ALL.Equals(dataScope))//所有权限
|
2021-12-26 18:26:38 +08:00
|
|
|
|
{
|
2022-01-22 16:57:38 +08:00
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
else if (DATA_SCOPE_CUSTOM.Equals(dataScope))//自定数据权限
|
|
|
|
|
|
{
|
2022-04-09 19:11:16 +08:00
|
|
|
|
//" OR {}.dept_id IN ( SELECT dept_id FROM sys_role_dept WHERE role_id = {} ) ", deptAlias, role.getRoleId()));
|
2023-05-23 14:11:52 +08:00
|
|
|
|
|
|
|
|
|
|
expUser.Or(it => SqlFunc.Subqueryable<SysRoleDept>().Where(f => f.DeptId == it.DeptId && f.RoleId == role.RoleId).Any());
|
2022-01-22 16:57:38 +08:00
|
|
|
|
}
|
|
|
|
|
|
else if (DATA_SCOPE_DEPT.Equals(dataScope))//本部门数据
|
|
|
|
|
|
{
|
2023-05-23 14:11:52 +08:00
|
|
|
|
expUser.Or(it => it.DeptId == user.DeptId);
|
2022-01-22 16:57:38 +08:00
|
|
|
|
}
|
|
|
|
|
|
else if (DATA_SCOPE_DEPT_AND_CHILD.Equals(dataScope))//本部门及以下数据
|
|
|
|
|
|
{
|
|
|
|
|
|
//SQl OR {}.dept_id IN ( SELECT dept_id FROM sys_dept WHERE dept_id = {} or find_in_set( {} , ancestors ) )
|
2022-04-10 09:07:43 +08:00
|
|
|
|
var allChildDepts = db.Queryable<SysDept>().ToChildList(it => it.ParentId, user.DeptId);
|
|
|
|
|
|
|
2023-05-23 14:11:52 +08:00
|
|
|
|
expUser.Or(it => allChildDepts.Select(f => f.DeptId).ToList().Contains(it.DeptId));
|
2022-01-22 16:57:38 +08:00
|
|
|
|
}
|
|
|
|
|
|
else if (DATA_SCOPE_SELF.Equals(dataScope))//仅本人数据
|
|
|
|
|
|
{
|
2023-05-23 14:11:52 +08:00
|
|
|
|
expUser.Or(it => it.UserId == user.UserId);
|
|
|
|
|
|
expRole.Or(it => user.RoleIds.Contains(it.RoleKey));
|
|
|
|
|
|
expLoginlog.And(it => it.UserName == user.UserName);
|
2021-12-26 18:26:38 +08:00
|
|
|
|
}
|
2023-05-23 14:11:52 +08:00
|
|
|
|
db.QueryFilter.AddTableFilter(expUser.ToExpression());
|
|
|
|
|
|
db.QueryFilter.AddTableFilter(expRole.ToExpression());
|
|
|
|
|
|
db.QueryFilter.AddTableFilter(expLoginlog.ToExpression());
|
2021-12-26 18:26:38 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|