web
This commit is contained in:
@@ -25,7 +25,7 @@ func NewProductService(productRepo *repository.ProductRepository, userRepo *repo
|
||||
}
|
||||
|
||||
// GetProductList 获取产品列表(前端用户)
|
||||
func (s *ProductService) GetProductList(page, pageSize int, categoryID uint, keyword string, minPrice, maxPrice float64, sort, sortType string) ([]model.Product, *utils.Pagination, error) {
|
||||
func (s *ProductService) GetProductList(page, pageSize int, categoryID uint, keyword string, minPrice, maxPrice float64, inStock *bool, sort, sortType string) ([]model.Product, *utils.Pagination, error) {
|
||||
if page <= 0 {
|
||||
page = 1
|
||||
}
|
||||
@@ -48,6 +48,9 @@ func (s *ProductService) GetProductList(page, pageSize int, categoryID uint, key
|
||||
if maxPrice > 0 {
|
||||
conditions["max_price"] = maxPrice
|
||||
}
|
||||
if inStock != nil {
|
||||
conditions["in_stock"] = *inStock
|
||||
}
|
||||
if sort != "" {
|
||||
conditions["sort"] = sort
|
||||
}
|
||||
@@ -128,10 +131,12 @@ func (s *ProductService) GetProductDetail(id uint) (*model.Product, error) {
|
||||
// CreateProduct 创建产品
|
||||
func (s *ProductService) CreateProduct(product *model.Product) error {
|
||||
// 验证分类是否存在
|
||||
if product.CategoryID > 0 {
|
||||
_, err := s.productRepo.GetCategoryByID(product.CategoryID)
|
||||
if err != nil {
|
||||
return errors.New("分类不存在")
|
||||
if len(product.CategoryID) > 0 {
|
||||
for _, catID := range product.CategoryID {
|
||||
_, err := s.productRepo.GetCategoryByID(catID)
|
||||
if err != nil {
|
||||
return errors.New("分类ID" + strconv.Itoa(int(catID)) + "不存在")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -146,28 +151,6 @@ func (s *ProductService) UpdateProduct(id uint, updates map[string]interface{})
|
||||
return errors.New("产品不存在")
|
||||
}
|
||||
|
||||
// 如果更新分类,验证分类是否存在
|
||||
if categoryID, ok := updates["category_id"]; ok {
|
||||
var catID uint
|
||||
switch v := categoryID.(type) {
|
||||
case uint:
|
||||
catID = v
|
||||
case float64:
|
||||
catID = uint(v)
|
||||
case int:
|
||||
catID = uint(v)
|
||||
default:
|
||||
return errors.New("分类ID格式错误")
|
||||
}
|
||||
|
||||
if catID > 0 {
|
||||
_, err := s.productRepo.GetCategoryByID(catID)
|
||||
if err != nil {
|
||||
return errors.New("分类不存在")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 处理 detail_images 字段 - 确保正确转换为 JSONSlice 类型
|
||||
if detailImages, ok := updates["detail_images"]; ok {
|
||||
switch v := detailImages.(type) {
|
||||
@@ -376,6 +359,11 @@ func (s *ProductService) GetCategories() ([]model.Category, error) {
|
||||
return s.productRepo.GetCategories()
|
||||
}
|
||||
|
||||
// GetCategoriesByPlatform 根据平台获取分类列表
|
||||
func (s *ProductService) GetCategoriesByPlatform(platform string) ([]model.Category, error) {
|
||||
return s.productRepo.GetCategoriesByPlatform(platform)
|
||||
}
|
||||
|
||||
// CreateCategory 创建分类
|
||||
func (s *ProductService) CreateCategory(category *model.Category) error {
|
||||
return s.productRepo.CreateCategory(category)
|
||||
@@ -492,7 +480,8 @@ func (s *ProductService) SearchProducts(keyword string, page, pageSize int, minP
|
||||
return []model.Product{}, utils.NewPagination(page, pageSize), nil
|
||||
}
|
||||
|
||||
return s.GetProductList(page, pageSize, 0, keyword, minPrice, maxPrice, sort, sortType)
|
||||
// 搜索不筛选库存,传递 nil
|
||||
return s.GetProductList(page, pageSize, 0, keyword, minPrice, maxPrice, nil, sort, sortType)
|
||||
}
|
||||
|
||||
// UpdateStock 更新库存
|
||||
|
||||
Reference in New Issue
Block a user