From b198bfc4234f08e6678f8756d2e9a0e3fe312563 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BE=90=E6=B6=9B?= Date: Sun, 21 Aug 2022 16:12:50 +0800 Subject: [PATCH] =?UTF-8?q?feat(report):=E5=AE=8C=E6=88=90=E8=AE=A1?= =?UTF-8?q?=E7=AE=97=E5=9B=AD=E5=8C=BA=E6=A6=82=E5=86=B5=E9=83=A8=E5=88=86?= =?UTF-8?q?=E7=9A=84=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 | 22 +++++++++++++++++++++- service/report.go | 43 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+), 1 deletion(-) diff --git a/controller/report.go b/controller/report.go index a5c1808..3fe9ff5 100644 --- a/controller/report.go +++ b/controller/report.go @@ -1,6 +1,7 @@ package controller import ( + "electricity_bill_calc/exceptions" "electricity_bill_calc/response" "electricity_bill_calc/security" "electricity_bill_calc/service" @@ -21,6 +22,7 @@ func InitializeReportController(router *gin.Engine) { 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) + router.POST("/report/:rid/summary/calculate", security.EnterpriseAuthorize, progressReportSummary) } func ensureReportBelongs(c *gin.Context, result *response.Result, requestReportId string) bool { @@ -170,5 +172,23 @@ func testCalculateReportSummary(c *gin.Context) { } summary.CalculatePrices() calcResults := tools.ConvertStructToMap(summary) - result.Json(http.StatusOK, "已完成园区概览的试计算。", gin.H{"result": lo.PickByKeys(calcResults, []string{"overallPrice", "criticalPrice", "peakPrice", "flat", "flatFee", "flatPrice", "valleyPrice"})}) + result.Json(http.StatusOK, "已完成园区概况的试计算。", gin.H{"result": lo.PickByKeys(calcResults, []string{"overallPrice", "criticalPrice", "peakPrice", "flat", "flatFee", "flatPrice", "valleyPrice"})}) +} + +func progressReportSummary(c *gin.Context) { + result := response.NewResult(c) + requestReportId := c.Param("rid") + if !ensureReportBelongs(c, result, requestReportId) { + return + } + err := service.ReportService.CalculateSummaryAndFinishStep(requestReportId) + if err != nil { + if nfErr, ok := err.(exceptions.NotFoundError); ok { + result.NotFound(nfErr.Error()) + } else { + result.Error(http.StatusInternalServerError, err.Error()) + } + return + } + result.Success("已经完成园区概况的计算,并可以进行到下一步骤。") } diff --git a/service/report.go b/service/report.go index dfcce86..9f3232f 100644 --- a/service/report.go +++ b/service/report.go @@ -1,6 +1,7 @@ package service import ( + "electricity_bill_calc/exceptions" "electricity_bill_calc/global" "electricity_bill_calc/model" "electricity_bill_calc/tools" @@ -160,3 +161,45 @@ func (_ReportService) UpdateReportSummary(summary *model.ReportSummary) error { _, err := global.DBConn.ID(summary.ReportId).Cols("overall", "overall_fee", "critical", "critical_fee", "peak", "peak_fee", "valley", "valley_fee", "basic_fee", "adjust_fee").Update(summary) return err } + +func (_ReportService) CalculateSummaryAndFinishStep(reportId string) error { + var report = new(model.Report) + has, err := global.DBConn.ID(reportId).NoAutoCondition().Get(report) + if err != nil { + return err + } + if !has { + return exceptions.NewNotFoundError("未找到指定报表") + } + var summary = new(model.ReportSummary) + has, err = global.DBConn.ID(reportId).NoAutoCondition().Get(summary) + if err != nil { + return err + } + if !has { + return exceptions.NewNotFoundError("未找到指定报表的园区概况") + } + tx := global.DBConn.NewSession() + if err = tx.Begin(); err != nil { + return err + } + defer tx.Close() + summary.CalculatePrices() + _, err = tx.ID(summary.ReportId).Cols("overall_price", "critical_price", "peak_price", "flat", "flat_fee", "flat_price", "valley_price").Update(summary) + if err != nil { + tx.Rollback() + return err + } + report.StepState.Summary = true + _, err = tx.ID(report.Id).Cols("step_state").Update(report) + if err != nil { + tx.Rollback() + return err + } + err = tx.Commit() + if err != nil { + tx.Rollback() + return err + } + return nil +}