Files
shgx_tz_mes_backend_sync/ZR.Admin.WebApi/Extensions/DbExtension.cs

151 lines
6.2 KiB
C#
Raw Normal View History

2021-12-26 18:26:38 +08:00
using Infrastructure;
using SqlSugar;
using SqlSugar.IOC;
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
SugarIocServices.AddSqlSugar(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-05-12 21:28:27 +08:00
ConnectionString = "替换成你的字符串",
DbType = IocDbType.MySql,
2022-04-09 19:11:16 +08:00
IsAutoCloseConnection = true
}
2022-05-12 21:28:27 +08:00
//...增加其他数据库
2022-04-09 19:11:16 +08:00
});
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-05-12 21:28:27 +08:00
2022-03-16 21:58:37 +08:00
#region db0
2022-05-12 21:28:27 +08:00
db.GetConnectionScope(0).Aop.OnLogExecuting = (sql, pars) =>
2022-03-16 21:58:37 +08:00
{
2022-05-12 21:28:27 +08:00
var param = db.GetConnectionScope(0).Utilities.SerializeObject(pars.ToDictionary(it => it.ParameterName, it => it.Value));
2022-04-09 21:50:37 +08:00
2022-04-09 19:11:16 +08:00
logger.Info($"【sql语句】{sql}{param}\n");
2022-03-16 21:58:37 +08:00
};
2022-01-22 16:57:38 +08:00
2022-05-12 21:28:27 +08:00
db.GetConnectionScope(0).Aop.OnError = (e) =>
2022-03-16 21:58:37 +08:00
{
logger.Error(e, $"执行SQL出错{e.Message}");
};
//SQL执行完
2022-05-12 21:28:27 +08:00
db.GetConnectionScope(0).Aop.OnLogExecuted = (sql, pars) =>
2022-03-16 21:58:37 +08:00
{
//执行完了可以输出SQL执行时间 (OnLogExecutedDelegate)
};
#endregion
#region db1
//Db1
db.GetConnection(1).Aop.OnLogExecuting = (sql, pars) =>
{
2022-04-09 19:11:16 +08:00
var param = db.GetConnection(1).Utilities.SerializeObject(pars.ToDictionary(it => it.ParameterName, it => it.Value));
2022-04-09 21:50:37 +08:00
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
});
}
public static void InitDb(this IServiceProvider service)
{
var db = DbScoped.SugarScope;
db.DbMaintenance.CreateDatabase();
//db.CodeFirst.
var baseType = typeof(SysBase);
var entityes = AssemblyUtils.GetAllTypes().Where(p => !p.IsAbstract && p != baseType && /*p.IsAssignableTo(baseType) && */p.GetCustomAttribute<SugarTable>()!=null).ToArray();
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;
//管理员不过滤
if (user.RoleIds.Any(f => f.Equals("admin"))) 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))//本部门数据
{
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 ) )
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))//仅本人数据
{
2022-03-18 21:49:58 +08:00
var filter1 = new TableFilterItem<SysUser>(it => it.UserId == user.UserId, true);
2022-04-09 19:11:16 +08:00
db.QueryFilter.Add(filter1);
2021-12-26 18:26:38 +08:00
}
}
}
}
}