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.

225 lines
9.5 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 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));
}
/// <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));
//}
}
}