model

package
v0.0.0-...-7f52212 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 24, 2024 License: MIT Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Banner struct {
	BaseModel
	Image string `gorm:"type:varchar(200); not null; default:''; comment:'图片路径'"`
	URL   string `gorm:"type:varchar(200); not null; default:''; comment:'跳转地址'"`
	Index int32  `gorm:"type:int; not null; default:1; comment:'排序'"`
}

func (Banner) TableName

func (Banner) TableName() string

type BaseModel

type BaseModel struct {
	// 在数据库中,一个表中的外键如果与另一个表中的主键类型不一致(如int和bigint),那么创建表会出错
	// 两种解决方案,第一种方式是加tag(如type:int/bigint);第二种方式是在struct中设置相同的类型
	ID        int32 `gorm:"primarykey; type:int"` // int32对应mysql中int,int64对应mysql中bigint
	CreatedAt time.Time
	UpdatedAt time.Time
	DeletedAt gorm.DeletedAt `gorm:"index"`
}

type Brand

type Brand struct {
	BaseModel
	Name string `gorm:"type:varchar(50); not null; default:''; comment:'名称'"` // string 对应数据库中的 longtext(其检索效率较低),如果要 string 类型则需要设置 tag
}

在实际开发过程中尽量设置为not null

func (Brand) TableName

func (Brand) TableName() string

设置表名

type Category

type Category struct {
	BaseModel
	Name             string     `gorm:"type:varchar(50); not null; default:''; comment:'名称'"` // string 对应数据库中的 longtext(其检索效率较低),如果要 string 类型则需要设置 tag
	Level            int32      `gorm:"type:int; not null; default:0; comment:'目录级,0表示未知'"`   // 由于proto中没有int类型,只有int32、int64,为了减少类型转换,所以这里设置为int32
	IsTab            bool       `gorm:"not null; default:false; comment:'是否为标签栏'"`            // bool 对应数据库中的 tinyint(false为0,true为1)
	ParentCategoryID int32      `gorm:"type:int; default:0; comment:'父分类,0表示无'"`
	ParentCategory   *Category  // 不加gorm的tag就不会生成表的字段
	SubCategory      []Category `gorm:"foreignKey:ParentCategoryID; references:ID"`
	URL              string     `gorm:"type:varchar(200); not null; comment:'地址'"`
}

在实际开发过程中尽量设置为not null

func (Category) TableName

func (Category) TableName() string

设置表名

type CategoryBrand

type CategoryBrand struct {
	BaseModel

	Brand      Brand
	Category   Category
	BrandID    int32 `gorm:"type:int; index:idx_category_brand,unique; not null; default:0; comment:'品牌ID,0表示未知'"` // 联合唯一索引
	CategoryID int32 `gorm:"type:int; index:idx_category_brand,unique; not null; default:0; comment:'分类ID,0表示未知'"`
}

func (CategoryBrand) TableName

func (CategoryBrand) TableName() string

设置表名

type Goods

type Goods struct {
	BaseModel

	BrandID    int32 `gorm:"type:int; not null; comment:'品牌id'"`
	CategoryID int32 `gorm:"type:int; not null; comment:'分类id'"`
	Brand      Brand // 默认情况下,BrandID 被隐含地用来在 Goods 和 Brand 之间创建一个外键关系
	Category   Category

	// bool类型数据
	OnSale   bool `gorm:"default:false; not null; comment:'是否上架'"`
	ShipFree bool `gorm:"default:false; not null; comment:'是否包邮'"`
	IsNew    bool `gorm:"default:false; not null; comment:'是否新品'"`
	IsHot    bool `gorm:"default:false; not null; comment:'是否热销'"`

	// string类型数据
	Name             string `gorm:"type:varchar(50); not null; default:''; comment:'商品名'"`
	GoodsSn          string `gorm:"type:varchar(50); not null; default:''; comment:'商品编号'"`
	GoodsBrief       string `gorm:"type:varchar(100); not null; default:''; comment:'商品简介'"`
	GoodsFrontImages string `gorm:"type:varchar(200); not null; default:''; comment:'商品首页图片'"`

	// 数值类型数据
	Stocks      int32   `gorm:"type:int; not null; default:0; comment:'库存量'"`
	ClickNum    int32   `gorm:"type:int; not null; default:0; comment:'点击量'"`
	FavNum      int32   `gorm:"type:int; not null; default:0; comment:'收藏量'"`
	SoldNum     int32   `gorm:"type:int; not null; default:0; comment:'销量'"`
	ShopPrice   float32 `gorm:"type:int; not null; default:0; comment:'商品价格'"` // float32 会自动转换为数据库中的 float 类型,int 类型会转换为 bigint
	MarketPrice float32 `gorm:"type:float; not null; default:0; comment:'市场价'"`

	// 切片类型数据
	Images     GormList `gorm:"type:varchar(2000); not null; comment:'商品图片'"` // 数据库中没有[]string数组类型,所以使用 GormList
	DescImages GormList `gorm:"type:varchar(2000); not null; comment:'商品详情图片'"`
}

func (Goods) TableName

func (Goods) TableName() string

type GormList

type GormList []string

func (*GormList) Scan

func (g *GormList) Scan(value interface{}) error

func (*GormList) Value

func (g *GormList) Value() (driver.Value, error)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL