using Apache.Arrow.Types;
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.Flow.Dtos;
using DS.WMS.Core.Invoice.Dtos;
using DS.WMS.Core.Op.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;
using System.Data;
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;
///
///
///
///
public CodeDataRuleService(IServiceProvider serviceProvider)
{
_serviceProvider = serviceProvider;
db = _serviceProvider.GetRequiredService();
user = _serviceProvider.GetRequiredService();
saasService = _serviceProvider.GetRequiredService();
}
///
/// 列表
///
///
///
public async Task>> GetCodeDataRuleList(GetOrCreateCodeDataRuleReq req)
{
var tenantDb = saasService.GetBizDbScopeById(user.TenantId);
var data = new List();
var userId = req.UserId;
var userInfo = await db.Queryable().Where(x => x.Id == userId).FirstAsync();
var roleIds = await db.Queryable().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().Where(x => roleIds.Contains(x.RoleId))
.Select(x => x.PermissionId).Distinct().ToListAsync();
var rules = await tenantDb.Queryable().Where(x => x.UserId == userId).ToListAsync();
var templateList = await tenantDb.Queryable()
.Where(x => x.Status == StatusEnum.Enable)
.Select()
.ToListAsync();
if (req.PermissionId == 0)
{
var perlist = await db.Queryable()
.Where(x => x.MenuType == 2 && x.IsHidden == false && permissions.Contains(x.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();
data = (from a in perlist
join b in rules on a.ParentId equals b.PermissionId into ruleList
from co in ruleList.DefaultIfEmpty()
select new CodeDataRuleRes()
{
Id = co?.Id ?? 0,
PermissionId = (long)a.ParentId,
PermissioName = a.Name,
TemplateName = co?.TemplateName,
Description = co?.Description,
Note = co?.Note,
UserId = userId,
UserName = co?.UserName,
VisibleTemplateId = co?.VisibleTemplateId ?? 0,
VisibleRuleScope = co?.VisibleRuleScope,
VisibleRuleScopeName = co?.VisibleRuleScopeName,
OperateTemplateId = co?.OperateTemplateId ?? 0,
OperateRuleScope = co?.OperateRuleScope,
OperateRuleScopeName = co?.OperateRuleScopeName,
Status = co?.Status,
OrderNo = co?.OrderNo ?? 100,
VisibleTemplates = templateList.Where(x=>x.RuleType == "visible" && x.PermissionId == (long)a.ParentId).ToList(),
OperateTemplates = templateList.Where(x => x.RuleType == "operate" && x.PermissionId == (long)a.ParentId).ToList(),
}).ToList();
}
else {
var perlist = await db.Queryable()
.Where(x => x.MenuType == 2 && x.IsHidden == false && permissions.Contains(x.PermissionId) && x.PermissionId == req.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();
//data = await perlist.
// LeftJoin((a, b) => a.ParentId == b.PermissionId)
// .Select((a, b) => new CodeDataRuleRes()
// {
// Id = b.Id,
// }, true).ToListAsync();
data = (from a in perlist
join b in rules on a.ParentId equals b.PermissionId into ruleList
from co in ruleList.DefaultIfEmpty()
select new CodeDataRuleRes()
{
Id = co?.Id ?? 0,
PermissionId = (long)a.ParentId,
PermissioName = a.Name,
TemplateName = co?.TemplateName,
Description = co?.Description,
Note = co?.Note,
UserId = userId,
UserName = co?.UserName,
VisibleTemplateId = co?.VisibleTemplateId ?? 0,
VisibleRuleScope = co?.VisibleRuleScope,
VisibleRuleScopeName = co?.VisibleRuleScopeName,
OperateTemplateId = co?.OperateTemplateId ?? 0,
OperateRuleScope = co?.OperateRuleScope,
OperateRuleScopeName = co?.OperateRuleScopeName,
Status = co?.Status,
OrderNo = co?.OrderNo ?? 100,
VisibleTemplates = templateList.Where(x => x.RuleType == "visible" && x.PermissionId == (long)a.ParentId).ToList(),
OperateTemplates = templateList.Where(x => x.RuleType == "operate" && x.PermissionId == (long)a.ParentId).ToList(),
}).ToList();
}
//if (!tenantDb.Queryable().Where(x=>x.UserId == req.UserId && x.PermissionId == req.PermissionId).Any()) {
// var visibleTemp = await tenantDb.Queryable().FirstAsync(x => x.PermissionId == req.PermissionId && x.RuleType == "visible" && x.RuleScope == "self");
// if (visibleTemp.IsNull())
// {
// return await Task.FromResult(DataResult>.Failed("请维护该权限下的本人可视权限模板!"));
// }
// var operateTemp = await tenantDb.Queryable().FirstAsync(x => x.PermissionId == req.PermissionId && x.RuleType == "operate" && x.RuleScope == "self");
// if (operateTemp.IsNull())
// {
// return await Task.FromResult(DataResult>.Failed("请维护该权限下的本人操作权限模板!"));
// }
// var userInfo = await db.Queryable().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();
//}
//data = await tenantDb.Queryable()
// .Where(x => x.UserId == req.UserId && x.PermissionId == req.PermissionId)
// .Select().ToListAsync();
return await Task.FromResult(DataResult>.Success(data,MultiLanguageConst.DataQuerySuccess));
//序列化查询条件
//var whereList = db.ConfigQuery.Context.Utilities.JsonToConditionalModels(request.QueryCondition);
//var data = await tenantDb.Queryable()
// .Where(whereList)
// .Select().ToQueryPageAsync(request.PageCondition);
//return data;
}
///
/// 数据权限指定范围
///
///
///
public async Task AssignDataRuleScope(AssignDataRuleScopeReq req)
{
var tenantDb = saasService.GetBizDbScopeById(user.TenantId);
if (req.Id == 0)
{
var template = await tenantDb.Queryable().FirstAsync(x => x.Id == req.TemplateId);
if (req.UserId == 0 || string.IsNullOrEmpty(req.UserId.ToString()))
{
return await Task.FromResult(DataResult.Failed("请传值用户Id!"));
}
if (template.IsNull())
{
return await Task.FromResult(DataResult.Failed("数据权限模板不存在!"));
}
var userInfo = await db.Queryable().Where(x => x.Id == req.UserId).FirstAsync();
var rule = new CodeDataRule()
{
UserId = req.UserId,
UserName = userInfo.UserName,
PermissionEntity = template.PermissionEntity,
PermissionId = template.PermissionId,
TemplateName = template.TemplateName,
Description = template.Description,
Note = template.Note,
};
if (req.RuleType == "visible")
{
rule.VisibleTemplateId = req.TemplateId;
rule.VisibleRuleScope = req.RuleScope;
rule.VisibleRuleScopeName = req.RuleScopeName;
}
else
{
rule.OperateTemplateId = req.TemplateId;
rule.OperateRuleScope = req.RuleScope;
rule.OperateRuleScopeName = req.RuleScopeName;
}
await tenantDb.Insertable(rule).ExecuteCommandAsync();
}
else
{
var info = await tenantDb.Queryable().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));
}
///
/// 数据权限复制
///
///
///
public async Task CopyDataRuleScope(CopyDataRuleScopeReq req)
{
var tenantDb = saasService.GetBizDbScopeById(user.TenantId);
if (req.CopyUserIds.Count == 0)
{
return await Task.FromResult(DataResult.Failed("权限复制人员不能为空!"));
}
if (db.Queryable().Where(x=> req.CopyUserIds.Contains(x.Id)).Count() == 0) {
return await Task.FromResult(DataResult.Failed("请检查复制人员信息!"));
}
var dataRules = tenantDb.Queryable().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().FirstAsync(x => x.Id == item);
var currentRules = await tenantDb.Queryable().Where(x => x.UserId == item).ToListAsync();
await tenantDb.Deleteable(currentRules).ExecuteCommandAsync();
var newRules = new List();
foreach (var rule in dataRules)
{
newRules.Add(new CodeDataRule()
{
UserId = userInfo.Id,
UserName = userInfo.UserName,
TemplateName = rule.TemplateName,
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 UserVisibleDataRuleScopeApply(UserDataRuleScopeApplyReq req)
{
var tenantDb = saasService.GetBizDbScopeById(user.TenantId);
var rule = await tenantDb.Queryable().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().Where(x => x.Id == userId).FirstAsync();
var roleIds = await db.Queryable().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().Where(x => roleIds.Contains(x.RoleId))
.Select(x => x.PermissionId).Distinct().ToListAsync();
var perlist = await db.Queryable()
.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();
var updateRules = new List();
foreach (var item in perlist)
{
var currentTemplate = await tenantDb.Queryable().Where(x => x.PermissionId == item.ParentId && x.RuleScope == rule.VisibleRuleScope).FirstAsync();
var currentRule = await tenantDb.Queryable().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,
TemplateName = currentTemplate.TemplateName,
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 UserOperateDataRuleScopeApply(UserDataRuleScopeApplyReq req)
{
var tenantDb = saasService.GetBizDbScopeById(user.TenantId);
var rule = await tenantDb.Queryable().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().Where(x => x.Id == userId).FirstAsync();
var roleIds = await db.Queryable().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().Where(x => roleIds.Contains(x.RoleId))
.Select(x => x.PermissionId).Distinct().ToListAsync();
var perlist = await db.Queryable()
.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();
var updateRules = new List();
foreach (var item in perlist)
{
var currentTemplate = await tenantDb.Queryable().Where(x => x.PermissionId == item.ParentId && x.RuleScope == rule.OperateRuleScope).FirstAsync();
var currentRule = await tenantDb.Queryable().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,
TemplateName = currentTemplate.TemplateName,
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));
}
///
/// 详情
///
///
///
//public async Task> GetCodeDataRuleInfo(string id)
//{
// var tenantDb = saasService.GetBizDbScopeById(user.TenantId);
// var data = await tenantDb.Queryable()
// .Where(x => x.Id == long.Parse(id) && x.Status == StatusEnum.Enable)
// .Select()
// .FirstAsync();
// return await Task.FromResult(DataResult.Success(data, MultiLanguageConst.DataQuerySuccess));
//}
//public async Task BatchDelCodeDataRule(IdModel req)
//{
// var tenantDb = saasService.GetBizDbScopeById(user.TenantId);
// var list = await tenantDb.Queryable().Where(x => req.Ids.Contains(x.Id)).ToListAsync();
// if (list.Count > 0)
// {
// tenantDb.Deleteable(list).ExecuteCommand();
// }
// return await Task.FromResult(DataResult.Successed("删除成功!", MultiLanguageConst.DataDelSuccess));
//}
}
}