DictionaryService.cs 4.4 KB

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