//
namespace SafeCampus.System;
///
///
///
public class SysPositionService : DbRepository, ISysPositionService
{
private readonly ISimpleCacheService _simpleCacheService;
private readonly ISysOrgService _sysOrgService;
private readonly IDictService _dictService;
public SysPositionService(ISimpleCacheService simpleCacheService, ISysOrgService sysOrgService, IDictService dictService)
{
_simpleCacheService = simpleCacheService;
_sysOrgService = sysOrgService;
_dictService = dictService;
}
#region 查询
///
/// 获取全部
///
///
public override async Task> GetListAsync()
{
//先从Redis拿
var sysPositions = _simpleCacheService.Get>(SystemConst.CACHE_SYS_POSITION);
if (sysPositions == null)
{
//redis没有就去数据库拿
sysPositions = await base.GetListAsync();
if (sysPositions.Count > 0)
{
//插入Redis
_simpleCacheService.Set(SystemConst.CACHE_SYS_POSITION, sysPositions);
}
}
return sysPositions;
}
///
public async Task> GetPositionListByIdList(IdListInput input)
{
var positions = await GetListAsync();
var positionList = positions.Where(it => input.IdList.Contains(it.Id)).ToList();// 获取指定ID的岗位列表
return positionList;
}
///
public async Task> Selector(PositionSelectorInput input)
{
var sysOrgList = await _sysOrgService.GetListAsync(false);//获取所有组织
var sysPositions = await GetListAsync();//获取所有职位
if (input.OrgIds != null)//根据数据范围查
{
sysOrgList = sysOrgList.Where(it => input.OrgIds.Contains(it.Id)).ToList();//在指定组织列表查询
sysPositions = sysPositions.Where(it => input.OrgIds.Contains(it.OrgId)).ToList();//在指定职位列表查询
}
var result = await ConstructPositionSelector(sysOrgList, sysPositions);//构造树
return result;
}
///
public async Task GetSysPositionById(long id)
{
var sysPositions = await GetListAsync();
var result = sysPositions.Where(it => it.Id == id).FirstOrDefault();
return result;
}
///
public async Task> Page(PositionPageInput input)
{
var orgIds = await _sysOrgService.GetOrgChildIds(input.OrgId);//获取下级组织
var query = Context.Queryable().WhereIF(input.OrgId > 0, it => orgIds.Contains(it.OrgId))//根据组织ID查询
.WhereIF(input.OrgIds != null, it => input.OrgIds.Contains(it.OrgId))//在指定组织列表查询
.WhereIF(!string.IsNullOrEmpty(input.Category), it => it.Category == input.Category)//根据分类
.WhereIF(!string.IsNullOrEmpty(input.Status), it => it.Status == input.Status)//根据状态
.WhereIF(!string.IsNullOrEmpty(input.SearchKey), it => it.Name.Contains(input.SearchKey))//根据关键字查询
.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(PositionTreeInput input)
{
var result = new List();//返回结果
var sysOrgList = await _sysOrgService.GetListAsync(false);//获取所有组织
var sysPositions = await GetListAsync();//获取所有职位
if (input.OrgIds != null)//根据数据范围查
{
sysOrgList = sysOrgList.Where(it => input.OrgIds.Contains(it.Id)).ToList();//在指定组织列表查询
sysPositions = sysPositions.Where(it => input.OrgIds.Contains(it.OrgId)).ToList();//在指定职位列表查询
}
var posCategory = await _dictService.GetChildrenByDictValue(SysDictConst.POSITION_CATEGORY);//获取职位分类
var topOrgList = sysOrgList.Where(it => it.ParentId == 0).ToList();//获取顶级组织
//遍历顶级组织
foreach (var org in topOrgList)
{
var childIds = await _sysOrgService.GetOrgChildIds(org.Id, true, sysOrgList);//获取组织下的所有子级ID
var orgPositions = sysPositions.Where(it => childIds.Contains(it.OrgId)).ToList();//获取组织下的职位
if (orgPositions.Count == 0) continue;
var positionTreeOutput = new PositionTreeOutput
{
Id = org.Id,
Name = org.Name,
IsPosition = false
};//实例化组织树
//获取组织下的职位职位分类
foreach (var category in posCategory)
{
var id = CommonUtils.GetSingleId();//生成唯一ID临时用,因为前端需要ID
var categoryTreeOutput = new PositionTreeOutput
{
Id = id,
Name = category.DictLabel,
IsPosition = false
};//实例化职位分类树
var positions = orgPositions.Where(it => it.Category == category.DictValue).ToList();//获取职位分类下的职位
//遍历职位,实例化职位树
positions.ForEach(it =>
{
categoryTreeOutput.Children.Add(new PositionTreeOutput()
{
Id = it.Id,
Name = it.Name,
IsPosition = true
});//添加职位
});
positionTreeOutput.Children.Add(categoryTreeOutput);
}
result.Add(positionTreeOutput);
}
return result;
}
///
public async Task Detail(BaseIdInput input)
{
return await GetSysPositionById(input.Id);
}
#endregion
#region 编辑
///
public async Task Edit(PositionEditInput input, string name = SystemConst.SYS_POS)
{
await CheckInput(input, name);//检查参数
var sysPosition = input.Adapt();//实体转换
if (await UpdateAsync(sysPosition))//更新数据
await RefreshCache();//刷新缓存
}
///
public async Task RefreshCache()
{
_simpleCacheService.Remove(SystemConst.CACHE_SYS_POSITION);//删除缓存
await GetListAsync();//重新写入缓存
}
#endregion
#region 新增
///
public async Task Add(PositionAddInput input, string name = SystemConst.SYS_POS)
{
await CheckInput(input, name);//检查参数
var sysPosition = input.Adapt();//实体转换
if (await InsertAsync(sysPosition))//插入数据
await RefreshCache();//刷新缓存
}
#endregion
#region 删除
///
public async Task Delete(BaseIdListInput input, string name = SystemConst.SYS_POS)
{
//获取所有ID
var ids = input.Ids;
if (ids.Count > 0)
{
//如果组织下有用户则不能删除
if (await Context.Queryable().AnyAsync(it => ids.Contains(it.PositionId)))
{
throw Oops.Bah($"请先删除{name}下的用户");
}
//删除职位
if (await DeleteByIdsAsync(ids.Cast