0
0

DictionaryService.cs 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. using Admin.Core.Common.Input;
  2. using Admin.Core.Common.Output;
  3. using Admin.Core.Model.Admin;
  4. using Admin.Core.Repository.Admin;
  5. using Admin.Core.Service.Admin.Dictionary.Input;
  6. using Admin.Core.Service.Admin.Dictionary.Output;
  7. using System.Threading.Tasks;
  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 dictionaryTypeId = input.Filter?.DictionaryTypeId;
  26. var list = await _dictionaryRepository.Select
  27. .WhereIf(dictionaryTypeId.HasValue && dictionaryTypeId.Value > 0, a => a.DictionaryTypeId == dictionaryTypeId)
  28. .WhereIf(key.NotNull(), a => a.Name.Contains(key) || a.Code.Contains(key))
  29. .Count(out var total)
  30. .OrderByDescending(true, c => c.Id)
  31. .Page(input.CurrentPage, input.PageSize)
  32. .ToListAsync<DictionaryListOutput>();
  33. var data = new PageOutput<DictionaryListOutput>()
  34. {
  35. List = list,
  36. Total = total
  37. };
  38. return ResponseOutput.Ok(data);
  39. }
  40. public async Task<IResponseOutput> AddAsync(DictionaryAddInput input)
  41. {
  42. var dictionary = Mapper.Map<DictionaryEntity>(input);
  43. var id = (await _dictionaryRepository.InsertAsync(dictionary)).Id;
  44. return ResponseOutput.Result(id > 0);
  45. }
  46. public async Task<IResponseOutput> UpdateAsync(DictionaryUpdateInput input)
  47. {
  48. if (!(input?.Id > 0))
  49. {
  50. return ResponseOutput.NotOk();
  51. }
  52. var entity = await _dictionaryRepository.GetAsync(input.Id);
  53. if (!(entity?.Id > 0))
  54. {
  55. return ResponseOutput.NotOk("数据字典不存在!");
  56. }
  57. Mapper.Map(input, entity);
  58. await _dictionaryRepository.UpdateAsync(entity);
  59. return ResponseOutput.Ok();
  60. }
  61. public async Task<IResponseOutput> DeleteAsync(long id)
  62. {
  63. var result = false;
  64. if (id > 0)
  65. {
  66. result = (await _dictionaryRepository.DeleteAsync(m => m.Id == id)) > 0;
  67. }
  68. return ResponseOutput.Result(result);
  69. }
  70. public async Task<IResponseOutput> SoftDeleteAsync(long id)
  71. {
  72. var result = await _dictionaryRepository.SoftDeleteAsync(id);
  73. return ResponseOutput.Result(result);
  74. }
  75. public async Task<IResponseOutput> BatchSoftDeleteAsync(long[] ids)
  76. {
  77. var result = await _dictionaryRepository.SoftDeleteAsync(ids);
  78. return ResponseOutput.Result(result);
  79. }
  80. }
  81. }