1
0

DictionaryService.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. namespace ZhonTai.Admin.Services.Dictionary;
  11. /// <summary>
  12. /// 数据字典服务
  13. /// </summary>
  14. [DynamicApi(Area = AdminConsts.AreaName)]
  15. public class DictionaryService : BaseService, IDictionaryService, IDynamicApi
  16. {
  17. private readonly IDictionaryRepository _dictionaryRepository;
  18. public DictionaryService(IDictionaryRepository dictionaryRepository)
  19. {
  20. _dictionaryRepository = dictionaryRepository;
  21. }
  22. /// <summary>
  23. /// 查询数据字典
  24. /// </summary>
  25. /// <param name="id"></param>
  26. /// <returns></returns>
  27. public async Task<DictionaryGetOutput> GetAsync(long id)
  28. {
  29. var result = await _dictionaryRepository.GetAsync<DictionaryGetOutput>(id);
  30. return result;
  31. }
  32. /// <summary>
  33. /// 查询数据字典列表
  34. /// </summary>
  35. /// <param name="input"></param>
  36. /// <returns></returns>
  37. [HttpPost]
  38. public async Task<PageOutput<DictionaryListOutput>> GetPageAsync(PageInput<DictionaryGetPageDto> input)
  39. {
  40. var key = input.Filter?.Name;
  41. var dictionaryTypeId = input.Filter?.DictionaryTypeId;
  42. var list = await _dictionaryRepository.Select
  43. .WhereDynamicFilter(input.DynamicFilter)
  44. .WhereIf(dictionaryTypeId.HasValue && dictionaryTypeId.Value > 0, a => a.DictionaryTypeId == dictionaryTypeId)
  45. .WhereIf(key.NotNull(), a => a.Name.Contains(key) || a.Code.Contains(key))
  46. .Count(out var total)
  47. .OrderByDescending(true, c => c.Id)
  48. .Page(input.CurrentPage, input.PageSize)
  49. .ToListAsync<DictionaryListOutput>();
  50. var data = new PageOutput<DictionaryListOutput>()
  51. {
  52. List = list,
  53. Total = total
  54. };
  55. return data;
  56. }
  57. /// <summary>
  58. /// 新增
  59. /// </summary>
  60. /// <param name="input"></param>
  61. /// <returns></returns>
  62. public async Task<long> AddAsync(DictionaryAddInput input)
  63. {
  64. var dictionary = Mapper.Map<DictionaryEntity>(input);
  65. await _dictionaryRepository.InsertAsync(dictionary);
  66. return dictionary.Id;
  67. }
  68. /// <summary>
  69. /// 修改
  70. /// </summary>
  71. /// <param name="input"></param>
  72. /// <returns></returns>
  73. public async Task UpdateAsync(DictionaryUpdateInput input)
  74. {
  75. var entity = await _dictionaryRepository.GetAsync(input.Id);
  76. if (!(entity?.Id > 0))
  77. {
  78. throw ResultOutput.Exception("数据字典不存在");
  79. }
  80. Mapper.Map(input, entity);
  81. await _dictionaryRepository.UpdateAsync(entity);
  82. }
  83. /// <summary>
  84. /// 彻底删除
  85. /// </summary>
  86. /// <param name="id"></param>
  87. /// <returns></returns>
  88. public async Task DeleteAsync(long id)
  89. {
  90. await _dictionaryRepository.DeleteAsync(m => m.Id == id);
  91. }
  92. /// <summary>
  93. /// 删除
  94. /// </summary>
  95. /// <param name="id"></param>
  96. /// <returns></returns>
  97. public async Task SoftDeleteAsync(long id)
  98. {
  99. await _dictionaryRepository.SoftDeleteAsync(id);
  100. }
  101. /// <summary>
  102. /// 批量删除
  103. /// </summary>
  104. /// <param name="ids"></param>
  105. /// <returns></returns>
  106. public async Task BatchSoftDeleteAsync(long[] ids)
  107. {
  108. await _dictionaryRepository.SoftDeleteAsync(ids);
  109. }
  110. }