forked from free-lancers/electricity_bill_calc_service
		
	fix(model):补充所有数据模型中用于记录操作时间的Hook。
This commit is contained in:
		| @@ -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) { | ||||
|   | ||||
| @@ -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 | ||||
| } | ||||
|   | ||||
| @@ -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 | ||||
| } | ||||
|   | ||||
| @@ -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 | ||||
| } | ||||
|   | ||||
| @@ -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 | ||||
| } | ||||
|   | ||||
| @@ -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 { | ||||
|   | ||||
| @@ -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 | ||||
| } | ||||
|   | ||||
| @@ -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 | ||||
| } | ||||
|   | ||||
| @@ -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 | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user