70 lines
2.0 KiB
Go
70 lines
2.0 KiB
Go
package model
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"github.com/jinzhu/copier"
|
|
"github.com/shopspring/decimal"
|
|
"github.com/uptrace/bun"
|
|
)
|
|
|
|
type UserDetail struct {
|
|
bun.BaseModel `bun:"table:user_detail,alias:d"`
|
|
CreatedAndModifiedWithUser `bun:"extend"`
|
|
DeletedWithUser `bun:"extend"`
|
|
Id string `bun:",pk,notnull" json:"-"`
|
|
Name *string `json:"name"`
|
|
Abbr *string `json:"abbr"`
|
|
Region *string `json:"region"`
|
|
Address *string `json:"address"`
|
|
Contact *string `json:"contact"`
|
|
Phone *string `json:"phone"`
|
|
UnitServiceFee decimal.Decimal `bun:"type:numeric,notnull" json:"unitServiceFee"`
|
|
ServiceExpiration Date `bun:"type:date,notnull" json:"serviceExpiration"`
|
|
}
|
|
|
|
type JoinedUserDetail struct {
|
|
UserDetail `bun:"extend"`
|
|
Id string `json:"id"`
|
|
Username string `json:"username"`
|
|
Type int8 `json:"type"`
|
|
Enabled bool `json:"enabled"`
|
|
}
|
|
|
|
type FullJoinedUserDetail struct {
|
|
UserDetail `bun:"extend"`
|
|
User `bun:"extend"`
|
|
}
|
|
|
|
type UserDetailSimplified struct {
|
|
bun.BaseModel `bun:"table:user_detail,alias:d"`
|
|
Id string `bun:",pk,notnull" json:"id"`
|
|
Name *string `json:"name"`
|
|
Abbr *string `json:"abbr"`
|
|
Region *string `json:"region"`
|
|
Address *string `json:"address"`
|
|
Contact *string `json:"contact"`
|
|
Phone *string `json:"phone"`
|
|
}
|
|
|
|
func FromUserDetail(user UserDetail) UserDetailSimplified {
|
|
dest := UserDetailSimplified{}
|
|
copier.Copy(&dest, user)
|
|
return dest
|
|
}
|
|
|
|
var _ bun.BeforeAppendModelHook = (*UserDetail)(nil)
|
|
|
|
func (d *UserDetail) BeforeAppendModel(ctx context.Context, query bun.Query) error {
|
|
oprTime := time.Now()
|
|
switch query.(type) {
|
|
case *bun.InsertQuery:
|
|
d.CreatedAt = oprTime
|
|
d.LastModifiedAt = &oprTime
|
|
case *bun.UpdateQuery:
|
|
d.LastModifiedAt = &oprTime
|
|
}
|
|
return nil
|
|
}
|