VzTreeAppServiceBase.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. using Abp.Application.Services.Dto;
  2. using Abp.Domain.Repositories;
  3. using VberZero.AppService.Base.Dto;
  4. using VberZero.AppService.Base.TreeBase.Dto;
  5. using VberZero.BaseSystem;
  6. namespace VberZero.AppService.Base.TreeBase;
  7. public abstract class VzTreeAppServiceBaseBase<TEntity, TEntityDto, TPrimaryKey, TG, TC, TU> : VzCrudAppServiceBase<TEntity, TEntityDto, TPrimaryKey, TG, TC, TU>, IVzTreeAppServiceBase<TEntityDto, TPrimaryKey, TG, TC, TU>
  8. where TEntity : TreeEntity<TPrimaryKey, TEntity>
  9. where TEntityDto : class, IVzTreeEntityDto<TPrimaryKey>
  10. where TG : class, IVzPagedRequestDto
  11. where TC : class, IVzTreeEntityDto<TPrimaryKey>
  12. where TU : class, IVzTreeEntityDto<TPrimaryKey>
  13. {
  14. protected VzTreeAppServiceBaseBase(IRepository<TEntity, TPrimaryKey> repository, string keyFiledName = null) : base(repository, keyFiledName)
  15. {
  16. }
  17. protected virtual string MoveUpPermissionName { get; set; }
  18. protected virtual string MoveDownPermissionName { get; set; }
  19. /// <summary>
  20. /// 查询全部记录(不分页)
  21. /// </summary>
  22. /// <param name="input"></param>
  23. /// <returns></returns>
  24. public override async Task<PagedResultDto<TEntityDto>> GetAll(TG input)
  25. {
  26. CheckGetAllPermission();
  27. var query = CreateFilteredQuery(input);
  28. var totalCount = await AsyncQueryableExecuter.CountAsync(query);
  29. query = ApplySorting(query, input);
  30. //query = ApplyPaging(query, input);
  31. var entities = await AsyncQueryableExecuter.ToListAsync(query);
  32. return new PagedResultDto<TEntityDto>(totalCount, entities.Select(MapToEntityDto).ToList());
  33. }
  34. public override async Task Create(TC input)
  35. {
  36. CheckCreatePermission();
  37. if (await CheckCreate(input) == null)
  38. {
  39. return;
  40. }
  41. var parent = await GetEntityById(input.ParentNo);
  42. if (parent == null)
  43. {
  44. ThrowError("FunctionHasNoParent");
  45. return;
  46. }
  47. input.Depth = parent.Depth + 1;
  48. input.IsLeaf = true;
  49. input.Path = input.Id == null ? string.Empty : $"{parent.Path}.{input.Id}";
  50. var entity = MapToEntity(input);
  51. await Repository.InsertAsync(entity);
  52. await CurrentUnitOfWork.SaveChangesAsync();
  53. if (input.Id == null)
  54. {
  55. entity.Path = $"{parent.Path}.{entity.Id}";
  56. await Repository.UpdateAsync(entity);
  57. }
  58. parent.IsLeaf = false;
  59. await Repository.UpdateAsync(parent);
  60. }
  61. public override async Task Update(TU input)
  62. {
  63. CheckUpdatePermission();
  64. var entity = await CheckUpdate(input);
  65. if (entity == null)
  66. {
  67. CheckErrors(NotExistMessage);
  68. return;
  69. }
  70. input.Id = entity.Id;
  71. input.ParentNo = entity.ParentNo;
  72. input.Path = entity.Path;
  73. input.Depth = entity.Depth;
  74. MapToEntity(input, entity);
  75. await Repository.UpdateAsync(entity);
  76. }
  77. public override async Task Delete(VzEntityDto<TPrimaryKey> input)
  78. {
  79. CheckDeletePermission();
  80. var entity = await CheckDelete(input);
  81. if (!entity.IsLeaf)
  82. {
  83. ThrowError("FunctionHasChildren");
  84. }
  85. await Repository.DeleteAsync(entity);
  86. await CurrentUnitOfWork.SaveChangesAsync();
  87. }
  88. protected virtual async Task<TC> CheckCreate(TC input)
  89. {
  90. return await Task.FromResult(input);
  91. }
  92. protected virtual async Task<TEntity> CheckUpdate(TU input)
  93. {
  94. var entity = await GetEntityById(input.Id);
  95. return await Task.FromResult(entity);
  96. }
  97. protected virtual async Task<TEntity> CheckDelete(VzEntityDto<TPrimaryKey> input)
  98. {
  99. var entity = await GetEntityById(input.Id);
  100. return await Task.FromResult(entity);
  101. }
  102. /// <summary>
  103. /// 上移
  104. /// </summary>
  105. /// <param name="input"></param>
  106. /// <returns></returns>
  107. public async Task MoveUp(VzTreeMoveDto<TPrimaryKey> input)
  108. {
  109. CheckPermission(MoveUpPermissionName);
  110. var entity = await GetEntityById(input.Id);
  111. int sort = entity.Sort;
  112. var prev = await GetEntityById(input.MoveId);
  113. int prevSort = prev.Sort;
  114. entity.Sort = prevSort;
  115. prev.Sort = sort;
  116. await CurrentUnitOfWork.SaveChangesAsync();
  117. }
  118. /// <summary>
  119. /// 下移
  120. /// </summary>
  121. /// <param name="input"></param>
  122. /// <returns></returns>
  123. public async Task MoveDown(VzTreeMoveDto<TPrimaryKey> input)
  124. {
  125. CheckPermission(MoveDownPermissionName);
  126. var entity = await GetEntityById(input.Id);
  127. int sort = entity.Sort;
  128. var next = await GetEntityById(input.MoveId);
  129. int nextSort = next.Sort;
  130. entity.Sort = nextSort;
  131. next.Sort = sort;
  132. await CurrentUnitOfWork.SaveChangesAsync();
  133. }
  134. }