From ef8bd0c61796293fdaaaf8fe757839a1d7a590ac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BE=90=E6=B6=9B?= Date: Sun, 21 Aug 2022 18:25:56 +0800 Subject: [PATCH] =?UTF-8?q?feat(report):=E5=AE=8C=E6=88=90=E5=BE=85?= =?UTF-8?q?=E6=91=8A=E8=96=84=E7=BB=B4=E6=8A=A4=E8=B4=B9=E7=9A=84=E6=89=B9?= =?UTF-8?q?=E9=87=8F=E5=AF=BC=E5=85=A5=E5=92=8C=E6=B7=BB=E5=8A=A0=E5=8A=9F?= =?UTF-8?q?=E8=83=BD=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- controller/report.go | 105 +++++++++++++++++++++++++++++++++++++++++++ service/report.go | 34 ++++++++++++++ 2 files changed, 139 insertions(+) diff --git a/controller/report.go b/controller/report.go index 3fe9ff5..ce13aa3 100644 --- a/controller/report.go +++ b/controller/report.go @@ -2,13 +2,16 @@ package controller import ( "electricity_bill_calc/exceptions" + "electricity_bill_calc/model" "electricity_bill_calc/response" "electricity_bill_calc/security" "electricity_bill_calc/service" "electricity_bill_calc/tools" + "log" "net/http" "time" + "github.com/fufuok/utils" "github.com/gin-gonic/gin" "github.com/jinzhu/copier" "github.com/samber/lo" @@ -23,6 +26,9 @@ func InitializeReportController(router *gin.Engine) { 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) + router.GET("/report/:rid/maintenance", security.EnterpriseAuthorize, fetchWillDilutedFees) + router.POST("/report/:rid/maintenance", security.EnterpriseAuthorize, createTemporaryWillDilutedFee) + router.POST("/report/:rid/maintenance/import", security.EnterpriseAuthorize, importPredefinedMaintenanceFees) } func ensureReportBelongs(c *gin.Context, result *response.Result, requestReportId string) bool { @@ -192,3 +198,102 @@ func progressReportSummary(c *gin.Context) { } result.Success("已经完成园区概况的计算,并可以进行到下一步骤。") } + +func fetchWillDilutedFees(c *gin.Context) { + result := response.NewResult(c) + requestReportId := c.Param("rid") + if !ensureReportBelongs(c, result, requestReportId) { + return + } + fees, err := service.ReportService.FetchWillDulutedMaintenanceFees(requestReportId) + if err != nil { + result.NotFound(err.Error()) + return + } + result.Json(http.StatusOK, "待摊薄费用已经获取到。", gin.H{"fees": fees}) +} + +type DilutedFeeCreationFormData struct { + ParkId string `json:"parkId" form:"parkId"` + Name string `json:"name" form:"name"` + Fee decimal.Decimal `json:"fee" form:"fee"` + Memo *string `json:"memo" form:"memo"` +} + +func createTemporaryWillDilutedFee(c *gin.Context) { + result := response.NewResult(c) + requestReportId := c.Param("rid") + if !ensureReportBelongs(c, result, requestReportId) { + return + } + formData := new(DilutedFeeCreationFormData) + c.BindJSON(formData) + report, err := service.ReportService.RetreiveReportIndex(requestReportId) + if err != nil { + result.NotFound(err.Error()) + return + } + if formData.ParkId != report.ParkId { + result.NotAccept("选择的园区与公示报表所属的园区不一致。") + return + } + newWillDilutedFee := new(model.WillDilutedFee) + copier.Copy(newWillDilutedFee, formData) + newWillDilutedFee.ReportId = report.Id + err = service.ReportService.CreateTemporaryWillDilutedMaintenanceFee(*newWillDilutedFee) + if err != nil { + result.Error(http.StatusInternalServerError, err.Error()) + return + } + result.Created("公示报表中所要使用的临时待摊薄费用已添加。") +} + +func importPredefinedMaintenanceFees(c *gin.Context) { + result := response.NewResult(c) + requestReportId := c.Param("rid") + if !ensureReportBelongs(c, result, requestReportId) { + return + } + report, err := service.ReportService.RetreiveReportIndex(requestReportId) + if err != nil { + result.NotFound(err.Error()) + return + } + maintenanceFees, err := service.MaintenanceFeeService.ListMaintenanceFees([]string{report.ParkId}) + if err != nil { + result.Error(http.StatusInternalServerError, err.Error()) + return + } + log.Printf("[cotroller] [debug] fees: %+v", maintenanceFees) + enabledMaintenanceFees := lo.Filter( + maintenanceFees, + func(elem model.MaintenanceFee, index int) bool { + return elem.Enabled + }, + ) + log.Printf("[cotroller] [debug] fees: %+v", enabledMaintenanceFees) + if len(enabledMaintenanceFees) == 0 { + result.NotFound("没有找到可供导入的配电维护费记录。") + return + } + dilutedFees := lo.Map( + enabledMaintenanceFees, + func(elem model.MaintenanceFee, index int) model.WillDilutedFee { + fee := &model.WillDilutedFee{ + Id: utils.UUIDString(), + ReportId: report.Id, + SourceId: lo.ToPtr(elem.Id), + Name: elem.Name, + Fee: elem.Fee, + Memo: elem.Memo, + } + return *fee + }, + ) + err = service.ReportService.BatchSaveMaintenanceFee(dilutedFees) + if err != nil { + result.Error(http.StatusInternalServerError, err.Error()) + return + } + result.Created("预定义的配电维护费已经导入。") +} diff --git a/service/report.go b/service/report.go index 9f3232f..b39d1d2 100644 --- a/service/report.go +++ b/service/report.go @@ -7,6 +7,7 @@ import ( "electricity_bill_calc/tools" "time" + "github.com/fufuok/utils" "github.com/google/uuid" "github.com/samber/lo" "xorm.io/builder" @@ -203,3 +204,36 @@ func (_ReportService) CalculateSummaryAndFinishStep(reportId string) error { } return nil } + +func (_ReportService) FetchWillDulutedMaintenanceFees(reportId string) ([]model.WillDilutedFee, error) { + fees := make([]model.WillDilutedFee, 0) + err := global.DBConn.Where(builder.Eq{"report_id": reportId}).Asc("created_at").Find(&fees) + if err != nil { + return make([]model.WillDilutedFee, 0), nil + } + return fees, nil +} + +func (_ReportService) CreateTemporaryWillDilutedMaintenanceFee(fee model.WillDilutedFee) error { + fee.Id = utils.UUIDString() + _, err := global.DBConn.Insert(fee) + return err +} + +func (_ReportService) BatchSaveMaintenanceFee(fees []model.WillDilutedFee) error { + tx := global.DBConn.NewSession() + if err := tx.Begin(); err != nil { + return err + } + defer tx.Close() + _, err := tx.Insert(fees) + if err != nil { + return err + } + err = tx.Commit() + if err != nil { + tx.Rollback() + return err + } + return nil +}