add: 师傅实体和服务类型

This commit is contained in:
于鹏
2025-10-06 15:58:11 +08:00
parent a1038f1b7b
commit ff0ec31ed0
18 changed files with 321 additions and 7 deletions

View File

@ -0,0 +1,75 @@
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();
}
}
}