package model import ( "database/sql/driver" "encoding/json" "time" "gorm.io/gorm" ) // JSONUintSlice 自定义JSON uint切片类型 type JSONUintSlice []uint func (j JSONUintSlice) Value() (driver.Value, error) { return json.Marshal(j) } func (j *JSONUintSlice) Scan(value interface{}) error { if value == nil { *j = nil return nil } bytes, ok := value.([]byte) if !ok { return nil } return json.Unmarshal(bytes, j) } // JSONSlice 自定义JSON切片类型 type JSONSlice []string func (j JSONSlice) Value() (driver.Value, error) { return json.Marshal(j) } func (j *JSONSlice) Scan(value interface{}) error { if value == nil { *j = nil return nil } bytes, ok := value.([]byte) if !ok { return nil } return json.Unmarshal(bytes, j) } // JSONMap 自定义JSON映射类型 type JSONMap map[string]interface{} func (j JSONMap) Value() (driver.Value, error) { if j == nil { return nil, nil } return json.Marshal(j) } func (j *JSONMap) Scan(value interface{}) error { if value == nil { *j = make(map[string]interface{}) return nil } switch v := value.(type) { case []byte: if len(v) == 0 { *j = make(map[string]interface{}) return nil } return json.Unmarshal(v, j) case string: if v == "" { *j = make(map[string]interface{}) return nil } return json.Unmarshal([]byte(v), j) default: *j = make(map[string]interface{}) return nil } } // Store 店铺 type Store struct { ID uint `json:"id" gorm:"primaryKey;autoIncrement"` Name string `json:"name" gorm:"size:100;not null"` Logo string `json:"logo" gorm:"size:255"` Description string `json:"description" gorm:"type:text"` Status int `json:"status" gorm:"default:1"` CreatedAt time.Time `json:"created_at"` UpdatedAt time.Time `json:"updated_at"` } // Category 商品分类 type Category struct { ID uint `json:"id" gorm:"primaryKey;autoIncrement"` Name string `json:"name" gorm:"size:50;not null"` ParentID *uint `json:"parent_id"` Level int `json:"level" gorm:"default:1"` Icon string `json:"icon" gorm:"size:255"` Description string `json:"description" gorm:"size:255"` Platform JSONSlice `json:"platform" gorm:"type:json;comment:平台标识列表"` Sort int `json:"sort" gorm:"default:0"` Status int `json:"status" gorm:"default:1"` CreatedAt time.Time `json:"created_at"` UpdatedAt time.Time `json:"updated_at"` // 关联字段 Children []Category `json:"children,omitempty" gorm:"-"` HasChildren bool `json:"hasChildren" gorm:"-"` } // Product 商品 type Product struct { ID uint `json:"id" gorm:"primaryKey;autoIncrement"` CategoryID JSONUintSlice `json:"category_id" gorm:"type:json;comment:分类ID列表"` // 改为JSON数组支持多分类 StoreID uint `json:"store_id" gorm:"default:1"` Name string `json:"name" gorm:"size:100;not null"` Description string `json:"description" gorm:"type:text"` Price float64 `json:"price" gorm:"type:decimal(10,2);not null"` OrigPrice float64 `json:"orig_price" gorm:"type:decimal(10,2)"` Stock int `json:"stock" gorm:"default:0"` Sales int `json:"sales" gorm:"default:0"` CommentCount int `json:"comment_count" gorm:"default:0"` AverageRating float64 `json:"average_rating" gorm:"type:decimal(3,2);default:0.00"` MainImage string `json:"main_image" gorm:"size:255"` Images JSONSlice `json:"images" gorm:"type:json"` VideoURL string `json:"video_url" gorm:"size:255"` DetailImages JSONSlice `json:"detail_images" gorm:"type:json"` Status int `json:"status" gorm:"default:1"` IsHot bool `json:"is_hot" gorm:"default:false"` IsNew bool `json:"is_new" gorm:"default:false"` IsRecommend bool `json:"is_recommend" gorm:"default:false"` LimitBuy int `json:"limit_buy" gorm:"default:0"` Points int `json:"points" gorm:"default:0"` Level int `json:"level" gorm:"default:0"` Weight float64 `json:"weight" gorm:"type:decimal(8,2)"` Unit string `json:"unit" gorm:"size:20"` Sort int `json:"sort" gorm:"default:0"` CreatedAt time.Time `json:"created_at"` UpdatedAt time.Time `json:"updated_at"` DeletedAt gorm.DeletedAt `json:"deleted_at,omitempty" gorm:"index"` // 关联数据 Categories []Category `json:"categories,omitempty" gorm:"-"` // 读取时填充的分类信息 Store Store `json:"store,omitempty" gorm:"foreignKey:StoreID"` SKUs []ProductSKU `json:"skus,omitempty" gorm:"foreignKey:ProductID"` Tags []ProductTag `json:"tags,omitempty" gorm:"many2many:ai_product_tag_relations;"` Specs []ProductSpec `json:"specs,omitempty" gorm:"foreignKey:ProductID"` ProductImages []ProductImage `json:"product_images,omitempty" gorm:"foreignKey:ProductID"` } // ProductSKU 商品SKU type ProductSKU struct { ID uint `json:"id" gorm:"primaryKey;autoIncrement"` ProductID uint `json:"product_id" gorm:"not null"` SKUCode string `json:"sku_code" gorm:"size:50;not null;uniqueIndex"` SpecValues JSONMap `json:"spec_values" gorm:"type:json;not null"` Price float64 `json:"price" gorm:"type:decimal(10,2);not null"` OriginalPrice float64 `json:"original_price" gorm:"type:decimal(10,2)"` Stock int `json:"stock" gorm:"default:0"` Image string `json:"image" gorm:"size:255"` Weight float64 `json:"weight" gorm:"type:decimal(8,2)"` Status int `json:"status" gorm:"default:1"` CreatedAt time.Time `json:"created_at"` UpdatedAt time.Time `json:"updated_at"` } // ProductTag 商品标签 type ProductTag struct { ID uint `json:"id" gorm:"primaryKey;autoIncrement"` Name string `json:"name" gorm:"size:50;not null"` Color string `json:"color" gorm:"size:20;default:#FF5722"` Sort int `json:"sort" gorm:"default:0"` Status int `json:"status" gorm:"default:1"` CreatedAt time.Time `json:"created_at"` UpdatedAt time.Time `json:"updated_at"` } // ProductSpec 商品规格 type ProductSpec struct { ID uint `json:"id" gorm:"primaryKey;autoIncrement"` ProductID uint `json:"product_id" gorm:"not null"` Name string `json:"name" gorm:"size:50;not null"` Value string `json:"value" gorm:"size:100;not null"` Price float64 `json:"price" gorm:"type:decimal(10,2)"` Stock int `json:"stock" gorm:"default:0"` SKU string `json:"sku" gorm:"size:50"` Sort int `json:"sort" gorm:"default:0"` CreatedAt time.Time `json:"created_at"` UpdatedAt time.Time `json:"updated_at"` } // ProductImage 商品图片 type ProductImage struct { ID uint `json:"id" gorm:"primaryKey;autoIncrement"` ProductID uint `json:"product_id" gorm:"not null"` ImageURL string `json:"image_url" gorm:"size:255;not null"` Sort int `json:"sort" gorm:"default:0"` CreatedAt time.Time `json:"created_at"` UpdatedAt time.Time `json:"updated_at"` } // ProductReview 商品评价 type ProductReview struct { ID uint `json:"id" gorm:"primaryKey;autoIncrement"` ProductID uint `json:"product_id" gorm:"not null"` UserID uint `json:"user_id" gorm:"not null"` OrderID *uint `json:"order_id"` Rating int `json:"rating" gorm:"not null"` Content string `json:"content" gorm:"type:text"` Images JSONSlice `json:"images" gorm:"type:json"` Status int `json:"status" gorm:"default:1"` CreatedAt time.Time `json:"created_at"` UpdatedAt time.Time `json:"updated_at"` } // TableName 指定表名 func (Store) TableName() string { return "ai_stores" } // TableName 指定表名 func (Category) TableName() string { return "ai_categories" } // TableName 指定表名 func (Product) TableName() string { return "ai_products" } // TableName 指定表名 func (ProductSKU) TableName() string { return "ai_product_skus" } // TableName 指定表名 func (ProductTag) TableName() string { return "ai_product_tags" } // TableName 指定表名 func (ProductSpec) TableName() string { return "ai_product_specs" } // TableName 指定表名 func (ProductImage) TableName() string { return "ai_product_images" } // TableName 指定表名 func (ProductReview) TableName() string { return "ai_product_reviews" }