You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

145 lines
5.4 KiB

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