0
0

DictionaryService.cs 4.3 KB

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