package model import ( "errors" "github.com/shopspring/decimal" "github.com/uptrace/bun" ) type ReportSummary struct { bun.BaseModel `bun:"table:report_summary,alias:rs"` ReportId string `bun:",pk,notnull" json:"-"` Overall decimal.Decimal `bun:"type:numeric,notnull" json:"overall"` OverallFee decimal.Decimal `bun:"type:numeric,notnull" json:"overallFee"` ConsumptionFee decimal.NullDecimal `bun:"type:numeric" json:"consumptionFee"` OverallPrice decimal.NullDecimal `bun:"type:numeric" json:"overallPrice"` Critical decimal.Decimal `bun:"type:numeric,notnull" json:"critical"` CriticalFee decimal.Decimal `bun:"type:numeric,notnull" json:"criticalFee"` CriticalPrice decimal.NullDecimal `bun:"type:numeric" json:"criticalPrice"` Peak decimal.Decimal `bun:"type:numeric,notnull" json:"peak"` PeakFee decimal.Decimal `bun:"type:numeric,notnull" json:"peakFee"` PeakPrice decimal.NullDecimal `bun:"type:numeric" json:"peakPrice"` Flat decimal.Decimal `bun:"type:numeric,notnull" json:"flat"` FlatFee decimal.Decimal `bun:"type:numeric,notnull" json:"flatFee"` FlatPrice decimal.NullDecimal `bun:"type:numeric" json:"flatPrice"` Valley decimal.Decimal `bun:"type:numeric,notnull" json:"valley"` ValleyFee decimal.Decimal `bun:"type:numeric,notnull" json:"valleyFee"` ValleyPrice decimal.NullDecimal `bun:"type:numeric" json:"valleyPrice"` Loss decimal.NullDecimal `bun:"type:numeric" json:"loss"` LossFee decimal.NullDecimal `bun:"type:numeric" json:"lossFee"` LossProportion decimal.NullDecimal `bun:"type:numeric" json:"lossProportion"` BasicFee decimal.Decimal `bun:"type:numeric,notnull" json:"basicFee"` BasicDilutedPrice decimal.NullDecimal `bun:"type:numeric" json:"basicDilutedPrice"` AdjustFee decimal.Decimal `bun:"type:numeric,notnull" json:"adjustFee"` AdjustDilutedPrice decimal.NullDecimal `bun:"type:numeric" json:"adjustDilutedPrice"` MaintenanceDilutedPrice decimal.NullDecimal `bun:"type:numeric" json:"maintencanceDilutedPrice"` LossDilutedPrice decimal.NullDecimal `bun:"type:numeric" json:"lossDilutedPrice"` PublicConsumptionDilutedPrice decimal.NullDecimal `bun:"type:numeric" json:"publicConsumptionDilutedPrice"` MaintenanceOverall decimal.NullDecimal `bun:"type:numeric" json:"maintenanceOverall"` FinalDilutedOverall decimal.NullDecimal `bun:"type:numeric" json:"finalDilutedOverall"` Customers Consumptions `bun:"type:jsonb" json:"customers"` Publics Consumptions `bun:"type:jsonb" json:"publics"` Diluteds Consumptions `bun:"type:jsonb" json:"diluteds"` } type Consumptions struct { Consumption decimal.NullDecimal `json:"consumption"` ConsumptionFee decimal.NullDecimal `json:"fee"` Critical decimal.NullDecimal `json:"critical"` CriticalFee decimal.NullDecimal `json:"criticalFee"` Peak decimal.NullDecimal `json:"peak"` PeakFee decimal.NullDecimal `json:"peakFee"` Flat decimal.NullDecimal `json:"flat"` FlatFee decimal.NullDecimal `json:"flatFee"` Valley decimal.NullDecimal `json:"valley"` ValleyFee decimal.NullDecimal `json:"valleyFee"` Proportion decimal.NullDecimal `json:"proportion"` } func (s ReportSummary) Validate() (bool, error) { amountSum := decimal.Sum(s.Critical, s.Peak, s.Valley) if amountSum.GreaterThan(s.Overall) { return false, errors.New("峰谷计量总量大于总计电量") } feeSum := decimal.Sum(s.CriticalFee, s.PeakFee, s.ValleyFee) if feeSum.GreaterThan(s.OverallFee) { return false, errors.New("峰谷计量费用大于总计费用") } return true, nil } func (s *ReportSummary) CalculatePrices() { s.ConsumptionFee = decimal.NewNullDecimal(s.OverallFee.Sub(s.BasicFee).Sub(s.AdjustFee)) if s.Overall.GreaterThan(decimal.Zero) { s.OverallPrice = decimal.NewNullDecimal(s.ConsumptionFee.Decimal.Div(s.Overall).RoundBank(8)) } else { s.OverallPrice = decimal.NewNullDecimal(decimal.Zero) } if s.Critical.GreaterThan(decimal.Zero) { s.CriticalPrice = decimal.NewNullDecimal(s.CriticalFee.Div(s.Critical).RoundBank(8)) } else { s.CriticalPrice = decimal.NewNullDecimal(decimal.Zero) } if s.Peak.GreaterThan(decimal.Zero) { s.PeakPrice = decimal.NewNullDecimal(s.PeakFee.Div(s.Peak).RoundBank(8)) } else { s.PeakPrice = decimal.NewNullDecimal(decimal.Zero) } if s.Valley.GreaterThan(decimal.Zero) { s.ValleyPrice = decimal.NewNullDecimal(s.ValleyFee.Div(s.Valley).RoundBank(8)) } else { s.ValleyPrice = decimal.NewNullDecimal(decimal.Zero) } s.Flat = s.Overall.Sub(s.Critical).Sub(s.Peak).Sub(s.Valley) s.FlatFee = s.ConsumptionFee.Decimal.Sub(s.CriticalFee).Sub(s.PeakFee).Sub(s.ValleyFee) if s.Flat.GreaterThan(decimal.Zero) { s.FlatPrice = decimal.NewNullDecimal(s.FlatFee.Div(s.Flat).RoundBank(8)) } else { s.FlatPrice = decimal.NewNullDecimal(decimal.Zero) } }