using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Znyc.Recruitment.Admin.Commons.Cache; using Znyc.Recruitment.Admin.Commons.Mapping; using Znyc.Recruitment.Admin.Commons.Services; using Znyc.Recruitment.Admin.Security.Dtos; using Znyc.Recruitment.Admin.Security.Entitys; using Znyc.Recruitment.Admin.Security.IRepositories; using Znyc.Recruitment.Admin.Security.IServices; namespace Znyc.Recruitment.Admin.Security.Services { public class DictionaryService : BaseService, IDictionaryService { private readonly IDictionaryRepository _repository; public DictionaryService(IDictionaryRepository repository) : base(repository) { _repository = repository; } /// /// 获取功能菜单适用于Vue 树形列表 /// /// public async Task> GetAllDictionaryTreeTable() { List reslist = new List(); IEnumerable diclist = await _repository.GetListWhereAsync(" IsDeleted = 0"); List list = diclist.OrderBy(t => t.Sort).ToList(); List oneDictionaryList = list.FindAll(t => t.ParentId == 0); foreach (DictionaryEntity item in oneDictionaryList) { DictionaryOutputDto dicTreeTableOutput = new DictionaryOutputDto(); dicTreeTableOutput = item.MapTo(); dicTreeTableOutput.Children = GetSubDictionary(list, item.Id).ToList(); reslist.Add(dicTreeTableOutput); } await SyncDictionaryCache(); return reslist; } /// /// 获取子集,递归调用 /// /// /// 父级Id /// private List GetSubDictionary(List data, long ParentId) { List list = new List(); DictionaryOutputDto dictionaryOutputDto = new DictionaryOutputDto(); List ChilList = data.FindAll(t => t.ParentId == ParentId); foreach (DictionaryEntity entity in ChilList) { dictionaryOutputDto = entity.MapTo(); dictionaryOutputDto.Children = GetSubDictionary(data, entity.Id).OrderBy(t => t.Sort) .MapTo(); list.Add(dictionaryOutputDto); } return list; } /// /// 根据编码查询字典分类 /// /// /// public async Task GetByEnCodAsynce(string enCode) { return await _repository.GetByEnCodAsynce(enCode); } /// /// 更新时判断分类编码是否存在(排除自己) /// /// 分类编码主键Id /// public async Task GetByEnCodAsynce(string enCode, long id) { return await _repository.GetByEnCodAsynce(enCode, id); } /// /// 查询单条数据字典 /// /// /// public async Task GetDictionaryById(long id) { DictionaryOutputDto result = (await _repository.GetSingleOrDefaultAsync(x => x.Id == id && x.IsDeleted == false)).MapTo(); return result; } /// /// 根据ParentId查询数据字典列表 /// /// /// public async Task> GetListByPidAsync(long pid) { List list = await SyncDictionaryCache(); list = list.Where(x => x.ParentId == pid).ToList(); return list; } /// /// 同步缓存 /// /// public async Task> SyncDictionaryCache() { CacheHelper cacheHelper = new CacheHelper(); List list = cacheHelper.Get>("dictionary:list"); if (list == null || list.Count < 1) { IEnumerable dicList = await repository.GetListWhereAsync(" IsDeleted=0 and IsEnabled=1 order by Sort"); list = dicList.MapTo(); cacheHelper.Add("dictionary:list", list, TimeSpan.FromDays(30)); } return list; } } }