|
|
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.Invoice.Dtos;
|
|
|
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;
|
|
|
|
|
|
/// <summary>
|
|
|
///
|
|
|
/// </summary>
|
|
|
public class TenantService : ITenantService
|
|
|
{
|
|
|
private readonly IServiceProvider _serviceProvider;
|
|
|
private readonly ISqlSugarClient db;
|
|
|
private readonly ISaasDbService saasService;
|
|
|
/// <summary>
|
|
|
///
|
|
|
/// </summary>
|
|
|
/// <param name="serviceProvider"></param>
|
|
|
public TenantService(IServiceProvider serviceProvider)
|
|
|
{
|
|
|
_serviceProvider = serviceProvider;
|
|
|
db = _serviceProvider.GetRequiredService<ISqlSugarClient>();
|
|
|
saasService = _serviceProvider.GetRequiredService<ISaasDbService>();
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
///
|
|
|
/// </summary>
|
|
|
/// <param name="request"></param>
|
|
|
/// <returns></returns>
|
|
|
public DataResult<List<TenantRes>> GetListByPage(PageRequest request)
|
|
|
{
|
|
|
//序列化查询条件
|
|
|
var whereList = db.ConfigQuery.Context.Utilities.JsonToConditionalModels(request.QueryCondition);
|
|
|
var data = db.Queryable<SysTenant>()
|
|
|
.Where(x => x.TenantType != 1)
|
|
|
.Where(whereList)
|
|
|
.Select<TenantRes>().ToQueryPage(request.PageCondition);
|
|
|
return data;
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
///
|
|
|
/// </summary>
|
|
|
/// <param name="id"></param>
|
|
|
/// <returns></returns>
|
|
|
public DataResult<TenantRes> GetTenantInfo(string id)
|
|
|
{
|
|
|
var data = db.Queryable<SysTenant>()
|
|
|
.Where(a => a.Id == long.Parse(id))
|
|
|
.Select<TenantRes>()
|
|
|
.First();
|
|
|
return DataResult<TenantRes>.Success(data);
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 获取租户权限
|
|
|
/// </summary>
|
|
|
/// <param name="id"></param>
|
|
|
/// <returns></returns>
|
|
|
public DataResult<long[]> GetTenantPermission(string id)
|
|
|
{
|
|
|
var data = db.Queryable<SysPermissionTenant>().Filter(null, true).Where(x => x.TenantId == long.Parse(id) && x.IsHidden == false)
|
|
|
.Select(x => x.PermissionId).ToArray();
|
|
|
return DataResult<long[]>.Success("获取数据成功!", data, MultiLanguageConst.DataQuerySuccess);
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 租户权限模块授权
|
|
|
/// </summary>
|
|
|
/// <param name="req"></param>
|
|
|
/// <returns></returns>
|
|
|
public DataResult UpdateTenantPermission(TenantPermissionReq req)
|
|
|
{
|
|
|
try
|
|
|
{
|
|
|
//开启事务
|
|
|
db.Ado.BeginTran();
|
|
|
|
|
|
var existPermissions = db.Queryable<SysPermissionTenant>().Filter(null, true)
|
|
|
.Where(x => x.TenantId == req.TenantId && x.IsHidden == false).Select(n => n.PermissionId).ToArray();
|
|
|
var hiddenPermissions = db.Queryable<SysPermissionTenant>().Filter(null, true)
|
|
|
.Where(x => x.TenantId == req.TenantId && x.IsHidden == true).Select(n => n.PermissionId).ToArray();
|
|
|
var tenant = db.Queryable<SysTenant>().First(x => x.Id == req.TenantId);
|
|
|
IEnumerable<long> delPermissions = existPermissions.AsQueryable().Except(req.PermissionIds);
|
|
|
//取消权限
|
|
|
if (delPermissions.Count() > 0)
|
|
|
{
|
|
|
var delPers = db.Queryable<SysPermissionTenant>().Filter(null, true).Where(x =>
|
|
|
x.TenantId == req.TenantId && delPermissions.Contains(x.PermissionId))
|
|
|
.ToList();
|
|
|
foreach (var item in delPers)
|
|
|
{
|
|
|
item.IsHidden = true;
|
|
|
}
|
|
|
db.Updateable(delPers).ExecuteCommand();
|
|
|
}
|
|
|
|
|
|
IEnumerable<long> addPermissions = req.PermissionIds.AsQueryable().Except(existPermissions);
|
|
|
if (addPermissions.Count() > 0)
|
|
|
{
|
|
|
var addPers = db.Queryable<SysPermission>().Filter(null, true)
|
|
|
.Where(x => addPermissions.Contains(x.Id)).ToList();
|
|
|
|
|
|
#region 处理租户管理员新增权限
|
|
|
|
|
|
var tenantRole = db.Queryable<SysRole>().Filter(null, true)
|
|
|
.Where(x => x.TenantId == req.TenantId && x.RoleCode == "tenantAdmin").First();
|
|
|
//写入租户管理员权限
|
|
|
var rolePerList = new List<SysRolePermission>();
|
|
|
foreach (var item in addPers)
|
|
|
{
|
|
|
if (!db.Queryable<SysRolePermission>().Where(x => x.RoleId == tenantRole.Id && x.PermissionId == item.Id).Any())
|
|
|
{
|
|
|
var rolePer = item.Adapt<SysRolePermission>();
|
|
|
rolePer.RoleId = tenantRole.Id;
|
|
|
rolePer.PermissionId = item.Id;
|
|
|
rolePer.IsPermission = 1;
|
|
|
|
|
|
rolePerList.Add(rolePer);
|
|
|
}
|
|
|
}
|
|
|
db.Insertable(rolePerList).ExecuteCommandAsync();
|
|
|
#endregion 处理租户管理员新增权限
|
|
|
var tenantPerList = new List<SysPermissionTenant>();
|
|
|
foreach (var item in addPers)
|
|
|
{
|
|
|
if (hiddenPermissions.Contains(item.Id))
|
|
|
{
|
|
|
var tenantPer = db.Queryable<SysPermissionTenant>().Filter(null, true).Where(x => x.PermissionId == item.Id && x.TenantId == req.TenantId && x.IsHidden == true).First();
|
|
|
tenantPer.IsHidden = false;
|
|
|
db.Updateable(tenantPer).ExecuteCommand();
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
var tenantPer = item.Adapt<SysPermissionTenant>();
|
|
|
tenantPer.Id = SnowFlakeSingle.Instance.NextId();//重新赋值
|
|
|
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<SysTenantPermissionAuth>().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();
|
|
|
ex.LogAsync(db).GetAwaiter().GetResult();
|
|
|
return DataResult.Failed("租户权限模块授权失败!" + ex, MultiLanguageConst.DataUpdateFailed);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
public DataResult<List<PermissionAuth>> GetPermissionAuthList(string id)
|
|
|
{
|
|
|
var list = new List<PermissionAuth>();
|
|
|
var tenant = db.Queryable<SysTenant>().First(x => x.Id == long.Parse(id));
|
|
|
|
|
|
var perAuthList = db.Queryable<SysTenantPermissionAuth>().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<List<PermissionAuth>>.Success("获取数据成功!", list, MultiLanguageConst.DataQuerySuccess);
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 获取租户表差异
|
|
|
/// </summary>
|
|
|
/// <param name="id"></param>
|
|
|
/// <returns></returns>
|
|
|
public DataResult<List<TableDifferenceInfo>> 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<List<TableDifferenceInfo>>.Success("获取数据成功!", data, MultiLanguageConst.DataQuerySuccess);
|
|
|
}
|
|
|
/// <summary>
|
|
|
/// 更新租户表差异
|
|
|
/// </summary>
|
|
|
/// <param name="req"></param>
|
|
|
/// <returns></returns>
|
|
|
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);
|
|
|
}
|
|
|
} |