fix: 修复订单状态机转换校验与结算确认目标状态
This commit is contained in:
@@ -3,6 +3,8 @@ package order
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
@@ -55,13 +57,39 @@ func toAPIOrder(in *orderservice.Orders) types.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 {
|
||||
current, err := svcCtx.OrderRpc.GetOrdersById(ctx, &orderservice.GetOrdersByIdReq{Id: orderID})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
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()
|
||||
@@ -87,7 +115,6 @@ func transitionOrderStatus(ctx context.Context, svcCtx *svc.ServiceContext, orde
|
||||
return err
|
||||
}
|
||||
|
||||
fromStatus := current.Orders.GetStatus()
|
||||
actorID, _ := contextj.UserIDFrom(ctx)
|
||||
actorRole := "system"
|
||||
if actorID > 0 {
|
||||
|
||||
Reference in New Issue
Block a user