fix: 修复订单状态机转换校验与结算确认目标状态

This commit is contained in:
zetaloop
2026-04-24 09:54:34 +08:00
parent e4fcc4e74e
commit 3b56910100
3 changed files with 32 additions and 4 deletions
@@ -28,7 +28,7 @@ func NewConfirmCloseOrderLogic(ctx context.Context, svcCtx *svc.ServiceContext)
} }
func (l *ConfirmCloseOrderLogic) ConfirmCloseOrder(req *types.PathId) (resp *types.EmptyResp, err error) { func (l *ConfirmCloseOrderLogic) ConfirmCloseOrder(req *types.PathId) (resp *types.EmptyResp, err error) {
if err = transitionOrderStatus(l.ctx, l.svcCtx, req.Id, "completed", false, true, true, false); err != nil { if err = transitionOrderStatus(l.ctx, l.svcCtx, req.Id, "pending_review", false, true, false, false); err != nil {
return nil, err return nil, err
} }
+29 -2
View File
@@ -3,6 +3,8 @@ package order
import ( import (
"context" "context"
"encoding/json" "encoding/json"
"errors"
"fmt"
"strconv" "strconv"
"time" "time"
@@ -55,13 +57,39 @@ func toAPIOrder(in *orderservice.Orders) types.Order {
return order return order
} }
// validTransitions defines the allowed order state transitions per the API spec.
var validTransitions = map[string][]string{
"pending_payment": {"pending_accept"},
"pending_accept": {"in_progress", "cancelled"},
"in_progress": {"pending_close", "disputed"},
"pending_close": {"pending_review", "disputed"},
"pending_review": {"completed"},
"disputed": {"pending_review"},
}
func transitionOrderStatus(ctx context.Context, svcCtx *svc.ServiceContext, orderID int64, toStatus string, setAcceptedAt bool, setClosedAt bool, setCompletedAt bool, setCancelledAt bool) error { func transitionOrderStatus(ctx context.Context, svcCtx *svc.ServiceContext, orderID int64, toStatus string, setAcceptedAt bool, setClosedAt bool, setCompletedAt bool, setCancelledAt bool) error {
current, err := svcCtx.OrderRpc.GetOrdersById(ctx, &orderservice.GetOrdersByIdReq{Id: orderID}) current, err := svcCtx.OrderRpc.GetOrdersById(ctx, &orderservice.GetOrdersByIdReq{Id: orderID})
if err != nil { if err != nil {
return err return err
} }
if current.GetOrders() == nil { if current.GetOrders() == nil {
return nil return errors.New("order not found")
}
fromStatus := current.Orders.GetStatus()
allowed, ok := validTransitions[fromStatus]
if !ok {
return fmt.Errorf("order status %q does not allow any transition", fromStatus)
}
found := false
for _, s := range allowed {
if s == toStatus {
found = true
break
}
}
if !found {
return fmt.Errorf("transition from %q to %q is not allowed", fromStatus, toStatus)
} }
now := time.Now().Unix() now := time.Now().Unix()
@@ -87,7 +115,6 @@ func transitionOrderStatus(ctx context.Context, svcCtx *svc.ServiceContext, orde
return err return err
} }
fromStatus := current.Orders.GetStatus()
actorID, _ := contextj.UserIDFrom(ctx) actorID, _ := contextj.UserIDFrom(ctx)
actorRole := "system" actorRole := "system"
if actorID > 0 { if actorID > 0 {
@@ -5,6 +5,7 @@ package order
import ( import (
"context" "context"
"errors"
"time" "time"
"juwan-backend/app/order/rpc/orderservice" "juwan-backend/app/order/rpc/orderservice"
@@ -37,7 +38,7 @@ func (l *ReorderLogic) Reorder(req *types.PathId) (resp *types.CreateOrderResp,
} }
oldOrder := oldOrderResp.GetOrders() oldOrder := oldOrderResp.GetOrders()
if oldOrder == nil { if oldOrder == nil {
return nil, nil return nil, errors.New("order not found")
} }
now := time.Now().Unix() now := time.Now().Unix()