Files
web_template/internal/service/shop.go
2026-07-10 10:06:24 +00:00

61 lines
1.5 KiB
Go

package service
import (
"gotest/internal/repository"
)
// ShopService 电商商品服务
type ShopService struct {
repo *repository.ShopRepository
}
// NewShopService 创建电商服务实例
func NewShopService(repo *repository.ShopRepository) *ShopService {
return &ShopService{repo: repo}
}
// ListCategories 获取分类列表
func (s *ShopService) ListCategories() (interface{}, error) {
return s.repo.ListCategories()
}
// ListProducts 获取商品列表
func (s *ShopService) ListProducts(categoryID uint, keyword, sort string, page, pageSize int) (map[string]interface{}, error) {
products, total, err := s.repo.ListProducts(categoryID, keyword, sort, page, pageSize)
if err != nil {
return nil, err
}
return map[string]interface{}{
"list": products,
"total": total,
"page": page,
"page_size": pageSize,
}, nil
}
// GetProduct 获取商品详情
func (s *ShopService) GetProduct(id uint) (interface{}, error) {
return s.repo.GetProduct(id)
}
// ListFeatured 获取推荐商品
func (s *ShopService) ListFeatured(limit int) (interface{}, error) {
return s.repo.ListFeaturedProducts(limit)
}
// GetHomeData 获取商城首页数据(分类 + 推荐商品)
func (s *ShopService) GetHomeData() (map[string]interface{}, error) {
categories, err := s.repo.ListCategories()
if err != nil {
return nil, err
}
featured, err := s.repo.ListFeaturedProducts(8)
if err != nil {
return nil, err
}
return map[string]interface{}{
"categories": categories,
"featured": featured,
}, nil
}