electricity_bill_calc_service/model/report_summary.go
2022-08-23 15:52:22 +08:00

103 lines
6.7 KiB
Go

package model
import (
"errors"
"github.com/shopspring/decimal"
)
type ReportSummary struct {
ReportId string `xorm:"varchar(120) pk not null" json:"-"`
Overall decimal.Decimal `xorm:"numeric(14,2) not null default 0" json:"overall"`
OverallFee decimal.Decimal `xorm:"numeric(14,2) not null default 0" json:"overallFee"`
ConsumptionFee decimal.NullDecimal `xorm:"numeric(14,2)" json:"consumptionFee"`
OverallPrice decimal.NullDecimal `xorm:"numeric(16,8)" json:"overallPrice"`
Critical decimal.Decimal `xorm:"numeric(14,2) not null default 0" json:"critical"`
CriticalFee decimal.Decimal `xorm:"numeric(14,2) not null default 0" json:"criticalFee"`
CriticalPrice decimal.NullDecimal `xorm:"numeric(16,8)" json:"criticalPrice"`
Peak decimal.Decimal `xorm:"numeric(14,2) not null default 0" json:"peak"`
PeakFee decimal.Decimal `xorm:"numeric(14,2) not null default 0" json:"peakFee"`
PeakPrice decimal.NullDecimal `xorm:"numeric(16,8)" json:"peakPrice"`
Flat decimal.Decimal `xorm:"numeric(14,2) not null default 0" json:"flat"`
FlatFee decimal.Decimal `xorm:"numeric(14,2) not null default 0" json:"flatFee"`
FlatPrice decimal.NullDecimal `xorm:"numeric(16,8)" json:"flatPrice"`
Valley decimal.Decimal `xorm:"numeric(14,2) not null default 0" json:"valley"`
ValleyFee decimal.Decimal `xorm:"numeric(14,2) not null default 0" json:"valleyFee"`
ValleyPrice decimal.NullDecimal `xorm:"numeric(16,8)" json:"valleyPrice"`
Loss decimal.NullDecimal `xorm:"numeric(14,2)" json:"loss"`
LossFee decimal.NullDecimal `xorm:"numeric(16,2)" json:"lossFee"`
LossProportion decimal.NullDecimal `xorm:"numeric(16,15)" json:"lossProportion"`
CustomerConsumption decimal.NullDecimal `xorm:"numeric(16,2)" json:"customerConsumption"`
CustomerConsumptionFee decimal.NullDecimal `xorm:"numeric(14,2)" json:"customerConsumptionFee"`
CustomerConsumptionCritical decimal.NullDecimal `xorm:"numeric(16,2)" json:"customerConsumptionCritical"`
CustomerConsumptionCriticalFee decimal.NullDecimal `xorm:"numeric(14,2)" json:"customerConsumptionCriticalFee"`
CustomerConsumptionPeak decimal.NullDecimal `xorm:"numeric(16,2)" json:"customerConsumptionPeak"`
CustomerConsumptionPeakFee decimal.NullDecimal `xorm:"numeric(14,2)" json:"customerConsumptionPeakFee"`
CustomerConsumptionFlat decimal.NullDecimal `xorm:"numeric(16,2)" json:"customerConsumptionFlat"`
CustomerConsumptionFlatFee decimal.NullDecimal `xorm:"numeric(14,2)" json:"customerConsumptionFlatFee"`
CustomerConsumptionValley decimal.NullDecimal `xorm:"numeric(16,2)" json:"customerConsumptionValley"`
CustomerConsumptionValleyFee decimal.NullDecimal `xorm:"numeric(14,2)" json:"customerConsumptionValleyFee"`
PublicConsumption decimal.NullDecimal `xorm:"numeric(14,2)" json:"publicConsumption"`
PublicConsumptionFee decimal.NullDecimal `xorm:"numeric(14,2)" json:"publicConsumptionFee"`
PublicConsumptionProportion decimal.NullDecimal `xorm:"numeric(16,15)" json:"publicConsumptionProportion"`
PublicConsumptionCritical decimal.NullDecimal `xorm:"numeric(16,2)" json:"publicConsumptionCritical"`
PublicConsumptionCriticalFee decimal.NullDecimal `xorm:"numeric(14,2)" json:"publicConsumptionCriticalFee"`
PublicConsumptionPeak decimal.NullDecimal `xorm:"numeric(16,2)" json:"publicConsumptionPeak"`
PublicConsumptionPeakFee decimal.NullDecimal `xorm:"numeric(14,2)" json:"publicConsumptionPeakFee"`
PublicConsumptionFlat decimal.NullDecimal `xorm:"numeric(16,2)" json:"publicConsumptionFlat"`
PublicConsumptionFlatFee decimal.NullDecimal `xorm:"numeric(14,2)" json:"publicConsumptionFlatFee"`
PublicConsumptionValley decimal.NullDecimal `xorm:"numeric(16,2)" json:"publicConsumptionValley"`
PublicConsumptionValleyFee decimal.NullDecimal `xorm:"numeric(14,2)" json:"publicConsumptionValleyFee"`
BasicFee decimal.Decimal `xorm:"numeric(14,2) not null default 0" json:"basicFee"`
BasicDilutedPrice decimal.NullDecimal `xorm:"numeric(18,8)" json:"basicDilutedPrice"`
AdjustFee decimal.Decimal `xorm:"numeric(14,2) not null default 0" json:"adjustFee"`
AdjustDilutedPrice decimal.NullDecimal `xorm:"numeric(18,8)" json:"adjustDilutedPrice"`
MaintenanceDilutedPrice decimal.NullDecimal `xorm:"numeric(16,8)" json:"maintencanceDilutedPrice"`
LossDilutedPrice decimal.NullDecimal `xorm:"numeric(16,8)" json:"lossDilutedPrice"`
PublicConsumptionDilutedPrice decimal.NullDecimal `xorm:"numeric(16,8)" json:"publicConsumptionDilutedPrice"`
FinalDilutedOverall decimal.NullDecimal `xorm:"numeric(14,2)" json:"finalDilutedOverall"`
}
func (ReportSummary) TableName() string {
return "report_summary"
}
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))
s.OverallPrice = decimal.NewNullDecimal(s.ConsumptionFee.Decimal.Div(s.Overall).RoundBank(8))
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.OverallFee.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)
}
}