You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

247 lines
9.7 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

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
{
/// <summary>
/// 职位管理服务
/// </summary>
[ApiDescriptionSettings("Application", Name = "Position", Order = 1)]
public class PositionService : IPositionService, IDynamicApiController, ITransient
{
private readonly SqlSugarRepository<Position> _rep;
//缓存机制
private readonly ISysCacheService sysCacheService;
public PositionService(SqlSugarRepository<Position> rep,
ISysCacheService sysCacheService)
{
this._rep = rep;
this.sysCacheService = sysCacheService;
}
/// <summary>
/// 分页查询组织机构
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpGet("/Position/page")]
public async Task<dynamic> 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<PositionOutput>()
.ToPagedListAsync(input.PageNo, input.PageSize);
return orgs.XnPagedResult();
}
/// <summary>
/// 获取组织机构树
/// </summary>
/// <returns></returns>
[HttpGet("/Position/tree")]
public async Task<dynamic> GetOrgTree([FromQuery] OrgInput input)
{
var dataScopeList = new List<long>();
//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<OrgTreeNode>().DoTreeBuild(orgs);
}
/// <summary>
/// 删除组织机构
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[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<SysRoleDataScope>().DeleteAsync(u => childIdList.Contains(u.SysOrgId));
// 级联删除该机构子机构对应的用户-数据范围关联信息
await _rep.Change<SysUserDataScope>().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);
}
/// <summary>
/// 增加组织机构
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[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<Position>();
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);
//}
}
/// <summary>
/// 获取职位管理列表
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpGet("/Position/list")]
public async Task<List<PositionOutput>> List([FromQuery] PositionInput input)
{;
var allProvinces = await _rep.AsQueryable().ToListAsync();
//先查询 省级别
var JobList = allProvinces.Where(x => x.PositionLevel == PositionEnum.JobList).ToList().Adapt<List<PositionOutput>>();
foreach (var province in JobList)
{
province.Children = allProvinces.Where(r => r.PositionLevel == PositionEnum.Subitem && r.ParentID == province.ID).ToList().Adapt<List<PositionOutput>>();
foreach (var city in province.Children)
{
city.Children = allProvinces.Where(r => r.PositionLevel == PositionEnum.Grandson && r.ParentID == city.ID).ToList().Adapt<List<PositionOutput>>()??null;
}
}
return JobList;
}
#region 私有方法
/// <summary>
/// 根据节点Id获取所有子节点Id集合包含自己
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
private async Task<List<long>> 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;
}
/// <summary>
/// (非管理员)获取当前用户数据范围机构Id
/// </summary>
/// <param name="dataScopes"></param>
/// <returns></returns>
private List<long> GetDataScopeList(List<long> dataScopes)
{
var dataScopeList = new List<long>();
// 如果是超级管理员则获取所有组织机构,否则只获取其数据范围的机构数据
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;
}
/// <summary>
/// 填充父Ids字段
/// </summary>
/// <param name="sysOrg"></param>
/// <returns></returns>
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
}
}