geekai/api/handler/reward_handler.go

78 lines
1.5 KiB
Go
Raw Normal View History

2023-07-21 18:26:51 +08:00
package handler
import (
"chatplus/core"
"chatplus/core/types"
"chatplus/store/model"
"chatplus/utils"
"chatplus/utils/resp"
"github.com/gin-gonic/gin"
"gorm.io/gorm"
"strings"
2023-07-21 18:26:51 +08:00
)
type RewardHandler struct {
BaseHandler
db *gorm.DB
}
func NewRewardHandler(server *core.AppServer, db *gorm.DB) *RewardHandler {
h := RewardHandler{db: db}
h.App = server
return &h
}
// Verify 打赏码核销
func (h *RewardHandler) Verify(c *gin.Context) {
var data struct {
2023-07-21 22:29:14 +08:00
TxId string `json:"tx_id"`
2023-07-21 18:26:51 +08:00
}
if err := c.ShouldBindJSON(&data); err != nil {
resp.ERROR(c, types.InvalidArgs)
return
}
// 移除转账单号中间的空格,防止有人复制的时候多复制了空格
data.TxId = strings.ReplaceAll(data.TxId, " ", "")
2023-07-21 18:26:51 +08:00
var item model.Reward
res := h.db.Where("tx_id = ?", data.TxId).First(&item)
if res.Error != nil {
2023-07-21 22:29:14 +08:00
resp.ERROR(c, "无效的众筹交易流水号!")
2023-07-21 18:26:51 +08:00
return
}
if item.Status {
2023-07-21 22:29:14 +08:00
resp.ERROR(c, "当前众筹交易流水号已经被核销,请不要重复核销!")
2023-07-21 18:26:51 +08:00
return
}
user, err := utils.GetLoginUser(c, h.db)
if err != nil {
resp.HACKER(c)
return
}
tx := h.db.Begin()
2023-07-21 22:29:14 +08:00
calls := (item.Amount + 0.1) * 10
2023-07-21 18:26:51 +08:00
res = h.db.Model(&user).UpdateColumn("calls", gorm.Expr("calls + ?", calls))
if res.Error != nil {
resp.ERROR(c, "更新数据库失败!")
return
}
// 更新核销状态
item.Status = true
2023-07-22 08:42:30 +08:00
item.UserId = user.Id
2023-07-21 18:26:51 +08:00
res = h.db.Updates(&item)
if res.Error != nil {
tx.Rollback()
resp.ERROR(c, "更新数据库失败!")
return
}
tx.Commit()
resp.SUCCESS(c)
}