You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
99 lines
3.3 KiB
99 lines
3.3 KiB
package impl
|
|
|
|
import (
|
|
"health-ai/internal/database"
|
|
"health-ai/internal/model"
|
|
)
|
|
|
|
type ProductRepository struct{}
|
|
|
|
func NewProductRepository() *ProductRepository {
|
|
return &ProductRepository{}
|
|
}
|
|
|
|
// GetByID 根据ID获取产品
|
|
func (r *ProductRepository) GetByID(id uint) (*model.Product, error) {
|
|
var product model.Product
|
|
err := database.DB.Where("is_active = ?", true).First(&product, id).Error
|
|
return &product, err
|
|
}
|
|
|
|
// GetAll 获取所有产品(分页)
|
|
func (r *ProductRepository) GetAll(page, pageSize int) ([]model.Product, int64, error) {
|
|
var products []model.Product
|
|
var total int64
|
|
|
|
db := database.DB.Model(&model.Product{}).Where("is_active = ?", true)
|
|
db.Count(&total)
|
|
|
|
offset := (page - 1) * pageSize
|
|
err := db.Offset(offset).Limit(pageSize).Find(&products).Error
|
|
return products, total, err
|
|
}
|
|
|
|
// GetByCategory 按分类获取产品
|
|
func (r *ProductRepository) GetByCategory(category string) ([]model.Product, error) {
|
|
var products []model.Product
|
|
err := database.DB.Where("category = ? AND is_active = ?", category, true).Find(&products).Error
|
|
return products, err
|
|
}
|
|
|
|
// GetByConstitution 根据体质获取推荐产品
|
|
func (r *ProductRepository) GetByConstitution(constitutionType string) ([]model.Product, error) {
|
|
var products []model.Product
|
|
err := database.DB.Joins("JOIN constitution_products ON products.id = constitution_products.product_id").
|
|
Where("constitution_products.constitution_type = ? AND products.is_active = ?", constitutionType, true).
|
|
Order("constitution_products.priority ASC").
|
|
Find(&products).Error
|
|
return products, err
|
|
}
|
|
|
|
// SearchByKeyword 根据症状关键词搜索产品
|
|
func (r *ProductRepository) SearchByKeyword(keyword string) ([]model.Product, error) {
|
|
var products []model.Product
|
|
|
|
// 先从症状-产品关联表搜索
|
|
subQuery := database.DB.Table("symptom_products").
|
|
Select("product_id").
|
|
Where("keyword LIKE ?", "%"+keyword+"%")
|
|
|
|
err := database.DB.Where("id IN (?) AND is_active = ?", subQuery, true).Find(&products).Error
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
// 如果没找到,从产品名称和描述中搜索
|
|
if len(products) == 0 {
|
|
err = database.DB.Where("(name LIKE ? OR description LIKE ? OR efficacy LIKE ?) AND is_active = ?",
|
|
"%"+keyword+"%", "%"+keyword+"%", "%"+keyword+"%", true).Find(&products).Error
|
|
}
|
|
|
|
return products, err
|
|
}
|
|
|
|
// GetConstitutionProducts 获取体质-产品关联
|
|
func (r *ProductRepository) GetConstitutionProducts(constitutionType string) ([]model.ConstitutionProduct, error) {
|
|
var cps []model.ConstitutionProduct
|
|
err := database.DB.Where("constitution_type = ?", constitutionType).Order("priority ASC").Find(&cps).Error
|
|
return cps, err
|
|
}
|
|
|
|
// CreatePurchaseHistory 创建购买历史
|
|
func (r *ProductRepository) CreatePurchaseHistory(history *model.PurchaseHistory) error {
|
|
return database.DB.Create(history).Error
|
|
}
|
|
|
|
// GetPurchaseHistory 获取用户购买历史
|
|
func (r *ProductRepository) GetPurchaseHistory(userID uint) ([]model.PurchaseHistory, error) {
|
|
var histories []model.PurchaseHistory
|
|
err := database.DB.Where("user_id = ?", userID).Order("purchased_at DESC").Find(&histories).Error
|
|
return histories, err
|
|
}
|
|
|
|
// BatchCreatePurchaseHistory 批量创建购买历史
|
|
func (r *ProductRepository) BatchCreatePurchaseHistory(histories []model.PurchaseHistory) error {
|
|
if len(histories) == 0 {
|
|
return nil
|
|
}
|
|
return database.DB.Create(&histories).Error
|
|
}
|
|
|