102 lines
2.9 KiB
Go
102 lines
2.9 KiB
Go
package base
|
|
|
|
import (
|
|
"backend/client"
|
|
"backend/msg"
|
|
"backend/util"
|
|
"fmt"
|
|
"log"
|
|
"time"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
type BaseModel interface {
|
|
Login(c *gin.Context)
|
|
VerifyToken(Uid string, Token string) error
|
|
}
|
|
|
|
type Param struct {
|
|
Uid int `form:"uid" json:"uid" binding:"required"`
|
|
AppId int `form:"appId" json:"appId" binding:"required"`
|
|
ServerId int `form:"serverId" json:"serverId" binding:"required"`
|
|
OrderId string `form:"orderId" json:"orderId" binding:"required"`
|
|
ChannelOrderId string
|
|
}
|
|
|
|
const (
|
|
orderShippingMaxAttempts = 100
|
|
orderShippingRetryBackoff = time.Second
|
|
)
|
|
|
|
func (p *Param) ChangeOrderStatus(Platform string, prodprice string) error {
|
|
// 校验成功 修改订单状态为已支付
|
|
AppConf, err := util.GetAppConfig(p.AppId)
|
|
if err != nil {
|
|
log.Print("get app config error:", err)
|
|
return err
|
|
}
|
|
Db := util.MPool.GetMysqlDB(AppConf, 1)
|
|
defer Db.Close()
|
|
var chargeId int
|
|
var price float64
|
|
var paystatus int
|
|
err = Db.QueryRow("SELECT `ProductId`, `Price`, `PayStatus` FROM `t_player_charge` WHERE `OrderId`=?", p.OrderId).Scan(&chargeId, &price, &paystatus)
|
|
if err != nil {
|
|
log.Print("query order error:", err, p.OrderId)
|
|
return err
|
|
}
|
|
util.LogStructured("info", "order_info", map[string]any{
|
|
"orderId": p.OrderId,
|
|
"price": price,
|
|
"prodprice": prodprice,
|
|
"chargeId": chargeId,
|
|
"paystatus": paystatus,
|
|
})
|
|
// if fmt.Sprintf("%.2f", price) != prodprice {
|
|
// return fmt.Errorf("订单金额不匹配,数据库金额:%.2f,回调金额:%s; 订单id:%s, chargeid:%d", price, prodprice, p.OrderId, chargeId)
|
|
// }
|
|
if paystatus != 0 {
|
|
return fmt.Errorf("订单已支付,无法重复发货; 订单id:%s, chargeid:%d", p.OrderId, chargeId)
|
|
}
|
|
_, err = Db.Exec("UPDATE `t_player_charge` SET `PayStatus`=1, `PayTime`=?, `PayChannelOrderId` = ? , `PayPlatform` =? WHERE `OrderId`=? AND `PayStatus`=0", time.Now().Unix(), p.ChannelOrderId, Platform, p.OrderId)
|
|
return err
|
|
}
|
|
|
|
func ShipOrder(appInfo Param) error {
|
|
request := &msg.ReqOrderShipping{
|
|
OrderSn: appInfo.OrderId,
|
|
Status: 1,
|
|
ChannelOrderSn: appInfo.ChannelOrderId,
|
|
}
|
|
|
|
var lastErr error
|
|
for attempt := 1; attempt <= orderShippingMaxAttempts; attempt++ {
|
|
err := client.OrderShipping(appInfo.AppId, appInfo.ServerId, request)
|
|
if err == nil {
|
|
util.LogStructured("info", "rpc order shipping success", map[string]any{
|
|
"AppId": appInfo.AppId,
|
|
"ServerId": appInfo.ServerId,
|
|
"OrderId": appInfo.OrderId,
|
|
"attempt": attempt,
|
|
})
|
|
return nil
|
|
}
|
|
|
|
lastErr = err
|
|
util.LogStructured("warn", "rpc order shipping retry", map[string]any{
|
|
"AppId": appInfo.AppId,
|
|
"ServerId": appInfo.ServerId,
|
|
"OrderId": appInfo.OrderId,
|
|
"attempt": attempt,
|
|
"error": err.Error(),
|
|
})
|
|
|
|
if attempt < orderShippingMaxAttempts {
|
|
time.Sleep(orderShippingRetryBackoff)
|
|
}
|
|
}
|
|
|
|
return fmt.Errorf("rpc order shipping failed after %d attempts: %w", orderShippingMaxAttempts, lastErr)
|
|
}
|