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.
 
 
 

195 lines
7.2 KiB

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<Dictionary, 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>> GetAllItemsTreeTable()
{
List<DictionaryOutputDto> reslist = new List<DictionaryOutputDto>();
IEnumerable<Dictionary> elist = await _repository.GetListWhereAsync("1=1");
List<Dictionary> list = elist.OrderBy(t => t.Sort).ToList();
List<Dictionary> oneMenuList = list.FindAll(t => t.ParentId == 0);
foreach (Dictionary item in oneMenuList)
{
DictionaryOutputDto menuTreeTableOutputDto = new DictionaryOutputDto();
menuTreeTableOutputDto = item.MapTo<DictionaryOutputDto>();
// menuTreeTableOutputDto.Children = GetSubMenus(list, item.Id).ToList<DictionaryOutputDto>();
reslist.Add(menuTreeTableOutputDto);
}
return reslist;
}
/// <summary>
/// 根据编码查询字典分类
/// </summary>
/// <param name="enCode"></param>
/// <returns></returns>
public async Task<Dictionary> GetByEnCodAsynce(string enCode)
{
return await _repository.GetByEnCodAsynce(enCode);
}
/// <summary>
/// 更新时判断分类编码是否存在(排除自己)
/// </summary>
/// <param name="enCode">分类编码</param
/// <param name="id">主键Id</param>
/// <returns></returns>
public async Task<Dictionary> GetByEnCodAsynce(string enCode, long id)
{
return await _repository.GetByEnCodAsynce(enCode, id);
}
/// <summary>
/// 获取子菜单,递归调用
/// </summary>
/// <param name="data"></param>
/// <param name="ParentId">父级Id</param>
/// <returns></returns>
private List<DictionaryOutputDto> GetSubMenus(List<Dictionary> data, long ParentId)
{
List<DictionaryOutputDto> list = new List<DictionaryOutputDto>();
DictionaryOutputDto menuTreeTableOutputDto = new DictionaryOutputDto();
List<Dictionary> ChilList = data.FindAll(t => t.ParentId == ParentId);
foreach (Dictionary entity in ChilList)
{
menuTreeTableOutputDto = entity.MapTo<DictionaryOutputDto>();
//menuTreeTableOutputDto.Children =
// GetSubMenus(data, entity.Id).OrderBy(t => t.Sort);
list.Add(menuTreeTableOutputDto);
}
return list;
}
/// <summary>
/// 根据条件查询数据字典列表
/// </summary>
/// <param name="ids"></param>
/// <param name="code"></param>
/// <returns></returns>
public async Task<List<DictionaryOutputDto>> GetDictionaryListByWhere(string ids, string code)
{
CacheHelper cacheHelper = new CacheHelper();
List<DictionaryOutputDto> list = cacheHelper.Get<List<DictionaryOutputDto>>("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;
}
/// <summary>
/// 查询单条数据字典
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public async Task<DictionaryOutputDto> GetDictionaryById(long id)
{
CacheHelper cacheHelper = new CacheHelper();
List<DictionaryOutputDto> list = cacheHelper.Get<List<DictionaryOutputDto>>("dcdictionary:list");
if (list == null)
{
list = await SyncDictionaryCache();
}
DictionaryOutputDto result = list.Find(x => x.Id == id);
return result;
}
/// <summary>
/// 异步分页查询
/// </summary>
/// <param name="search"></param>
/// <returns></returns>
public async Task<PageResult<DictionaryOutputDto>> 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<Dictionary> list = await repository.FindWithPagerAsync(where, pagerInfo, search.Sort, order);
List<DictionaryOutputDto> listDto = list.MapTo<DictionaryOutputDto>();
PageResult<DictionaryOutputDto> pageResult = new PageResult<DictionaryOutputDto>
{
CurrentPage = pagerInfo.CurrenetPageIndex,
Items = listDto,
ItemsPerPage = pagerInfo.PageSize,
TotalItems = pagerInfo.RecordCount
};
return pageResult;
}
/// <summary>
/// 同步缓存
/// </summary>
/// <returns></returns>
public async Task<List<DictionaryOutputDto>> SyncDictionaryCache()
{
CacheHelper cacheHelper = new CacheHelper();
IEnumerable<Dictionary> list = await repository.GetListWhereAsync(" IsDeleted=0 and IsEnabled=1 order by Sort");
List<DictionaryOutputDto> dicList = list.MapTo<DictionaryOutputDto>();
cacheHelper.Add("dcdictionary:list", dicList, TimeSpan.FromDays(30));
return dicList;
}
}
}