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

213 lines
6.9 KiB
C#

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>
[ApiDescriptionSettings(Name = "SysDataUserMenu", Order = 150)]
public class SysDataUserMenuService : ISysDataUserMenu, IDynamicApiController, ITransient
{
private readonly SqlSugarRepository<SysDataUserMenu> _rep; // 用户数据范围表仓储
private readonly ISysCacheService _sysCache;//缓存
private readonly SqlSugarRepository<SysOrg> _sysOrgRep; // 组织机构表仓储
private readonly SqlSugarRepository<SysEmp> _sysEmpRep; // 员工表仓储
private readonly SqlSugarRepository<SysUser> _sysuser; // 员工表
private readonly SqlSugarRepository<SysUserDataScope> _sysUserDataScopeRep; // 用户数据范围表仓储
public SysDataUserMenuService(SqlSugarRepository<SysDataUserMenu> rep, ISysCacheService sysCache, SqlSugarRepository<SysUser> sysuser, SqlSugarRepository<SysUserDataScope> sysUserDataScopeRep, SqlSugarRepository<SysEmp> sysEmpRep, SqlSugarRepository<SysOrg> sysOrgRep)
{
_rep = rep;
_sysCache = sysCache;
_sysuser = sysuser;
_sysUserDataScopeRep = sysUserDataScopeRep;
_sysEmpRep = sysEmpRep;
_sysOrgRep = sysOrgRep;
}
/// <summary>
/// 获取权限
/// </summary>
/// <param name="userid"></param>
/// <returns></returns>
[HttpGet("/SysDataUserMenu/GetGrantData")]
public async Task<dynamic> GetGrantData(long userid)
{
return await _rep.AsQueryable().Where(x => x.SysUserId == userid).ToListAsync();
}
/// <summary>
/// 获取用户自定义权限
/// </summary>
/// <param name="userid"></param>
/// <returns></returns>
[HttpGet("/SysDataUserMenu/GetGrantRightData")]
public async Task<dynamic> GetGrantRightData(long userid)
{
return await _sysUserDataScopeRep.AsQueryable().Where(x => x.SysUserId == userid).ToListAsync();
}
/// <summary>
/// 授权用户数据
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[SqlSugarUnitOfWork]
[HttpPost("/SysDataUserMenu/GrantData")]
public async Task GrantData(SysDataUserMenuDto input)
{
try
{
if (input.UserId == 0)
{
throw Oops.Bah("没有获取到用户id");
}
///先清理权限
await _rep.DeleteAsync(x => x.SysUserId == input.UserId);
var list = new List<SysDataUserMenu>();
input.childrens.ForEach(u =>
{
list.Add(
new SysDataUserMenu
{
SysUserId = input.UserId,
SysMenuId = u.MenuId,
DataScopeType = u.DataScopeType
});
});
await _rep.InsertAsync(list);
var userdatalist = _rep.AsQueryable().ToListAsync();
_sysCache.Set(CommonConst.CACHE_KEY_USERDATASCOPE, userdatalist);
}
catch (System.Exception)
{
throw;
}
}
/// <summary>
/// 授权用户自定义数据
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[SqlSugarUnitOfWork]
[HttpPost("/SysDataUserMenu/GrantRightData")]
public async Task GrantRightData(RightList input)
{
try
{
if (input == null)
{
throw Oops.Bah("请上传正确数据!");
}
await _sysUserDataScopeRep.DeleteAsync(x => x.SysUserId == input.UserId);
var list = new List<SysUserDataScope>();
input.childrens.ForEach(u =>
{
list.Add(
new SysUserDataScope
{
SysUserId = input.UserId,
SysMenuId = u.MenuId,
SysOrgId = u.OrgId
});
});
await _sysUserDataScopeRep.InsertAsync(list);
//var userdatalist = _rep.AsQueryable().ToListAsync();
//_sysCache.Set(CommonConst.CACHE_KEY_USERDATASCOPE, userdatalist);
}
catch (System.Exception)
{
throw;
}
}
/// <summary>
/// 获取权限集合
/// </summary>
/// <returns></returns>
public async Task<List<long>> GetDataScopeList(long menuid)
{
List<SysDataUserMenu> list = new List<SysDataUserMenu>();
List<long> datascope = new List<long>();
//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);
//}
list = await _rep.AsQueryable().ToListAsync();
_sysCache.Set(CommonConst.CACHE_KEY_USERDATASCOPE, list);
DataScopeType _dataScopeType = list.Where(x => x.SysMenuId == menuid && x.SysUserId == UserManager.UserId).Select(x => x.DataScopeType).FirstOrDefault();
if (_dataScopeType == DataScopeType.ALL)
{
datascope = null;
}
else if (_dataScopeType == DataScopeType.SELF)
{
datascope.Add(UserManager.UserId);
}
else if (_dataScopeType == DataScopeType.DEPT)
{
datascope = await _sysEmpRep.AsQueryable().InnerJoin<SysEmp>((d, t) => d.OrgId == t.OrgId && d.Id == UserManager.UserId).Select(d => d.Id).ToListAsync();
}
else if (_dataScopeType == DataScopeType.DEPT_WITH_CHILD)
{
//获取本部门
long orgid = _sysEmpRep.Where(x => x.Id == UserManager.UserId).Select(x => x.OrgId).First();
//获取下属部门及下属部门
var orglist = await _sysOrgRep.Where(x => x.TenantId == UserManager.TENANT_ID && x.Pids.Contains(orgid.ToString())).Select(x => x.Id).ToListAsync();
orglist.Add(orgid);
datascope = await _sysEmpRep.Where(x => orglist.Contains(x.OrgId)).Select(x => x.Id).ToListAsync();
}
else if (_dataScopeType == DataScopeType.DEFINE)
{
datascope = await _sysUserDataScopeRep.AsQueryable().InnerJoin<SysEmp>((d, t) => d.SysOrgId == t.OrgId && d.SysMenuId == menuid && d.SysUserId == UserManager.UserId).Select((d, t) => t.Id).ToListAsync();
}
else {
datascope.Add(UserManager.UserId);
}
return datascope;
}
}