2024-11-09 14:59:27 +08:00
|
|
|
package model
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2024-11-09 21:24:02 +08:00
|
|
|
"strings"
|
2024-11-09 14:59:27 +08:00
|
|
|
|
|
|
|
"gorm.io/gorm"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
ItemStateOnSale = "onsale"
|
2024-11-09 16:36:28 +08:00
|
|
|
ItemStateDelete = "delete"
|
2024-11-09 14:59:27 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
// type 是外部输入的 []int 类型,需要转换成 string 类型存储到数据库
|
|
|
|
type ItemModel struct {
|
|
|
|
BaseModel
|
|
|
|
Itemname string
|
|
|
|
Types []int `gorm:"-"`
|
|
|
|
TypeString string
|
|
|
|
Price int
|
|
|
|
Desc string
|
|
|
|
Img string
|
2024-11-09 16:36:28 +08:00
|
|
|
State string
|
|
|
|
Uid uint
|
2024-11-09 14:59:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *ItemModel) TableName() string {
|
|
|
|
return "items"
|
|
|
|
}
|
|
|
|
func (i *ItemModel) BeforeCreate(tx *gorm.DB) (err error) {
|
|
|
|
types_string, _ := json.Marshal(i.Types)
|
2024-11-09 21:24:02 +08:00
|
|
|
types := string(types_string)
|
|
|
|
i.TypeString = strings.Replace(types, ",", "][", -1)
|
2024-11-09 14:59:27 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *ItemModel) AfterFind(tx *gorm.DB) (err error) {
|
2024-11-09 21:24:02 +08:00
|
|
|
type_string := strings.Replace(i.TypeString, "][", ",", -1)
|
|
|
|
err = json.Unmarshal([]byte(type_string), &i.Types)
|
2024-11-09 14:59:27 +08:00
|
|
|
return
|
|
|
|
}
|