DictionaryDetailAppService.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. using BaseService.BaseData.DataDictionaryManagement.Dto;
  2. using BaseService.Permissions;
  3. using Microsoft.AspNetCore.Authorization;
  4. using Microsoft.EntityFrameworkCore;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using System.Threading.Tasks;
  9. using Volo.Abp;
  10. using Volo.Abp.Application.Dtos;
  11. using Volo.Abp.Application.Services;
  12. using Volo.Abp.Domain.Repositories;
  13. namespace BaseService.BaseData.DataDictionaryManagement
  14. {
  15. [Authorize(BaseServicePermissions.DataDictionary.Default)]
  16. public class DictionaryDetailAppService : ApplicationService, IDictionaryDetailAppService
  17. {
  18. private readonly IRepository<DataDictionary, Guid> _masterRepository;
  19. private readonly IRepository<DataDictionaryDetail, Guid> _detailRepository;
  20. public DictionaryDetailAppService(
  21. IRepository<DataDictionaryDetail, Guid> detailRepository,
  22. IRepository<DataDictionary, Guid> masterRepository)
  23. {
  24. _detailRepository = detailRepository;
  25. _masterRepository = masterRepository;
  26. }
  27. [Authorize(BaseServicePermissions.DataDictionary.Create)]
  28. public async Task<DictionaryDetailDto> Create(CreateOrUpdateDictionaryDetailDto input)
  29. {
  30. var master = await _masterRepository.FirstOrDefaultAsync(_ => _.Id == input.Pid);
  31. if (master == null)
  32. {
  33. throw new BusinessException("未找到字典!");
  34. }
  35. var exist = await _detailRepository.FirstOrDefaultAsync(_ => _.Label == input.Label);
  36. if (exist != null)
  37. {
  38. throw new BusinessException("名称:" + input.Label + "字典已存在");
  39. }
  40. var result = await _detailRepository.InsertAsync(new DataDictionaryDetail(
  41. GuidGenerator.Create(),
  42. input.Pid,
  43. input.Label,
  44. input.Value,
  45. input.Sort));
  46. return ObjectMapper.Map<DataDictionaryDetail, DictionaryDetailDto>(result);
  47. }
  48. [Authorize(BaseServicePermissions.DataDictionary.Delete)]
  49. public async Task Delete(List<Guid> ids)
  50. {
  51. foreach (var id in ids)
  52. {
  53. await _detailRepository.DeleteAsync(id);
  54. }
  55. }
  56. public async Task<DictionaryDetailDto> Get(Guid id)
  57. {
  58. var result = await _detailRepository.GetAsync(id);
  59. return ObjectMapper.Map<DataDictionaryDetail, DictionaryDetailDto>(result);
  60. }
  61. /// <summary>
  62. /// 分页查询
  63. /// </summary>
  64. /// <param name="input">分页条件</param>
  65. /// <returns></returns>
  66. public async Task<PagedResultDto<DictionaryDetailDto>> GetAll(GetDictionaryDetailInputDto input)
  67. {
  68. var query = (await _detailRepository.GetQueryableAsync()).Where(_ => _.Pid == input.Pid);
  69. var items = await query.OrderBy(_ => _.Sort)
  70. .Skip(input.SkipCount)
  71. .Take(input.MaxResultCount)
  72. .ToListAsync();
  73. var dots = ObjectMapper.Map<List<DataDictionaryDetail>, List<DictionaryDetailDto>>(items);
  74. var totalCount = await query.CountAsync();
  75. return new PagedResultDto<DictionaryDetailDto>(totalCount, dots);
  76. }
  77. /// <summary>
  78. /// 列表查询
  79. /// </summary>
  80. /// <param name="name">父级字典名称</param>
  81. /// <returns></returns>
  82. public async Task<ListResultDto<DictionaryDetailDto>> GetAllByDictionaryName(string name)
  83. {
  84. var master = await _masterRepository.GetAsync(_ => _.Name == name);
  85. var details = await (await _detailRepository.GetQueryableAsync()).Where(_ => _.Pid == master.Id)
  86. .OrderBy(_ => _.Sort)
  87. .ToListAsync();
  88. return new ListResultDto<DictionaryDetailDto>(ObjectMapper.Map<List<DataDictionaryDetail>, List<DictionaryDetailDto>>(details));
  89. }
  90. [Authorize(BaseServicePermissions.DataDictionary.Update)]
  91. public async Task<DictionaryDetailDto> Update(Guid id, CreateOrUpdateDictionaryDetailDto input)
  92. {
  93. var detail = await _detailRepository.GetAsync(id);
  94. detail.Label = input.Label;
  95. detail.Value = input.Value;
  96. detail.Sort = input.Sort;
  97. return ObjectMapper.Map<DataDictionaryDetail, DictionaryDetailDto>(detail);
  98. }
  99. }
  100. }