talent/model.go

139 lines
2.3 KiB
Go
Raw Permalink Normal View History

2021-12-30 09:16:20 +00:00
package main
import (
"time"
"github.com/rs/xid"
2022-01-03 05:06:26 +00:00
"github.com/rs/zerolog/log"
2021-12-30 09:16:20 +00:00
"gorm.io/gorm"
)
// User User model
type User struct {
ID string `gorm:"primaryKey"`
Email string
LastLogin time.Time
2021-12-30 13:39:00 +00:00
Products []Product `gorm:"many2many:orders;"`
2021-12-30 09:16:20 +00:00
ProfileID uint `gorm:"default:NULL"`
Profile Profile
CreatedAt time.Time
UpdatedAt time.Time
}
type Profile struct {
ID uint `gorm:"primaryKey"`
2022-01-04 12:39:28 +00:00
Name string
Brief string
Img string
Age int
Gender string
Occupation string
PersonalID string
Phone string
Address string
2021-12-30 09:16:20 +00:00
CreatedAt time.Time
UpdatedAt time.Time
}
2022-01-04 12:39:28 +00:00
type Price struct {
ProfileID uint `gorm:"primaryKey"`
ProductID uint `gorm:"primaryKey"`
PlanPrice float32
PlanUnit int
Detials string
}
2021-12-30 09:16:20 +00:00
type Product struct {
ID uint `gorm:"primaryKey"`
2022-01-03 05:06:26 +00:00
Categories []Category `gorm:"many2many:product_categories;"`
2022-01-04 05:01:42 +00:00
Img string
2021-12-30 09:16:20 +00:00
Name string
Brief string
Description string
CreatedAt time.Time
UpdatedAt time.Time
}
2022-01-04 14:27:59 +00:00
type Sales struct {
Img string
Name string
Brief string
}
2022-01-03 05:06:26 +00:00
type Category struct {
Name string `gorm:"primaryKey"`
}
2022-01-04 05:01:42 +00:00
const (
OrderReceived = "RECEIVED"
OrderPrepared = "PREPARED"
OrderClaiming = "CLAIMING"
OrderClaimAccept = "CLAIM_ACCEPT"
OrderClaimReject = "CLAIM_REJECT"
OrderClaimPaid = "CLAIM_PAID"
)
2021-12-30 09:16:20 +00:00
type Order struct {
2022-01-03 05:06:26 +00:00
ID string
2021-12-30 09:16:20 +00:00
UserID string `gorm:"primaryKey"`
ProductID uint `gorm:"primaryKey"`
2022-01-03 05:06:26 +00:00
Start time.Time
End time.Time
Attachment string
2022-01-04 05:01:42 +00:00
Price float32
Status string
2022-01-04 08:15:45 +00:00
Claimed float32
2021-12-30 09:16:20 +00:00
CreatedAt time.Time
UpdatedAt time.Time
}
2021-12-30 13:39:00 +00:00
type Node struct {
ID uint `gorm:"primaryKey"`
Name string
UserID string
CreatedAt time.Time
}
2021-12-30 09:16:20 +00:00
// 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
}
2022-01-03 05:06:26 +00:00
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()
}
2022-01-04 05:01:42 +00:00
o.Status = OrderReceived
2022-01-03 05:06:26 +00:00
return
}