feat(charge):基本完成所有用户服务延期记录部分的功能,待测。

This commit is contained in:
徐涛
2022-08-15 15:40:33 +08:00
parent 68c34f540f
commit f033691aa4
5 changed files with 232 additions and 11 deletions

View File

@@ -1,13 +1,17 @@
package controller
import (
"electricity_bill_calc/model"
"electricity_bill_calc/repository"
"electricity_bill_calc/response"
"electricity_bill_calc/security"
"electricity_bill_calc/service"
"net/http"
"strconv"
"time"
"github.com/gin-gonic/gin"
"github.com/shopspring/decimal"
)
type _ChargesController struct {
@@ -22,7 +26,8 @@ func InitializeChargesController(router *gin.Engine) {
}
ChargesController.Router.GET("/charges", security.OPSAuthorize, listAllCharges)
ChargesController.Router.POST("/charge", security.OPSAuthorize, recordNewCharge)
ChargesController.Router.PUT("/charge/:uid/:seq", security.OPSAuthorize, modifyChargeState)
}
func listAllCharges(c *gin.Context) {
@@ -46,3 +51,55 @@ func listAllCharges(c *gin.Context) {
gin.H{"records": charges},
)
}
type _NewChargeFormData struct {
UserId string `json:"userId" form:"userId"`
Fee decimal.NullDecimal `json:"fee" form:"fee"`
Discount decimal.NullDecimal `json:"discount" form:"discount"`
Amount decimal.NullDecimal `json:"amount" form:"amount"`
ChargeTo time.Time `json:"chargeTo" form:"chargeTo" time_format:"simple_date" time_location:"shanghai"`
}
func recordNewCharge(c *gin.Context) {
result := response.NewResult(c)
formData := new(_NewChargeFormData)
c.BindJSON(formData)
currentTime := time.Now()
newRecord := &model.UserCharge{
UserId: formData.UserId,
Fee: formData.Fee,
Discount: formData.Discount,
Amount: formData.Amount,
Settled: true,
SettledAt: &currentTime,
ChargeTo: formData.ChargeTo,
}
err := service.ChargeService.CreateChargeRecord(newRecord, true)
if err != nil {
result.Error(http.StatusInternalServerError, err.Error())
return
}
result.Success("指定用户的服务已延期。")
}
type _StateChangeFormData struct {
Cancelled bool `json:"cancelled"`
}
func modifyChargeState(c *gin.Context) {
result := response.NewResult(c)
formData := new(_StateChangeFormData)
c.BindJSON(formData)
requestUserID := c.Param("uid")
requestChargeSeq, err := strconv.Atoi(c.Param("seq"))
if err != nil {
result.Error(http.StatusNotAcceptable, "参数[记录流水号]解析错误。")
return
}
err = service.ChargeService.CancelCharge(int64(requestChargeSeq), requestUserID)
if err != nil {
result.Error(http.StatusInternalServerError, err.Error())
return
}
result.Success("指定用户服务延期记录状态已经更新。")
}