package model import ( "context" "time" "github.com/uptrace/bun" ) const ( REPORT_NOT_WITHDRAW int8 = iota REPORT_WITHDRAW_APPLIED REPORT_WITHDRAW_DENIED REPORT_WITHDRAW_GRANTED ) type Report struct { bun.BaseModel `bun:"table:report,alias:r"` CreatedAndModified `bun:"extend"` Id string `bun:",pk,notnull" json:"id"` ParkId string `bun:",notnull" json:"parkId"` Period time.Time `bun:"type:date,notnull" json:"period" time_format:"simple_date" time_location:"shanghai"` Category int8 `bun:"type:smallint,notnull,default:0" json:"category"` SubmeterType int8 `bun:"meter_04kv_type,type:smallint,notnull,default:0" json:"meter04kvType"` StepState Steps `bun:"type:jsonb,notnull" json:"stepState"` Published bool `bun:",notnull,default:false" json:"published"` PublishedAt *time.Time `bun:"type:timestamptz,nullzero" json:"publishedAt" time_format:"simple_datetime" time_location:"shanghai"` Withdraw int8 `bun:"type:smallint,notnull,default:0" json:"withdraw"` LastWithdrawAppliedAt *time.Time `bun:"type:timestamptz,nullzero" json:"lastWithdrawAppliedAt" time_format:"simple_datetime" time_location:"shanghai"` LastWithdrawAuditAt *time.Time `bun:"type:timestamptz,nullzero" json:"lastWithdrawAuditAt" time_format:"simple_datetime" time_location:"shanghai"` Park *Park `bun:"rel:belongs-to,join:park_id=id" json:"-"` Summary *ReportSummary `bun:"rel:has-one,join:id=report_id" json:"-"` WillDilutedFees []*WillDilutedFee `bun:"rel:has-many,join:id=report_id" json:"-"` EndUsers []*EndUserDetail `bun:"rel:has-many,join:id=report_id,join:park_id=park_id" json:"-"` } type Steps struct { Summary bool `json:"summary"` WillDiluted bool `json:"willDiluted"` Submeter bool `json:"submeter"` Calculate bool `json:"calculate"` Preview bool `json:"preview"` Publish bool `json:"publish"` } func NewSteps() Steps { return Steps{ Summary: false, WillDiluted: false, Submeter: false, Calculate: false, Preview: false, Publish: false, } } type ParkNewestReport struct { Park Park `bun:"extends" json:"park"` Report *Report `bun:"extends" json:"report"` } func (p *ParkNewestReport) AfterLoad() { if p.Report != nil && len(p.Report.Id) == 0 { p.Report = nil } } type ReportIndexSimplified struct { bun.BaseModel `bun:"table:report,alias:r"` Id string `bun:",pk,notnull" json:"id"` ParkId string `bun:",notnull" json:"parkId"` Period Date `bun:"type:date,notnull" json:"period"` StepState Steps `bun:"type:jsonb,notnull" json:"stepState"` Published bool `bun:",notnull,default:false" json:"published"` PublishedAt *time.Time `bun:"type:timestampz" json:"publishedAt" time_format:"simple_datetime" time_location:"shanghai"` Withdraw int8 `bun:"type:smallint,notnull,default:0" json:"withdraw"` LastWithdrawAppliedAt *time.Time `bun:"type:timestamptz" json:"lastWithdrawAppliedAt" time_format:"simple_datetime" time_location:"shanghai"` LastWithdrawAuditAt *time.Time `bun:"type:timestamptz" json:"lastWithdrawAuditAt" time_format:"simple_datetime" time_location:"shanghai"` } type JoinedReportForWithdraw struct { Report Report `bun:"extends" json:"report"` 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 }