You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

437 lines
19 KiB
C#

using DS.Module.Core;
using DS.Module.Core.Data;
using DS.Module.Core.Extensions;
using DS.Module.SqlSugar;
using DS.Module.UserModule;
using DS.WMS.Core.Code.Dtos;
using DS.WMS.Core.Code.Entity;
using DS.WMS.Core.Code.Interface;
using DS.WMS.Core.Invoice.Dtos;
using DS.WMS.Core.Sys.Dtos;
using DS.WMS.Core.Sys.Entity;
using LanguageExt;
using Mapster;
using Microsoft.AspNet.SignalR.Hubs;
using Microsoft.Extensions.DependencyInjection;
using SqlSugar;
namespace DS.WMS.Core.Code.Method
{
public class CodeDataRuleService : ICodeDataRuleService
{
private readonly IServiceProvider _serviceProvider;
private readonly ISqlSugarClient db;
private readonly IUser user;
private readonly ISaasDbService saasService;
/// <summary>
///
/// </summary>
/// <param name="serviceProvider"></param>
public CodeDataRuleService(IServiceProvider serviceProvider)
{
_serviceProvider = serviceProvider;
db = _serviceProvider.GetRequiredService<ISqlSugarClient>();
user = _serviceProvider.GetRequiredService<IUser>();
saasService = _serviceProvider.GetRequiredService<ISaasDbService>();
}
/// <summary>
/// 列表
/// </summary>
/// <param name="req"></param>
/// <returns></returns>
public async Task<DataResult<List<CodeDataRuleRes>>> GetCodeDataRuleList(GetOrCreateCodeDataRuleReq req)
{
var tenantDb = saasService.GetBizDbScopeById(user.TenantId);
if (!tenantDb.Queryable<CodeDataRule>().Where(x=>x.UserId == req.UserId && x.PermissionId == req.PermissionId).Any()) {
var visibleTemp = await tenantDb.Queryable<CodeDataRuleTemplate>().FirstAsync(x => x.PermissionId == req.PermissionId && x.RuleType == "visible" && x.RuleScope == "self");
if (visibleTemp.IsNull())
{
return await Task.FromResult(DataResult<List<CodeDataRuleRes>>.Failed("请维护该权限下的本人可视权限模板!"));
}
var operateTemp = await tenantDb.Queryable<CodeDataRuleTemplate>().FirstAsync(x => x.PermissionId == req.PermissionId && x.RuleType == "operate" && x.RuleScope == "self");
if (operateTemp.IsNull())
{
return await Task.FromResult(DataResult<List<CodeDataRuleRes>>.Failed("请维护该权限下的本人操作权限模板!"));
}
var userInfo = await db.Queryable<SysUser>().FirstAsync(x => x.Id == req.UserId);
var rule = new CodeDataRule() {
UserId = req.UserId,
UserName = userInfo.UserName,
VisibleTemplateId = visibleTemp.Id,
VisibleRuleScope =visibleTemp.RuleScope,
VisibleRuleScopeName = visibleTemp.RuleScopeName,
PermissionEntity = visibleTemp.PermissionEntity,
OperateTemplateId = operateTemp.Id,
OperateRuleScope = operateTemp.RuleScope,
OperateRuleScopeName = operateTemp.RuleScopeName,
PermissionId = req.PermissionId,
TemplateName = visibleTemp.TemplateName,
Description = visibleTemp.Description,
Note = visibleTemp.Note,
};
await tenantDb.Insertable(rule).ExecuteCommandAsync();
}
var data = await tenantDb.Queryable<CodeDataRule>()
.Where(x => x.UserId == req.UserId && x.PermissionId == req.PermissionId)
.Select<CodeDataRuleRes>().ToListAsync();
return await Task.FromResult(DataResult<List<CodeDataRuleRes>>.Success(data,MultiLanguageConst.DataQuerySuccess));
//序列化查询条件
//var whereList = db.ConfigQuery.Context.Utilities.JsonToConditionalModels(request.QueryCondition);
//var data = await tenantDb.Queryable<CodeDataRule>()
// .Where(whereList)
// .Select<CodeDataRuleRes>().ToQueryPageAsync(request.PageCondition);
//return data;
}
/// <summary>
/// 数据权限指定范围
/// </summary>
/// <param name="req"></param>
/// <returns></returns>
public async Task<DataResult> AssignDataRuleScope(AssignDataRuleScopeReq req)
{
var tenantDb = saasService.GetBizDbScopeById(user.TenantId);
var info = await tenantDb.Queryable<CodeDataRule>().FirstAsync(x => x.Id == req.Id);
if (info.IsNull())
{
return await Task.FromResult(DataResult.Failed("数据权限不存在!"));
}
if (req.RuleType == "visible")
{
info.VisibleTemplateId = req.TemplateId;
info.VisibleRuleScope = req.RuleScope;
info.VisibleRuleScopeName = req.RuleScopeName;
}
else
{
info.OperateTemplateId = req.TemplateId;
info.OperateRuleScope = req.RuleScope;
info.OperateRuleScopeName = req.RuleScopeName;
}
await tenantDb.Updateable(info).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommandAsync();
return await Task.FromResult(DataResult.Successed("更新成功!", MultiLanguageConst.DataUpdateSuccess));
}
/// <summary>
/// 数据权限复制
/// </summary>
/// <param name="req"></param>
/// <returns></returns>
public async Task<DataResult> CopyDataRuleScope(CopyDataRuleScopeReq req)
{
var tenantDb = saasService.GetBizDbScopeById(user.TenantId);
if (req.CopyUserIds.Count == 0)
{
return await Task.FromResult(DataResult.Failed("权限复制人员不能为空!"));
}
if (db.Queryable<SysUser>().Where(x=> !req.CopyUserIds.Contains(x.Id)).Any()) {
return await Task.FromResult(DataResult.Failed("请检查复制人员信息!"));
}
var dataRules = tenantDb.Queryable<CodeDataRule>().Where(x=>x.UserId == req.UserId).ToList();
if (dataRules.Count == 0) {
return await Task.FromResult(DataResult.Failed("当前人员数据权限不能为空!"));
}
try
{
await tenantDb.Ado.BeginTranAsync();
foreach (var item in req.CopyUserIds)
{
var userInfo = await db.Queryable<SysUser>().FirstAsync(x => x.Id == item);
var currentRules = await tenantDb.Queryable<CodeDataRule>().Where(x => x.UserId == item).ToListAsync();
await tenantDb.Deleteable(currentRules).ExecuteCommandAsync();
var newRules = new List<CodeDataRule>();
foreach (var rule in dataRules)
{
newRules.Add(new CodeDataRule()
{
UserId = userInfo.Id,
UserName = userInfo.UserName,
VisibleTemplateId = rule.VisibleTemplateId,
VisibleRuleScope = rule.VisibleRuleScope,
VisibleRuleScopeName = rule.VisibleRuleScopeName,
PermissionEntity = rule.PermissionEntity,
OperateTemplateId = rule.OperateTemplateId,
OperateRuleScope = rule.OperateRuleScope,
OperateRuleScopeName = rule.OperateRuleScopeName,
PermissionId = rule.PermissionId,
Description = rule.Description,
Status = rule.Status,
Note = rule.Note,
});
}
await tenantDb.Insertable(newRules).ExecuteCommandAsync();
}
await tenantDb.Ado.CommitTranAsync();
}
catch (Exception ex)
{
await tenantDb.Ado.RollbackTranAsync();
return DataResult.FailedWithDesc(nameof(MultiLanguageConst.Operation_Failed));
}
return await Task.FromResult(DataResult.Successed("更新成功!", MultiLanguageConst.DataUpdateSuccess));
}
public async Task<DataResult> UserVisibleDataRuleScopeApply(UserDataRuleScopeApplyReq req)
{
var tenantDb = saasService.GetBizDbScopeById(user.TenantId);
var rule = await tenantDb.Queryable<CodeDataRule>().Where(x=>x.Id == req.RuleId).FirstAsync();
if (rule.IsNull()) {
return await Task.FromResult(DataResult.Failed("数据权限信息不存在!"));
}
if (string.IsNullOrEmpty(rule.VisibleRuleScope))
{
return await Task.FromResult(DataResult.Failed("请维护该数据权限信息的可视范围!"));
}
var userId = req.UserId;
var userInfo = await db.Queryable<SysUser>().Where(x => x.Id == userId).FirstAsync();
var roleIds = await db.Queryable<SysRoleUser>().Where(x => x.UserId == userId).Select(x => x.RoleId).ToListAsync();
if (roleIds.Count == 0)
{
return await Task.FromResult(DataResult.Failed("该用户未绑定角色!"));
}
var permissions = await db.Queryable<SysRolePermission>().Where(x => roleIds.Contains(x.RoleId))
.Select(x => x.PermissionId).Distinct().ToListAsync();
var perlist = await db.Queryable<SysPermissionTenant>()
.Where(x => x.MenuType == 2 && x.IsHidden == false && permissions.Contains(x.PermissionId) && x.PermissionId!= rule.PermissionId &&
(x.PermissionType == 1 || x.PermissionType == 0))
.OrderBy(x => x.SortCode)
.Select(a => new RouteItem
{
Id = a.Id,
ParentId = a.PermissionId,
Path = a.Url,
Name = a.PermissionName,
EnName = a.PermissionEnName,
Component = a.Component,
Redirect = a.Redirect,
SortCode = a.SortCode,
Meta = new RouteMeta()
{
Title = a.Title,
Icon = a.Icon,
}
})
.ToListAsync();
try
{
await tenantDb.Ado.BeginTranAsync();
var newRules = new List<CodeDataRule>();
var updateRules = new List<CodeDataRule>();
foreach (var item in perlist)
{
var currentTemplate = await tenantDb.Queryable<CodeDataRuleTemplate>().Where(x => x.PermissionId == item.ParentId && x.RuleScope == rule.VisibleRuleScope).FirstAsync();
var currentRule = await tenantDb.Queryable<CodeDataRule>().Where(x => x.PermissionId == item.ParentId && x.UserId == req.UserId).FirstAsync();
if (currentRule.IsNull())
{
if (currentTemplate.IsNotNull())
{
newRules.Add(new CodeDataRule()
{
UserId = userInfo.Id,
UserName = userInfo.UserName,
VisibleTemplateId = currentTemplate.Id,
VisibleRuleScope = currentTemplate.RuleScope,
VisibleRuleScopeName = currentTemplate.RuleScopeName,
PermissionEntity = currentTemplate.PermissionEntity,
PermissionId = currentTemplate.PermissionId,
Description = currentTemplate.Description,
Status = currentTemplate.Status,
Note = currentTemplate.Note,
});
}
}
else {
currentRule.VisibleTemplateId = currentTemplate.Id;
currentRule.VisibleRuleScope = currentTemplate.RuleScope;
currentRule.VisibleRuleScopeName = currentTemplate.RuleScopeName;
updateRules.Add(currentRule);
}
}
if (newRules.Count>0)
{
await tenantDb.Insertable(newRules).ExecuteCommandAsync();
}
if (updateRules.Count > 0)
{
await tenantDb.Updateable(updateRules).ExecuteCommandAsync();
}
await tenantDb.Ado.CommitTranAsync();
}
catch (Exception ex)
{
await tenantDb.Ado.RollbackTranAsync();
return DataResult.FailedWithDesc(nameof(MultiLanguageConst.Operation_Failed));
}
return await Task.FromResult(DataResult.Successed("更新成功!", MultiLanguageConst.DataUpdateSuccess));
}
public async Task<DataResult> UserOperateDataRuleScopeApply(UserDataRuleScopeApplyReq req)
{
var tenantDb = saasService.GetBizDbScopeById(user.TenantId);
var rule = await tenantDb.Queryable<CodeDataRule>().Where(x => x.Id == req.RuleId).FirstAsync();
if (rule.IsNull())
{
return await Task.FromResult(DataResult.Failed("数据权限信息不存在!"));
}
if (string.IsNullOrEmpty(rule.OperateRuleScope))
{
return await Task.FromResult(DataResult.Failed("请维护该数据权限信息的操作范围!"));
}
var userId = req.UserId;
var userInfo = await db.Queryable<SysUser>().Where(x => x.Id == userId).FirstAsync();
var roleIds = await db.Queryable<SysRoleUser>().Where(x => x.UserId == userId).Select(x => x.RoleId).ToListAsync();
if (roleIds.Count == 0)
{
return await Task.FromResult(DataResult.Failed("该用户未绑定角色!"));
}
var permissions = await db.Queryable<SysRolePermission>().Where(x => roleIds.Contains(x.RoleId))
.Select(x => x.PermissionId).Distinct().ToListAsync();
var perlist = await db.Queryable<SysPermissionTenant>()
.Where(x => x.MenuType == 2 && x.IsHidden == false && permissions.Contains(x.PermissionId) && x.PermissionId != rule.PermissionId &&
(x.PermissionType == 1 || x.PermissionType == 0))
.OrderBy(x => x.SortCode)
.Select(a => new RouteItem
{
Id = a.Id,
ParentId = a.PermissionId,
Path = a.Url,
Name = a.PermissionName,
EnName = a.PermissionEnName,
Component = a.Component,
Redirect = a.Redirect,
SortCode = a.SortCode,
Meta = new RouteMeta()
{
Title = a.Title,
Icon = a.Icon,
}
})
.ToListAsync();
try
{
await tenantDb.Ado.BeginTranAsync();
var newRules = new List<CodeDataRule>();
var updateRules = new List<CodeDataRule>();
foreach (var item in perlist)
{
var currentTemplate = await tenantDb.Queryable<CodeDataRuleTemplate>().Where(x => x.PermissionId == item.ParentId && x.RuleScope == rule.OperateRuleScope).FirstAsync();
var currentRule = await tenantDb.Queryable<CodeDataRule>().Where(x => x.PermissionId == item.ParentId && x.UserId == req.UserId).FirstAsync();
if (currentRule.IsNull())
{
if (currentTemplate.IsNotNull())
{
newRules.Add(new CodeDataRule()
{
UserId = userInfo.Id,
UserName = userInfo.UserName,
OperateTemplateId = currentTemplate.Id,
OperateRuleScope = currentTemplate.RuleScope,
OperateRuleScopeName = currentTemplate.RuleScopeName,
PermissionEntity = currentTemplate.PermissionEntity,
PermissionId = currentTemplate.PermissionId,
Description = currentTemplate.Description,
Status = currentTemplate.Status,
Note = currentTemplate.Note,
});
}
}
else
{
currentRule.OperateTemplateId = currentTemplate.Id;
currentRule.OperateRuleScope = currentTemplate.RuleScope;
currentRule.OperateRuleScopeName = currentTemplate.RuleScopeName;
updateRules.Add(currentRule);
}
}
if (newRules.Count > 0)
{
await tenantDb.Insertable(newRules).ExecuteCommandAsync();
}
if (updateRules.Count > 0)
{
await tenantDb.Updateable(updateRules).ExecuteCommandAsync();
}
await tenantDb.Ado.CommitTranAsync();
}
catch (Exception ex)
{
await tenantDb.Ado.RollbackTranAsync();
return DataResult.FailedWithDesc(nameof(MultiLanguageConst.Operation_Failed));
}
return await Task.FromResult(DataResult.Successed("更新成功!", MultiLanguageConst.DataUpdateSuccess));
}
/// <summary>
/// 详情
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
//public async Task<DataResult<CodeDataRuleRes>> GetCodeDataRuleInfo(string id)
//{
// var tenantDb = saasService.GetBizDbScopeById(user.TenantId);
// var data = await tenantDb.Queryable<CodeDataRule>()
// .Where(x => x.Id == long.Parse(id) && x.Status == StatusEnum.Enable)
// .Select<CodeDataRuleRes>()
// .FirstAsync();
// return await Task.FromResult(DataResult<CodeDataRuleRes>.Success(data, MultiLanguageConst.DataQuerySuccess));
//}
//public async Task<DataResult> BatchDelCodeDataRule(IdModel req)
//{
// var tenantDb = saasService.GetBizDbScopeById(user.TenantId);
// var list = await tenantDb.Queryable<CodeDataRule>().Where(x => req.Ids.Contains(x.Id)).ToListAsync();
// if (list.Count > 0)
// {
// tenantDb.Deleteable(list).ExecuteCommand();
// }
// return await Task.FromResult(DataResult.Successed("删除成功!", MultiLanguageConst.DataDelSuccess));
//}
}
}