diff --git a/model/end_user_detail.go b/model/end_user_detail.go index 49ee837..778c35d 100644 --- a/model/end_user_detail.go +++ b/model/end_user_detail.go @@ -1,7 +1,9 @@ package model import ( + "context" "errors" + "time" "github.com/shopspring/decimal" "github.com/uptrace/bun" @@ -59,6 +61,20 @@ type EndUserDetail struct { Origin *Meter04KV `bun:"rel:belongs-to,join:park_id=park_id,join:meter_04kv_id=code"` } +var _ bun.BeforeAppendModelHook = (*EndUserDetail)(nil) + +func (d *EndUserDetail) 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 +} + func (d EndUserDetail) Validate() (bool, error) { lastPeriodSum := decimal.Sum(d.LastPeriodCritical, d.LastPeriodPeak, d.LastPeriodValley) if lastPeriodSum.GreaterThan(d.LastPeriodOverall) { diff --git a/model/maintenance_fee.go b/model/maintenance_fee.go index 89e1850..7ff38fd 100644 --- a/model/maintenance_fee.go +++ b/model/maintenance_fee.go @@ -1,6 +1,9 @@ package model import ( + "context" + "time" + "github.com/shopspring/decimal" "github.com/uptrace/bun" ) @@ -16,3 +19,17 @@ type MaintenanceFee struct { Memo *string `bun:"type:text" json:"memo"` Enabled bool `bun:",notnull" json:"enabled"` } + +var _ bun.BeforeAppendModelHook = (*MaintenanceFee)(nil) + +func (f *MaintenanceFee) BeforeAppendModel(ctx context.Context, query bun.Query) error { + oprTime := time.Now() + switch query.(type) { + case *bun.InsertQuery: + f.CreatedAt = oprTime + f.LastModifiedAt = &oprTime + case *bun.UpdateQuery: + f.LastModifiedAt = &oprTime + } + return nil +} diff --git a/model/meter_04kv.go b/model/meter_04kv.go index 9ff8fbf..6aac9c4 100644 --- a/model/meter_04kv.go +++ b/model/meter_04kv.go @@ -1,6 +1,9 @@ package model import ( + "context" + "time" + "github.com/shopspring/decimal" "github.com/uptrace/bun" ) @@ -21,3 +24,17 @@ type Meter04KV struct { Enabled bool `bun:",notnull,default:true" json:"enabled"` ParkDetail *Park `bun:"rel:belongs-to,join:park_id=id" json:"-"` } + +var _ bun.BeforeAppendModelHook = (*Meter04KV)(nil) + +func (m *Meter04KV) BeforeAppendModel(ctx context.Context, query bun.Query) error { + oprTime := time.Now() + switch query.(type) { + case *bun.InsertQuery: + m.CreatedAt = oprTime + m.LastModifiedAt = &oprTime + case *bun.UpdateQuery: + m.LastModifiedAt = &oprTime + } + return nil +} diff --git a/model/park.go b/model/park.go index d6a3916..23a488d 100644 --- a/model/park.go +++ b/model/park.go @@ -1,6 +1,7 @@ package model import ( + "context" "time" "github.com/jinzhu/copier" @@ -71,3 +72,17 @@ func FromPark(park Park) ParkSimplified { copier.Copy(&dest, park) return dest } + +var _ bun.BeforeAppendModelHook = (*Park)(nil) + +func (p *Park) BeforeAppendModel(ctx context.Context, query bun.Query) error { + oprTime := time.Now() + switch query.(type) { + case *bun.InsertQuery: + p.CreatedAt = oprTime + p.LastModifiedAt = &oprTime + case *bun.UpdateQuery: + p.LastModifiedAt = &oprTime + } + return nil +} diff --git a/model/report.go b/model/report.go index 07f49eb..79191e1 100644 --- a/model/report.go +++ b/model/report.go @@ -1,6 +1,7 @@ package model import ( + "context" "time" "github.com/uptrace/bun" @@ -82,3 +83,17 @@ type JoinedReportForWithdraw struct { Park ParkSimplified `bun:"extends" json:"park"` User UserDetailSimplified `bun:"extends" json:"user"` } + +var _ bun.BeforeAppendModelHook = (*Report)(nil) + +func (p *Report) BeforeAppendModel(ctx context.Context, query bun.Query) error { + oprTime := time.Now() + switch query.(type) { + case *bun.InsertQuery: + p.CreatedAt = oprTime + p.LastModifiedAt = &oprTime + case *bun.UpdateQuery: + p.LastModifiedAt = &oprTime + } + return nil +} diff --git a/model/shared.go b/model/shared.go index 4b35935..84d2fe0 100644 --- a/model/shared.go +++ b/model/shared.go @@ -3,7 +3,7 @@ package model import "time" type Created struct { - CreatedAt time.Time `bun:"type:timestamptz,notnull,default:current_timestamp" json:"createdAt" time_format:"simple_datetime" time_location:"shanghai"` + CreatedAt time.Time `bun:"type:timestamptz,notnull" json:"createdAt" time_format:"simple_datetime" time_location:"shanghai"` } type CreatedWithUser struct { @@ -22,7 +22,7 @@ type DeletedWithUser struct { type CreatedAndModified struct { Created `bun:"extend"` - LastModifiedAt *time.Time `bun:"type:timestamptz,nullzero,default:current_timestamp" json:"lastModifiedAt" time_format:"simple_datetime" time_location:"shanghai"` + LastModifiedAt *time.Time `bun:"type:timestamptz,nullzero" json:"lastModifiedAt" time_format:"simple_datetime" time_location:"shanghai"` } type CreatedAndModifiedWithUser struct { diff --git a/model/user.go b/model/user.go index 690866d..3420fb7 100644 --- a/model/user.go +++ b/model/user.go @@ -1,6 +1,11 @@ package model -import "github.com/uptrace/bun" +import ( + "context" + "time" + + "github.com/uptrace/bun" +) const ( USER_TYPE_ENT int8 = iota @@ -31,3 +36,13 @@ type UserWithCredentials struct { Type int8 `bun:"type:smallint,notnull" json:"type"` Enabled bool `bun:",notnull" json:"enabled"` } + +var _ bun.BeforeAppendModelHook = (*User)(nil) + +func (u *User) BeforeAppendModel(ctx context.Context, query bun.Query) error { + switch query.(type) { + case *bun.InsertQuery: + u.CreatedAt = time.Now() + } + return nil +} diff --git a/model/user_charges.go b/model/user_charges.go index 86fdef7..70a89fd 100644 --- a/model/user_charges.go +++ b/model/user_charges.go @@ -1,6 +1,7 @@ package model import ( + "context" "time" "github.com/shopspring/decimal" @@ -29,3 +30,14 @@ 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 +} diff --git a/model/user_detail.go b/model/user_detail.go index 5c186cf..76969b4 100644 --- a/model/user_detail.go +++ b/model/user_detail.go @@ -1,6 +1,7 @@ package model import ( + "context" "time" "github.com/jinzhu/copier" @@ -52,3 +53,17 @@ func FromUserDetail(user UserDetail) 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 +}