product
This commit is contained in:
@ -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; }
|
||||
}
|
||||
}
|
||||
|
||||
@ -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
|
||||
{
|
||||
/// <summary>
|
||||
/// 嵌套子类
|
||||
/// </summary>
|
||||
public List<CreateProductDto> Children { get; set; } = new();
|
||||
}
|
||||
|
||||
public class CreateProductDto
|
||||
{
|
||||
/// <summary>
|
||||
/// 分类名称
|
||||
/// </summary>
|
||||
public string Name { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// 商品编码
|
||||
/// </summary>
|
||||
public string Code { get; private set; }
|
||||
/// <summary>
|
||||
/// 商品价格
|
||||
/// </summary>
|
||||
public decimal Price { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// 商品描述
|
||||
/// </summary>
|
||||
public string Description { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// 分类类型(大类或小类)
|
||||
/// </summary>
|
||||
public string Type { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// 父分类ID
|
||||
/// </summary>
|
||||
public Guid? ParentId { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// 商品状态(在售/下架等)
|
||||
/// </summary>
|
||||
public string Status { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// 排序字段
|
||||
/// </summary>
|
||||
public int Order { get; private set; }
|
||||
}
|
||||
|
||||
public class UpdateProductDto: CreateProductDto
|
||||
{
|
||||
public Guid Id { get; set; }
|
||||
}
|
||||
}
|
||||
@ -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<OrderDto> GetAsync(Guid id);
|
||||
Task<PagedResultDto<OrderDto>> GetListAsync(PagedResultRequestDto input);
|
||||
}
|
||||
}
|
||||
|
||||
@ -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<ProductDto> GetAsync(Guid id);
|
||||
Task<PagedResultDto<ProductDto>> GetListAsync(PagedResultRequestDto input);
|
||||
}
|
||||
}
|
||||
@ -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
|
||||
/// <summary>
|
||||
/// 获取所有订单列表
|
||||
/// </summary>
|
||||
public async Task<PagedResultDto<OrderDto>> GetListAsync(PageListInput input)
|
||||
public async Task<PagedResultDto<OrderDto>> 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();
|
||||
|
||||
|
||||
@ -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;
|
||||
|
||||
/// <summary>
|
||||
/// 创建产品
|
||||
/// </summary>
|
||||
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);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 删除
|
||||
/// </summary>
|
||||
public async Task DeleteAsync(Guid id)
|
||||
{
|
||||
await repository.DeleteAsync(x => x.Id == id);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 查询
|
||||
/// </summary>
|
||||
public async Task<ProductDto> GetAsync(Guid id)
|
||||
{
|
||||
var product = await repository.GetAsync(x => x.Id == id);
|
||||
return ObjectMapper.Map<Product, ProductDto>(product);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 查询集合
|
||||
/// </summary>
|
||||
/// <param name="input"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<PagedResultDto<ProductDto>> 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<Product>, List<ProductDto>>(query);
|
||||
|
||||
return new PagedResultDto<ProductDto>(totalCount, productDtos);
|
||||
}
|
||||
|
||||
private static void BuildChildren(Product parent, List<Product> 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); // 递归
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 修改
|
||||
/// </summary>
|
||||
/// <param name="input"></param>
|
||||
/// <returns></returns>
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
99
modules/admin/src/KonSoft.Admin.Domain/Entities/Product.cs
Normal file
99
modules/admin/src/KonSoft.Admin.Domain/Entities/Product.cs
Normal file
@ -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<Guid>(id)
|
||||
{
|
||||
/// <summary>
|
||||
/// 分类名称
|
||||
/// </summary>
|
||||
public string Name { get; private set; } = name;
|
||||
|
||||
/// <summary>
|
||||
/// 商品编码
|
||||
/// </summary>
|
||||
public string Code { get; private set; } = code;
|
||||
|
||||
/// <summary>
|
||||
/// 商品价格
|
||||
/// </summary>
|
||||
public decimal Price { get; private set; } = price;
|
||||
|
||||
/// <summary>
|
||||
/// 商品描述
|
||||
/// </summary>
|
||||
public string Description { get; private set; } = description;
|
||||
|
||||
/// <summary>
|
||||
/// 分类类型(大类或小类)
|
||||
/// </summary>
|
||||
public string Type { get; private set; } = type;
|
||||
|
||||
/// <summary>
|
||||
/// 父分类ID
|
||||
/// </summary>
|
||||
public Guid? ParentId { get; private set; } = parentId;
|
||||
|
||||
/// <summary>
|
||||
/// 商品状态(在售/下架等)
|
||||
/// </summary>
|
||||
public string Status { get; private set; } = status;
|
||||
|
||||
/// <summary>
|
||||
/// 排序字段
|
||||
/// </summary>
|
||||
public int Order { get; private set; } = order;
|
||||
|
||||
[NotMapped]
|
||||
public List<Product> Children { get; set; } = new();
|
||||
|
||||
/// <summary>
|
||||
/// 修改商品信息
|
||||
/// </summary>
|
||||
public void Update(string name, string code, decimal price, string description, int order)
|
||||
{
|
||||
Name = name;
|
||||
Code = code;
|
||||
Price = price;
|
||||
Description = description;
|
||||
Order = order;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 修改商品分类
|
||||
/// </summary>
|
||||
public void ChangeCategory(string type, Guid? parentId)
|
||||
{
|
||||
Type = type;
|
||||
ParentId = parentId;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 上架商品
|
||||
/// </summary>
|
||||
public void PutOnSale()
|
||||
{
|
||||
Status = "在售";
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 下架商品
|
||||
/// </summary>
|
||||
public void TakeOffSale()
|
||||
{
|
||||
Status = "下架";
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 修改价格
|
||||
/// </summary>
|
||||
public void ChangePrice(decimal newPrice)
|
||||
{
|
||||
Price = newPrice;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -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<Product>
|
||||
{
|
||||
Task<List<Product>> GetPageRootListAsync(int skipCount, int maxResult, Expression<Func<Product, bool>> where);
|
||||
}
|
||||
}
|
||||
@ -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<AdminDbContext, Product>, IProductRepository
|
||||
{
|
||||
public ProductRepository(IDbContextProvider<AdminDbContext> dbContextProvider) : base(dbContextProvider)
|
||||
{
|
||||
}
|
||||
|
||||
public async Task<List<Product>> GetPageRootListAsync(int skipCount, int maxResultCount, Expression<Func<Product, bool>> where)
|
||||
{
|
||||
var queryable = await GetQueryableAsync();
|
||||
return queryable.PageBy(skipCount, maxResultCount).Where(where).OrderBy(x => x.Order).ToList();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user