using System.Reflection;
using DS.Module.Core;
using DS.Module.Core.Data;
using DS.Module.Core.Extensions;
using DS.Module.Core.Helpers;
using DS.Module.SqlSugar;
using DS.WMS.Core.Sys.Dtos;
using DS.WMS.Core.Sys.Entity;
using DS.WMS.Core.Sys.Interface;
using Mapster;
using Microsoft.Extensions.DependencyInjection;
using SqlSugar;
namespace DS.WMS.Core.Sys.Method;
///
///
///
public class TenantService : ITenantService
{
private readonly IServiceProvider _serviceProvider;
private readonly ISqlSugarClient db;
private readonly ISaasDbService saasService;
///
///
///
///
public TenantService(IServiceProvider serviceProvider)
{
_serviceProvider = serviceProvider;
db = _serviceProvider.GetRequiredService();
saasService = _serviceProvider.GetRequiredService();
}
///
///
///
///
///
public DataResult> GetListByPage(PageRequest request)
{
//序列化查询条件
var whereList = db.ConfigQuery.Context.Utilities.JsonToConditionalModels(request.QueryCondition);
var data = db.Queryable()
.Where(x => x.TenantType != 1)
.Where(whereList)
.Select().ToQueryPage(request.PageCondition);
return data;
}
///
///
///
///
///
public DataResult GetTenantInfo(string id)
{
var data = db.Queryable()
.Where(a => a.Id == long.Parse(id))
.Select()
.First();
return DataResult.Success(data);
}
///
/// 获取租户权限
///
///
///
public DataResult GetTenantPermission(string id)
{
var data = db.Queryable().Filter(null, true).Where(x => x.TenantId == long.Parse(id))
.Select(x => x.PermissionId).ToArray();
return DataResult.Success("获取数据成功!", data, MultiLanguageConst.DataQuerySuccess);
}
///
/// 租户权限模块授权
///
///
///
public DataResult UpdateTenantPermission(TenantPermissionReq req)
{
try
{
//开启事务
db.Ado.BeginTran();
var existPermissions = db.Queryable().Filter(null, true)
.Where(x => x.TenantId == req.TenantId).Select(n => n.PermissionId).ToArray();
var tenant = db.Queryable().First(x => x.Id == req.TenantId);
IEnumerable delPermissions = existPermissions.AsQueryable().Except(req.PermissionIds);
// if (delPermissions.Count() > 0)
// {
// var delPers = db.Queryable().Filter(null, true).Where(x =>
// x.TenantId == req.TenantId && delPermissions.Contains(x.PermissionId))
// .ToList();
// db.Deleteable(delPers).ExecuteCommand();
// }
IEnumerable addPermissions = req.PermissionIds.AsQueryable().Except(existPermissions);
if (addPermissions.Count() > 0)
{
var addPers = db.Queryable().Filter(null, true)
.Where(x => addPermissions.Contains(x.Id)).ToList();
#region 处理租户管理员新增权限
var tenantRole = db.Queryable().Filter(null, true)
.Where(x => x.TenantId == req.TenantId && x.RoleCode == "tenantAdmin").First();
//写入租户管理员权限
var rolePerList = new List();
foreach (var item in addPers)
{
var rolePer = item.Adapt();
rolePer.RoleId = tenantRole.Id;
rolePer.PermissionId = item.Id;
rolePer.IsPermission = 1;
rolePerList.Add(rolePer);
}
db.Insertable(rolePerList).ExecuteCommandAsync();
#endregion 处理租户管理员新增权限
var tenantPerList = new List();
foreach (var item in addPers)
{
var tenantPer = item.Adapt();
tenantPer.TenantId = req.TenantId;
tenantPer.PermissionId = item.Id;
tenantPerList.Add(tenantPer);
}
db.Insertable(tenantPerList).ExecuteCommand();
}
foreach (var item in req.PermissionAuthList)
{
var perAuth = db.Queryable().Filter(null, true)
.Where(x => x.TenantId == req.TenantId && x.PermissionId == item.PermissionId).First();
if (perAuth.IsNotNull())
{
if (perAuth.AuthNum != EncrypteHelper.EncryptData(item.AuthNum.ToString(), tenant.AppSecret))
{
perAuth.AuthNum = EncrypteHelper.EncryptData(item.AuthNum.ToString(), tenant.AppSecret);
db.Updateable(perAuth).ExecuteCommand();
}
}
else
{
var addPerAuth = new SysTenantPermissionAuth()
{
TenantId = tenant.Id,
PermissionId = item.PermissionId,
AuthNum = EncrypteHelper.EncryptData(item.AuthNum.ToString(), tenant.AppSecret)
};
db.Insertable(addPerAuth).ExecuteCommand();
}
}
db.Ado.CommitTran();
return DataResult.Successed("租户权限模块授权成功!", MultiLanguageConst.DataUpdateSuccess);
}
catch (Exception ex)
{
db.Ado.RollbackTran();
return DataResult.Failed("租户权限模块授权失败!" + ex, MultiLanguageConst.DataUpdateFailed);
}
}
public DataResult> GetPermissionAuthList(string id)
{
var list = new List();
var tenant = db.Queryable().First(x => x.Id == long.Parse(id));
var perAuthList = db.Queryable().Filter(null, true)
.Where(x => x.TenantId == long.Parse(id)).ToList();
foreach (var item in perAuthList)
{
list.Add(new PermissionAuth()
{
PermissionId = item.PermissionId,
AuthNum = Int32.Parse(EncrypteHelper.DecryptData(item.AuthNum, tenant.AppSecret)) ,
});
}
return DataResult>.Success("获取数据成功!", list, MultiLanguageConst.DataQuerySuccess);
}
///
/// 获取租户表差异
///
///
///
public DataResult> GetSaasTableDifference(string id)
{
var path = AppDomain.CurrentDomain.RelativeSearchPath ?? AppDomain.CurrentDomain.BaseDirectory;
var referencedAssemblies = Directory.GetFiles(path, "DS.WMS.Core.dll").Select(Assembly.LoadFrom).ToArray();
var types = referencedAssemblies
.SelectMany(a => a.DefinedTypes)
.Select(type => type.AsType())
.Where(it=>it.FullName.Contains("DS.WMS.Core.Code.Entity.")).ToArray();
// Type[] types= Assembly
// .LoadFrom("DS.WMS.Core.dll")//如果 .dll报错,可以换成 xxx.exe 有些生成的是exe
// .GetTypes().Where(it=>it.FullName.Contains("DS.WMS.Core.Code.Entity."))//命名空间过滤,当然你也可以写其他条件过滤
// .ToArray();
var tenantDb = saasService.GetBizDbScopeById(id);
var data = tenantDb.CodeFirst.GetDifferenceTables(types).ToDiffList();
return DataResult>.Success("获取数据成功!", data, MultiLanguageConst.DataQuerySuccess);
}
///
/// 更新租户表差异
///
///
///
public DataResult UpdateSaasTableInfo(IdModel req)
{
// Type[] types= Assembly
// .LoadFrom("DS.WMS.Core.dll")//如果 .dll报错,可以换成 xxx.exe 有些生成的是exe
// .GetTypes().Where(it=>it.FullName.Contains("DS.WMS.Core.Code.Entity."))//命名空间过滤,当然你也可以写其他条件过滤
// .ToArray();
var path = AppDomain.CurrentDomain.RelativeSearchPath ?? AppDomain.CurrentDomain.BaseDirectory;
var referencedAssemblies = Directory.GetFiles(path, "DS.WMS.Core.dll").Select(Assembly.LoadFrom).ToArray();
var types = referencedAssemblies
.SelectMany(a => a.DefinedTypes)
.Select(type => type.AsType())
.Where(it=>it.FullName.Contains("DS.WMS.Core.Code.Entity.")).ToArray();
var tenantDb = saasService.GetBizDbScopeById(req.Id);
StaticConfig.CodeFirst_MySqlCollate="utf8mb4_0900_ai_ci";//较高版本支持
tenantDb.CodeFirst.InitTables(types);
return DataResult.Successed("更新租户表信息成功!", MultiLanguageConst.DataUpdateSuccess);
}
}