forked from free-lancers/electricity_bill_calc_service
		
	feat(report):安排部分基本报表计算。
This commit is contained in:
		| @@ -1,6 +1,10 @@ | |||||||
| package model | package model | ||||||
|  |  | ||||||
| import "github.com/shopspring/decimal" | import ( | ||||||
|  | 	"errors" | ||||||
|  |  | ||||||
|  | 	"github.com/shopspring/decimal" | ||||||
|  | ) | ||||||
|  |  | ||||||
| type EndUserDetail struct { | type EndUserDetail struct { | ||||||
| 	CreatedAndModified       `xorm:"extends"` | 	CreatedAndModified       `xorm:"extends"` | ||||||
| @@ -32,6 +36,7 @@ type EndUserDetail struct { | |||||||
| 	AdjustValley             decimal.Decimal     `xorm:"numeric(14,2) not null default 0" json:"adjustValley"` | 	AdjustValley             decimal.Decimal     `xorm:"numeric(14,2) not null default 0" json:"adjustValley"` | ||||||
| 	Overall                  decimal.NullDecimal `xorm:"numeric(14,2)" json:"overall"` | 	Overall                  decimal.NullDecimal `xorm:"numeric(14,2)" json:"overall"` | ||||||
| 	OverallFee               decimal.NullDecimal `xorm:"numeric(14,2)" json:"overallFee"` | 	OverallFee               decimal.NullDecimal `xorm:"numeric(14,2)" json:"overallFee"` | ||||||
|  | 	OverallProporttion       decimal.Decimal     `xorm:"numeric(16,15) not null default 0" json:"-"` | ||||||
| 	Critical                 decimal.NullDecimal `xorm:"numeric(14,2)" json:"critical"` | 	Critical                 decimal.NullDecimal `xorm:"numeric(14,2)" json:"critical"` | ||||||
| 	CriticalFee              decimal.NullDecimal `xorm:"numeric(18,8)" json:"criticalFee"` | 	CriticalFee              decimal.NullDecimal `xorm:"numeric(18,8)" json:"criticalFee"` | ||||||
| 	Peak                     decimal.NullDecimal `xorm:"numeric(14,2)" json:"peak"` | 	Peak                     decimal.NullDecimal `xorm:"numeric(14,2)" json:"peak"` | ||||||
| @@ -52,3 +57,23 @@ type EndUserDetail struct { | |||||||
| func (EndUserDetail) TableName() string { | func (EndUserDetail) TableName() string { | ||||||
| 	return "end_user_detail" | 	return "end_user_detail" | ||||||
| } | } | ||||||
|  |  | ||||||
|  | func (d EndUserDetail) Validate() (bool, error) { | ||||||
|  | 	lastPeriodSum := decimal.Sum(d.LastPeriodCritical, d.LastPeriodPeak, d.LastPeriodValley) | ||||||
|  | 	if lastPeriodSum.GreaterThan(d.LastPeriodOverall) { | ||||||
|  | 		return false, errors.New("上期峰谷计量总量大于上期总计电量") | ||||||
|  | 	} | ||||||
|  | 	currentPeriodSum := decimal.Sum(d.CurrentPeriodCritical, d.CurrentPeriodPeak, d.CurrentPeriodValley) | ||||||
|  | 	if currentPeriodSum.GreaterThan(d.CurrentPeriodOverall) { | ||||||
|  | 		return false, errors.New("本期峰谷计量总量大于本期总计电量") | ||||||
|  | 	} | ||||||
|  | 	return true, nil | ||||||
|  | } | ||||||
|  |  | ||||||
|  | func (d *EndUserDetail) CalculatePeriod() { | ||||||
|  | 	d.Overall = decimal.NewNullDecimal(d.CurrentPeriodOverall.Sub(d.LastPeriodOverall).Add(d.AdjustOverall).RoundBank(2)) | ||||||
|  | 	d.Critical = decimal.NewNullDecimal(d.CurrentPeriodCritical.Sub(d.LastPeriodCritical).Add(d.AdjustCritical).RoundBank(2)) | ||||||
|  | 	d.Peak = decimal.NewNullDecimal(d.CurrentPeriodPeak.Sub(d.LastPeriodPeak).Add(d.AdjustPeak).RoundBank(2)) | ||||||
|  | 	d.Flat = decimal.NewNullDecimal(d.CurrentPeriodFlat.Sub(d.LastPeriodFlat).Add(d.AdjustFlat).RoundBank(2)) | ||||||
|  | 	d.Valley = decimal.NewNullDecimal(d.CurrentPeriodValley.Sub(d.LastPeriodValley).Add(d.AdjustValley).RoundBank(2)) | ||||||
|  | } | ||||||
|   | |||||||
| @@ -1,12 +1,16 @@ | |||||||
| package model | package model | ||||||
|  |  | ||||||
| import "github.com/shopspring/decimal" | import ( | ||||||
|  | 	"errors" | ||||||
|  |  | ||||||
|  | 	"github.com/shopspring/decimal" | ||||||
|  | ) | ||||||
|  |  | ||||||
| type ReportSummary struct { | type ReportSummary struct { | ||||||
| 	ReportId                      string              `xorm:"varchar(120) pk not null" json:"-"` | 	ReportId                      string              `xorm:"varchar(120) pk not null" json:"-"` | ||||||
| 	Overall                       decimal.Decimal     `xorm:"numeric(14,2) not null default 0" json:"overall"` | 	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"` | 	OverallFee                    decimal.Decimal     `xorm:"numeric(14,2) not null default 0" json:"overallFee"` | ||||||
| 	OverallPrice                  decimal.Decimal     `xorm:"numeric(16,8)" json:"overallPrice"` | 	OverallPrice                  decimal.NullDecimal `xorm:"numeric(16,8)" json:"overallPrice"` | ||||||
| 	Critical                      decimal.Decimal     `xorm:"numeric(14,2) not null default 0" json:"critical"` | 	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"` | 	CriticalFee                   decimal.Decimal     `xorm:"numeric(14,2) not null default 0" json:"criticalFee"` | ||||||
| 	CriticalPrice                 decimal.NullDecimal `xorm:"numeric(16,8)" json:"criticalPrice"` | 	CriticalPrice                 decimal.NullDecimal `xorm:"numeric(16,8)" json:"criticalPrice"` | ||||||
| @@ -35,3 +39,41 @@ type ReportSummary struct { | |||||||
| func (ReportSummary) TableName() string { | func (ReportSummary) TableName() string { | ||||||
| 	return "report_summary" | 	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) | ||||||
|  | 	} | ||||||
|  | } | ||||||
|   | |||||||
| @@ -7,7 +7,6 @@ import ( | |||||||
| 	"time" | 	"time" | ||||||
|  |  | ||||||
| 	"github.com/google/uuid" | 	"github.com/google/uuid" | ||||||
| 	"github.com/shopspring/decimal" |  | ||||||
| 	"xorm.io/builder" | 	"xorm.io/builder" | ||||||
| ) | ) | ||||||
|  |  | ||||||
| @@ -155,13 +154,3 @@ func (_ReportService) RetreiveParkSummary(rid string) (*model.ReportSummary, err | |||||||
| 	} | 	} | ||||||
| 	return summary, nil | 	return summary, nil | ||||||
| } | } | ||||||
|  |  | ||||||
| func (_ReportService) CheckSummaryOverall(summary model.ReportSummary) bool { |  | ||||||
| 	var sum = decimal.Sum(summary.Critical, summary.Peak, summary.Valley) |  | ||||||
| 	return sum.LessThan(summary.Overall) |  | ||||||
| } |  | ||||||
|  |  | ||||||
| func (_ReportService) CheckSummaryOverallFee(summary model.ReportSummary) bool { |  | ||||||
| 	var sum = decimal.Sum(summary.CriticalFee, summary.PeakFee, summary.ValleyFee) |  | ||||||
| 	return sum.LessThan(summary.OverallFee) |  | ||||||
| } |  | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user