refactor(charge):基本完成用户续费记录接口到fiber框架的切换。
This commit is contained in:
parent
4b08952916
commit
e13193de6d
|
@ -9,35 +9,33 @@ import (
|
||||||
"strconv"
|
"strconv"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gofiber/fiber/v2"
|
||||||
"github.com/shopspring/decimal"
|
"github.com/shopspring/decimal"
|
||||||
)
|
)
|
||||||
|
|
||||||
func InitializeChargesController(router *gin.Engine) {
|
func InitializeChargesController(app *fiber.App) {
|
||||||
router.GET("/charges", security.OPSAuthorize, listAllCharges)
|
app.Get("/charges", security.OPSAuthorize, listAllCharges)
|
||||||
router.POST("/charge", security.OPSAuthorize, recordNewCharge)
|
app.Post("/charge", security.OPSAuthorize, recordNewCharge)
|
||||||
router.PUT("/charge/:uid/:seq", security.OPSAuthorize, modifyChargeState)
|
app.Put("/charge/:uid/:seq", security.OPSAuthorize, modifyChargeState)
|
||||||
}
|
}
|
||||||
|
|
||||||
func listAllCharges(c *gin.Context) {
|
func listAllCharges(c *fiber.Ctx) error {
|
||||||
result := response.NewResult(c)
|
result := response.NewResult(c)
|
||||||
requestPage, err := strconv.Atoi(c.DefaultQuery("page", "1"))
|
requestPage, err := strconv.Atoi(c.Query("page", "1"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
result.NotAccept("查询参数[page]格式不正确。")
|
return result.NotAccept("查询参数[page]格式不正确。")
|
||||||
return
|
|
||||||
}
|
}
|
||||||
requestKeyword := c.DefaultQuery("keyword", "")
|
requestKeyword := c.Query("keyword", "")
|
||||||
requestBeginDate := c.DefaultQuery("begin", "")
|
requestBeginDate := c.Query("begin", "")
|
||||||
requestEndDate := c.DefaultQuery("end", "")
|
requestEndDate := c.Query("end", "")
|
||||||
charges, total, err := service.ChargeService.ListPagedChargeRecord(requestKeyword, requestBeginDate, requestEndDate, requestPage)
|
charges, total, err := service.ChargeService.ListPagedChargeRecord(requestKeyword, requestBeginDate, requestEndDate, requestPage)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
result.NotFound(err.Error())
|
return result.NotFound(err.Error())
|
||||||
return
|
|
||||||
}
|
}
|
||||||
result.Json(
|
return result.Json(
|
||||||
http.StatusOK, "已获取到符合条件的计费记录。",
|
http.StatusOK, "已获取到符合条件的计费记录。",
|
||||||
response.NewPagedResponse(requestPage, total).ToMap(),
|
response.NewPagedResponse(requestPage, total).ToMap(),
|
||||||
gin.H{"records": charges},
|
fiber.Map{"records": charges},
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -49,10 +47,12 @@ type _NewChargeFormData struct {
|
||||||
ChargeTo model.Date `json:"chargeTo" form:"chargeTo"`
|
ChargeTo model.Date `json:"chargeTo" form:"chargeTo"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func recordNewCharge(c *gin.Context) {
|
func recordNewCharge(c *fiber.Ctx) error {
|
||||||
result := response.NewResult(c)
|
result := response.NewResult(c)
|
||||||
formData := new(_NewChargeFormData)
|
formData := new(_NewChargeFormData)
|
||||||
c.BindJSON(formData)
|
if err := c.BodyParser(formData); err != nil {
|
||||||
|
return result.UnableToParse("无法解析提交的数据。")
|
||||||
|
}
|
||||||
currentTime := time.Now()
|
currentTime := time.Now()
|
||||||
newRecord := &model.UserCharge{
|
newRecord := &model.UserCharge{
|
||||||
UserId: formData.UserId,
|
UserId: formData.UserId,
|
||||||
|
@ -65,30 +65,29 @@ func recordNewCharge(c *gin.Context) {
|
||||||
}
|
}
|
||||||
err := service.ChargeService.CreateChargeRecord(newRecord, true)
|
err := service.ChargeService.CreateChargeRecord(newRecord, true)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
result.Error(http.StatusInternalServerError, err.Error())
|
return result.Error(http.StatusInternalServerError, err.Error())
|
||||||
return
|
|
||||||
}
|
}
|
||||||
result.Created("指定用户的服务已延期。")
|
return result.Created("指定用户的服务已延期。")
|
||||||
}
|
}
|
||||||
|
|
||||||
type _StateChangeFormData struct {
|
type _StateChangeFormData struct {
|
||||||
Cancelled bool `json:"cancelled"`
|
Cancelled bool `json:"cancelled"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func modifyChargeState(c *gin.Context) {
|
func modifyChargeState(c *fiber.Ctx) error {
|
||||||
result := response.NewResult(c)
|
result := response.NewResult(c)
|
||||||
formData := new(_StateChangeFormData)
|
formData := new(_StateChangeFormData)
|
||||||
c.BindJSON(formData)
|
if err := c.BodyParser(formData); err != nil {
|
||||||
requestUserID := c.Param("uid")
|
return result.UnableToParse("无法解析提交的数据。")
|
||||||
requestChargeSeq, err := strconv.Atoi(c.Param("seq"))
|
}
|
||||||
if err != nil {
|
requestUserID := c.Params("uid", "")
|
||||||
result.Error(http.StatusNotAcceptable, "参数[记录流水号]解析错误。")
|
requestChargeSeq, err := strconv.Atoi(c.Params("seq", "-1"))
|
||||||
return
|
if err != nil || requestChargeSeq == -1 {
|
||||||
|
return result.Error(http.StatusNotAcceptable, "参数[记录流水号]解析错误。")
|
||||||
}
|
}
|
||||||
err = service.ChargeService.CancelCharge(int64(requestChargeSeq), requestUserID)
|
err = service.ChargeService.CancelCharge(int64(requestChargeSeq), requestUserID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
result.Error(http.StatusInternalServerError, err.Error())
|
return result.Error(http.StatusInternalServerError, err.Error())
|
||||||
return
|
|
||||||
}
|
}
|
||||||
result.Updated("指定用户服务延期记录状态已经更新。")
|
return result.Updated("指定用户服务延期记录状态已经更新。")
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user