web
This commit is contained in:
@@ -2,6 +2,8 @@ package repository
|
||||
|
||||
import (
|
||||
"dianshang/internal/model"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
@@ -46,7 +48,7 @@ func (r *ProductRepository) GetList(offset, limit int, conditions map[string]int
|
||||
for key, value := range conditions {
|
||||
switch key {
|
||||
case "category_id":
|
||||
// 支持包含子分类的筛选
|
||||
// category_id 现在是 JSON 数组,使用 JSON_CONTAINS 查询
|
||||
var catID uint
|
||||
switch v := value.(type) {
|
||||
case uint:
|
||||
@@ -57,12 +59,21 @@ func (r *ProductRepository) GetList(offset, limit int, conditions map[string]int
|
||||
catID = uint(v)
|
||||
}
|
||||
if catID > 0 {
|
||||
// 获取包含子分类的所有分类ID
|
||||
categoryIDs, err := r.getCategoryIDsIncludingChildren(catID)
|
||||
if err == nil && len(categoryIDs) > 0 {
|
||||
query = query.Where("category_id IN (?)", categoryIDs)
|
||||
// 使用 JSON_CONTAINS 查询包含任意一个分类ID的商品
|
||||
// 构建 OR 条件:JSON_CONTAINS(category_id, '1') OR JSON_CONTAINS(category_id, '2') ...
|
||||
conditions := make([]string, len(categoryIDs))
|
||||
args := make([]interface{}, len(categoryIDs))
|
||||
for i, id := range categoryIDs {
|
||||
conditions[i] = "JSON_CONTAINS(category_id, ?)"
|
||||
args[i] = fmt.Sprintf("%d", id)
|
||||
}
|
||||
query = query.Where(strings.Join(conditions, " OR "), args...)
|
||||
} else {
|
||||
// 兜底:如果获取子分类失败,退化为当前分类
|
||||
query = query.Where("category_id = ?", catID)
|
||||
// 兜底:如果获取子分类失败,只查询当前分类
|
||||
query = query.Where("JSON_CONTAINS(category_id, ?)", fmt.Sprintf("%d", catID))
|
||||
}
|
||||
}
|
||||
case "keyword":
|
||||
@@ -71,6 +82,15 @@ func (r *ProductRepository) GetList(offset, limit int, conditions map[string]int
|
||||
query = query.Where("price >= ?", value)
|
||||
case "max_price":
|
||||
query = query.Where("price <= ?", value)
|
||||
case "in_stock":
|
||||
// 库存筛选:true=有货,false=缺货
|
||||
if inStockValue, ok := value.(bool); ok {
|
||||
if inStockValue {
|
||||
query = query.Where("stock > ?", 0)
|
||||
} else {
|
||||
query = query.Where("stock = ?", 0)
|
||||
}
|
||||
}
|
||||
case "is_hot":
|
||||
if value.(string) == "true" {
|
||||
query = query.Where("is_hot = ?", true)
|
||||
@@ -129,16 +149,15 @@ func (r *ProductRepository) GetList(offset, limit int, conditions map[string]int
|
||||
}
|
||||
}
|
||||
|
||||
// 获取列表,预加载分类
|
||||
err := query.Preload("Category").
|
||||
Offset(offset).Limit(limit).Order(orderBy).Find(&products).Error
|
||||
// 获取列表
|
||||
err := query.Offset(offset).Limit(limit).Order(orderBy).Find(&products).Error
|
||||
return products, total, err
|
||||
}
|
||||
|
||||
// GetByID 根据ID获取产品详情
|
||||
func (r *ProductRepository) GetByID(id uint) (*model.Product, error) {
|
||||
var product model.Product
|
||||
err := r.db.Preload("Category").Preload("Specs").Preload("SKUs", "status = ?", 1).
|
||||
err := r.db.Preload("Specs").Preload("SKUs", "status = ?", 1).
|
||||
Where("id = ?", id).First(&product).Error
|
||||
return &product, err
|
||||
}
|
||||
@@ -216,8 +235,21 @@ func (r *ProductRepository) RestoreStock(id uint, quantity int) error {
|
||||
|
||||
// GetCategories 获取分类列表
|
||||
func (r *ProductRepository) GetCategories() ([]model.Category, error) {
|
||||
return r.GetCategoriesByPlatform("")
|
||||
}
|
||||
|
||||
// GetCategoriesByPlatform 根据平台获取分类列表
|
||||
func (r *ProductRepository) GetCategoriesByPlatform(platformCode string) ([]model.Category, error) {
|
||||
var allCategories []model.Category
|
||||
err := r.db.Where("status = ?", 1).Order("level ASC, sort DESC, created_at ASC").Find(&allCategories).Error
|
||||
query := r.db.Where("status = ?", 1)
|
||||
|
||||
// 如果指定了平台,筛选包含该平台的分类
|
||||
if platformCode != "" {
|
||||
// 使用 JSON_CONTAINS 查询包含指定平台的分类
|
||||
query = query.Where("JSON_CONTAINS(platform, ?)", `"`+platformCode+`"`)
|
||||
}
|
||||
|
||||
err := query.Order("level ASC, sort DESC, created_at ASC").Find(&allCategories).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -409,8 +441,7 @@ func (r *ProductRepository) DeleteProductSpec(id uint) error {
|
||||
// GetHotProducts 获取热门产品
|
||||
func (r *ProductRepository) GetHotProducts(limit int) ([]model.Product, error) {
|
||||
var products []model.Product
|
||||
err := r.db.Preload("Category").
|
||||
Where("status = ? AND is_hot = ?", 1, 1).
|
||||
err := r.db.Where("status = ? AND is_hot = ?", 1, 1).
|
||||
Order("sales DESC, created_at DESC").Limit(limit).Find(&products).Error
|
||||
return products, err
|
||||
}
|
||||
@@ -418,8 +449,7 @@ func (r *ProductRepository) GetHotProducts(limit int) ([]model.Product, error) {
|
||||
// GetRecommendProducts 获取推荐产品
|
||||
func (r *ProductRepository) GetRecommendProducts(limit int) ([]model.Product, error) {
|
||||
var products []model.Product
|
||||
err := r.db.Preload("Category").
|
||||
Where("status = ? AND is_recommend = ?", 1, 1).
|
||||
err := r.db.Where("status = ? AND is_recommend = ?", 1, 1).
|
||||
Order("sort DESC, created_at DESC").Limit(limit).Find(&products).Error
|
||||
return products, err
|
||||
}
|
||||
@@ -660,7 +690,7 @@ func (r *ProductRepository) AssignTagsToProduct(productID uint, tagIDs []uint) e
|
||||
func (r *ProductRepository) GetLowStockProducts(threshold int) ([]model.Product, error) {
|
||||
var products []model.Product
|
||||
err := r.db.Where("stock <= ? AND status = ?", threshold, 1).
|
||||
Preload("Category").Find(&products).Error
|
||||
Find(&products).Error
|
||||
return products, err
|
||||
}
|
||||
|
||||
@@ -698,7 +728,7 @@ func (r *ProductRepository) GetInventoryStatistics() (map[string]interface{}, er
|
||||
// GetProductsForExport 获取用于导出的商品数据
|
||||
func (r *ProductRepository) GetProductsForExport(conditions map[string]interface{}) ([]model.Product, error) {
|
||||
var products []model.Product
|
||||
query := r.db.Model(&model.Product{}).Preload("Category")
|
||||
query := r.db.Model(&model.Product{})
|
||||
|
||||
// 添加查询条件
|
||||
for key, value := range conditions {
|
||||
|
||||
Reference in New Issue
Block a user