package model import ( "context" "time" "github.com/shopspring/decimal" "github.com/uptrace/bun" ) type UserCharge struct { bun.BaseModel `bun:"table:user_charge,alias:c"` Created `bun:"extend"` Seq int64 `bun:"type:bigint,pk,notnull,autoincrement" json:"seq"` UserId string `bun:",notnull" json:"userId"` Fee decimal.NullDecimal `bun:"type:numeric" json:"fee"` Discount decimal.NullDecimal `bun:"type:numeric" json:"discount"` Amount decimal.NullDecimal `bun:"type:numeric" json:"amount"` ChargeTo Date `bun:"type:date,notnull" json:"chargeTo"` Settled bool `bun:",notnull" json:"settled"` SettledAt *time.Time `bun:"type:timestamptz" json:"settledAt" time_format:"simple_datetime" time_location:"shanghai"` Cancelled bool `bun:",notnull" json:"cancelled"` CancelledAt *time.Time `bun:"type:timestamptz" json:"cancelledAt" time_format:"simple_datetime" time_location:"shanghai"` Refunded bool `bun:",notnull" json:"refunded"` RefundedAt *time.Time `bun:"type:timestamptz" json:"refundedAt" time_format:"simple_datetime" time_location:"shanghai"` Detail *UserDetail `bun:"rel:belongs-to,join:user_id=id" json:"-"` } type ChargeWithName struct { UserDetail `bun:"extend"` UserCharge `bun:"extend"` } var _ bun.BeforeAppendModelHook = (*UserCharge)(nil) func (uc *UserCharge) BeforeAppendModel(ctx context.Context, query bun.Query) error { oprTime := time.Now() switch query.(type) { case *bun.InsertQuery: uc.CreatedAt = oprTime } return nil }