talent/model.go

133 lines
2.2 KiB
Go

package main
import (
"time"
"github.com/rs/xid"
"github.com/rs/zerolog/log"
"gorm.io/gorm"
)
// User User model
type User struct {
ID string `gorm:"primaryKey"`
Email string
LastLogin time.Time
Products []Product `gorm:"many2many:orders;"`
ProfileID uint `gorm:"default:NULL"`
Profile Profile
CreatedAt time.Time
UpdatedAt time.Time
}
type Profile struct {
ID uint `gorm:"primaryKey"`
Name string
Brief string
Img string
Age int
Gender string
Occupation string
PersonalID string
Phone string
Address string
CreatedAt time.Time
UpdatedAt time.Time
}
type Price struct {
ProfileID uint `gorm:"primaryKey"`
ProductID uint `gorm:"primaryKey"`
PlanPrice float32
PlanUnit int
Detials string
}
type Product struct {
ID uint `gorm:"primaryKey"`
Categories []Category `gorm:"many2many:product_categories;"`
Img string
Name string
Brief string
Description string
CreatedAt time.Time
UpdatedAt time.Time
}
type Category struct {
Name string `gorm:"primaryKey"`
}
const (
OrderReceived = "RECEIVED"
OrderPrepared = "PREPARED"
OrderClaiming = "CLAIMING"
OrderClaimAccept = "CLAIM_ACCEPT"
OrderClaimReject = "CLAIM_REJECT"
OrderClaimPaid = "CLAIM_PAID"
)
type Order struct {
ID string
UserID string `gorm:"primaryKey"`
ProductID uint `gorm:"primaryKey"`
Start time.Time
End time.Time
Attachment string
Price float32
Status string
Claimed float32
CreatedAt time.Time
UpdatedAt time.Time
}
type Node struct {
ID uint `gorm:"primaryKey"`
Name string
UserID string
CreatedAt time.Time
}
// BeforeCreate set UID
func (u *User) BeforeCreate(tx *gorm.DB) (err error) {
if u.ID == "" {
u.ID = xid.New().String()
}
u.LastLogin = time.Now()
return
}
func (u *User) AfterFind(tx *gorm.DB) (err error) {
if u.ProfileID != 0 {
profile := &Profile{}
if err := tx.First(profile, "id = ?", u.ProfileID).Error; err != nil {
log.Error().Err(err).Msg("get profile error")
} else {
u.Profile = *profile
}
}
return
}
func (o *Order) BeforeCreate(tx *gorm.DB) (err error) {
if o.ID == "" {
o.ID = xid.New().String()
}
o.Status = OrderReceived
return
}