forked from free-lancers/electricity_bill_calc_service
		
	[计算相关]计算线损以及调整线损(完成)
This commit is contained in:
		| @@ -26,6 +26,8 @@ type ReportIndex struct { | ||||
| 	Message                     *string         `json:"message"` | ||||
| 	CreatedAt                   types.DateTime  `json:"createdAt" db:"created_at"` | ||||
| 	LastModifiedAt              types.DateTime  `json:"lastModifiedAt" db:"last_modified_at"` | ||||
| 	AuthorizedLossRate          float64         `json:"authorized_loss_rate" db:"authorized_loss_rate"` | ||||
| 	AuthorizedLossRateIncrement float64         `json:"authorized_loss_rate_increment" db:"authorized_loss_rate_increment"` | ||||
| } | ||||
|  | ||||
| type ReportSummary struct { | ||||
|   | ||||
| @@ -1,7 +1,10 @@ | ||||
| package calculate | ||||
|  | ||||
| import ( | ||||
| 	"electricity_bill_calc/model" | ||||
| 	"electricity_bill_calc/model/calculate" | ||||
| 	"errors" | ||||
| 	"fmt" | ||||
| 	"github.com/shopspring/decimal" | ||||
| ) | ||||
|  | ||||
| @@ -37,3 +40,50 @@ func removeDuplicates(meters []calculate.Meter) []calculate.Meter { | ||||
| 	} | ||||
| 	return result | ||||
| } | ||||
|  | ||||
| //计算线损以及调整线损 | ||||
| func LossCalculate(report *model.ReportIndex, Public []calculate.Meter, | ||||
| 	publicTotal decimal.Decimal, summary calculate.Summary) error { | ||||
| 	summary.Loss = summary.Overall.Amount.Sub(summary.TotalConsumption) | ||||
|  | ||||
| 	var summaryAmount decimal.Decimal | ||||
| 	if summary.Overall.Amount == decimal.Zero { | ||||
| 		summaryAmount = decimal.NewFromFloat(1.0) | ||||
| 	} else { | ||||
| 		summaryAmount = summary.Overall.Amount | ||||
| 	} | ||||
|  | ||||
| 	summary.LossProportion = summary.Loss.Div(summaryAmount) | ||||
|  | ||||
| 	var authorizedLossRate decimal.Decimal | ||||
| 	if summary.LossProportion.InexactFloat64() > report.AuthorizedLossRate { | ||||
| 		authorizedLossRate = summary.LossProportion | ||||
| 	} else { | ||||
| 		return errors.New(fmt.Sprintf("经过核算园区的线损率为:{%.8f}, 核定线损率为:{%.8f}", summary.LossProportion.InexactFloat64(),authorizedLossRate.InexactFloat64())) | ||||
| 	} | ||||
|  | ||||
| 	summary.AuthoizeLoss = model.ConsumptionUnit{ | ||||
| 		Amount:     decimal.NewFromFloat(summary.Overall.Amount.InexactFloat64() * authorizedLossRate.InexactFloat64()), | ||||
| 		Fee:        decimal.NewFromFloat((summary.Overall.Amount.InexactFloat64() * authorizedLossRate.InexactFloat64()) * summary.Overall.Price.InexactFloat64()), | ||||
| 		Price:      summary.Overall.Price, | ||||
| 		Proportion: authorizedLossRate, | ||||
| 	} | ||||
|  | ||||
| 	differentialLoss := summary.LossDilutedPrice.Sub(summary.AuthoizeLoss.Amount) | ||||
|  | ||||
| 	if publicTotal.InexactFloat64() <= decimal.Zero.InexactFloat64() { | ||||
| 		return errors.New("园区公共表计的电量总和为非正值,或者园区未设置公共表计,无法计算核定线损") | ||||
| 	} | ||||
|  | ||||
| 	for _, meter := range Public { | ||||
| 		amountProportion := meter.Overall.Amount.InexactFloat64() / publicTotal.InexactFloat64() | ||||
| 		adjustAmount := differentialLoss.InexactFloat64() * decimal.NewFromFloat(-1.0).InexactFloat64() | ||||
| 		meter.AdjustLoss = model.ConsumptionUnit{ | ||||
| 			Amount:     decimal.NewFromFloat(adjustAmount), | ||||
| 			Fee:        decimal.NewFromFloat(adjustAmount * summary.LossDilutedPrice.InexactFloat64()), | ||||
| 			Price:      summary.LossDilutedPrice, | ||||
| 			Proportion: decimal.NewFromFloat(amountProportion), | ||||
| 		} | ||||
| 	} | ||||
| 	return nil | ||||
| } | ||||
|   | ||||
| @@ -65,6 +65,12 @@ func MainCalculateProcess(rid string) { | ||||
| 	//计算所有表计的总电量 | ||||
| 	parkTotal := TotalConsumptionCalculate(tenementReports, summary) | ||||
|  | ||||
| 	err = LossCalculate(report, parkMetersReports, parkTotal, summary) | ||||
| 	if err != nil { | ||||
| 		fmt.Println("9", err) | ||||
| 		return | ||||
| 	} | ||||
|  | ||||
| 	fmt.Println(meterRelations, poolingMetersReports, parkMetersReports, parkTotal) | ||||
|  | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user