v0.1
This commit is contained in:
@@ -4,30 +4,32 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/shopspring/decimal"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// Product 产品实体
|
||||
type Product struct {
|
||||
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 float64 `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:"是否组合包"`
|
||||
|
||||
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:"组合包项目列表"`
|
||||
// SEO信息
|
||||
SEOTitle string `gorm:"type:varchar(200)" comment:"SEO标题"`
|
||||
SEODescription string `gorm:"type:text" comment:"SEO描述"`
|
||||
SEOKeywords string `gorm:"type:text" comment:"SEO关键词"`
|
||||
|
||||
|
||||
// 关联关系
|
||||
Category *ProductCategory `gorm:"foreignKey:CategoryID" comment:"产品分类"`
|
||||
|
||||
|
||||
CreatedAt time.Time `gorm:"autoCreateTime" comment:"创建时间"`
|
||||
UpdatedAt time.Time `gorm:"autoUpdateTime" comment:"更新时间"`
|
||||
DeletedAt gorm.DeletedAt `gorm:"index" comment:"软删除时间"`
|
||||
@@ -56,13 +58,6 @@ func (p *Product) CanBeSubscribed() bool {
|
||||
return p.IsValid() && p.IsVisible
|
||||
}
|
||||
|
||||
// GetDisplayPrice 获取显示价格
|
||||
func (p *Product) GetDisplayPrice() float64 {
|
||||
if p.Price < 0 {
|
||||
return 0
|
||||
}
|
||||
return p.Price
|
||||
}
|
||||
|
||||
// UpdateSEO 更新SEO信息
|
||||
func (p *Product) UpdateSEO(title, description, keywords string) {
|
||||
@@ -96,7 +91,6 @@ func (p *Product) SetAsPackage() {
|
||||
p.IsPackage = true
|
||||
}
|
||||
|
||||
// SetAsSingle 设置为单个产品
|
||||
func (p *Product) SetAsSingle() {
|
||||
p.IsPackage = false
|
||||
}
|
||||
func (p *Product) IsCombo() bool {
|
||||
return p.IsPackage
|
||||
}
|
||||
159
internal/domains/product/entities/product_api_config.go
Normal file
159
internal/domains/product/entities/product_api_config.go
Normal file
@@ -0,0 +1,159 @@
|
||||
package entities
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// ProductApiConfig 产品API配置实体
|
||||
type ProductApiConfig struct {
|
||||
ID string `gorm:"primaryKey;type:varchar(36)" comment:"配置ID"`
|
||||
ProductID string `gorm:"type:varchar(36);not null;uniqueIndex" comment:"产品ID"`
|
||||
|
||||
// 请求参数配置
|
||||
RequestParams string `gorm:"type:json;not null" comment:"请求参数配置JSON"`
|
||||
|
||||
// 响应字段配置
|
||||
ResponseFields string `gorm:"type:json;not null" comment:"响应字段配置JSON"`
|
||||
|
||||
// 响应示例
|
||||
ResponseExample string `gorm:"type:json;not null" comment:"响应示例JSON"`
|
||||
|
||||
// 关联关系
|
||||
Product *Product `gorm:"foreignKey:ProductID" comment:"产品"`
|
||||
|
||||
CreatedAt time.Time `gorm:"autoCreateTime" comment:"创建时间"`
|
||||
UpdatedAt time.Time `gorm:"autoUpdateTime" comment:"更新时间"`
|
||||
DeletedAt gorm.DeletedAt `gorm:"index" comment:"软删除时间"`
|
||||
}
|
||||
|
||||
// RequestParam 请求参数结构
|
||||
type RequestParam struct {
|
||||
Name string `json:"name" comment:"参数名称"`
|
||||
Field string `json:"field" comment:"参数字段名"`
|
||||
Type string `json:"type" comment:"参数类型"`
|
||||
Required bool `json:"required" comment:"是否必填"`
|
||||
Description string `json:"description" comment:"参数描述"`
|
||||
Example string `json:"example" comment:"参数示例"`
|
||||
Validation string `json:"validation" comment:"验证规则"`
|
||||
}
|
||||
|
||||
// ResponseField 响应字段结构
|
||||
type ResponseField struct {
|
||||
Name string `json:"name" comment:"字段名称"`
|
||||
Path string `json:"path" comment:"字段路径"`
|
||||
Type string `json:"type" comment:"字段类型"`
|
||||
Description string `json:"description" comment:"字段描述"`
|
||||
Required bool `json:"required" comment:"是否必填"`
|
||||
Example string `json:"example" comment:"字段示例"`
|
||||
}
|
||||
|
||||
// BeforeCreate GORM钩子:创建前自动生成UUID
|
||||
func (pac *ProductApiConfig) BeforeCreate(tx *gorm.DB) error {
|
||||
if pac.ID == "" {
|
||||
pac.ID = uuid.New().String()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
|
||||
// Validate 验证产品API配置
|
||||
func (pac *ProductApiConfig) Validate() error {
|
||||
if pac.ProductID == "" {
|
||||
return NewValidationError("产品ID不能为空")
|
||||
}
|
||||
if pac.RequestParams == "" {
|
||||
return NewValidationError("请求参数配置不能为空")
|
||||
}
|
||||
if pac.ResponseFields == "" {
|
||||
return NewValidationError("响应字段配置不能为空")
|
||||
}
|
||||
if pac.ResponseExample == "" {
|
||||
return NewValidationError("响应示例不能为空")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// NewValidationError 创建验证错误
|
||||
func NewValidationError(message string) error {
|
||||
return &ValidationError{Message: message}
|
||||
}
|
||||
|
||||
// ValidationError 验证错误
|
||||
type ValidationError struct {
|
||||
Message string
|
||||
}
|
||||
|
||||
func (e *ValidationError) Error() string {
|
||||
return e.Message
|
||||
}
|
||||
|
||||
// GetRequestParams 获取请求参数列表
|
||||
func (pac *ProductApiConfig) GetRequestParams() ([]RequestParam, error) {
|
||||
var params []RequestParam
|
||||
if pac.RequestParams != "" {
|
||||
err := json.Unmarshal([]byte(pac.RequestParams), ¶ms)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
return params, nil
|
||||
}
|
||||
|
||||
// SetRequestParams 设置请求参数列表
|
||||
func (pac *ProductApiConfig) SetRequestParams(params []RequestParam) error {
|
||||
data, err := json.Marshal(params)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
pac.RequestParams = string(data)
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetResponseFields 获取响应字段列表
|
||||
func (pac *ProductApiConfig) GetResponseFields() ([]ResponseField, error) {
|
||||
var fields []ResponseField
|
||||
if pac.ResponseFields != "" {
|
||||
err := json.Unmarshal([]byte(pac.ResponseFields), &fields)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
return fields, nil
|
||||
}
|
||||
|
||||
// SetResponseFields 设置响应字段列表
|
||||
func (pac *ProductApiConfig) SetResponseFields(fields []ResponseField) error {
|
||||
data, err := json.Marshal(fields)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
pac.ResponseFields = string(data)
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetResponseExample 获取响应示例
|
||||
func (pac *ProductApiConfig) GetResponseExample() (map[string]interface{}, error) {
|
||||
var example map[string]interface{}
|
||||
if pac.ResponseExample != "" {
|
||||
err := json.Unmarshal([]byte(pac.ResponseExample), &example)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
return example, nil
|
||||
}
|
||||
|
||||
// SetResponseExample 设置响应示例
|
||||
func (pac *ProductApiConfig) SetResponseExample(example map[string]interface{}) error {
|
||||
data, err := json.Marshal(example)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
pac.ResponseExample = string(data)
|
||||
return nil
|
||||
}
|
||||
@@ -9,17 +9,17 @@ import (
|
||||
|
||||
// ProductCategory 产品分类实体
|
||||
type ProductCategory struct {
|
||||
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:"分类描述"`
|
||||
Sort int `gorm:"default:0" comment:"排序"`
|
||||
IsEnabled bool `gorm:"default:true" comment:"是否启用"`
|
||||
IsVisible bool `gorm:"default:true" comment:"是否展示"`
|
||||
|
||||
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:"分类描述"`
|
||||
Sort int `gorm:"default:0" comment:"排序"`
|
||||
IsEnabled bool `gorm:"default:true" comment:"是否启用"`
|
||||
IsVisible bool `gorm:"default:true" comment:"是否展示"`
|
||||
|
||||
// 关联关系
|
||||
Products []Product `gorm:"foreignKey:CategoryID" comment:"产品列表"`
|
||||
|
||||
|
||||
CreatedAt time.Time `gorm:"autoCreateTime" comment:"创建时间"`
|
||||
UpdatedAt time.Time `gorm:"autoUpdateTime" comment:"更新时间"`
|
||||
DeletedAt gorm.DeletedAt `gorm:"index" comment:"软删除时间"`
|
||||
@@ -61,4 +61,4 @@ func (pc *ProductCategory) Show() {
|
||||
// Hide 隐藏分类
|
||||
func (pc *ProductCategory) Hide() {
|
||||
pc.IsVisible = false
|
||||
}
|
||||
}
|
||||
|
||||
32
internal/domains/product/entities/product_package_item.go
Normal file
32
internal/domains/product/entities/product_package_item.go
Normal file
@@ -0,0 +1,32 @@
|
||||
package entities
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// ProductPackageItem 产品组合包项目
|
||||
type ProductPackageItem struct {
|
||||
ID string `gorm:"primaryKey;type:varchar(36)"`
|
||||
PackageID string `gorm:"type:varchar(36);not null;index" comment:"组合包产品ID"`
|
||||
ProductID string `gorm:"type:varchar(36);not null;index" comment:"子产品ID"`
|
||||
SortOrder int `gorm:"default:0" comment:"排序"`
|
||||
|
||||
// 关联关系
|
||||
Package *Product `gorm:"foreignKey:PackageID" comment:"组合包产品"`
|
||||
Product *Product `gorm:"foreignKey:ProductID" comment:"子产品"`
|
||||
|
||||
CreatedAt time.Time `gorm:"autoCreateTime"`
|
||||
UpdatedAt time.Time `gorm:"autoUpdateTime"`
|
||||
DeletedAt gorm.DeletedAt `gorm:"index"`
|
||||
}
|
||||
|
||||
// BeforeCreate GORM钩子:创建前自动生成UUID
|
||||
func (ppi *ProductPackageItem) BeforeCreate(tx *gorm.DB) error {
|
||||
if ppi.ID == "" {
|
||||
ppi.ID = uuid.New().String()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
53
internal/domains/product/entities/product_parameter.go
Normal file
53
internal/domains/product/entities/product_parameter.go
Normal file
@@ -0,0 +1,53 @@
|
||||
package entities
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// ProductParameter 产品参数配置实体
|
||||
type ProductParameter struct {
|
||||
ID string `gorm:"primaryKey;type:varchar(36)" comment:"参数配置ID"`
|
||||
ProductID string `gorm:"type:varchar(36);not null;index" comment:"产品ID"`
|
||||
Name string `gorm:"type:varchar(100);not null" comment:"参数名称"`
|
||||
Field string `gorm:"type:varchar(50);not null" comment:"参数字段名"`
|
||||
Type string `gorm:"type:varchar(20);not null;default:'string'" comment:"参数类型"`
|
||||
Required bool `gorm:"default:true" comment:"是否必填"`
|
||||
Description string `gorm:"type:text" comment:"参数描述"`
|
||||
Example string `gorm:"type:varchar(200)" comment:"参数示例"`
|
||||
Validation string `gorm:"type:text" comment:"验证规则"`
|
||||
SortOrder int `gorm:"default:0" comment:"排序"`
|
||||
|
||||
// 关联关系
|
||||
Product *Product `gorm:"foreignKey:ProductID" comment:"产品"`
|
||||
|
||||
CreatedAt time.Time `gorm:"autoCreateTime" comment:"创建时间"`
|
||||
UpdatedAt time.Time `gorm:"autoUpdateTime" comment:"更新时间"`
|
||||
DeletedAt gorm.DeletedAt `gorm:"index" comment:"软删除时间"`
|
||||
}
|
||||
|
||||
// BeforeCreate GORM钩子:创建前自动生成UUID
|
||||
func (pp *ProductParameter) BeforeCreate(tx *gorm.DB) error {
|
||||
if pp.ID == "" {
|
||||
pp.ID = uuid.New().String()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// IsValid 检查参数配置是否有效
|
||||
func (pp *ProductParameter) IsValid() bool {
|
||||
return pp.DeletedAt.Time.IsZero()
|
||||
}
|
||||
|
||||
// GetValidationRules 获取验证规则
|
||||
func (pp *ProductParameter) GetValidationRules() map[string]interface{} {
|
||||
if pp.Validation == "" {
|
||||
return nil
|
||||
}
|
||||
|
||||
// 这里可以解析JSON格式的验证规则
|
||||
// 暂时返回空map,后续可以扩展
|
||||
return make(map[string]interface{})
|
||||
}
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/shopspring/decimal"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
@@ -12,7 +13,7 @@ type Subscription struct {
|
||||
ID string `gorm:"primaryKey;type:varchar(36)" comment:"订阅ID"`
|
||||
UserID string `gorm:"type:varchar(36);not null;index" comment:"用户ID"`
|
||||
ProductID string `gorm:"type:varchar(36);not null;index" comment:"产品ID"`
|
||||
Price float64 `gorm:"type:decimal(10,2);not null" comment:"订阅价格"`
|
||||
Price decimal.Decimal `gorm:"type:decimal(10,2);not null" comment:"订阅价格"`
|
||||
APIUsed int64 `gorm:"default:0" comment:"已使用API调用次数"`
|
||||
|
||||
// 关联关系
|
||||
|
||||
Reference in New Issue
Block a user