YiTao/model/item_model.go
2024-11-09 14:59:27 +08:00

38 lines
698 B
Go

package model
import (
"encoding/json"
"gorm.io/gorm"
)
const (
ItemStateOnSale = "onsale"
)
// type 是外部输入的 []int 类型,需要转换成 string 类型存储到数据库
type ItemModel struct {
BaseModel
Itemname string
Types []int `gorm:"-"`
TypeString string
Price int
Desc string
Img string
States string
}
func (m *ItemModel) TableName() string {
return "items"
}
func (i *ItemModel) BeforeCreate(tx *gorm.DB) (err error) {
types_string, _ := json.Marshal(i.Types)
i.TypeString = string(types_string)
return
}
func (i *ItemModel) AfterFind(tx *gorm.DB) (err error) {
err = json.Unmarshal([]byte(i.TypeString), &i.Types)
return
}