using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Znyc.Admin.Commons.Cache; using Znyc.Admin.Commons.Mapping; using Znyc.Admin.Commons.Pages; using Znyc.Admin.Commons.Services; using Znyc.Admin.Security.Dtos; using Znyc.Admin.Security.Entitys; using Znyc.Admin.Security.IRepositories; using Znyc.Admin.Security.IServices; namespace Znyc.Admin.Security.Services { public class DictionaryService : BaseService, IDictionaryService { private readonly IDictionaryRepository _repository; public DictionaryService(IDictionaryRepository repository) : base(repository) { _repository = repository; } /// /// 获取功能菜单适用于Vue 树形列表 /// /// public async Task> GetAllItemsTreeTable() { List reslist = new List(); IEnumerable elist = await _repository.GetListWhereAsync("1=1"); List list = elist.OrderBy(t => t.Sort).ToList(); List oneMenuList = list.FindAll(t => t.ParentId == 0); foreach (Dictionary item in oneMenuList) { DictionaryOutputDto menuTreeTableOutputDto = new DictionaryOutputDto(); menuTreeTableOutputDto = item.MapTo(); // menuTreeTableOutputDto.Children = GetSubMenus(list, item.Id).ToList(); reslist.Add(menuTreeTableOutputDto); } return reslist; } /// /// 根据编码查询字典分类 /// /// /// 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); } /// /// 获取子菜单,递归调用 /// /// /// 父级Id /// private List GetSubMenus(List data, long ParentId) { List list = new List(); DictionaryOutputDto menuTreeTableOutputDto = new DictionaryOutputDto(); List ChilList = data.FindAll(t => t.ParentId == ParentId); foreach (Dictionary entity in ChilList) { menuTreeTableOutputDto = entity.MapTo(); //menuTreeTableOutputDto.Children = // GetSubMenus(data, entity.Id).OrderBy(t => t.Sort); list.Add(menuTreeTableOutputDto); } return list; } /// /// 根据条件查询数据字典列表 /// /// /// /// public async Task> GetDictionaryListByWhere(string ids, string code) { CacheHelper cacheHelper = new CacheHelper(); List list = cacheHelper.Get>("dictionary:list"); if (list == null) { list = await SyncDictionaryCache(); } string[] idsInput = { }; if (!string.IsNullOrEmpty(ids)) { idsInput = ids.Split(","); } if (!string.IsNullOrEmpty(code)) { list = list.Where(x => x.Code == code).ToList(); } if (!string.IsNullOrEmpty(ids) && !string.IsNullOrEmpty(code)) { list = list.Where(x => idsInput.Contains(x.Id.ToString()) && x.Code == code).ToList(); } return list; } /// /// 查询单条数据字典 /// /// /// public async Task GetDictionaryById(long id) { CacheHelper cacheHelper = new CacheHelper(); List list = cacheHelper.Get>("dcdictionary:list"); if (list == null) { list = await SyncDictionaryCache(); } DictionaryOutputDto result = list.Find(x => x.Id == id); return result; } /// /// 异步分页查询 /// /// /// public async Task> FindWithPagerSearchAsync(SearchUserModel search) { bool order = search.Order == "asc" ? false : true; string where = GetDataPrivilege(false); if (!string.IsNullOrEmpty(search.Keywords)) { @where += string.Format( " and ( Name like '%{0}%' or Value like '%{0}%' or Description like '%{0}%')", search.Keywords); } if (!string.IsNullOrEmpty(search.StartTime)) { @where += " and ModifiedTime >='" + search.StartTime + " 00:00:00'"; } if (!string.IsNullOrEmpty(search.EndTime)) { @where += " and ModifiedTime <='" + search.EndTime + " 23:59:59'"; } PagerInfo pagerInfo = new PagerInfo { CurrenetPageIndex = search.CurrenetPageIndex, PageSize = search.PageSize }; List list = await repository.FindWithPagerAsync(where, pagerInfo, search.Sort, order); List listDto = list.MapTo(); PageResult pageResult = new PageResult { CurrentPage = pagerInfo.CurrenetPageIndex, Items = listDto, ItemsPerPage = pagerInfo.PageSize, TotalItems = pagerInfo.RecordCount }; return pageResult; } /// /// 同步缓存 /// /// public async Task> SyncDictionaryCache() { CacheHelper cacheHelper = new CacheHelper(); IEnumerable list = await repository.GetListWhereAsync(" IsDeleted=0 and IsEnabled=1 order by Sort"); List dicList = list.MapTo(); cacheHelper.Add("dcdictionary:list", dicList, TimeSpan.FromDays(30)); return dicList; } } }