electricity_bill_calc_service/model/report_summary.go

106 lines
6.0 KiB
Go

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:",notnull,default:0" json:"overall"`
OverallFee decimal.Decimal `bun:",notnull,default:0" json:"overallFee"`
ConsumptionFee decimal.NullDecimal `json:"consumptionFee"`
OverallPrice decimal.NullDecimal `json:"overallPrice"`
Critical decimal.Decimal `bun:",notnull,default:0" json:"critical"`
CriticalFee decimal.Decimal `bun:",notnull,default:0" json:"criticalFee"`
CriticalPrice decimal.NullDecimal `json:"criticalPrice"`
Peak decimal.Decimal `bun:",notnull,default:0" json:"peak"`
PeakFee decimal.Decimal `bun:",notnull,default:0" json:"peakFee"`
PeakPrice decimal.NullDecimal `json:"peakPrice"`
Flat decimal.Decimal `bun:",notnull,default:0" json:"flat"`
FlatFee decimal.Decimal `bun:",notnull,default:0" json:"flatFee"`
FlatPrice decimal.NullDecimal `json:"flatPrice"`
Valley decimal.Decimal `bun:",notnull,default:0" json:"valley"`
ValleyFee decimal.Decimal `bun:",notnull,default:0" json:"valleyFee"`
ValleyPrice decimal.NullDecimal `json:"valleyPrice"`
Loss decimal.NullDecimal `json:"loss"`
LossFee decimal.NullDecimal `json:"lossFee"`
LossProportion decimal.NullDecimal `json:"lossProportion"`
CustomerConsumption decimal.NullDecimal `json:"customerConsumption"`
CustomerConsumptionFee decimal.NullDecimal `json:"customerConsumptionFee"`
CustomerConsumptionCritical decimal.NullDecimal `json:"customerConsumptionCritical"`
CustomerConsumptionCriticalFee decimal.NullDecimal `json:"customerConsumptionCriticalFee"`
CustomerConsumptionPeak decimal.NullDecimal `json:"customerConsumptionPeak"`
CustomerConsumptionPeakFee decimal.NullDecimal `json:"customerConsumptionPeakFee"`
CustomerConsumptionFlat decimal.NullDecimal `json:"customerConsumptionFlat"`
CustomerConsumptionFlatFee decimal.NullDecimal `json:"customerConsumptionFlatFee"`
CustomerConsumptionValley decimal.NullDecimal `json:"customerConsumptionValley"`
CustomerConsumptionValleyFee decimal.NullDecimal `json:"customerConsumptionValleyFee"`
PublicConsumption decimal.NullDecimal `json:"publicConsumption"`
PublicConsumptionFee decimal.NullDecimal `json:"publicConsumptionFee"`
PublicConsumptionProportion decimal.NullDecimal `json:"publicConsumptionProportion"`
PublicConsumptionCritical decimal.NullDecimal `json:"publicConsumptionCritical"`
PublicConsumptionCriticalFee decimal.NullDecimal `json:"publicConsumptionCriticalFee"`
PublicConsumptionPeak decimal.NullDecimal `json:"publicConsumptionPeak"`
PublicConsumptionPeakFee decimal.NullDecimal `json:"publicConsumptionPeakFee"`
PublicConsumptionFlat decimal.NullDecimal `json:"publicConsumptionFlat"`
PublicConsumptionFlatFee decimal.NullDecimal `json:"publicConsumptionFlatFee"`
PublicConsumptionValley decimal.NullDecimal `json:"publicConsumptionValley"`
PublicConsumptionValleyFee decimal.NullDecimal `json:"publicConsumptionValleyFee"`
BasicFee decimal.Decimal `bun:",notnull,default:0" json:"basicFee"`
BasicDilutedPrice decimal.NullDecimal `json:"basicDilutedPrice"`
AdjustFee decimal.Decimal `bun:",notnull,default:0" json:"adjustFee"`
AdjustDilutedPrice decimal.NullDecimal `json:"adjustDilutedPrice"`
MaintenanceDilutedPrice decimal.NullDecimal `json:"maintencanceDilutedPrice"`
LossDilutedPrice decimal.NullDecimal `json:"lossDilutedPrice"`
PublicConsumptionDilutedPrice decimal.NullDecimal `json:"publicConsumptionDilutedPrice"`
MaintenanceOverall decimal.NullDecimal `json:"maintenanceOverall"`
FinalDilutedOverall decimal.NullDecimal `json:"finalDilutedOverall"`
}
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)
}
}