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