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"` 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"` PublicConsumption decimal.NullDecimal `xorm:"numeric(14,2)" json:"publicConsumption"` PublicConsumptionFee decimal.NullDecimal `xorm:"numeric(14,2)" json:"publicConsumptionFee"` 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"` } 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.OverallPrice = decimal.NewNullDecimal(s.OverallFee.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) } }