using Abp.Application.Services.Dto; using Abp.Domain.Repositories; using VberZero.AppService.Base.Dto; using VberZero.AppService.Base.TreeBase.Dto; using VberZero.BaseSystem; namespace VberZero.AppService.Base.TreeBase; public abstract class VzTreeAppServiceBaseBase : VzCrudAppServiceBase, IVzTreeAppServiceBase where TEntity : TreeEntity where TEntityDto : class, IVzTreeEntityDto where TG : class, IVzPagedRequestDto where TC : class, IVzTreeEntityDto where TU : class, IVzTreeEntityDto { protected VzTreeAppServiceBaseBase(IRepository repository, string keyFiledName = null) : base(repository, keyFiledName) { } protected virtual string MoveUpPermissionName { get; set; } protected virtual string MoveDownPermissionName { get; set; } /// /// 查询全部记录(不分页) /// /// /// public override async Task> GetAll(TG input) { CheckGetAllPermission(); var query = CreateFilteredQuery(input); var totalCount = await AsyncQueryableExecuter.CountAsync(query); query = ApplySorting(query, input); //query = ApplyPaging(query, input); var entities = await AsyncQueryableExecuter.ToListAsync(query); return new PagedResultDto(totalCount, entities.Select(MapToEntityDto).ToList()); } public override async Task Create(TC input) { CheckCreatePermission(); if (await CheckCreate(input) == null) { return; } var parent = await GetEntityById(input.ParentNo); if (parent == null) { ThrowError("FunctionHasNoParent"); return; } input.Depth = parent.Depth + 1; input.IsLeaf = true; input.Path = input.Id == null ? string.Empty : $"{parent.Path}.{input.Id}"; var entity = MapToEntity(input); await Repository.InsertAsync(entity); await CurrentUnitOfWork.SaveChangesAsync(); if (input.Id == null) { entity.Path = $"{parent.Path}.{entity.Id}"; await Repository.UpdateAsync(entity); } parent.IsLeaf = false; await Repository.UpdateAsync(parent); } public override async Task Update(TU input) { CheckUpdatePermission(); var entity = await CheckUpdate(input); if (entity == null) { CheckErrors(NotExistMessage); return; } input.Id = entity.Id; input.ParentNo = entity.ParentNo; input.Path = entity.Path; input.Depth = entity.Depth; MapToEntity(input, entity); await Repository.UpdateAsync(entity); } public override async Task Delete(VzEntityDto input) { CheckDeletePermission(); var entity = await CheckDelete(input); if (!entity.IsLeaf) { ThrowError("FunctionHasChildren"); } await Repository.DeleteAsync(entity); await CurrentUnitOfWork.SaveChangesAsync(); } protected virtual async Task CheckCreate(TC input) { return await Task.FromResult(input); } protected virtual async Task CheckUpdate(TU input) { var entity = await GetEntityById(input.Id); return await Task.FromResult(entity); } protected virtual async Task CheckDelete(VzEntityDto input) { var entity = await GetEntityById(input.Id); return await Task.FromResult(entity); } /// /// 上移 /// /// /// public async Task MoveUp(VzTreeMoveDto input) { CheckPermission(MoveUpPermissionName); var entity = await GetEntityById(input.Id); int sort = entity.Sort; var prev = await GetEntityById(input.MoveId); int prevSort = prev.Sort; entity.Sort = prevSort; prev.Sort = sort; await CurrentUnitOfWork.SaveChangesAsync(); } /// /// 下移 /// /// /// public async Task MoveDown(VzTreeMoveDto input) { CheckPermission(MoveDownPermissionName); var entity = await GetEntityById(input.Id); int sort = entity.Sort; var next = await GetEntityById(input.MoveId); int nextSort = next.Sort; entity.Sort = nextSort; next.Sort = sort; await CurrentUnitOfWork.SaveChangesAsync(); } }