//
namespace SafeCampus.System;
///
///
///
public class DictService : DbRepository, IDictService
{
private readonly ISimpleCacheService _simpleCacheService;
public DictService(ISimpleCacheService simpleCacheService)
{
_simpleCacheService = simpleCacheService;
}
///
public async Task> Page(DictPageInput input)
{
var query = Context.Queryable().Where(it => it.Category == input.Category)//根据分类查询
.Where(it => it.ParentId == input.ParentId)//根据父ID查询
.WhereIF(!string.IsNullOrEmpty(input.SearchKey),
it => it.DictLabel.Contains(input.SearchKey) || it.DictValue.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 Add(DictAddInput input)
{
await CheckInput(input);//检查参数
var devDict = input.Adapt();//实体转换
if (await InsertAsync(devDict))//插入数据
await RefreshCache();//刷新缓存
}
///
public async Task Edit(DictAddInput input)
{
await CheckInput(input);//检查参数
var devDict = input.Adapt();//实体转换
if (await UpdateAsync(devDict))//更新数据
await RefreshCache();//刷新缓存
}
///
public async Task Delete(DictDeleteInput input)
{
//获取所有ID
var ids = input.Ids;
if (ids.Count > 0)
{
//获取所有字典
var dictList = await GetListAsync();
//判断是否有系统字典
var frm = dictList.Any(it => ids.Contains(it.Id) && it.Category == CateGoryConst.DICT_FRM);
//如果是系统字典提示不可删除
if (frm) throw Oops.Bah("不可删除系统内置字典");
var deleteIds = new List();//要删除的id列表
deleteIds.AddRange(ids);//
ids.ForEach(it =>
{
//获取子节点
var children = GetDevDictChildren(dictList, it);
//提取ID
var childrenIds = children.Select(c => c.Id).ToList();
deleteIds.AddRange(childrenIds);
});
//删除数据
if (await DeleteByIdsAsync(deleteIds.Cast