fix(model):补充所有数据模型中用于记录操作时间的Hook。
This commit is contained in:
parent
e40ba55825
commit
7b8ee5ddbd
@ -1,7 +1,9 @@
|
|||||||
package model
|
package model
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/shopspring/decimal"
|
"github.com/shopspring/decimal"
|
||||||
"github.com/uptrace/bun"
|
"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"`
|
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) {
|
func (d EndUserDetail) Validate() (bool, error) {
|
||||||
lastPeriodSum := decimal.Sum(d.LastPeriodCritical, d.LastPeriodPeak, d.LastPeriodValley)
|
lastPeriodSum := decimal.Sum(d.LastPeriodCritical, d.LastPeriodPeak, d.LastPeriodValley)
|
||||||
if lastPeriodSum.GreaterThan(d.LastPeriodOverall) {
|
if lastPeriodSum.GreaterThan(d.LastPeriodOverall) {
|
||||||
|
@ -1,6 +1,9 @@
|
|||||||
package model
|
package model
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/shopspring/decimal"
|
"github.com/shopspring/decimal"
|
||||||
"github.com/uptrace/bun"
|
"github.com/uptrace/bun"
|
||||||
)
|
)
|
||||||
@ -16,3 +19,17 @@ type MaintenanceFee struct {
|
|||||||
Memo *string `bun:"type:text" json:"memo"`
|
Memo *string `bun:"type:text" json:"memo"`
|
||||||
Enabled bool `bun:",notnull" json:"enabled"`
|
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
|
||||||
|
}
|
||||||
|
@ -1,6 +1,9 @@
|
|||||||
package model
|
package model
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/shopspring/decimal"
|
"github.com/shopspring/decimal"
|
||||||
"github.com/uptrace/bun"
|
"github.com/uptrace/bun"
|
||||||
)
|
)
|
||||||
@ -21,3 +24,17 @@ type Meter04KV struct {
|
|||||||
Enabled bool `bun:",notnull,default:true" json:"enabled"`
|
Enabled bool `bun:",notnull,default:true" json:"enabled"`
|
||||||
ParkDetail *Park `bun:"rel:belongs-to,join:park_id=id" json:"-"`
|
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
|
||||||
|
}
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package model
|
package model
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/jinzhu/copier"
|
"github.com/jinzhu/copier"
|
||||||
@ -71,3 +72,17 @@ func FromPark(park Park) ParkSimplified {
|
|||||||
copier.Copy(&dest, park)
|
copier.Copy(&dest, park)
|
||||||
return dest
|
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
|
||||||
|
}
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package model
|
package model
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/uptrace/bun"
|
"github.com/uptrace/bun"
|
||||||
@ -82,3 +83,17 @@ type JoinedReportForWithdraw struct {
|
|||||||
Park ParkSimplified `bun:"extends" json:"park"`
|
Park ParkSimplified `bun:"extends" json:"park"`
|
||||||
User UserDetailSimplified `bun:"extends" json:"user"`
|
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
|
||||||
|
}
|
||||||
|
@ -3,7 +3,7 @@ package model
|
|||||||
import "time"
|
import "time"
|
||||||
|
|
||||||
type Created struct {
|
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 {
|
type CreatedWithUser struct {
|
||||||
@ -22,7 +22,7 @@ type DeletedWithUser struct {
|
|||||||
|
|
||||||
type CreatedAndModified struct {
|
type CreatedAndModified struct {
|
||||||
Created `bun:"extend"`
|
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 {
|
type CreatedAndModifiedWithUser struct {
|
||||||
|
@ -1,6 +1,11 @@
|
|||||||
package model
|
package model
|
||||||
|
|
||||||
import "github.com/uptrace/bun"
|
import (
|
||||||
|
"context"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/uptrace/bun"
|
||||||
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
USER_TYPE_ENT int8 = iota
|
USER_TYPE_ENT int8 = iota
|
||||||
@ -31,3 +36,13 @@ type UserWithCredentials struct {
|
|||||||
Type int8 `bun:"type:smallint,notnull" json:"type"`
|
Type int8 `bun:"type:smallint,notnull" json:"type"`
|
||||||
Enabled bool `bun:",notnull" json:"enabled"`
|
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
|
||||||
|
}
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package model
|
package model
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/shopspring/decimal"
|
"github.com/shopspring/decimal"
|
||||||
@ -29,3 +30,14 @@ type ChargeWithName struct {
|
|||||||
UserDetail `bun:"extend"`
|
UserDetail `bun:"extend"`
|
||||||
UserCharge `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
|
||||||
|
}
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package model
|
package model
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/jinzhu/copier"
|
"github.com/jinzhu/copier"
|
||||||
@ -52,3 +53,17 @@ func FromUserDetail(user UserDetail) UserDetailSimplified {
|
|||||||
copier.Copy(&dest, user)
|
copier.Copy(&dest, user)
|
||||||
return dest
|
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
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user