using KonSoft.Admin.Dtos; using KonSoft.Admin.Entities; using KonSoft.Admin.IApplicationServices; using KonSoft.Admin.Repositories; using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Volo.Abp; using Volo.Abp.Application.Dtos; using Volo.Abp.Application.Services; using Volo.Abp.Domain.Repositories; namespace KonSoft.Admin.ApplicationServices { public class ProductAppService(IProductRepository repository) : ApplicationService, IProductService { private readonly IProductRepository repository = repository; /// /// 创建产品 /// public async Task CreateAsync(CreateProductDto input) { var id = Guid.NewGuid(); var product = new Product(id, input.Name, input.Code, input.Price, input.Description, input.Type, input.ParentId, input.Status, input.Order); await repository.InsertAsync(product); } /// /// 删除 /// public async Task DeleteAsync(Guid id) { await repository.DeleteAsync(x => x.Id == id); } /// /// 查询 /// public async Task GetAsync(Guid id) { var product = await repository.GetAsync(x => x.Id == id); return ObjectMapper.Map(product); } /// /// 查询集合 /// /// /// public async Task> GetListAsync(PagedResultRequestDto input) { var query = await repository.GetPageRootListAsync(input.SkipCount, input.MaxResultCount, x => x.ParentId == null); var all = await repository.GetListAsync(x => x.ParentId != null); foreach (var item in query) { BuildChildren(item, all); } var totalCount = await repository.CountAsync(x => x.ParentId == null); var productDtos = ObjectMapper.Map, List>(query); return new PagedResultDto(totalCount, productDtos); } private static void BuildChildren(Product parent, List all) { var children = all .Where(x => x.ParentId == parent.Id) .OrderBy(x => x.Order) .ToList(); parent.Children = children; foreach (var child in children) { BuildChildren(child, all); // 递归 } } /// /// 修改 /// /// /// public async Task UpdateAsync(UpdateProductDto input) { // 根据ID查询订单,如果不存在则抛出异常【修改前端必然看到了,数据若查不到提示自定义异常信息】 var order = await repository.FindAsync(o => o.Id == input.Id) ?? throw new BusinessException("商品找不到").WithData("Id", input.Id); // 将输入的DTO字段映射到订单实体 ObjectMapper.Map(input, order); // 更新订单 await repository.UpdateAsync(order); } } }