2021-12-26 18:26:38 +08:00
|
|
|
|
using Infrastructure;
|
|
|
|
|
|
using Microsoft.Extensions.Configuration;
|
|
|
|
|
|
using SqlSugar;
|
|
|
|
|
|
using SqlSugar.IOC;
|
|
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
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)
|
|
|
|
|
|
{
|
|
|
|
|
|
string connStr = Configuration.GetConnectionString(OptionsSetting.ConnAdmin);
|
|
|
|
|
|
string connStrBus = Configuration.GetConnectionString(OptionsSetting.ConnBus);
|
2022-03-16 21:58:37 +08:00
|
|
|
|
|
2021-12-26 18:26:38 +08:00
|
|
|
|
int dbType = Convert.ToInt32(Configuration[OptionsSetting.ConnDbType]);
|
|
|
|
|
|
int dbType_bus = Convert.ToInt32(Configuration[OptionsSetting.ConnBusDbType]);
|
|
|
|
|
|
|
|
|
|
|
|
SugarIocServices.AddSqlSugar(new List<IocConfig>() {
|
|
|
|
|
|
new IocConfig() {
|
|
|
|
|
|
ConfigId = "0",
|
|
|
|
|
|
ConnectionString = connStr,
|
|
|
|
|
|
DbType = (IocDbType)dbType,
|
2022-03-05 16:48:40 +08:00
|
|
|
|
IsAutoCloseConnection = true
|
2021-12-26 18:26:38 +08:00
|
|
|
|
}, new IocConfig() {
|
|
|
|
|
|
ConfigId = "1",
|
|
|
|
|
|
ConnectionString = connStrBus,
|
|
|
|
|
|
DbType = (IocDbType)dbType_bus,
|
2022-03-05 16:48:40 +08:00
|
|
|
|
IsAutoCloseConnection = true
|
2021-12-26 18:26:38 +08:00
|
|
|
|
}
|
|
|
|
|
|
});
|
2022-03-16 21:58:37 +08:00
|
|
|
|
SugarIocServices.ConfigurationSugar(db =>
|
2021-12-26 18:26:38 +08:00
|
|
|
|
{
|
2022-03-16 21:58:37 +08:00
|
|
|
|
#region db0
|
|
|
|
|
|
db.GetConnection(0).Aop.OnLogExecuting = (sql, pars) =>
|
|
|
|
|
|
{
|
2022-03-18 09:52:17 +08:00
|
|
|
|
var param = db.Utilities.SerializeObject(pars.ToDictionary(it => it.ParameterName, it => it.Value));
|
2021-12-26 18:26:38 +08:00
|
|
|
|
|
2022-03-16 21:58:37 +08:00
|
|
|
|
FilterData();
|
2021-12-26 18:26:38 +08:00
|
|
|
|
|
2022-03-18 09:52:17 +08:00
|
|
|
|
logger.Info($"【sql语句】{sql},{param}");
|
2022-03-16 21:58:37 +08:00
|
|
|
|
};
|
2022-01-22 16:57:38 +08:00
|
|
|
|
|
2022-03-16 21:58:37 +08:00
|
|
|
|
db.GetConnection(0).Aop.OnError = (e) =>
|
|
|
|
|
|
{
|
|
|
|
|
|
logger.Error(e, $"执行SQL出错:{e.Message}");
|
|
|
|
|
|
};
|
|
|
|
|
|
//SQL执行完
|
|
|
|
|
|
db.GetConnection(0).Aop.OnLogExecuted = (sql, pars) =>
|
|
|
|
|
|
{
|
|
|
|
|
|
//执行完了可以输出SQL执行时间 (OnLogExecutedDelegate)
|
|
|
|
|
|
};
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
#region db1
|
|
|
|
|
|
//Db1
|
|
|
|
|
|
db.GetConnection(1).Aop.OnLogExecuting = (sql, pars) =>
|
|
|
|
|
|
{
|
|
|
|
|
|
var param = DbScoped.SugarScope.Utilities.SerializeObject(pars.ToDictionary(it => it.ParameterName, it => it.Value));
|
|
|
|
|
|
|
2022-03-18 09:52:17 +08:00
|
|
|
|
logger.Info($"【sql语句】{sql}, {param}");
|
2022-03-16 21:58:37 +08:00
|
|
|
|
};
|
|
|
|
|
|
//Db1错误日志
|
|
|
|
|
|
db.GetConnection(1).Aop.OnError = (e) =>
|
|
|
|
|
|
{
|
|
|
|
|
|
logger.Error($"执行Sql语句失败:{e.Sql},原因:{e.Message}");
|
|
|
|
|
|
};
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
});
|
2021-12-26 18:26:38 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2022-01-22 20:47:48 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 分页获取count 不会追加sql
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
private static void FilterData()
|
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;
|
|
|
|
|
|
//管理员不过滤
|
|
|
|
|
|
if (user.RoleIds.Any(f => f.Equals("admin"))) return;
|
|
|
|
|
|
|
|
|
|
|
|
foreach (var role in user.Roles)
|
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-01-22 20:47:48 +08:00
|
|
|
|
//有问题
|
2022-01-22 16:57:38 +08:00
|
|
|
|
//var roleDepts = db0.Queryable<SysRoleDept>()
|
|
|
|
|
|
//.Where(f => f.RoleId == role.RoleId).Select(f => f.DeptId).ToList();
|
|
|
|
|
|
//var filter1 = new TableFilterItem<SysDept>(it => roleDepts.Contains(it.DeptId));
|
2022-01-22 20:47:48 +08:00
|
|
|
|
//DbScoped.SugarScope.GetConnection(0).QueryFilter.Add(filter1);
|
2022-01-22 16:57:38 +08:00
|
|
|
|
}
|
|
|
|
|
|
else if (DATA_SCOPE_DEPT.Equals(dataScope))//本部门数据
|
|
|
|
|
|
{
|
|
|
|
|
|
//有问题添加后的SQL 语句 是 AND deptId = @deptId
|
|
|
|
|
|
var exp = Expressionable.Create<SysDept>();
|
|
|
|
|
|
exp.Or(it => it.DeptId == user.DeptId);
|
|
|
|
|
|
var filter1 = new TableFilterItem<SysDept>(exp.ToExpression());
|
|
|
|
|
|
DbScoped.SugarScope.GetConnection(0).QueryFilter.Add(filter1);
|
|
|
|
|
|
Console.WriteLine("本部门数据过滤");
|
|
|
|
|
|
}
|
|
|
|
|
|
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 ) )
|
|
|
|
|
|
}
|
|
|
|
|
|
else if (DATA_SCOPE_SELF.Equals(dataScope))//仅本人数据
|
|
|
|
|
|
{
|
|
|
|
|
|
var filter1 = new TableFilterItem<SysUser>(it => it.UserId == user.UserId);
|
|
|
|
|
|
DbScoped.SugarScope.GetConnection(0).QueryFilter.Add(filter1);
|
2021-12-26 18:26:38 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|