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.
BookingHeChuan/Myshipping.Core/Service/User/SysDataUserMenu.cs

256 lines
9.1 KiB
C#

2 years ago
using Furion.DependencyInjection;
using Furion.DynamicApiController;
using Furion.FriendlyException;
using Myshipping.Core.Entity;
using Mapster;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;
using System.Linq;
using System.Threading.Tasks;
using System.Collections.Generic;
using System;
using Myshipping.Core.Service.CommonDB.Dto;
using Microsoft.Extensions.Logging;
using Myshipping.Core.Entity.CommonDB;
using Myshipping.Core.Service.User.Dto;
namespace Myshipping.Core.Service;
/// <summary>
/// 用户数据范围
/// </summary>
2 years ago
[ApiDescriptionSettings(Name = "SysDataUserMenu", Order = 150)]
2 years ago
public class SysDataUserMenuService : ISysDataUserMenu, IDynamicApiController, ITransient
2 years ago
{
private readonly SqlSugarRepository<SysDataUserMenu> _rep; // 用户数据范围表仓储
private readonly ISysCacheService _sysCache;//缓存
private readonly SqlSugarRepository<SysOrg> _sysOrgRep; // 组织机构表仓储
private readonly SqlSugarRepository<SysEmp> _sysEmpRep; // 员工表仓储
2 years ago
private readonly SqlSugarRepository<SysUser> _sysuser; // 员工表
1 year ago
private readonly SqlSugarRepository<SysUserDataScope> _sysUserDataScopeRep; // 自定义范围
2 years ago
public SysDataUserMenuService(SqlSugarRepository<SysDataUserMenu> rep, ISysCacheService sysCache, SqlSugarRepository<SysUser> sysuser, SqlSugarRepository<SysUserDataScope> sysUserDataScopeRep, SqlSugarRepository<SysEmp> sysEmpRep, SqlSugarRepository<SysOrg> sysOrgRep)
2 years ago
{
_rep = rep;
_sysCache = sysCache;
2 years ago
_sysuser = sysuser;
2 years ago
_sysUserDataScopeRep = sysUserDataScopeRep;
2 years ago
_sysEmpRep = sysEmpRep;
2 years ago
_sysOrgRep = sysOrgRep;
2 years ago
}
2 years ago
/// <summary>
/// 获取权限
/// </summary>
/// <param name="userid"></param>
/// <returns></returns>
[HttpGet("/SysDataUserMenu/GetGrantData")]
2 years ago
public async Task<dynamic> GetGrantData(long userid)
{
2 years ago
return await _rep.AsQueryable().Where(x => x.SysUserId == userid).ToListAsync();
}
1 year ago
2 years ago
/// <summary>
/// 获取用户自定义权限
/// </summary>
/// <param name="userid"></param>
/// <returns></returns>
[HttpGet("/SysDataUserMenu/GetGrantRightData")]
2 years ago
public async Task<dynamic> GetGrantRightData(long userid)
{
return await _sysUserDataScopeRep.AsQueryable().Where(x => x.SysUserId == userid).ToListAsync();
}
2 years ago
/// <summary>
/// 授权用户数据
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[SqlSugarUnitOfWork]
2 years ago
[HttpPost("/SysDataUserMenu/GrantData")]
2 years ago
public async Task GrantData(SysDataUserMenuDto input)
{
try
{
if (input.UserIdList?.Any() != true)
2 years ago
{
throw Oops.Bah("没有获取到用户id");
}
// 先清理权限
foreach (var userId in input.UserIdList)
2 years ago
{
await _rep.DeleteAsync(x => x.SysUserId == userId);
var list = new List<SysDataUserMenu>();
input.childrens.ForEach(u =>
2 years ago
{
list.Add(
new SysDataUserMenu
{
SysUserId = userId,
SysMenuId = u.MenuId,
DataScopeType = u.DataScopeType,
IsEdit = u.IsEdit
});
2 years ago
});
await _rep.InsertAsync(list);
}
1 year ago
var userdatalist = await _rep.AsQueryable().ToListAsync();
2 years ago
_sysCache.Set(CommonConst.CACHE_KEY_USERDATASCOPE, userdatalist);
}
catch (System.Exception)
{
throw;
}
}
2 years ago
/// <summary>
/// 授权用户自定义数据
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[SqlSugarUnitOfWork]
[HttpPost("/SysDataUserMenu/GrantRightData")]
public async Task GrantRightData(RightList input)
{
try
{
2 years ago
if (input == null)
2 years ago
{
throw Oops.Bah("请上传正确数据!");
}
if (input.UserIdList?.Any() != true)
{
throw Oops.Bah("没有获取到用户id");
}
2 years ago
1 year ago
if (input.childrens != null && input.childrens.Count() > 0)
1 year ago
{
foreach (var item in input.UserIdList)
2 years ago
{
await _sysUserDataScopeRep.DeleteAsync(x => x.SysUserId == item);
var list = new List<SysUserDataScope>();
input.childrens.ForEach(u =>
2 years ago
{
list.Add(new SysUserDataScope
{
SysUserId = item,
SysMenuId = u.MenuId,
SysOrgId = u.OrgId,
IsEdit = u.IsEdit
});
2 years ago
});
await _sysUserDataScopeRep.InsertAsync(list);
}
2 years ago
1 year ago
var userdatalist = await _sysUserDataScopeRep.AsQueryable().ToListAsync();
1 year ago
_sysCache.Set(CommonConst.CACHE_KEY_USERDATASCOPEZDY, userdatalist);
2 years ago
}
1 year ago
2 years ago
}
catch (System.Exception)
{
throw;
}
}
1 year ago
/// <summary>
/// 获取权限合集
/// </summary>
/// <param name="menuid">菜单id</param>
/// <param name="IsEdit">是否查看编辑权限</param>
/// <returns></returns>
public async Task<List<long>> GetDataScopeList(long menuid, bool IsEdit = false)
2 years ago
{
List<SysDataUserMenu> list = new List<SysDataUserMenu>();
1 year ago
List<SysUserDataScope> zdylist = new List<SysUserDataScope>();
1 year ago
List<SysOrg> org = new List<SysOrg>();
List<SysEmp> emp = new List<SysEmp>();
2 years ago
List<long> datascope = new List<long>();
1 year ago
if (_sysCache.Exists(CommonConst.CACHE_KEY_SysOrg))
{
org = await _sysCache.GetAsync<List<SysOrg>>(CommonConst.CACHE_KEY_SysOrg);
}
else
{
org = await _sysOrgRep.AsQueryable().ToListAsync();
_sysCache.Set(CommonConst.CACHE_KEY_SysOrg, org);
}
if (_sysCache.Exists(CommonConst.CACHE_KEY_SysEmp))
{
emp = await _sysCache.GetAsync<List<SysEmp>>(CommonConst.CACHE_KEY_SysEmp);
}
else
{
emp = await _sysEmpRep.AsQueryable().ToListAsync();
_sysCache.Set(CommonConst.CACHE_KEY_SysEmp, emp);
}
1 year ago
if (_sysCache.Exists(CommonConst.CACHE_KEY_USERDATASCOPE))
{
list = await _sysCache.GetAsync<List<SysDataUserMenu>>(CommonConst.CACHE_KEY_USERDATASCOPE);
}
else
{
list = await _rep.AsQueryable().ToListAsync();
_sysCache.Set(CommonConst.CACHE_KEY_USERDATASCOPE, list);
}
1 year ago
if (_sysCache.Exists(CommonConst.CACHE_KEY_USERDATASCOPEZDY))
{
zdylist = await _sysCache.GetAsync<List<SysUserDataScope>>(CommonConst.CACHE_KEY_USERDATASCOPEZDY);
}
else
{
zdylist = await _sysUserDataScopeRep.AsQueryable().ToListAsync();
_sysCache.Set(CommonConst.CACHE_KEY_USERDATASCOPEZDY, list);
}
1 year ago
DataScopeType _dataScopeType = list.Where(x => x.SysMenuId == menuid && x.SysUserId == UserManager.UserId).WhereIF(IsEdit == true, x => x.IsEdit == true).Select(x => x.DataScopeType).FirstOrDefault();
1 year ago
//全部数据返回null
2 years ago
if (_dataScopeType == DataScopeType.ALL)
{
2 years ago
datascope = null;
2 years ago
}
2 years ago
else if (_dataScopeType == DataScopeType.SELF)
{
2 years ago
datascope.Add(UserManager.UserId);
}
2 years ago
else if (_dataScopeType == DataScopeType.DEPT)
{
1 year ago
var orgidlist = emp.Where(x => x.Id == UserManager.UserId).Select(x => x.OrgId).ToList();
datascope = emp.Where(x => orgidlist.Contains(x.OrgId)).Select(x => x.Id).ToList();
2 years ago
}
2 years ago
else if (_dataScopeType == DataScopeType.DEPT_WITH_CHILD)
2 years ago
{
//获取本部门
1 year ago
long orgid = emp.Where(x => x.Id == UserManager.UserId).Select(x => x.OrgId).First();
2 years ago
//获取下属部门及下属部门
1 year ago
var orglist = org.Where(x => x.TenantId == UserManager.TENANT_ID && x.Pids.Contains(orgid.ToString())).Select(x => x.Id).ToList();
2 years ago
orglist.Add(orgid);
1 year ago
datascope = emp.Where(x => orglist.Contains(x.OrgId)).Select(x => x.Id).ToList();
2 years ago
}
2 years ago
else if (_dataScopeType == DataScopeType.DEFINE)
2 years ago
{
1 year ago
var sysorg = zdylist.Where(x => x.SysUserId == UserManager.UserId).Select(x => x.SysOrgId).ToList();
List<long> orglist = new List<long>();
1 year ago
foreach (var orgId in sysorg)
{
1 year ago
var o = org.Where(x => x.Pids.Contains(orgId.ToString())).Select(x => x.Id).ToList();
1 year ago
orglist = orglist.Union(o).ToList();
}
1 year ago
var useridlist = emp.Where(x => orglist.Contains(x.OrgId)).Select(x => x.Id).ToList();
1 year ago
datascope = useridlist;
2 years ago
}
1 year ago
else
{
2 years ago
datascope.Add(UserManager.UserId);
}
2 years ago
return datascope;
}
}