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.

338 lines
14 KiB
C#

1 year ago
using DS.Module.Core;
using DS.Module.Core.Extensions;
using DS.Module.Core.Helpers;
1 year ago
using DS.Module.UserModule;
using DS.WMS.Core.Sys.Dtos;
using DS.WMS.Core.Sys.Entity;
using DS.WMS.Core.Sys.Interface;
1 year ago
using Mapster;
using Microsoft.Extensions.DependencyInjection;
using SqlSugar;
namespace DS.WMS.Core.Sys.Method;
1 year ago
/// <summary>
///
/// </summary>
public class SysRoleService : ISysRoleService
{
private readonly IServiceProvider _serviceProvider;
private readonly ISqlSugarClient db;
private readonly IUser user;
1 year ago
private readonly ICommonService _commonService;
1 year ago
/// <summary>
///
/// </summary>
/// <param name="serviceProvider"></param>
public SysRoleService(IServiceProvider serviceProvider)
{
_serviceProvider = serviceProvider;
db = _serviceProvider.GetRequiredService<ISqlSugarClient>();
user = _serviceProvider.GetRequiredService<IUser>();
1 year ago
_commonService = _serviceProvider.GetRequiredService<ICommonService>();
1 year ago
}
/// <summary>
///
/// </summary>
/// <param name="request"></param>
/// <returns></returns>
public DataResult<List<SysRole>> GetListByPage(PageRequest request)
{
1 year ago
// var query = _commonService.GetDataRuleFilter<SysRole>();
1 year ago
//序列化查询条件
var whereList = db.ConfigQuery.Context.Utilities.JsonToConditionalModels(request.QueryCondition);
var data = db.Queryable<SysRole>()
.Where(whereList).ToQueryPage(request.PageCondition);
return data;
}
/// <summary>
///
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
public DataResult EditRole(RoleInput model)
{
11 months ago
if (model.Id == 0)
1 year ago
{
var isExist = db.Queryable<SysRole>().Filter(null, true).Where(x => x.RoleCode == model.RoleCode).First();
1 year ago
if (isExist != null)
{
return DataResult.Failed("角色唯一编码已存在!", MultiLanguageConst.UserCodeExist);
1 year ago
}
var data = model.Adapt<SysRole>();
var role = db.Insertable(data).ExecuteReturnEntity();
return DataResult.Successed("添加成功!", role.Id, MultiLanguageConst.DataCreateSuccess);
1 year ago
}
else
{
var info = db.Queryable<SysRole>().Where(x => x.Id == model.Id).First();
info = model.Adapt(info);
db.Updateable(info).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommand();
return DataResult.Successed("更新成功!", MultiLanguageConst.DataUpdateSuccess);
1 year ago
}
}
/// <summary>
///
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public DataResult<SysRole> GetRoleInfo(string id)
{
var data = db.Queryable<SysRole>()
11 months ago
.Where(a => a.Id == long.Parse(id))
1 year ago
.First();
return DataResult<SysRole>.Success(data, MultiLanguageConst.DataQuerySuccess);
1 year ago
}
/// <summary>
/// 获取角色权限树
/// </summary>
/// <returns></returns>
public DataResult<RolePermissionTreeViewModel> GetRolePermissionTree()
{
var data = new RolePermissionTreeViewModel();
//查询一级菜单
var info = db.Queryable<SysPermission>().Filter(null, true)
.OrderBy(st => st.SortCode, OrderByType.Asc)
11 months ago
.Where(st => st.ParentId == 0 && st.MenuType == 1)//&& (st.PermissionType == (int)PermissionType.Public || st.PermissionType == user.PermissionIdentity )
1 year ago
.Select(a => new RolePermissionTree
{
Title = a.PermissionName,
Key = a.Id,
}).ToList();
//循环下级权限
if (info.Count > 0)
{
foreach (var item in info)
{
var childs = db.Queryable<SysPermission>().Filter(null, true).Where(x => x.MenuType == 2 && x.ParentId == item.Key)
//&& (x.PermissionType == (int)PermissionType.Public || x.PermissionType == user.PermissionIdentity )
.Select(a => new RolePermissionTree
{
Title = a.PermissionName,
Key = a.Id,
})
.ToList();
if (childs.Count > 0)
{
foreach (var child in childs)
{
var buttons = db.Queryable<SysPermission>().Filter(null, true).Where(x => x.MenuType == 3 && x.ParentId == child.Key)
.Select(a => new RolePermissionTree
{
Title = a.PermissionName,
Key = a.Id,
})
.ToList();
if (buttons.Count > 0)
child.Children = buttons;
}
item.Children = childs;
}
}
}
data.TreeData = info;
11 months ago
data.Ids = db.Queryable<SysPermission>().Select(x => x.Id.ToString()).ToArray();
return DataResult<RolePermissionTreeViewModel>.Success("获取数据成功!", data, MultiLanguageConst.DataQuerySuccess);
1 year ago
}
/// <summary>
/// 获取角色权限树-客户端
/// </summary>
/// <returns></returns>
public DataResult<RolePermissionTreeViewModel> GetClientRolePermissionTree()
{
var data = new RolePermissionTreeViewModel();
//查询一级菜单
var info = db.Queryable<SysPermissionTenant>()
.OrderBy(st => st.SortCode, OrderByType.Asc)
.Where(st => st.ParentId == 0 && st.MenuType == 1)//&& (st.PermissionType == (int)PermissionType.Public || st.PermissionType == user.PermissionIdentity )
.Select(a => new RolePermissionTree
{
Title = a.PermissionName,
EnTitle = a.PermissionEnName,
Key = a.PermissionId,
}).ToList();
//循环下级权限
if (info.Count > 0)
{
foreach (var item in info)
{
var childs = db.Queryable<SysPermissionTenant>().Where(x => x.MenuType == 2 && x.ParentId == item.Key)
//&& (x.PermissionType == (int)PermissionType.Public || x.PermissionType == user.PermissionIdentity )
.Select(a => new RolePermissionTree
{
Title = a.PermissionName,
EnTitle = a.PermissionEnName,
Key = a.PermissionId,
})
.ToList();
if (childs.Count > 0)
{
foreach (var child in childs)
{
var buttons = db.Queryable<SysPermission>().Filter(null, true).Where(x => (x.MenuType == 3 || x.MenuType == 2) && x.ParentId == child.Key)
.Select(a => new RolePermissionTree
{
Title = a.PermissionName,
EnTitle = a.PermissionEnName,
8 months ago
Key = a.Id,
})
.ToList();
if (buttons.Count > 0)
child.Children = buttons;
}
item.Children = childs;
}
}
}
1 year ago
data.TreeData = info;
data.Ids = db.Queryable<SysPermission>().Select(x => x.Id.ToString()).ToArray();
return DataResult<RolePermissionTreeViewModel>.Success("获取数据成功!", data, MultiLanguageConst.DataQuerySuccess);
}
1 year ago
/// <summary>
/// 获取角色权限
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
11 months ago
public DataResult<long?[]> GetRolePermission(string id)
1 year ago
{
11 months ago
var data = db.Queryable<SysRolePermission>().Where(x => x.RoleId == long.Parse(id) && x.IsPermission == 1).Select(x => x.PermissionId).ToArray();
return DataResult<long?[]>.Success("获取数据成功!", data, MultiLanguageConst.DataQuerySuccess);
1 year ago
}
1 year ago
public DataResult UpdateRolePermission(RolePermissionInput model)
{
var existPermissions = db.Queryable<SysRolePermission>().Where(x => x.RoleId == model.RoleId && x.IsPermission == 1).Select(n => n.PermissionId).ToArray();
11 months ago
IEnumerable<long?> delPermissions = existPermissions.AsQueryable().Except(model.PermissionIds);
1 year ago
if (delPermissions.Count() > 0)
{
db.Updateable<SysRolePermission>()
.SetColumns(it => it.IsPermission == 0)//SetColumns是可以叠加的 写2个就2个字段赋值
.Where(it => it.RoleId == model.RoleId && delPermissions.Contains(it.PermissionId))
.ExecuteCommand();
}
11 months ago
IEnumerable<long?> addPermissions = model.PermissionIds.AsQueryable().Except(existPermissions);
1 year ago
if (addPermissions.Count() > 0)
{
var existNoPermissions = db.Queryable<SysRolePermission>().Where(x => x.RoleId == model.RoleId && x.IsPermission == 0).Select(n => n.PermissionId).ToArray();
11 months ago
IEnumerable<long?> updatePermissions = existNoPermissions.AsQueryable().Except(addPermissions);
1 year ago
if (updatePermissions.Count() > 0)
{
db.Updateable<SysRolePermission>()
.SetColumns(it => it.IsPermission == 1)
.Where(it => it.RoleId == model.RoleId && updatePermissions.Contains(it.PermissionId))
.ExecuteCommand();
}
11 months ago
IEnumerable<long?> addRolePermissions = addPermissions.AsQueryable().Except(existNoPermissions);
1 year ago
foreach (var item in addRolePermissions)
{
var permission = new SysRolePermission
{
RoleId = model.RoleId,
PermissionId = item,
IsPermission = 1,
};
db.Insertable(permission).ExecuteCommand();
}
}
return DataResult.Successed("更新成功!", MultiLanguageConst.DataUpdateSuccess);
1 year ago
}
public DataResult UpdateClientRolePermission(RolePermissionInput model)
{
var existPermissions = db.Queryable<SysRolePermission>().Where(x => x.RoleId == model.RoleId && x.IsPermission == 1).Select(n => n.PermissionId).ToArray();
IEnumerable<long?> delPermissions = existPermissions.AsQueryable().Except(model.PermissionIds);
if (delPermissions.Count() > 0)
{
db.Updateable<SysRolePermission>()
.SetColumns(it => it.IsPermission == 0)//SetColumns是可以叠加的 写2个就2个字段赋值
.Where(it => it.RoleId == model.RoleId && delPermissions.Contains(it.PermissionId))
.ExecuteCommand();
}
IEnumerable<long?> addPermissions = model.PermissionIds.AsQueryable().Except(existPermissions);
if (addPermissions.Count() > 0)
{
var existNoPermissions = db.Queryable<SysRolePermission>().Where(x => x.RoleId == model.RoleId && x.IsPermission == 0).Select(n => n.PermissionId).ToArray();
IEnumerable<long?> updatePermissions = existNoPermissions.AsQueryable().Except(addPermissions);
if (updatePermissions.Count() > 0)
{
db.Updateable<SysRolePermission>()
.SetColumns(it => it.IsPermission == 1)
.Where(it => it.RoleId == model.RoleId && updatePermissions.Contains(it.PermissionId))
.ExecuteCommand();
}
IEnumerable<long?> addRolePermissions = addPermissions.AsQueryable().Except(existNoPermissions);
#region 租户模块权限检测
var tenant = db.Queryable<SysTenant>().Where(x => x.Id == long.Parse(user.TenantId)).First();
foreach (var item in addRolePermissions)
{
var per = db.Queryable<SysPermission>().Where(x => x.Id == item)
.First();
if (per != null && per.MenuType == 2)
{
//取加密授权信息
var perAuth = db.Queryable<SysTenantPermissionAuth>().Where(x => x.PermissionId == item).First();
if (perAuth.IsNotNull())
{
//解密
var authNum = EncrypteHelper.DecryptData(perAuth.AuthNum, tenant.AppSecret);
//角色列表去重
var roleList = db.Queryable<SysRolePermission>()
.Where(x => x.PermissionId == item && x.IsPermission == 1).Select(n => n.RoleId).ToList();
roleList.Add(model.RoleId);
roleList.Distinct();
//用户列表去重
var userList = new List<long?>();
foreach (var role in roleList)
{
var userArr = db.Queryable<SysRoleUser>().Where(x => x.RoleId == role).Select(n => n.UserId)
.ToList();
userList.AddRange(userArr);
}
userList.Distinct();
//比较授权
var userCount = userList.Count();
if (userCount > int.Parse(authNum))
{
return DataResult.Failed(string.Format("{0}模块数量超出授权数量{1}", per.PermissionName, int.Parse(authNum)), MultiLanguageConst.PerAuthNumLimit);
}
}
}
}
#endregion
foreach (var item in addRolePermissions)
{
var permission = new SysRolePermission
{
RoleId = model.RoleId,
PermissionId = item,
IsPermission = 1,
};
db.Insertable(permission).ExecuteCommand();
}
}
return DataResult.Successed("更新成功!", MultiLanguageConst.DataUpdateSuccess);
}
1 year ago
}