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 +}