- 新增用户订单号字段以区分内部订单号 - 修改订单表结构添加 user_order_id 字段及索引 - 更新 CreateOrder 接口支持用户订单号参数-重构 CreateOrder 和 GetPaymentUrl 方法返回统一结果对象 - 新增模型定义用于封装订单创建与支付结果 - 调整相关逻辑方法签名与调用方式适配新结构- 优化订单创建流程增加内部订单号生成逻辑 - 完善订单查询逻辑确保正确关联用户订单号- 更新控制器层对接新版服务接口- 升级 Cookie 状态及订单状态管理枚举类型使用
208 lines
6.3 KiB
Go
208 lines
6.3 KiB
Go
package jd_cookie
|
|
|
|
import (
|
|
"context"
|
|
v1 "kami/api/jd_cookie/v1"
|
|
"kami/internal/consts"
|
|
"kami/internal/dao"
|
|
"kami/internal/model"
|
|
"kami/internal/model/entity"
|
|
"kami/utility/config"
|
|
|
|
"github.com/gogf/gf/v2/errors/gerror"
|
|
"github.com/gogf/gf/v2/os/gtime"
|
|
"github.com/gogf/gf/v2/util/gconv"
|
|
)
|
|
|
|
// GetPaymentUrl 获取支付链接
|
|
func (s *sJdCookie) GetPaymentUrl(ctx context.Context, orderId string) (result *model.PaymentResult, err error) {
|
|
if orderId == "" {
|
|
return nil, gerror.New("订单号不能为空")
|
|
}
|
|
|
|
// 获取订单信息
|
|
order, err := s.getOrderByOrderId(ctx, orderId)
|
|
if err != nil {
|
|
return nil, gerror.Wrap(err, "查询订单失败")
|
|
}
|
|
if order == nil {
|
|
return nil, gerror.New(consts.ErrCodeOrderNotFound)
|
|
}
|
|
|
|
// 获取关联的京东订单
|
|
jdOrder, err := s.getJdOrderByJdOrderId(ctx, order.JdOrderId)
|
|
if err != nil {
|
|
return nil, gerror.Wrap(err, "查询京东订单失败")
|
|
}
|
|
if jdOrder == nil {
|
|
return nil, gerror.New(consts.ErrCodeJdOrderNotFound)
|
|
}
|
|
|
|
jdOrderId := jdOrder.JdOrderId
|
|
wxPayUrl := jdOrder.WxPayUrl
|
|
|
|
// 检查支付链接是否有效
|
|
if jdOrder.WxPayExpireAt != nil && gtime.Now().After(jdOrder.WxPayExpireAt) {
|
|
// 支付链接已过期,尝试刷新
|
|
newWxPayUrl, refreshErr := s.refreshPaymentUrl(ctx, jdOrder.JdOrderId, jdOrder.PayId, jdOrder.CookieId, orderId)
|
|
if refreshErr != nil {
|
|
// 刷新失败,标记旧订单为失效
|
|
_ = s.UpdateJdOrderStatus(ctx, jdOrder.JdOrderId, consts.JdOrderStatusExpired, "刷新支付链接失败")
|
|
_ = s.RecordJdOrderHistory(ctx, jdOrder.JdOrderId, consts.JdOrderChangeTypeInvalid, orderId, jdOrder.WxPayUrl)
|
|
_ = s.RecordCookieHistory(ctx, jdOrder.CookieId, consts.CookieChangeTypeRefreshFail, 0, 0, orderId, 0)
|
|
|
|
// 解绑旧京东订单
|
|
_ = s.updateJdOrderCurrentOrderId(ctx, jdOrder.JdOrderId, "")
|
|
|
|
// 创建新的京东订单(带重试机制)
|
|
retryRes, createErr := s.createNewJdOrderWithRetry(ctx, &model.CreateNewJdOrderWithRetryReq{
|
|
OrderId: orderId,
|
|
Amount: gconv.Float64(order.Amount),
|
|
Category: consts.RedeemOrderCardCategory(order.Category),
|
|
})
|
|
if createErr != nil {
|
|
return nil, gerror.Wrap(createErr, "刷新失败且创建新订单失败")
|
|
}
|
|
|
|
// 更新订单关联的京东订单ID
|
|
_ = s.updateOrderJdOrderId(ctx, orderId, retryRes.JdOrderId, retryRes.WxPayUrl)
|
|
|
|
// 更新京东订单的当前关联订单ID
|
|
_ = s.updateJdOrderCurrentOrderId(ctx, retryRes.JdOrderId, orderId)
|
|
|
|
// 记录Cookie使用历史
|
|
_ = s.RecordCookieHistory(ctx, retryRes.CookieId, consts.CookieChangeTypeUse, 0, 0, orderId, 0)
|
|
|
|
// 记录订单重新绑定历史
|
|
_ = s.RecordOrderHistory(ctx, orderId, consts.OrderChangeTypeRebind, retryRes.JdOrderId)
|
|
|
|
// 返回新的支付信息
|
|
jdOrderId = retryRes.JdOrderId
|
|
wxPayUrl = retryRes.WxPayUrl
|
|
} else {
|
|
// 刷新成功,更新支付链接
|
|
wxPayUrl = newWxPayUrl
|
|
_ = s.updateJdOrderPaymentUrl(ctx, jdOrderId, wxPayUrl)
|
|
}
|
|
}
|
|
|
|
// 更新订单最后请求时间
|
|
_ = s.updateOrderLastRequest(ctx, orderId)
|
|
|
|
return &model.PaymentResult{
|
|
WxPayUrl: wxPayUrl,
|
|
JdOrderId: jdOrderId,
|
|
OrderId: order.OrderId,
|
|
}, nil
|
|
}
|
|
|
|
// GetOrder 获取单个订单
|
|
func (s *sJdCookie) GetOrder(ctx context.Context, orderId string) (order *v1.OrderInfo, err error) {
|
|
if orderId == "" {
|
|
return nil, gerror.New("订单号不能为空")
|
|
}
|
|
|
|
orderEntity, err := s.getOrderByOrderId(ctx, orderId)
|
|
if err != nil {
|
|
return nil, gerror.Wrap(err, "查询订单失败")
|
|
}
|
|
if orderEntity == nil {
|
|
return nil, gerror.New(consts.ErrCodeOrderNotFound)
|
|
}
|
|
|
|
order = &v1.OrderInfo{
|
|
OrderId: orderEntity.OrderId,
|
|
Amount: gconv.Float64(orderEntity.Amount),
|
|
Category: orderEntity.Category,
|
|
JdOrderId: orderEntity.JdOrderId,
|
|
Status: consts.OrderStatus(orderEntity.Status),
|
|
WxPayUrl: orderEntity.WxPayUrl,
|
|
LastRequest: orderEntity.LastRequestAt.Format("2006-01-02 15:04:05"),
|
|
CreatedAt: orderEntity.CreatedAt.Format("2006-01-02 15:04:05"),
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
// GetOrderStatus 查询订单状态
|
|
func (s *sJdCookie) GetOrderStatus(ctx context.Context, orderId string) (order *v1.OrderInfo, err error) {
|
|
if orderId == "" {
|
|
return nil, gerror.New("订单号不能为空")
|
|
}
|
|
|
|
orderEntity, err := s.getOrderByOrderId(ctx, orderId)
|
|
if err != nil {
|
|
return nil, gerror.Wrap(err, "查询订单失败")
|
|
}
|
|
if orderEntity == nil {
|
|
return nil, gerror.New(consts.ErrCodeOrderNotFound)
|
|
}
|
|
|
|
order = &v1.OrderInfo{
|
|
OrderId: orderEntity.OrderId,
|
|
Amount: gconv.Float64(orderEntity.Amount),
|
|
Category: orderEntity.Category,
|
|
JdOrderId: orderEntity.JdOrderId,
|
|
Status: consts.OrderStatus(orderEntity.Status),
|
|
WxPayUrl: orderEntity.WxPayUrl,
|
|
LastRequest: orderEntity.LastRequestAt.Format("2006-01-02 15:04:05"),
|
|
CreatedAt: orderEntity.CreatedAt.Format("2006-01-02 15:04:05"),
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
// ListOrder 订单列表查询
|
|
func (s *sJdCookie) ListOrder(ctx context.Context, page, size int, status consts.OrderStatus, startTime, endTime string) (list []*v1.OrderInfo, total int, err error) {
|
|
if page <= 0 {
|
|
page = 1
|
|
}
|
|
if size <= 0 {
|
|
size = 20
|
|
}
|
|
|
|
m := dao.V1JdCookieOrder.Ctx(ctx).DB(config.GetDatabaseV1())
|
|
|
|
// 构建查询条件
|
|
if status > 0 {
|
|
m = m.Where(dao.V1JdCookieOrder.Columns().Status, int(status))
|
|
}
|
|
if startTime != "" {
|
|
m = m.WhereGTE(dao.V1JdCookieOrder.Columns().CreatedAt, startTime)
|
|
}
|
|
if endTime != "" {
|
|
m = m.WhereLTE(dao.V1JdCookieOrder.Columns().CreatedAt, endTime)
|
|
}
|
|
|
|
// 查询总数
|
|
total, err = m.Count()
|
|
if err != nil {
|
|
return nil, 0, gerror.Wrap(err, "查询总数失败")
|
|
}
|
|
|
|
// 查询列表数据
|
|
var orders []*entity.V1JdCookieOrder
|
|
err = m.Page(page, size).OrderDesc(dao.V1JdCookieOrder.Columns().CreatedAt).Scan(&orders)
|
|
if err != nil {
|
|
return nil, 0, gerror.Wrap(err, "查询订单列表失败")
|
|
}
|
|
|
|
// 转换为响应格式
|
|
list = make([]*v1.OrderInfo, 0, len(orders))
|
|
for _, orderEntity := range orders {
|
|
info := &v1.OrderInfo{
|
|
OrderId: orderEntity.OrderId,
|
|
Amount: gconv.Float64(orderEntity.Amount),
|
|
Category: orderEntity.Category,
|
|
JdOrderId: orderEntity.JdOrderId,
|
|
Status: consts.OrderStatus(orderEntity.Status),
|
|
WxPayUrl: orderEntity.WxPayUrl,
|
|
LastRequest: orderEntity.LastRequestAt.Format("2006-01-02 15:04:05"),
|
|
CreatedAt: orderEntity.CreatedAt.Format("2006-01-02 15:04:05"),
|
|
}
|
|
list = append(list, info)
|
|
}
|
|
|
|
return
|
|
}
|