From 1419e37ed50eb5d9c991d4f95766a4b67a8898e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=9F=BA=E6=9C=AC=E6=89=8E=E6=A0=B9=E6=91=84=E5=BD=B1?= <1446802857@qq.com> Date: Wed, 15 Oct 2025 20:28:06 +0800 Subject: [PATCH] product --- .../Dtos/OrderDto.cs | 7 +- .../Dtos/ProductDto.cs | 66 ++++++++++++ .../IApplicationServices/IOrderAppService.cs | 8 +- .../IApplicationServices/IProductService.cs | 17 +++ .../ApplicationServices/OrderAppService.cs | 7 +- .../ApplicationServices/ProductAppService.cs | 100 ++++++++++++++++++ .../KonSoft.Admin.Domain/Entities/Product.cs | 99 +++++++++++++++++ .../Repositories/IProductRepository.cs | 14 +++ .../Repositories/ProductRepository.cs | 25 +++++ 9 files changed, 329 insertions(+), 14 deletions(-) create mode 100644 modules/admin/src/KonSoft.Admin.Application.Contracts/Dtos/ProductDto.cs create mode 100644 modules/admin/src/KonSoft.Admin.Application.Contracts/IApplicationServices/IProductService.cs create mode 100644 modules/admin/src/KonSoft.Admin.Application/ApplicationServices/ProductAppService.cs create mode 100644 modules/admin/src/KonSoft.Admin.Domain/Entities/Product.cs create mode 100644 modules/admin/src/KonSoft.Admin.Domain/Repositories/IProductRepository.cs create mode 100644 modules/admin/src/KonSoft.Admin.EntityFrameworkCore/EntityFrameworkCore/Repositories/ProductRepository.cs diff --git a/modules/admin/src/KonSoft.Admin.Application.Contracts/Dtos/OrderDto.cs b/modules/admin/src/KonSoft.Admin.Application.Contracts/Dtos/OrderDto.cs index 7996e16..0bfee44 100644 --- a/modules/admin/src/KonSoft.Admin.Application.Contracts/Dtos/OrderDto.cs +++ b/modules/admin/src/KonSoft.Admin.Application.Contracts/Dtos/OrderDto.cs @@ -4,6 +4,7 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; +using Volo.Abp.Application.Dtos; namespace KonSoft.Admin.Dtos { @@ -22,10 +23,4 @@ namespace KonSoft.Admin.Dtos public AddressDto Address { get; set; } public string Remark { get; set; } } - - public class PageListInput - { - public int Index { get; set; } - public int PageSize { get; set; } - } } diff --git a/modules/admin/src/KonSoft.Admin.Application.Contracts/Dtos/ProductDto.cs b/modules/admin/src/KonSoft.Admin.Application.Contracts/Dtos/ProductDto.cs new file mode 100644 index 0000000..43ac051 --- /dev/null +++ b/modules/admin/src/KonSoft.Admin.Application.Contracts/Dtos/ProductDto.cs @@ -0,0 +1,66 @@ + +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Xml.Linq; + +namespace KonSoft.Admin.Dtos +{ + public class ProductDto: CreateProductDto + { + /// + /// 嵌套子类 + /// + public List Children { get; set; } = new(); + } + + public class CreateProductDto + { + /// + /// 分类名称 + /// + public string Name { get; private set; } + + /// + /// 商品编码 + /// + public string Code { get; private set; } + /// + /// 商品价格 + /// + public decimal Price { get; private set; } + + /// + /// 商品描述 + /// + public string Description { get; private set; } + + /// + /// 分类类型(大类或小类) + /// + public string Type { get; private set; } + + /// + /// 父分类ID + /// + public Guid? ParentId { get; private set; } + + /// + /// 商品状态(在售/下架等) + /// + public string Status { get; private set; } + + /// + /// 排序字段 + /// + public int Order { get; private set; } + } + + public class UpdateProductDto: CreateProductDto + { + public Guid Id { get; set; } + } +} diff --git a/modules/admin/src/KonSoft.Admin.Application.Contracts/IApplicationServices/IOrderAppService.cs b/modules/admin/src/KonSoft.Admin.Application.Contracts/IApplicationServices/IOrderAppService.cs index 6d82e97..f90cc7c 100644 --- a/modules/admin/src/KonSoft.Admin.Application.Contracts/IApplicationServices/IOrderAppService.cs +++ b/modules/admin/src/KonSoft.Admin.Application.Contracts/IApplicationServices/IOrderAppService.cs @@ -1,9 +1,7 @@ using KonSoft.Admin.Dtos; using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; using System.Threading.Tasks; +using Volo.Abp.Application.Dtos; using Volo.Abp.Application.Services; namespace KonSoft.Admin.IApplicationServices @@ -22,5 +20,9 @@ namespace KonSoft.Admin.IApplicationServices Task CompleteServiceAsync(Guid orderId); Task ConfirmAsync(Guid orderId); Task CancelAsync(Guid orderId, string reason); + Task DeleteAsync(params Guid[] ids); + Task EditAsync(OrderDto input); + Task GetAsync(Guid id); + Task> GetListAsync(PagedResultRequestDto input); } } diff --git a/modules/admin/src/KonSoft.Admin.Application.Contracts/IApplicationServices/IProductService.cs b/modules/admin/src/KonSoft.Admin.Application.Contracts/IApplicationServices/IProductService.cs new file mode 100644 index 0000000..6f554f2 --- /dev/null +++ b/modules/admin/src/KonSoft.Admin.Application.Contracts/IApplicationServices/IProductService.cs @@ -0,0 +1,17 @@ +using KonSoft.Admin.Dtos; +using System; +using System.Threading.Tasks; +using Volo.Abp.Application.Dtos; +using Volo.Abp.Application.Services; + +namespace KonSoft.Admin.IApplicationServices +{ + public interface IProductService: IApplicationService + { + Task CreateAsync(CreateProductDto input); + Task UpdateAsync(UpdateProductDto input); + Task DeleteAsync(Guid id); + Task GetAsync(Guid id); + Task> GetListAsync(PagedResultRequestDto input); + } +} diff --git a/modules/admin/src/KonSoft.Admin.Application/ApplicationServices/OrderAppService.cs b/modules/admin/src/KonSoft.Admin.Application/ApplicationServices/OrderAppService.cs index ad42859..1134803 100644 --- a/modules/admin/src/KonSoft.Admin.Application/ApplicationServices/OrderAppService.cs +++ b/modules/admin/src/KonSoft.Admin.Application/ApplicationServices/OrderAppService.cs @@ -5,13 +5,11 @@ using KonSoft.Admin.Repositories; using System; using System.Collections.Generic; using System.Linq; -using System.Text; using System.Threading.Tasks; using Volo.Abp; using Volo.Abp.Application.Dtos; using Volo.Abp.Application.Services; using Volo.Abp.Domain.Repositories; -using Volo.Abp.ObjectMapping; namespace KonSoft.Admin.ApplicationServices { @@ -152,11 +150,10 @@ namespace KonSoft.Admin.ApplicationServices /// /// 获取所有订单列表 /// - public async Task> GetListAsync(PageListInput input) + public async Task> GetListAsync(PagedResultRequestDto input) { - var skipCount = (input.Index - 1) * input.PageSize; // 查询所有订单 - var orders = await _orderRepository.GetPagedListAsync(skipCount, input.PageSize, "Id"); + var orders = await _orderRepository.GetPagedListAsync(input.SkipCount, input.MaxResultCount, "Id"); var totalCount = await _orderRepository.CountAsync(); diff --git a/modules/admin/src/KonSoft.Admin.Application/ApplicationServices/ProductAppService.cs b/modules/admin/src/KonSoft.Admin.Application/ApplicationServices/ProductAppService.cs new file mode 100644 index 0000000..f0e08e3 --- /dev/null +++ b/modules/admin/src/KonSoft.Admin.Application/ApplicationServices/ProductAppService.cs @@ -0,0 +1,100 @@ +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); + } + } +} \ No newline at end of file diff --git a/modules/admin/src/KonSoft.Admin.Domain/Entities/Product.cs b/modules/admin/src/KonSoft.Admin.Domain/Entities/Product.cs new file mode 100644 index 0000000..580460f --- /dev/null +++ b/modules/admin/src/KonSoft.Admin.Domain/Entities/Product.cs @@ -0,0 +1,99 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations.Schema; +using System.Linq; +using Volo.Abp.Domain.Entities.Auditing; + +namespace KonSoft.Admin.Entities +{ + public class Product(Guid id, string name, string code, decimal price, string description, string type, Guid? parentId, string status = "在售", int order = 0) : FullAuditedAggregateRoot(id) + { + /// + /// 分类名称 + /// + public string Name { get; private set; } = name; + + /// + /// 商品编码 + /// + public string Code { get; private set; } = code; + + /// + /// 商品价格 + /// + public decimal Price { get; private set; } = price; + + /// + /// 商品描述 + /// + public string Description { get; private set; } = description; + + /// + /// 分类类型(大类或小类) + /// + public string Type { get; private set; } = type; + + /// + /// 父分类ID + /// + public Guid? ParentId { get; private set; } = parentId; + + /// + /// 商品状态(在售/下架等) + /// + public string Status { get; private set; } = status; + + /// + /// 排序字段 + /// + public int Order { get; private set; } = order; + + [NotMapped] + public List Children { get; set; } = new(); + + /// + /// 修改商品信息 + /// + public void Update(string name, string code, decimal price, string description, int order) + { + Name = name; + Code = code; + Price = price; + Description = description; + Order = order; + } + + /// + /// 修改商品分类 + /// + public void ChangeCategory(string type, Guid? parentId) + { + Type = type; + ParentId = parentId; + } + + /// + /// 上架商品 + /// + public void PutOnSale() + { + Status = "在售"; + } + + /// + /// 下架商品 + /// + public void TakeOffSale() + { + Status = "下架"; + } + + /// + /// 修改价格 + /// + public void ChangePrice(decimal newPrice) + { + Price = newPrice; + } + } +} \ No newline at end of file diff --git a/modules/admin/src/KonSoft.Admin.Domain/Repositories/IProductRepository.cs b/modules/admin/src/KonSoft.Admin.Domain/Repositories/IProductRepository.cs new file mode 100644 index 0000000..bbe1aab --- /dev/null +++ b/modules/admin/src/KonSoft.Admin.Domain/Repositories/IProductRepository.cs @@ -0,0 +1,14 @@ +using KonSoft.Admin.Entities; +using System; +using System.Collections.Generic; +using System.Linq.Expressions; +using System.Threading.Tasks; +using Volo.Abp.Domain.Repositories; + +namespace KonSoft.Admin.Repositories +{ + public interface IProductRepository : IRepository + { + Task> GetPageRootListAsync(int skipCount, int maxResult, Expression> where); + } +} diff --git a/modules/admin/src/KonSoft.Admin.EntityFrameworkCore/EntityFrameworkCore/Repositories/ProductRepository.cs b/modules/admin/src/KonSoft.Admin.EntityFrameworkCore/EntityFrameworkCore/Repositories/ProductRepository.cs new file mode 100644 index 0000000..c49f828 --- /dev/null +++ b/modules/admin/src/KonSoft.Admin.EntityFrameworkCore/EntityFrameworkCore/Repositories/ProductRepository.cs @@ -0,0 +1,25 @@ +using KonSoft.Admin.Entities; +using KonSoft.Admin.Repositories; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Linq.Expressions; +using System.Threading.Tasks; +using Volo.Abp.Domain.Repositories.EntityFrameworkCore; +using Volo.Abp.EntityFrameworkCore; + +namespace KonSoft.Admin.EntityFrameworkCore.Repositories +{ + public class ProductRepository : EfCoreRepository, IProductRepository + { + public ProductRepository(IDbContextProvider dbContextProvider) : base(dbContextProvider) + { + } + + public async Task> GetPageRootListAsync(int skipCount, int maxResultCount, Expression> where) + { + var queryable = await GetQueryableAsync(); + return queryable.PageBy(skipCount, maxResultCount).Where(where).OrderBy(x => x.Order).ToList(); + } + } +}