using OpenAuth.Repository.Domain; using SqlSugar; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace OpenAuth.App.BasicQueryService { public class PositionManager { ISqlSugarClient client; public PositionManager(ISqlSugarClient client) { this.client = client; } private async Task HasRelation(string beginId, Dictionary map) { bool res = false; var entity = await client.Queryable().Where(a => a.Id.ToString() == beginId).FirstAsync(); if (entity == null || entity.ParentId.ToString() == "0") { res = false; } else if (map.ContainsKey(entity.ParentId.ToString())) { res = true; } else { res = await HasRelation(entity.ParentId.ToString(), map); } return res; } /// /// 判断是否是上级 /// /// 自己的岗位 /// 对方的岗位 /// public async Task IsUp(List myIds, List otherIds) { bool res = false; if (myIds.Count > 0 && otherIds.Count > 0) { Dictionary map = new Dictionary(); foreach (var otherItem in myIds) { if (!map.ContainsKey(otherItem)) { map.Add(otherItem, 1); } } foreach (var myItem in otherIds) { if (await HasRelation(myItem, map)) { res = true; break; } } } return res; } /// /// 判断是否是下级 /// /// 自己的岗位 /// 对方的岗位 /// public async Task IsDown(List myIds, List otherIds) { bool res = false; if (myIds.Count > 0 && otherIds.Count > 0) { Dictionary map = new Dictionary(); foreach (var myItem in myIds) { if (!map.ContainsKey(myItem)) { map.Add(myItem, 1); } } foreach (var otherItem in otherIds) { if (await HasRelation(otherItem, map)) { res = true; break; } } } return res; } /// /// 获取上级岗位人员ID /// /// 岗位 /// 级数 /// public async Task> GetUpIdList(List postIds, int level) { List res = new List(); if (postIds.Count > 0 && level > 0 && level < 6) {// 现在支持1-5级查找 bool isHave = false; // 判断是否指定级数的职位 foreach (var postId in postIds) { isHave = false; var entity = await client.Queryable().Where(a => a.Id.ToString() == postId).FirstAsync(); if (entity != null) { string parentId = entity.ParentId.ToString(); SysPosition parentEntity = null; for (int i = 0; i < level; i++) { parentEntity = await client.Queryable().Where(a => a.Id.ToString() == parentId).FirstAsync(); if (parentEntity != null) { parentId = parentEntity.ParentId.ToString(); if (i == (level - 1)) { isHave = true; } } else { break; } } if (isHave) { if (parentEntity != null) { res.Add(parentEntity.Id.ToString()); } } } } } return res; } /// /// 获取下级岗位人员ID /// /// 岗位 /// 级数 /// public async Task> GetDownIdList(List postIds, int level) { List res = new List(); if (postIds.Count > 0 && level > 0 && level < 6) {// 现在支持1-5级查找 bool isHave = false; // 判断是否指定级数的职位 List parentList = new List(); parentList.AddRange(postIds); for (int i = 0; i < level; i++) { parentList = (List)await client.Queryable().Where(a => parentList.Contains(a.ParentId.ToString())).Select(a => a.Id.ToString()).ToListAsync(); if (parentList.Count > 0) { if (i == (level - 1)) { isHave = true; } } else { break; } } if (isHave) { res.AddRange(parentList); } } return res; } } }