| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148 |
- 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<TEntity, TEntityDto, TPrimaryKey, TG, TC, TU> : VzCrudAppServiceBase<TEntity, TEntityDto, TPrimaryKey, TG, TC, TU>, IVzTreeAppServiceBase<TEntityDto, TPrimaryKey, TG, TC, TU>
- where TEntity : TreeEntity<TPrimaryKey, TEntity>
- where TEntityDto : class, IVzTreeEntityDto<TPrimaryKey>
- where TG : class, IVzPagedRequestDto
- where TC : class, IVzTreeEntityDto<TPrimaryKey>
- where TU : class, IVzTreeEntityDto<TPrimaryKey>
- {
- protected VzTreeAppServiceBaseBase(IRepository<TEntity, TPrimaryKey> repository, string keyFiledName = null) : base(repository, keyFiledName)
- {
- }
- protected virtual string MoveUpPermissionName { get; set; }
- protected virtual string MoveDownPermissionName { get; set; }
- /// <summary>
- /// 查询全部记录(不分页)
- /// </summary>
- /// <param name="input"></param>
- /// <returns></returns>
- public override async Task<PagedResultDto<TEntityDto>> 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<TEntityDto>(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<TPrimaryKey> input)
- {
- CheckDeletePermission();
- var entity = await CheckDelete(input);
- if (!entity.IsLeaf)
- {
- ThrowError("FunctionHasChildren");
- }
- await Repository.DeleteAsync(entity);
- await CurrentUnitOfWork.SaveChangesAsync();
- }
- protected virtual async Task<TC> CheckCreate(TC input)
- {
- return await Task.FromResult(input);
- }
- protected virtual async Task<TEntity> CheckUpdate(TU input)
- {
- var entity = await GetEntityById(input.Id);
- return await Task.FromResult(entity);
- }
- protected virtual async Task<TEntity> CheckDelete(VzEntityDto<TPrimaryKey> input)
- {
- var entity = await GetEntityById(input.Id);
- return await Task.FromResult(entity);
- }
- /// <summary>
- /// 上移
- /// </summary>
- /// <param name="input"></param>
- /// <returns></returns>
- public async Task MoveUp(VzTreeMoveDto<TPrimaryKey> 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();
- }
- /// <summary>
- /// 下移
- /// </summary>
- /// <param name="input"></param>
- /// <returns></returns>
- public async Task MoveDown(VzTreeMoveDto<TPrimaryKey> 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();
- }
- }
|