electricity_bill_calc_service/model/park.go

90 lines
3.3 KiB
Go

package model
import (
"context"
"time"
"github.com/jinzhu/copier"
"github.com/shopspring/decimal"
"github.com/uptrace/bun"
)
const (
CATEGORY_TWO_PART int8 = iota
CATEGORY_SINGLE_PV
CATEGORY_SINGLE_NON_PV
)
const (
CUSTOMER_METER_NON_PV int8 = iota
CUSTOMER_METER_PV
)
type Park struct {
bun.BaseModel `bun:"table:park,alias:p"`
CreatedAndModified `bun:"extend"`
Deleted `bun:"extend"`
Id string `bun:",pk,notnull" json:"id"`
UserId string `bun:",notnull" json:"userId"`
Name string `bun:",notnull" json:"name"`
Abbr *string `json:"abbr"`
Area decimal.NullDecimal `bun:"type:numeric" json:"area"`
TenementQuantity decimal.NullDecimal `bun:"type:numeric" json:"tenement"`
Capacity decimal.NullDecimal `bun:"type:numeric" json:"capacity"`
Category int8 `bun:"type:smallint,notnull" json:"category"`
SubmeterType int8 `bun:"meter_04kv_type,type:smallint,notnull" json:"meter04kvType"`
Region *string `json:"region"`
Address *string `json:"address"`
Contact *string `json:"contact"`
Phone *string `json:"phone"`
Enabled bool `bun:",notnull" json:"enabled"`
EnterpriseIndex *User `bun:"rel:belongs-to,join:user_id=id" json:"-"`
Enterprise *UserDetail `bun:"rel:belongs-to,join:user_id=id" json:"-"`
MaintenanceFees []*MaintenanceFee `bun:"rel:has-many,join:id=park_id" json:"-"`
Meters []*Meter04KV `bun:"rel:has-many,join:id=park_id" json:"-"`
Reports []*Report `bun:"rel:has-many,join:id=park_id" json:"-"`
}
type ParkSimplified struct {
bun.BaseModel `bun:"table:park,alias:p"`
Id string `bun:",pk,notnull" json:"id"`
UserId string `bun:",notnull" json:"userId"`
Name string `bun:",notnull" json:"name"`
Abbr *string `json:"abbr"`
Area decimal.NullDecimal `json:"area"`
TenementQuantity decimal.NullDecimal `json:"tenement"`
Capacity decimal.NullDecimal `json:"capacity"`
Category int8 `bun:"type:smallint,notnull" json:"category"`
SubmeterType int8 `bun:"meter_04kv_type,type:smallint,notnull" json:"meter04kvType"`
Region *string `json:"region"`
Address *string `json:"address"`
Contact *string `json:"contact"`
Phone *string `json:"phone"`
}
type ParkPeriodStatistics struct {
Id string `bun:"park__id,notnull" json:"id"`
Name string `bun:"park__name,notnull" json:"name"`
Period *Date `bun:"type:date" json:"period"`
}
func FromPark(park Park) ParkSimplified {
dest := 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
}