DictionaryService.cs 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. using System.Threading.Tasks;
  2. using AutoMapper;
  3. using Admin.Core.Model.Output;
  4. using Admin.Core.Model.Input;
  5. using Admin.Core.Model.Admin;
  6. using Admin.Core.Repository.Admin;
  7. using Admin.Core.Service.Admin.Dictionary.Input;
  8. using Admin.Core.Service.Admin.Dictionary.Output;
  9. namespace Admin.Core.Service.Admin.Dictionary
  10. {
  11. public class DictionaryService : IDictionaryService
  12. {
  13. private readonly IMapper _mapper;
  14. private readonly IDictionaryRepository _dictionaryRepository;
  15. public DictionaryService(IMapper mapper, IDictionaryRepository dictionaryRepository)
  16. {
  17. _mapper = mapper;
  18. _dictionaryRepository = dictionaryRepository;
  19. }
  20. public async Task<IResponseOutput> GetAsync(long id)
  21. {
  22. var result = await _dictionaryRepository.GetAsync<DictionaryGetOutput>(id);
  23. return ResponseOutput.Ok(result);
  24. }
  25. public async Task<IResponseOutput> PageAsync(PageInput<DictionaryEntity> input)
  26. {
  27. var key = input.Filter?.Name;
  28. var list = await _dictionaryRepository.Select
  29. .WhereIf(key.NotNull(), a => a.Name.Contains(key) || a.Code.Contains(key))
  30. .Count(out var total)
  31. .OrderByDescending(true, c => c.Id)
  32. .Page(input.CurrentPage, input.PageSize)
  33. .ToListAsync<DictionaryListOutput>();
  34. var data = new PageOutput<DictionaryListOutput>()
  35. {
  36. List = list,
  37. Total = total
  38. };
  39. return ResponseOutput.Ok(data);
  40. }
  41. public async Task<IResponseOutput> AddAsync(DictionaryAddInput input)
  42. {
  43. var dictionary = _mapper.Map<DictionaryEntity>(input);
  44. var id = (await _dictionaryRepository.InsertAsync(dictionary)).Id;
  45. return ResponseOutput.Result(id > 0);
  46. }
  47. public async Task<IResponseOutput> UpdateAsync(DictionaryUpdateInput input)
  48. {
  49. if (!(input?.Id > 0))
  50. {
  51. return ResponseOutput.NotOk();
  52. }
  53. var entity = await _dictionaryRepository.GetAsync(input.Id);
  54. if (!(entity?.Id > 0))
  55. {
  56. return ResponseOutput.NotOk("Êý¾Ý×ֵ䲻´æÔÚ£¡");
  57. }
  58. _mapper.Map(input, entity);
  59. await _dictionaryRepository.UpdateAsync(entity);
  60. return ResponseOutput.Ok();
  61. }
  62. public async Task<IResponseOutput> DeleteAsync(long id)
  63. {
  64. var result = false;
  65. if (id > 0)
  66. {
  67. result = (await _dictionaryRepository.DeleteAsync(m => m.Id == id)) > 0;
  68. }
  69. return ResponseOutput.Result(result);
  70. }
  71. public async Task<IResponseOutput> SoftDeleteAsync(long id)
  72. {
  73. var result = await _dictionaryRepository.SoftDeleteAsync(id);
  74. return ResponseOutput.Result(result);
  75. }
  76. }
  77. }