From 957eb445b5b0960f296ebcd15022d8bb4d18f15a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BE=90=E6=B6=9B?= Date: Sun, 21 Aug 2022 13:39:57 +0800 Subject: [PATCH] =?UTF-8?q?feat(report):=E5=AE=8C=E6=88=90=E6=8A=A5?= =?UTF-8?q?=E8=A1=A8=E4=B8=AD=E5=9B=AD=E5=8C=BA=E6=A6=82=E5=86=B5=E7=9A=84?= =?UTF-8?q?=E8=AF=95=E8=AE=A1=E7=AE=97=E5=8A=9F=E8=83=BD=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- controller/report.go | 21 ++++++++++++++++++++- service/report.go | 2 +- tools/utils.go | 9 +++++++++ 3 files changed, 30 insertions(+), 2 deletions(-) diff --git a/controller/report.go b/controller/report.go index 0838698..a5c1808 100644 --- a/controller/report.go +++ b/controller/report.go @@ -4,11 +4,13 @@ import ( "electricity_bill_calc/response" "electricity_bill_calc/security" "electricity_bill_calc/service" + "electricity_bill_calc/tools" "net/http" "time" "github.com/gin-gonic/gin" "github.com/jinzhu/copier" + "github.com/samber/lo" "github.com/shopspring/decimal" ) @@ -18,6 +20,7 @@ func InitializeReportController(router *gin.Engine) { router.GET("/report/:rid/step/state", security.EnterpriseAuthorize, fetchReportStepStates) router.GET("/report/:rid/summary", security.EnterpriseAuthorize, fetchReportParkSummary) router.PUT("/report/:rid/summary", security.EnterpriseAuthorize, fillReportSummary) + router.GET("/report/:rid/summary/calculate", security.EnterpriseAuthorize, testCalculateReportSummary) } func ensureReportBelongs(c *gin.Context, result *response.Result, requestReportId string) bool { @@ -142,7 +145,7 @@ func fillReportSummary(c *gin.Context) { c.BindJSON(formData) originSummary, err := service.ReportService.RetreiveReportSummary(requestReportId) if err != nil { - result.Error(http.StatusInternalServerError, err.Error()) + result.NotFound(err.Error()) return } copier.Copy(originSummary, formData) @@ -153,3 +156,19 @@ func fillReportSummary(c *gin.Context) { } result.Updated("指定电费公示报表中的园区概况基本数据已经完成更新。") } + +func testCalculateReportSummary(c *gin.Context) { + result := response.NewResult(c) + requestReportId := c.Param("rid") + if !ensureReportBelongs(c, result, requestReportId) { + return + } + summary, err := service.ReportService.RetreiveReportSummary(requestReportId) + if err != nil { + result.NotFound(err.Error()) + return + } + summary.CalculatePrices() + calcResults := tools.ConvertStructToMap(summary) + result.Json(http.StatusOK, "已完成园区概览的试计算。", gin.H{"result": lo.PickByKeys(calcResults, []string{"overallPrice", "criticalPrice", "peakPrice", "flat", "flatFee", "flatPrice", "valleyPrice"})}) +} diff --git a/service/report.go b/service/report.go index 5bc899b..dfcce86 100644 --- a/service/report.go +++ b/service/report.go @@ -149,7 +149,7 @@ func (_ReportService) RetreiveReportIndex(rid string) (*model.Report, error) { func (_ReportService) RetreiveReportSummary(rid string) (*model.ReportSummary, error) { var summary = new(model.ReportSummary) - _, err := global.DBConn.ID(rid).Get(summary) + _, err := global.DBConn.ID(rid).NoAutoCondition().Get(summary) if err != nil { return nil, err } diff --git a/tools/utils.go b/tools/utils.go index cd2592b..0f3d6c7 100644 --- a/tools/utils.go +++ b/tools/utils.go @@ -1,6 +1,7 @@ package tools import ( + "encoding/json" "strings" "github.com/mozillazg/go-pinyin" @@ -21,3 +22,11 @@ func PinyinAbbr(source string) string { finalAbbr := strings.Join(abbrCollect, "") return finalAbbr } + +// 将给定结构体的内容通过JSON转换为`map[string]interface{}`类型的值 +func ConvertStructToMap[T any](origin T) map[string]interface{} { + incr, _ := json.Marshal(origin) + var dest map[string]interface{} + json.Unmarshal(incr, &dest) + return dest +}