2025-07-15 13:21:34 +08:00
|
|
|
|
package entities
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
|
"time"
|
|
|
|
|
|
|
2025-07-20 20:53:26 +08:00
|
|
|
|
"github.com/google/uuid"
|
2025-07-28 01:46:39 +08:00
|
|
|
|
"github.com/shopspring/decimal"
|
2025-07-15 13:21:34 +08:00
|
|
|
|
"gorm.io/gorm"
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
// Product 产品实体
|
|
|
|
|
|
type Product struct {
|
2025-07-28 01:46:39 +08:00
|
|
|
|
ID string `gorm:"primaryKey;type:varchar(36)" comment:"产品ID"`
|
|
|
|
|
|
Name string `gorm:"type:varchar(100);not null" comment:"产品名称"`
|
|
|
|
|
|
Code string `gorm:"type:varchar(50);uniqueIndex;not null" comment:"产品编号"`
|
|
|
|
|
|
Description string `gorm:"type:text" comment:"产品简介"`
|
|
|
|
|
|
Content string `gorm:"type:text" comment:"产品内容"`
|
|
|
|
|
|
CategoryID string `gorm:"type:varchar(36);not null" comment:"产品分类ID"`
|
|
|
|
|
|
Price decimal.Decimal `gorm:"type:decimal(10,2);not null;default:0" comment:"产品价格"`
|
|
|
|
|
|
IsEnabled bool `gorm:"default:true" comment:"是否启用"`
|
|
|
|
|
|
IsVisible bool `gorm:"default:true" comment:"是否展示"`
|
|
|
|
|
|
IsPackage bool `gorm:"default:false" comment:"是否组合包"`
|
|
|
|
|
|
// 组合包相关关联
|
|
|
|
|
|
PackageItems []*ProductPackageItem `gorm:"foreignKey:PackageID" comment:"组合包项目列表"`
|
2025-07-15 13:21:34 +08:00
|
|
|
|
// SEO信息
|
|
|
|
|
|
SEOTitle string `gorm:"type:varchar(200)" comment:"SEO标题"`
|
|
|
|
|
|
SEODescription string `gorm:"type:text" comment:"SEO描述"`
|
|
|
|
|
|
SEOKeywords string `gorm:"type:text" comment:"SEO关键词"`
|
2025-07-28 01:46:39 +08:00
|
|
|
|
|
2025-07-15 13:21:34 +08:00
|
|
|
|
// 关联关系
|
|
|
|
|
|
Category *ProductCategory `gorm:"foreignKey:CategoryID" comment:"产品分类"`
|
2025-07-28 01:46:39 +08:00
|
|
|
|
|
2025-07-15 13:21:34 +08:00
|
|
|
|
CreatedAt time.Time `gorm:"autoCreateTime" comment:"创建时间"`
|
|
|
|
|
|
UpdatedAt time.Time `gorm:"autoUpdateTime" comment:"更新时间"`
|
|
|
|
|
|
DeletedAt gorm.DeletedAt `gorm:"index" comment:"软删除时间"`
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-07-20 20:53:26 +08:00
|
|
|
|
// BeforeCreate GORM钩子:创建前自动生成UUID
|
|
|
|
|
|
func (p *Product) BeforeCreate(tx *gorm.DB) error {
|
|
|
|
|
|
if p.ID == "" {
|
|
|
|
|
|
p.ID = uuid.New().String()
|
|
|
|
|
|
}
|
|
|
|
|
|
return nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-07-15 13:21:34 +08:00
|
|
|
|
// IsValid 检查产品是否有效
|
|
|
|
|
|
func (p *Product) IsValid() bool {
|
|
|
|
|
|
return p.DeletedAt.Time.IsZero() && p.IsEnabled
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// IsVisibleToUser 检查产品是否对用户可见
|
|
|
|
|
|
func (p *Product) IsVisibleToUser() bool {
|
|
|
|
|
|
return p.IsValid() && p.IsVisible
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// CanBeSubscribed 检查产品是否可以订阅
|
|
|
|
|
|
func (p *Product) CanBeSubscribed() bool {
|
2025-07-28 23:28:24 +08:00
|
|
|
|
return p.IsValid()
|
2025-07-15 13:21:34 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// UpdateSEO 更新SEO信息
|
|
|
|
|
|
func (p *Product) UpdateSEO(title, description, keywords string) {
|
|
|
|
|
|
p.SEOTitle = title
|
|
|
|
|
|
p.SEODescription = description
|
|
|
|
|
|
p.SEOKeywords = keywords
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Enable 启用产品
|
|
|
|
|
|
func (p *Product) Enable() {
|
|
|
|
|
|
p.IsEnabled = true
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Disable 禁用产品
|
|
|
|
|
|
func (p *Product) Disable() {
|
|
|
|
|
|
p.IsEnabled = false
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Show 显示产品
|
|
|
|
|
|
func (p *Product) Show() {
|
|
|
|
|
|
p.IsVisible = true
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Hide 隐藏产品
|
|
|
|
|
|
func (p *Product) Hide() {
|
|
|
|
|
|
p.IsVisible = false
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// SetAsPackage 设置为组合包
|
|
|
|
|
|
func (p *Product) SetAsPackage() {
|
|
|
|
|
|
p.IsPackage = true
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-07-28 01:46:39 +08:00
|
|
|
|
func (p *Product) IsCombo() bool {
|
|
|
|
|
|
return p.IsPackage
|
|
|
|
|
|
}
|