From 689b1018ab2743d33bbb5a87dd0ed2c63a5875a1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BE=90=E6=B6=9B?= Date: Tue, 16 Aug 2022 10:12:31 +0800 Subject: [PATCH] =?UTF-8?q?enhance(fee):=E6=94=B9=E8=BF=9B=E7=BB=B4?= =?UTF-8?q?=E6=8A=A4=E8=B4=B9=E5=BD=92=E5=B1=9E=E5=88=A4=E6=96=AD=E9=80=BB?= =?UTF-8?q?=E8=BE=91=EF=BC=8C=E5=90=8C=E6=97=B6=E5=AE=8C=E6=88=90=E7=BB=B4?= =?UTF-8?q?=E6=8A=A4=E8=B4=B9=E9=83=A8=E5=88=86=E6=8E=A5=E5=8F=A3=E7=9A=84?= =?UTF-8?q?=E6=B5=8B=E8=AF=95=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- controller/maintenance_fee.go | 37 ++++++++++++++++++++++++++++++++++- router/router.go | 1 + service/maintenance_fee.go | 23 ++++++++++++++++++++++ 3 files changed, 60 insertions(+), 1 deletion(-) diff --git a/controller/maintenance_fee.go b/controller/maintenance_fee.go index 310108e..7122c61 100644 --- a/controller/maintenance_fee.go +++ b/controller/maintenance_fee.go @@ -25,6 +25,7 @@ func InitializeMaintenanceFeeController(router *gin.Engine) { MaintenanceFeeController.Router.GET("/maintenance/fee", security.EnterpriseAuthorize, listMaintenanceFees) MaintenanceFeeController.Router.POST("/maintenance/fee", security.EnterpriseAuthorize, createMaintenanceFeeRecord) + MaintenanceFeeController.Router.PUT("/maintenance/fee/:mid", security.EnterpriseAuthorize, modifyMaintenanceFeeRecord) } func listMaintenanceFees(c *gin.Context) { @@ -70,7 +71,7 @@ type _FeeCreationFormData 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"` + Memo *string `json:"memo" form:"memo"` } func createMaintenanceFeeRecord(c *gin.Context) { @@ -100,3 +101,37 @@ func createMaintenanceFeeRecord(c *gin.Context) { } result.Created("新维护费记录已经创建。") } + +type _FeeModificationFormData struct { + Fee decimal.Decimal `json:"fee" form:"fee"` + Memo *string `json:"memo" form:"memo"` +} + +func modifyMaintenanceFeeRecord(c *gin.Context) { + result := response.NewResult(c) + requestFee := c.Param("mid") + formData := new(_FeeModificationFormData) + c.BindJSON(formData) + userSession, err := _retreiveSession(c) + if err != nil { + result.Unauthorized(err.Error()) + return + } + sure, err := service.MaintenanceFeeService.EnsureFeeBelongs(userSession.Uid, requestFee) + if err != nil { + result.Error(http.StatusInternalServerError, err.Error()) + return + } + if !sure { + result.Unauthorized("所操作维护费记录不属于当前用户。") + return + } + newFeeState := new(model.MaintenanceFee) + copier.Copy(newFeeState, formData) + err = service.MaintenanceFeeService.ModifyMaintenanceFee(*newFeeState) + if err != nil { + result.Error(http.StatusInternalServerError, err.Error()) + return + } + result.Success("指定维护费条目已更新。") +} diff --git a/router/router.go b/router/router.go index 6a0502c..5d09163 100644 --- a/router/router.go +++ b/router/router.go @@ -19,6 +19,7 @@ func Router() *gin.Engine { controller.InitializeRegionController(router) controller.InitializeChargesController(router) controller.InitializeParkController(router) + controller.InitializeMaintenanceFeeController(router) return router } diff --git a/service/maintenance_fee.go b/service/maintenance_fee.go index 4a74498..35fa96d 100644 --- a/service/maintenance_fee.go +++ b/service/maintenance_fee.go @@ -30,6 +30,7 @@ func (_MaintenanceFeeService) ListMaintenanceFees(pid []string) ([]model.Mainten func (_MaintenanceFeeService) CreateMaintenanceFeeRecord(fee model.MaintenanceFee) error { fee.Id = uuid.New().String() + fee.Enabled = true _, err := global.DBConn.Insert(fee) if err != nil { return err @@ -72,3 +73,25 @@ func (_MaintenanceFeeService) DeleteMaintenanceFee(fid string) error { } return nil } + +func (_MaintenanceFeeService) EnsureFeeBelongs(uid, mid string) (bool, error) { + var fee = make([]model.MaintenanceFee, 0) + err := global.DBConn. + ID(mid).Limit(1).Find(&fee) + if err != nil { + return false, err + } + if len(fee) == 0 { + return false, nil + } + var park = make([]model.Park, 0) + err = global.DBConn. + ID(fee[0].ParkId).Limit(1).Find(&park) + if err != nil { + return false, err + } + if len(park) == 0 { + return false, nil + } + return park[0].UserId == uid, nil +}