76 lines
2.3 KiB
C#
76 lines
2.3 KiB
C#
using KonSoft.Admin.Dtos;
|
|
using KonSoft.Admin.Entities;
|
|
using KonSoft.Admin.IApplicationServices;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using Volo.Abp.Application.Services;
|
|
using Volo.Abp.Domain.Repositories;
|
|
|
|
namespace KonSoft.Admin.ApplicationServices
|
|
{
|
|
public class ServiceCategoryAppService : ApplicationService, IServiceCategoryAppService
|
|
{
|
|
private readonly IRepository<ServiceCategory, Guid> _repository;
|
|
|
|
|
|
public ServiceCategoryAppService(IRepository<ServiceCategory, Guid> repository)
|
|
{
|
|
_repository = repository;
|
|
}
|
|
|
|
public async Task<ServiceCategoryDto> CreateAsync(CreateServiceCategoryDto input)
|
|
{
|
|
ServiceCategory parent = null;
|
|
if (input.ParentId.HasValue)
|
|
{
|
|
parent = await _repository.GetAsync(input.ParentId.Value);
|
|
}
|
|
|
|
var category = new ServiceCategory(Guid.NewGuid(), input.Name);
|
|
category.SetParent(parent);
|
|
|
|
|
|
await _repository.InsertAsync(category);
|
|
|
|
|
|
return ObjectMapper.Map<ServiceCategory, ServiceCategoryDto>(category);
|
|
}
|
|
|
|
public async Task DeleteAsync(Guid id)
|
|
{
|
|
var hasChildren = await _repository.AnyAsync(c => c.ParentId == id);
|
|
if (hasChildren) throw new InvalidOperationException("存在子节点无法删除!");
|
|
|
|
|
|
await _repository.DeleteAsync(o => o.Id == id);
|
|
}
|
|
|
|
public async Task<List<ServiceCategoryDto>> GetTreeAsync()
|
|
{
|
|
var allCategories = await _repository.GetListAsync();
|
|
|
|
// 构建树
|
|
var lookup = allCategories.ToDictionary(c => c.Id, c => ObjectMapper.Map<ServiceCategory, ServiceCategoryDto>(c));
|
|
var rootList = new List<ServiceCategoryDto>();
|
|
|
|
|
|
foreach (var dto in lookup.Values)
|
|
{
|
|
if (dto.ParentId.HasValue && lookup.ContainsKey(dto.ParentId.Value))
|
|
{
|
|
lookup[dto.ParentId.Value].Children.Add(dto);
|
|
}
|
|
else
|
|
{
|
|
rootList.Add(dto);
|
|
}
|
|
}
|
|
|
|
return rootList.OrderBy(c => c.Name).ToList();
|
|
}
|
|
}
|
|
}
|