2021-12-26 18:26:38 +08: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
|
|
|
|
//全部数据权限
|
|
|
|
|
|
public static string DATA_SCOPE_ALL = "1";
|
|
|
|
|
|
//自定数据权限
|
|
|
|
|
|
public static string DATA_SCOPE_CUSTOM = "2";
|
|
|
|
|
|
//部门数据权限
|
|
|
|
|
|
public static string DATA_SCOPE_DEPT = "3";
|
|
|
|
|
|
//部门及以下数据权限
|
|
|
|
|
|
public static string DATA_SCOPE_DEPT_AND_CHILD = "4";
|
|
|
|
|
|
//仅本人数据权限
|
|
|
|
|
|
public static string DATA_SCOPE_SELF = "5";
|
|
|
|
|
|
|
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-05-12 21:28:27 +08:00
|
|
|
|
//...增加其他数据库
|
2022-12-16 16:08:39 +08:00
|
|
|
|
};
|
|
|
|
|
|
SugarIocServices.AddSqlSugar(iocList);
|
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
|
|
|
|
{
|
2022-12-16 16:08:39 +08:00
|
|
|
|
SetSugarAop(db, iocConfig);
|
|
|
|
|
|
});
|
2022-03-16 21:58:37 +08:00
|
|
|
|
});
|
2022-12-16 16:08:39 +08:00
|
|
|
|
}
|
2022-10-25 08:12:15 +08:00
|
|
|
|
|
2022-12-16 16:08:39 +08:00
|
|
|
|
private static void SetSugarAop(SqlSugarClient db, IocConfig iocConfig)
|
|
|
|
|
|
{
|
|
|
|
|
|
var config = db.GetConnection(iocConfig.ConfigId).CurrentConnectionConfig;
|
|
|
|
|
|
|
|
|
|
|
|
string configId = config.ConfigId;
|
|
|
|
|
|
db.GetConnectionScope(configId).Aop.OnLogExecuting = (sql, pars) =>
|
|
|
|
|
|
{
|
2022-12-20 16:06:59 +08:00
|
|
|
|
string log = $"【db{configId} SQL语句】{UtilMethods.GetSqlString(config.DbType, sql, pars)}\n";
|
2022-12-16 16:08:39 +08:00
|
|
|
|
if (sql.StartsWith("UPDATE", StringComparison.OrdinalIgnoreCase) || sql.StartsWith("INSERT", StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
|
logger.Warn(log);
|
2023-03-01 17:30:13 +08:00
|
|
|
|
else if (sql.StartsWith("DELETE", StringComparison.OrdinalIgnoreCase) || sql.StartsWith("TRUNCATE", StringComparison.OrdinalIgnoreCase))
|
2022-12-16 16:08:39 +08:00
|
|
|
|
logger.Error(log);
|
2023-03-01 17:30:13 +08:00
|
|
|
|
else
|
|
|
|
|
|
logger.Info(log);
|
2022-12-16 16:08:39 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
db.GetConnectionScope(configId).Aop.OnError = (e) =>
|
|
|
|
|
|
{
|
|
|
|
|
|
Console.ForegroundColor = ConsoleColor.Red;
|
|
|
|
|
|
logger.Error(e, $"执行SQL出错:{e.Message}");
|
|
|
|
|
|
};
|
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;
|
|
|
|
|
|
db.DbMaintenance.CreateDatabase();
|
|
|
|
|
|
//db.CodeFirst.
|
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();
|
2022-07-01 16:29:02 +08:00
|
|
|
|
db.CodeFirst.SetStringDefaultLength(512).InitTables(entityes);
|
2021-12-26 18:26:38 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
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);
|
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
|
|
|
|
{
|
2022-01-22 16:57:38 +08:00
|
|
|
|
string dataScope = role.DataScope;
|
|
|
|
|
|
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()));
|
|
|
|
|
|
var filter1 = new TableFilterItem<SysUser>(it => SqlFunc.Subqueryable<SysRoleDept>().Where(f => f.DeptId == it.DeptId && f.RoleId == role.RoleId).Any());
|
|
|
|
|
|
db.QueryFilter.Add(filter1);
|
2022-01-22 16:57:38 +08:00
|
|
|
|
}
|
|
|
|
|
|
else if (DATA_SCOPE_DEPT.Equals(dataScope))//本部门数据
|
|
|
|
|
|
{
|
2022-04-10 09:07:43 +08:00
|
|
|
|
var filter1 = new TableFilterItem<SysUser>(it => it.DeptId == user.DeptId);
|
2022-04-09 19:11:16 +08:00
|
|
|
|
db.QueryFilter.Add(filter1);
|
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);
|
|
|
|
|
|
|
|
|
|
|
|
var filter1 = new TableFilterItem<SysUser>(it => allChildDepts.Select(f => f.DeptId).ToList().Contains(it.DeptId));
|
|
|
|
|
|
db.QueryFilter.Add(filter1);
|
|
|
|
|
|
|
|
|
|
|
|
var filter2 = new TableFilterItem<SysDept>(it => allChildDepts.Select(f => f.DeptId).ToList().Contains(it.DeptId));
|
|
|
|
|
|
db.QueryFilter.Add(filter2);
|
2022-01-22 16:57:38 +08:00
|
|
|
|
}
|
|
|
|
|
|
else if (DATA_SCOPE_SELF.Equals(dataScope))//仅本人数据
|
|
|
|
|
|
{
|
2023-03-01 17:30:13 +08:00
|
|
|
|
//var filter1 = new TableFilterItem<SysUser>(it => it.UserId == user.UserId, true);
|
|
|
|
|
|
//db.QueryFilter.Add(filter1);
|
|
|
|
|
|
db.QueryFilter.AddTableFilter<SysUser>(it => it.UserId == user.UserId);
|
2021-12-26 18:26:38 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|