using GDZZ.Core; using Furion.DependencyInjection; using Furion.DynamicApiController; using Mapster; using Microsoft.AspNetCore.Mvc; using SqlSugar; using System.Linq; using System.Threading.Tasks; using GDZZ.Application.Entity; using System.Collections.Generic; using System; using GDZZ.Core.Service; using Furion.FriendlyException; using GDZZ.Core.Entity; using Furion; namespace GDZZ.Application { /// /// 职位管理服务 /// [ApiDescriptionSettings("Application", Name = "Position", Order = 1)] public class PositionService : IPositionService, IDynamicApiController, ITransient { private readonly SqlSugarRepository _rep; //缓存机制 private readonly ISysCacheService sysCacheService; public PositionService(SqlSugarRepository rep, ISysCacheService sysCacheService) { this._rep = rep; this.sysCacheService = sysCacheService; } /// /// 分页查询组织机构 /// /// /// [HttpGet("/Position/page")] public async Task QueryPage([FromQuery] PageOrgInput input) { var dataScopeList = GetDataScopeList(await DataFilterExtensions.GetDataScopeIdList(FilterType.Org)); var orgs = await this._rep.AsQueryable() .WhereIF(!string.IsNullOrWhiteSpace(input.Name), u => u.PositionName.Contains(input.Name.Trim())) .WhereIF(!string.IsNullOrWhiteSpace(input.Id), u => u.Id == long.Parse(input.Id.Trim())) .WhereIF(!string.IsNullOrWhiteSpace(input.Pid), u => u.Pids.Contains(input.Pid.Trim()) || u.Id == long.Parse(input.Pid.Trim())) .WhereIF(dataScopeList.Any(), u => dataScopeList.Contains(u.Id)) // 非管理员范围限制 .Where(u => u.Status != CommonStatus.DELETED) .OrderBy(u => u.Sort) .Select() .ToPagedListAsync(input.PageNo, input.PageSize); return orgs.XnPagedResult(); } /// /// 获取组织机构树 /// /// [HttpGet("/Position/tree")] public async Task GetOrgTree([FromQuery] OrgInput input) { var dataScopeList = new List(); //if (!UserManager.IsSuperAdmin && !UserManager.IsTenantAdmin) //{ // var dataScopes = await DataFilterExtensions.GetDataScopeIdList(FilterType.Org); // if (dataScopes.Count < 1) // return dataScopeList; // dataScopeList = GetDataScopeList(dataScopes); //} var orgs = await this._rep.Where(dataScopeList.Any(), u => dataScopeList.Contains(u.Id)) .Where(u => u.Status == (int)CommonStatus.ENABLE).OrderBy(u => u.Sort) .Select(u => new OrgTreeNode { Id = u.Id, ParentId = u.ParentID, Title = u.PositionName, Value = u.Id.ToString(), Weight = u.Sort }).ToListAsync(); return new TreeBuildUtil().DoTreeBuild(orgs); } /// /// 删除组织机构 /// /// /// [HttpPost("/Position/delete")] public async Task Delete(DeleteOrgInput input) { var sysOrg = await this._rep.FirstOrDefaultAsync(u => u.Id == long.Parse(input.Id)); // 级联删除子节点 var childIdList = await GetChildIdListWithSelfById(sysOrg.Id); try { _rep.CurrentBeginTran(); // 级联删除该机构及子机构对应的角色-数据范围关联信息 await _rep.Change().DeleteAsync(u => childIdList.Contains(u.SysOrgId)); // 级联删除该机构子机构对应的用户-数据范围关联信息 await _rep.Change().DeleteAsync(u => childIdList.Contains(u.SysOrgId)); await _rep.DeleteAsync(u => childIdList.Contains(u.Id)); _rep.CurrentCommitTran(); } catch (System.Exception) { _rep.CurrentRollbackTran(); throw; } await this.sysCacheService.DelByPatternAsync(CommonConst.CACHE_KEY_DATASCOPE); await this.sysCacheService.DelByPatternAsync(CommonConst.CACHE_KEY_USERSDATASCOPE); } /// /// 增加组织机构 /// /// /// [HttpPost("/Position/add")] public async Task Add (AddPositionInput input) { var isExist = await this._rep.AnyAsync(u => u.PositionName == input.PositionName || u.Code == input.Code); if (isExist) throw Oops.Oh(ErrorCode.D2002); var sysOrg = input.Adapt(); await FillPids(sysOrg); var newOrg = await this._rep.InsertReturnEntityAsync(sysOrg); //// 当前用户不是超级管理员时,将新增的公司加到用户的数据权限 //if (App.User.FindFirst(ClaimConst.CLAINM_SUPERADMIN)?.Value != ((int)AdminType.SuperAdmin).ToString()) //{ // var userId = App.User.FindFirst(ClaimConst.CLAINM_USERID)?.Value; // await this.sysCacheService.DelByPatternAsync(CommonConst.CACHE_KEY_DATASCOPE); // await this.sysCacheService.DelByPatternAsync(CommonConst.CACHE_KEY_USERSDATASCOPE); //} } /// /// 获取职位管理列表 /// /// /// [HttpGet("/Position/list")] public async Task> List([FromQuery] PositionInput input) {; var allProvinces = await _rep.AsQueryable().ToListAsync(); //先查询 省级别 var JobList = allProvinces.Where(x => x.PositionLevel == PositionEnum.JobList).ToList().Adapt>(); foreach (var province in JobList) { province.Children = allProvinces.Where(r => r.PositionLevel == PositionEnum.Subitem && r.ParentID == province.ID).ToList().Adapt>(); foreach (var city in province.Children) { city.Children = allProvinces.Where(r => r.PositionLevel == PositionEnum.Grandson && r.ParentID == city.ID).ToList().Adapt>()??null; } } return JobList; } #region 私有方法 /// /// 根据节点Id获取所有子节点Id集合,包含自己 /// /// /// private async Task> GetChildIdListWithSelfById(long id) { var childIdList = await this._rep .Where(u => u.Pids.Contains(id.ToString())) .Select(u => u.Id) .ToListAsync(); childIdList.Add(id); return childIdList; } /// /// (非管理员)获取当前用户数据范围(机构Id) /// /// /// private List GetDataScopeList(List dataScopes) { var dataScopeList = new List(); // 如果是超级管理员则获取所有组织机构,否则只获取其数据范围的机构数据 if (!UserManager.IsSuperAdmin && !UserManager.IsTenantAdmin) { if (dataScopes.Count < 1) return dataScopeList; // 此处获取所有的上级节点,用于构造完整树 dataScopes.ForEach(u => { var sysOrg = this._rep.FirstOrDefault(c => c.Id == u); var parentAndChildIdListWithSelf = sysOrg.Pids.TrimEnd(',').Replace("[", "").Replace("]", "") .Split(",").Select(u => long.Parse(u)).ToList(); parentAndChildIdListWithSelf.Add(sysOrg.Id); dataScopeList.AddRange(parentAndChildIdListWithSelf); }); } return dataScopeList; } /// /// 填充父Ids字段 /// /// /// private async Task FillPids(Position sysOrg) { if (sysOrg.ParentID == 0L) { sysOrg.Pids = "[" + 0 + "],"; } else { var t = await this._rep.FirstOrDefaultAsync(u => u.Id == sysOrg.ParentID); sysOrg.Pids = t.Pids + "[" + t.Id + "],"; } } #endregion } }