DictionaryService.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. using System.Threading.Tasks;
  2. using ZhonTai.Admin.Core.Dto;
  3. using ZhonTai.Admin.Domain.Dictionary;
  4. using ZhonTai.Admin.Services.Dictionary.Dto;
  5. using ZhonTai.Admin.Domain.Dictionary.Dto;
  6. using ZhonTai.DynamicApi;
  7. using ZhonTai.DynamicApi.Attributes;
  8. using Microsoft.AspNetCore.Mvc;
  9. using ZhonTai.Admin.Core.Consts;
  10. using System.Linq;
  11. namespace ZhonTai.Admin.Services.Dictionary;
  12. /// <summary>
  13. /// 数据字典服务
  14. /// </summary>
  15. [Order(60)]
  16. [DynamicApi(Area = AdminConsts.AreaName)]
  17. public class DictionaryService : BaseService, IDictionaryService, IDynamicApi
  18. {
  19. private readonly IDictionaryRepository _dictionaryRepository;
  20. public DictionaryService(IDictionaryRepository dictionaryRepository)
  21. {
  22. _dictionaryRepository = dictionaryRepository;
  23. }
  24. /// <summary>
  25. /// 查询
  26. /// </summary>
  27. /// <param name="id"></param>
  28. /// <returns></returns>
  29. public async Task<DictionaryGetOutput> GetAsync(long id)
  30. {
  31. var result = await _dictionaryRepository.GetAsync<DictionaryGetOutput>(id);
  32. return result;
  33. }
  34. /// <summary>
  35. /// 查询分页
  36. /// </summary>
  37. /// <param name="input"></param>
  38. /// <returns></returns>
  39. [HttpPost]
  40. public async Task<PageOutput<DictionaryListOutput>> GetPageAsync(PageInput<DictionaryGetPageDto> input)
  41. {
  42. var key = input.Filter?.Name;
  43. var dictionaryTypeId = input.Filter?.DictionaryTypeId;
  44. var list = await _dictionaryRepository.Select
  45. .WhereDynamicFilter(input.DynamicFilter)
  46. .WhereIf(dictionaryTypeId.HasValue && dictionaryTypeId.Value > 0, a => a.DictionaryTypeId == dictionaryTypeId)
  47. .WhereIf(key.NotNull(), a => a.Name.Contains(key) || a.Code.Contains(key))
  48. .Count(out var total)
  49. .OrderByDescending(true, c => c.Id)
  50. .Page(input.CurrentPage, input.PageSize)
  51. .ToListAsync<DictionaryListOutput>();
  52. var data = new PageOutput<DictionaryListOutput>()
  53. {
  54. List = list,
  55. Total = total
  56. };
  57. return data;
  58. }
  59. /// <summary>
  60. /// 新增
  61. /// </summary>
  62. /// <param name="input"></param>
  63. /// <returns></returns>
  64. public async Task<long> AddAsync(DictionaryAddInput input)
  65. {
  66. if (await _dictionaryRepository.Select.AnyAsync(a => a.DictionaryTypeId == input.DictionaryTypeId && a.Name == input.Name))
  67. {
  68. throw ResultOutput.Exception($"字典已存在");
  69. }
  70. if (input.Code.NotNull() && await _dictionaryRepository.Select.AnyAsync(a => a.DictionaryTypeId == input.DictionaryTypeId && a.Code == input.Code))
  71. {
  72. throw ResultOutput.Exception($"字典编码已存在");
  73. }
  74. if (input.Code.NotNull() && await _dictionaryRepository.Select.AnyAsync(a => a.DictionaryTypeId == input.DictionaryTypeId && a.Code == input.Code))
  75. {
  76. throw ResultOutput.Exception($"字典编码已存在");
  77. }
  78. if (input.Value.NotNull() && await _dictionaryRepository.Select.AnyAsync(a => a.DictionaryTypeId == input.DictionaryTypeId && a.Value == input.Value))
  79. {
  80. throw ResultOutput.Exception($"字典值已存在");
  81. }
  82. var dictionary = Mapper.Map<DictionaryEntity>(input);
  83. await _dictionaryRepository.InsertAsync(dictionary);
  84. return dictionary.Id;
  85. }
  86. /// <summary>
  87. /// 修改
  88. /// </summary>
  89. /// <param name="input"></param>
  90. /// <returns></returns>
  91. public async Task UpdateAsync(DictionaryUpdateInput input)
  92. {
  93. var entity = await _dictionaryRepository.GetAsync(input.Id);
  94. if (!(entity?.Id > 0))
  95. {
  96. throw ResultOutput.Exception("字典不存在");
  97. }
  98. if (await _dictionaryRepository.Select.AnyAsync(a => a.Id != input.Id && a.DictionaryTypeId == input.DictionaryTypeId && a.Name == input.Name))
  99. {
  100. throw ResultOutput.Exception($"字典已存在");
  101. }
  102. if (input.Code.NotNull() && await _dictionaryRepository.Select.AnyAsync(a => a.Id != input.Id && a.DictionaryTypeId == input.DictionaryTypeId && a.Code == input.Code))
  103. {
  104. throw ResultOutput.Exception($"字典编码已存在");
  105. }
  106. if (input.Value.NotNull() && await _dictionaryRepository.Select.AnyAsync(a => a.Id != input.Id && a.DictionaryTypeId == input.DictionaryTypeId && a.Value == input.Value))
  107. {
  108. throw ResultOutput.Exception($"字典值已存在");
  109. }
  110. Mapper.Map(input, entity);
  111. await _dictionaryRepository.UpdateAsync(entity);
  112. }
  113. /// <summary>
  114. /// 彻底删除
  115. /// </summary>
  116. /// <param name="id"></param>
  117. /// <returns></returns>
  118. public async Task DeleteAsync(long id)
  119. {
  120. await _dictionaryRepository.DeleteAsync(m => m.Id == id);
  121. }
  122. /// <summary>
  123. /// 批量彻底删除
  124. /// </summary>
  125. /// <param name="ids"></param>
  126. /// <returns></returns>
  127. public async Task BatchDeleteAsync(long[] ids)
  128. {
  129. await _dictionaryRepository.DeleteAsync(a => ids.Contains(a.Id));
  130. }
  131. /// <summary>
  132. /// 删除
  133. /// </summary>
  134. /// <param name="id"></param>
  135. /// <returns></returns>
  136. public async Task SoftDeleteAsync(long id)
  137. {
  138. await _dictionaryRepository.SoftDeleteAsync(id);
  139. }
  140. /// <summary>
  141. /// 批量删除
  142. /// </summary>
  143. /// <param name="ids"></param>
  144. /// <returns></returns>
  145. public async Task BatchSoftDeleteAsync(long[] ids)
  146. {
  147. await _dictionaryRepository.SoftDeleteAsync(ids);
  148. }
  149. }