DictionaryService.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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.Output;
  5. using ZhonTai.Plate.Admin.Service.Dictionary.Input;
  6. using ZhonTai.Plate.Admin.Domain.Dictionary.Dto;
  7. namespace ZhonTai.Plate.Admin.Service.Dictionary
  8. {
  9. public class DictionaryService : BaseService, IDictionaryService
  10. {
  11. private readonly IDictionaryRepository _dictionaryRepository;
  12. public DictionaryService(IDictionaryRepository dictionaryRepository)
  13. {
  14. _dictionaryRepository = dictionaryRepository;
  15. }
  16. public async Task<IResultOutput> GetAsync(long id)
  17. {
  18. var result = await _dictionaryRepository.GetAsync<DictionaryGetOutput>(id);
  19. return ResultOutput.Ok(result);
  20. }
  21. public async Task<IResultOutput> GetPageAsync(PageInput<DictionaryGetPageDto> input)
  22. {
  23. var key = input.Filter?.Name;
  24. var dictionaryTypeId = input.Filter?.DictionaryTypeId;
  25. var list = await _dictionaryRepository.Select
  26. .WhereIf(dictionaryTypeId.HasValue && dictionaryTypeId.Value > 0, a => a.DictionaryTypeId == dictionaryTypeId)
  27. .WhereIf(key.NotNull(), a => a.Name.Contains(key) || a.Code.Contains(key))
  28. .Count(out var total)
  29. .OrderByDescending(true, c => c.Id)
  30. .Page(input.CurrentPage, input.PageSize)
  31. .ToListAsync<DictionaryListOutput>();
  32. var data = new PageOutput<DictionaryListOutput>()
  33. {
  34. List = list,
  35. Total = total
  36. };
  37. return ResultOutput.Ok(data);
  38. }
  39. public async Task<IResultOutput> AddAsync(DictionaryAddInput input)
  40. {
  41. var dictionary = Mapper.Map<DictionaryEntity>(input);
  42. var id = (await _dictionaryRepository.InsertAsync(dictionary)).Id;
  43. return ResultOutput.Result(id > 0);
  44. }
  45. public async Task<IResultOutput> UpdateAsync(DictionaryUpdateInput input)
  46. {
  47. if (!(input?.Id > 0))
  48. {
  49. return ResultOutput.NotOk();
  50. }
  51. var entity = await _dictionaryRepository.GetAsync(input.Id);
  52. if (!(entity?.Id > 0))
  53. {
  54. return ResultOutput.NotOk("数据字典不存在!");
  55. }
  56. Mapper.Map(input, entity);
  57. await _dictionaryRepository.UpdateAsync(entity);
  58. return ResultOutput.Ok();
  59. }
  60. public async Task<IResultOutput> DeleteAsync(long id)
  61. {
  62. var result = false;
  63. if (id > 0)
  64. {
  65. result = (await _dictionaryRepository.DeleteAsync(m => m.Id == id)) > 0;
  66. }
  67. return ResultOutput.Result(result);
  68. }
  69. public async Task<IResultOutput> SoftDeleteAsync(long id)
  70. {
  71. var result = await _dictionaryRepository.SoftDeleteAsync(id);
  72. return ResultOutput.Result(result);
  73. }
  74. public async Task<IResultOutput> BatchSoftDeleteAsync(long[] ids)
  75. {
  76. var result = await _dictionaryRepository.SoftDeleteAsync(ids);
  77. return ResultOutput.Result(result);
  78. }
  79. }
  80. }