//
using System.Diagnostics;
namespace SafeCampus.System;
///
public class SysOrgService : DbRepository, ISysOrgService
{
private readonly ILogger _logger;
private readonly ISimpleCacheService _simpleCacheService;
private readonly IEventPublisher _eventPublisher;
public SysOrgService(ILogger logger, ISimpleCacheService simpleCacheService, IEventPublisher eventPublisher)
{
_logger = logger;
_simpleCacheService = simpleCacheService;
_eventPublisher = eventPublisher;
}
#region 查询
///
public async Task> GetListAsync(bool showDisabled = true)
{
var sysOrgList = _simpleCacheService.Get>(SystemConst.CACHE_SYS_ORG);
if (sysOrgList == null)
{
//redis没有就去数据库拿
sysOrgList = await base.GetListAsync();
if (sysOrgList.Count > 0)
{
//插入Redis
_simpleCacheService.Set(SystemConst.CACHE_SYS_ORG, sysOrgList);
}
}
if (!showDisabled)
{
sysOrgList = sysOrgList.Where(it => it.Status == CommonStatusConst.ENABLE).ToList();
}
return sysOrgList;
}
///
public async Task GetSysOrgById(long id)
{
var sysOrg = await GetListAsync();
var result = sysOrg.Where(it => it.Id == id).FirstOrDefault();
return result;
}
///
public async Task> GetChildListById(long orgId, bool isContainOneself = true, List sysOrgList = null)
{
//获取所有组织
sysOrgList ??= await GetListAsync();
//查找下级
var childList = GetSysOrgChildren(sysOrgList, orgId);
if (isContainOneself)//如果包含自己
{
//获取自己的组织信息
var self = sysOrgList.Where(it => it.Id == orgId).FirstOrDefault();
if (self != null) childList.Insert(0, self);//如果组织不为空就插到第一个
}
return childList;
}
///
public async Task> GetOrgChildIds(long orgId, bool isContainOneself = true, List sysOrgList = null)
{
var orgIds = new List();//组织列表
if (orgId > 0)//如果orgId有值
{
//获取所有子集
var childList = await GetChildListById(orgId, isContainOneself, sysOrgList);
orgIds = childList.Select(x => x.Id).ToList();//提取ID列表
}
return orgIds;
}
///
public async Task> Page(SysOrgPageInput input)
{
var query = Context.Queryable()
.WhereIF(input.ParentId > 0,
it => it.ParentId == input.ParentId || it.Id == input.ParentId || SqlFunc.JsonLike(it.ParentIdList, input.ParentId.ToString()))//父级
.WhereIF(!string.IsNullOrEmpty(input.Name), it => it.Name.Contains(input.Name))//根据名称查询
.WhereIF(!string.IsNullOrEmpty(input.Category), it => it.Category == input.Category)//根据分类查询
.WhereIF(!string.IsNullOrEmpty(input.Code), it => it.Code.Contains(input.Code))//根据编码查询
.WhereIF(!string.IsNullOrEmpty(input.Status), it => it.Status == input.Status)//根据状态查询
.OrderByIF(!string.IsNullOrEmpty(input.SortField), $"{input.SortField} {input.SortOrder}").OrderBy(it => it.SortCode)
.OrderBy(it => it.CreateTime);//排序
var pageInfo = await query.ToPagedListAsync(input.PageNum, input.PageSize);//分页
return pageInfo;
}
///
public async Task> Tree(List orgIds = null, SysOrgTreeInput treeInput = null)
{
long parentId = SafeCampusConst.ZERO;//父级ID
//获取所有组织
var sysOrgList = await GetListAsync(false);
if (orgIds != null)
sysOrgList = GetParentListByIds(sysOrgList, orgIds);//如果组织ID不为空则获取组织ID列表的所有父节点
//如果选择器ID不为空则表示是懒加载,只加载子节点
if (treeInput != null && treeInput.ParentId != null)
{
parentId = treeInput.ParentId.Value;
sysOrgList = GetSysOrgChildrenLazy(sysOrgList, treeInput.ParentId.Value);//获取懒加载下级
}
sysOrgList = sysOrgList.OrderBy(it => it.SortCode).ToList();//排序
//构建组织树
var result = ConstructOrgTrees(sysOrgList, parentId);
return result;
}
///
public async Task Detail(BaseIdInput input)
{
var sysOrgList = await GetListAsync();
var orgDetail = sysOrgList.Where(it => it.Id == input.Id).FirstOrDefault();
if (orgDetail.DirectorId != null)
{
//获取主管信息
orgDetail.DirectorInfo = await Tenant.QueryableWithAttr().Where(it => it.Id == orgDetail.DirectorId).Select(it =>
new UserSelectorOutPut
{
Id = it.Id,
Name = it.Name,
Account = it.Account
}).FirstAsync();
}
return orgDetail;
}
///
public List GetOrgParents(List allOrgList, long orgId, bool includeSelf = true)
{
//找到组织
var sysOrgList = allOrgList.Where(it => it.Id == orgId).FirstOrDefault();
if (sysOrgList != null)//如果组织不为空
{
var data = new List();
var parents = GetOrgParents(allOrgList, sysOrgList.ParentId, includeSelf);//递归获取父节点
data.AddRange(parents);//添加父节点;
if (includeSelf)
data.Add(sysOrgList);//添加到列表
return data;//返回结果
}
return new List();
}
///
public bool IsExistOrgByName(List sysOrgList, string orgName, long parentId,
out long orgId)
{
orgId = 0;
var sysOrg = sysOrgList.Where(it => it.ParentId == parentId && it.Name == orgName).FirstOrDefault();
if (sysOrg != null)
{
orgId = sysOrg.Id;
return true;
}
return false;
}
///
public async Task> GetOrgListByIdList(IdListInput input)
{
var sysOrgList = await GetListAsync();
var orgList = sysOrgList.Where(it => input.IdList.Contains(it.Id)).ToList();// 获取指定ID的组织列表
return orgList;
}
///
public async Task> GetTenantList()
{
var tenantList = _simpleCacheService.Get>(SystemConst.CACHE_SYS_TENANT);
if (tenantList == null)
{
var orgList = await GetListAsync(false);
tenantList = orgList.Where(it => it.Category == CateGoryConst.ORG_COMPANY).ToList();
if (tenantList.Count > 0)
{
//插入Redis
_simpleCacheService.Set(SystemConst.CACHE_SYS_TENANT, tenantList);
}
}
return tenantList;
}
///
public async Task GetTenantIdByOrgId(long orgId, List sysOrgList = null)
{
//先从缓存拿租户Id
var tenantId = _simpleCacheService.HashGetOne(SystemConst.CACHE_SYS_ORG_TENANT, orgId.ToString());
if (tenantId == null)
{
//获取所有组织
sysOrgList ??= await GetListAsync();
var userOrg = sysOrgList.FirstOrDefault(it => it.Id == orgId);
if (userOrg != null)
{
//如果是公司直接返回
if (userOrg.Category == CateGoryConst.ORG_COMPANY)
{
tenantId = userOrg.Id;
}
else
{
var parentIds = userOrg.ParentIdList;//获取父级ID列表
//从最后一个往前遍历,取第一个公司ID为租户ID
for (var i = parentIds.Count - 1; i >= 0; i--)
{
var parentId = parentIds[i];
var org = sysOrgList.FirstOrDefault(it => it.Id == parentId);
if (org.Category == CateGoryConst.ORG_COMPANY)
{
tenantId = org.Id;//租户ID
break;
}
}
}
if (tenantId != null)
_simpleCacheService.HashAdd(SystemConst.CACHE_SYS_ORG_TENANT, orgId.ToString(), tenantId);//插入缓存
}
}
return tenantId;
}
#endregion 查询
#region 新增
///
public async Task Add(SysOrgAddInput input, string name = SystemConst.SYS_ORG)
{
await CheckInput(input, name);//检查参数
var sysOrg = input.Adapt();//实体转换
if (await InsertAsync(sysOrg))//插入数据
await RefreshCache();//刷新缓存
}
///
public async Task Copy(SysOrgCopyInput input)
{
var orgList = await GetListAsync();//获取所有
var positionList = await Context.Queryable().ToListAsync();//获取所有职位
var ids = new HashSet();//定义不重复Id集合
var addOrgList = new List();//添加机构列表
var addPositionList = new List();//添加职位列表
var alreadyIds = new HashSet();//定义已经复制过得组织Id
ids.AddRange(input.Ids);//加到集合
if (ids.Contains(input.TargetId))
throw Oops.Bah("不能包含自己");
//获取目标组织
var target = orgList.Where(it => it.Id == input.TargetId).FirstOrDefault();
if (target != null)
{
//需要复制的组织名称列表
var orgNames = orgList.Where(it => ids.Contains(it.Id)).Select(it => it.Name).ToList();
//目标组织的一级子组织名称列表
var targetChildNames = orgList.Where(it => it.ParentId == input.TargetId).Select(it => it.Name).ToList();
orgNames.ForEach(it =>
{
if (targetChildNames.Contains(it)) throw Oops.Bah($"已存在{it}");
});
foreach (var id in input.Ids)
{
var org = orgList.Where(o => o.Id == id).FirstOrDefault();//获取组织
if (org != null && !alreadyIds.Contains(id))
{
alreadyIds.Add(id);//添加到已复制列表
RedirectOrg(org);//生成新的实体
org.ParentId = input.TargetId;//父id为目标Id
addOrgList.Add(org);
//是否包含职位
if (input.ContainsPosition)
{
var positions = positionList.Where(p => p.OrgId == id).ToList();//获取机构下的职位
positions.ForEach(p =>
{
p.OrgId = org.Id;//赋值新的机构ID
p.Id = CommonUtils.GetSingleId();//生成新的ID
p.Code = RandomHelper.CreateRandomString(10);//生成新的Code
addPositionList.Add(p);//添加到职位列表
});
}
//是否包含下级
if (input.ContainsChild)
{
var childIds = await GetOrgChildIds(id, false);//获取下级id列表
alreadyIds.AddRange(childIds);//添加到已复制id
var childList = orgList.Where(c => childIds.Contains(c.Id)).ToList();//获取下级
var sysOrgChildren = CopySysOrgChildren(childList, id, org.Id, input.ContainsPosition,
positionList);//赋值下级组织
addOrgList.AddRange(sysOrgChildren.Item1);//添加到组织列表
addPositionList.AddRange(sysOrgChildren.Item2);//添加到职位列表
}
}
}
orgList.AddRange(addOrgList);//要添加的组织添加到组织列表
//遍历机构重新赋值全称和父Id列表
addOrgList.ForEach(it =>
{
it.Names = it.Name;
if (it.ParentId != SafeCampusConst.ZERO)
{
GetNames(orgList, it.ParentId, it.Name, out var names,
out var parentIdList);
it.Names = names;
it.ParentIdList = parentIdList;
}
});
//事务
var result = await Tenant.UseTranAsync(async () =>
{
await InsertRangeAsync(addOrgList);//插入组织
if (addPositionList.Count > 0)
{
await Context.Insertable(addPositionList).ExecuteCommandAsync();
}
});
if (result.IsSuccess)//如果成功了
{
await RefreshCache();//刷新缓存
if (addPositionList.Count > 0) _simpleCacheService.Remove(SystemConst.CACHE_SYS_POSITION);//删除机构缓存
}
else
{
_logger.LogError(result.ErrorException, result.ErrorMessage);
throw Oops.Oh($"复制失败");
}
}
}
#endregion 新增
#region 编辑
///
public async Task Edit(SysOrgEditInput input, string name = SystemConst.SYS_ORG)
{
await CheckInput(input, name);//检查参数
var sysOrg = input.Adapt();//实体转换
if (await UpdateAsync(sysOrg))//更新数据
{
//如果状态为禁用
if (sysOrg.Status == CommonStatusConst.DISABLED)
{
var orgIds = await GetOrgChildIds(sysOrg.Id, true);//获取所有下级
await _eventPublisher.PublishAsync(EventSubscriberConst.CLEAR_ORG_TOKEN, orgIds);//清除角色下用户缓存
}
await RefreshCache();//刷新缓存
}
}
#endregion 编辑
#region 删除
///
public async Task Delete(BaseIdListInput input, string name = SystemConst.SYS_ORG)
{
//获取所有ID
var ids = input.Ids;
if (ids.Count > 0)
{
var sysOrgList = await GetListAsync();//获取所有组织
var sysDeleteOrgList = new List();//需要删除的组织ID集合
ids.ForEach(it =>
{
var children = GetSysOrgChildren(sysOrgList, it);//查找下级组织
sysDeleteOrgList.AddRange(children.Select(it => it.Id).ToList());
sysDeleteOrgList.Add(it);
});
//如果组织下有用户则不能删除
if (await Context.Queryable().AnyAsync(it => sysDeleteOrgList.Contains(it.OrgId)))
{
throw Oops.Bah($"请先删除{name}下的用户");
}
//判断组织下是否有角色
var hasRole = await Context.Queryable().Where(it => sysDeleteOrgList.Contains(it.OrgId.Value)).CountAsync() > 0;
if (hasRole) throw Oops.Bah($"请先删除{name}下的角色");
// 判断组织下是否有职位
var hasPosition = await Context.Queryable().Where(it => sysDeleteOrgList.Contains(it.OrgId)).CountAsync() > 0;
if (hasPosition) throw Oops.Bah($"请先删除{name}下的职位");
//删除组织
if (await DeleteByIdsAsync(sysDeleteOrgList.Cast