123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170 |
- using System.Threading.Tasks;
- using ZhonTai.Admin.Core.Dto;
- using ZhonTai.Admin.Domain.Dictionary;
- using ZhonTai.Admin.Services.Dictionary.Dto;
- using ZhonTai.Admin.Domain.Dictionary.Dto;
- using ZhonTai.DynamicApi;
- using ZhonTai.DynamicApi.Attributes;
- using Microsoft.AspNetCore.Mvc;
- using ZhonTai.Admin.Core.Consts;
- using System.Linq;
- namespace ZhonTai.Admin.Services.Dictionary;
- /// <summary>
- /// 数据字典服务
- /// </summary>
- [Order(60)]
- [DynamicApi(Area = AdminConsts.AreaName)]
- public class DictionaryService : BaseService, IDictionaryService, IDynamicApi
- {
- private readonly IDictionaryRepository _dictionaryRepository;
- public DictionaryService(IDictionaryRepository dictionaryRepository)
- {
- _dictionaryRepository = dictionaryRepository;
- }
- /// <summary>
- /// 查询
- /// </summary>
- /// <param name="id"></param>
- /// <returns></returns>
- public async Task<DictionaryGetOutput> GetAsync(long id)
- {
- var result = await _dictionaryRepository.GetAsync<DictionaryGetOutput>(id);
- return result;
- }
- /// <summary>
- /// 查询分页
- /// </summary>
- /// <param name="input"></param>
- /// <returns></returns>
- [HttpPost]
- public async Task<PageOutput<DictionaryListOutput>> GetPageAsync(PageInput<DictionaryGetPageDto> input)
- {
- var key = input.Filter?.Name;
- var dictionaryTypeId = input.Filter?.DictionaryTypeId;
- var list = await _dictionaryRepository.Select
- .WhereDynamicFilter(input.DynamicFilter)
- .WhereIf(dictionaryTypeId.HasValue && dictionaryTypeId.Value > 0, a => a.DictionaryTypeId == dictionaryTypeId)
- .WhereIf(key.NotNull(), a => a.Name.Contains(key) || a.Code.Contains(key))
- .Count(out var total)
- .OrderByDescending(true, c => c.Id)
- .Page(input.CurrentPage, input.PageSize)
- .ToListAsync<DictionaryListOutput>();
- var data = new PageOutput<DictionaryListOutput>()
- {
- List = list,
- Total = total
- };
- return data;
- }
- /// <summary>
- /// 新增
- /// </summary>
- /// <param name="input"></param>
- /// <returns></returns>
- public async Task<long> AddAsync(DictionaryAddInput input)
- {
- if (await _dictionaryRepository.Select.AnyAsync(a => a.DictionaryTypeId == input.DictionaryTypeId && a.Name == input.Name))
- {
- throw ResultOutput.Exception($"字典已存在");
- }
- if (input.Code.NotNull() && await _dictionaryRepository.Select.AnyAsync(a => a.DictionaryTypeId == input.DictionaryTypeId && a.Code == input.Code))
- {
- throw ResultOutput.Exception($"字典编码已存在");
- }
- if (input.Code.NotNull() && await _dictionaryRepository.Select.AnyAsync(a => a.DictionaryTypeId == input.DictionaryTypeId && a.Code == input.Code))
- {
- throw ResultOutput.Exception($"字典编码已存在");
- }
- if (input.Value.NotNull() && await _dictionaryRepository.Select.AnyAsync(a => a.DictionaryTypeId == input.DictionaryTypeId && a.Value == input.Value))
- {
- throw ResultOutput.Exception($"字典值已存在");
- }
- var dictionary = Mapper.Map<DictionaryEntity>(input);
- await _dictionaryRepository.InsertAsync(dictionary);
- return dictionary.Id;
- }
- /// <summary>
- /// 修改
- /// </summary>
- /// <param name="input"></param>
- /// <returns></returns>
- public async Task UpdateAsync(DictionaryUpdateInput input)
- {
- var entity = await _dictionaryRepository.GetAsync(input.Id);
- if (!(entity?.Id > 0))
- {
- throw ResultOutput.Exception("字典不存在");
- }
- if (await _dictionaryRepository.Select.AnyAsync(a => a.Id != input.Id && a.DictionaryTypeId == input.DictionaryTypeId && a.Name == input.Name))
- {
- throw ResultOutput.Exception($"字典已存在");
- }
- if (input.Code.NotNull() && await _dictionaryRepository.Select.AnyAsync(a => a.Id != input.Id && a.DictionaryTypeId == input.DictionaryTypeId && a.Code == input.Code))
- {
- throw ResultOutput.Exception($"字典编码已存在");
- }
- if (input.Value.NotNull() && await _dictionaryRepository.Select.AnyAsync(a => a.Id != input.Id && a.DictionaryTypeId == input.DictionaryTypeId && a.Value == input.Value))
- {
- throw ResultOutput.Exception($"字典值已存在");
- }
- Mapper.Map(input, entity);
- await _dictionaryRepository.UpdateAsync(entity);
- }
- /// <summary>
- /// 彻底删除
- /// </summary>
- /// <param name="id"></param>
- /// <returns></returns>
- public async Task DeleteAsync(long id)
- {
- await _dictionaryRepository.DeleteAsync(m => m.Id == id);
- }
- /// <summary>
- /// 批量彻底删除
- /// </summary>
- /// <param name="ids"></param>
- /// <returns></returns>
- public async Task BatchDeleteAsync(long[] ids)
- {
- await _dictionaryRepository.DeleteAsync(a => ids.Contains(a.Id));
- }
- /// <summary>
- /// 删除
- /// </summary>
- /// <param name="id"></param>
- /// <returns></returns>
- public async Task SoftDeleteAsync(long id)
- {
- await _dictionaryRepository.SoftDeleteAsync(id);
- }
- /// <summary>
- /// 批量删除
- /// </summary>
- /// <param name="ids"></param>
- /// <returns></returns>
- public async Task BatchSoftDeleteAsync(long[] ids)
- {
- await _dictionaryRepository.SoftDeleteAsync(ids);
- }
- }
|