统一发货口径并保护结果数据
This commit is contained in:
@@ -366,11 +366,12 @@ func (s *FulfillmentService) UpdateFulfillment(in FulfillmentUpdateInput) (*mode
|
||||
return nil, errors.New("无效的订单状态")
|
||||
}
|
||||
nextOrderStatus := in.Status
|
||||
resultData := ""
|
||||
var resultData string
|
||||
hasResultData := in.ResultData != nil
|
||||
if in.ResultData != nil {
|
||||
raw, err := json.Marshal(in.ResultData)
|
||||
if err != nil {
|
||||
return nil, errors.New("履约结果无法序列化")
|
||||
return nil, errors.New("发货结果无法序列化")
|
||||
}
|
||||
resultData = string(raw)
|
||||
}
|
||||
@@ -394,7 +395,9 @@ func (s *FulfillmentService) UpdateFulfillment(in FulfillmentUpdateInput) (*mode
|
||||
}
|
||||
now := time.Now()
|
||||
updates := map[string]interface{}{"order_status": nextOrderStatus}
|
||||
updates["result_data"] = resultData
|
||||
if hasResultData {
|
||||
updates["result_data"] = resultData
|
||||
}
|
||||
if in.ProviderOrderNo != "" {
|
||||
updates["provider_order_no"] = in.ProviderOrderNo
|
||||
}
|
||||
@@ -473,7 +476,7 @@ func (s *FulfillmentService) markProcessingTimeout(id uint, timeout time.Duratio
|
||||
if err := validateOrderStatusTransition(&order, model.OrderStatusShipFailed, fulfillmentTransitionTimeout); err != nil {
|
||||
return nil
|
||||
}
|
||||
reason := fmt.Sprintf("履约超时:订单已处于 delivering 超过 %d 分钟", int(timeout.Minutes()))
|
||||
reason := fmt.Sprintf("发货超时:订单已处于 delivering 超过 %d 分钟", int(timeout.Minutes()))
|
||||
updates := map[string]interface{}{"order_status": model.OrderStatusShipFailed}
|
||||
updates["failure_reason"] = reason
|
||||
updates["result_data"] = buildProcessingTimeoutResultData(order.ResultData, timeout, now)
|
||||
@@ -747,22 +750,22 @@ func CanFulfill(order *model.FulfillmentOrder) (bool, string) {
|
||||
}
|
||||
|
||||
func orderCallbackData(order *model.FulfillmentOrder) map[string]interface{} {
|
||||
canFulfill, cannotFulfillReason := CanFulfill(order)
|
||||
canShip, cannotShipReason := CanFulfill(order)
|
||||
return map[string]interface{}{
|
||||
"order_no": order.OrderNo,
|
||||
"client_order_no": order.ClientOrderNo,
|
||||
"product_sku": order.ProductSKU,
|
||||
"quantity": order.Quantity,
|
||||
"base_amount": order.BaseAmount,
|
||||
"fee_type": order.FeeType,
|
||||
"service_fee_amount": order.ServiceFeeAmount,
|
||||
"amount": order.Amount,
|
||||
"currency": order.Currency,
|
||||
"order_status": normalizeOrderStatus(order),
|
||||
"can_fulfill": canFulfill,
|
||||
"cannot_fulfill_reason": cannotFulfillReason,
|
||||
"provider_order_no": order.ProviderOrderNo,
|
||||
"failure_reason": order.FailureReason,
|
||||
"order_no": order.OrderNo,
|
||||
"client_order_no": order.ClientOrderNo,
|
||||
"product_sku": order.ProductSKU,
|
||||
"quantity": order.Quantity,
|
||||
"base_amount": order.BaseAmount,
|
||||
"fee_type": order.FeeType,
|
||||
"service_fee_amount": order.ServiceFeeAmount,
|
||||
"amount": order.Amount,
|
||||
"currency": order.Currency,
|
||||
"order_status": normalizeOrderStatus(order),
|
||||
"can_ship": canShip,
|
||||
"cannot_ship_reason": cannotShipReason,
|
||||
"provider_order_no": order.ProviderOrderNo,
|
||||
"failure_reason": order.FailureReason,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1097,7 +1100,7 @@ func (s *FulfillmentService) HandleShipNotify(in ShipNotifyInput) (*ShipNotifyRe
|
||||
return err
|
||||
}
|
||||
|
||||
// 已履约成功:success 推送幂等成功。状态读取和后续更新必须在同一把行锁内完成。
|
||||
// 已交付:success 推送幂等成功。状态读取和后续更新必须在同一把行锁内完成。
|
||||
if normalizeOrderStatus(&order) == model.OrderStatusDelivered && in.ShipStatus == "success" {
|
||||
result = ShipNotifyResult{
|
||||
OrderNo: order.OrderNo,
|
||||
@@ -1105,7 +1108,7 @@ func (s *FulfillmentService) HandleShipNotify(in ShipNotifyInput) (*ShipNotifyRe
|
||||
Message: "订单已交付,幂等成功",
|
||||
}
|
||||
return writeAudit(tx, &order.MerchantID, nil, nil, "ship.notify", "fulfillment_order", order.OrderNo,
|
||||
shipNotifyAuditMetadata(in, normalizeOrderStatus(&order), "订单已履约成功,幂等忽略"))
|
||||
shipNotifyAuditMetadata(in, normalizeOrderStatus(&order), "订单已交付,幂等忽略"))
|
||||
}
|
||||
|
||||
if normalizeOrderStatus(&order) == model.OrderStatusCancelled {
|
||||
@@ -1265,7 +1268,7 @@ func buildProcessingTimeoutResultData(existing string, timeout time.Duration, no
|
||||
m["timeout_minutes"] = int(timeout.Minutes())
|
||||
m["timeout_at"] = timeutil.FormatAPITime(now)
|
||||
m["ship_status"] = "failed"
|
||||
m["fail_reason"] = fmt.Sprintf("履约超时:订单已处于 delivering 超过 %d 分钟", int(timeout.Minutes()))
|
||||
m["fail_reason"] = fmt.Sprintf("发货超时:订单已处于 delivering 超过 %d 分钟", int(timeout.Minutes()))
|
||||
raw, err := json.Marshal(m)
|
||||
if err != nil {
|
||||
return existing
|
||||
|
||||
Reference in New Issue
Block a user