forked from free-lancers/electricity_bill_calc_service
		
	enhance(fee):改进维护费归属判断逻辑,同时完成维护费部分接口的测试。
This commit is contained in:
		@@ -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("指定维护费条目已更新。")
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -19,6 +19,7 @@ func Router() *gin.Engine {
 | 
			
		||||
	controller.InitializeRegionController(router)
 | 
			
		||||
	controller.InitializeChargesController(router)
 | 
			
		||||
	controller.InitializeParkController(router)
 | 
			
		||||
	controller.InitializeMaintenanceFeeController(router)
 | 
			
		||||
 | 
			
		||||
	return router
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -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
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user