1
0

DictionaryService.cs 2.8 KB

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