From 738472a5fbd982323879327768d8aba44790c96e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BE=90=E6=B6=9B?= Date: Sat, 20 Aug 2022 16:15:17 +0800 Subject: [PATCH] =?UTF-8?q?enhance(report):=E5=9C=A8=E5=88=9D=E5=A7=8B?= =?UTF-8?q?=E5=8C=96=E6=8A=A5=E8=A1=A8=E7=9A=84=E6=97=B6=E5=80=99=EF=BC=8C?= =?UTF-8?q?=E4=B8=80=E5=B9=B6=E5=88=9D=E5=A7=8B=E5=8C=96=E5=9B=AD=E5=8C=BA?= =?UTF-8?q?=E7=94=B5=E9=87=8F=E6=A6=82=E8=A6=81=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- controller/report.go | 4 +--- service/report.go | 29 +++++++++++++++++++++++------ 2 files changed, 24 insertions(+), 9 deletions(-) diff --git a/controller/report.go b/controller/report.go index 7cb4f9c..eab0663 100644 --- a/controller/report.go +++ b/controller/report.go @@ -1,7 +1,6 @@ package controller import ( - "electricity_bill_calc/model" "electricity_bill_calc/response" "electricity_bill_calc/security" "electricity_bill_calc/service" @@ -64,8 +63,7 @@ func initializeNewReport(c *gin.Context) { result.NotAccept("只能初始化已发布报表下一个月份的新报表。") return } - newReport := model.Report{ParkId: requestParkId, Period: reportPeriod} - err = service.ReportService.InitializeNewReport(newReport) + err = service.ReportService.InitializeNewReport(requestParkId, reportPeriod) if err != nil { result.Error(http.StatusInternalServerError, err.Error()) return diff --git a/service/report.go b/service/report.go index ba829b6..1dc0ef9 100644 --- a/service/report.go +++ b/service/report.go @@ -104,13 +104,30 @@ func (_ReportService) IsNewPeriodValid(uid string, period time.Time) (bool, erro } } -func (_ReportService) InitializeNewReport(report model.Report) (err error) { - report.Id = uuid.New().String() - report.StepState = model.NewSteps() - report.Published = false - report.Withdraw = model.REPORT_NOT_WITHDRAW - _, err = global.DBConn.Insert(report) +func (_ReportService) InitializeNewReport(parkId string, period time.Time) (err error) { + newReport := model.Report{ + Id: uuid.New().String(), + ParkId: parkId, + StepState: model.NewSteps(), + Published: false, + Withdraw: model.REPORT_NOT_WITHDRAW, + } + newReportSummary := model.ReportSummary{ + ReportId: newReport.Id, + } + tx := global.DBConn.NewSession() + if err = tx.Begin(); err != nil { + return + } + defer tx.Close() + _, err = tx.Insert(newReport, newReportSummary) if err != nil { + tx.Rollback() + return + } + err = tx.Commit() + if err != nil { + tx.Rollback() return } return nil