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;
}
}
}