feat: 完善 Rock 发货异常闭环与绑定判定
This commit is contained in:
@@ -36,12 +36,12 @@ func canRefreshRockBinding(status string) bool {
|
||||
|
||||
// RefreshTokenBinding 通过兑换码调用上游 refresh,人工同步一次绑定状态。
|
||||
//
|
||||
// 规则(只同步上游实时状态,不再用本地预期 UID 拦截):
|
||||
// 规则(只同步本轮真实事件,并与本地预期 UID 交叉校验):
|
||||
// - 上游状态异常(abnormal)→ 本地同步 abnormal(已上报失效);
|
||||
// - 上游明确返回「Token 不存在」(404) → 本地同步 abnormal(已上报失效);
|
||||
// - 上游已发货 → 本地同步 redeemed;
|
||||
// - 上游已绑定 → 本地直接同步 bound,并清空绑定错误标记;
|
||||
// - 上游未绑定 → 本地状态保持不变(不自动把 bound 退回 using)。
|
||||
// - 本轮 redeemed_at / deliveryList → 本地同步 redeemed;
|
||||
// - 本轮 bind_changed_at → 本地同步 bound/mismatch;
|
||||
// - 只有历史残留事件 → 不推进本轮状态,错误的 bound 修复回 using。
|
||||
func (s *RockService) RefreshTokenBinding(ctx context.Context, tokenID uint) (*TokenBindingRefreshResult, error) {
|
||||
return s.refreshTokenBinding(ctx, tokenID, nil)
|
||||
}
|
||||
@@ -119,13 +119,13 @@ func (s *RockService) refreshTokenBinding(ctx context.Context, tokenID uint, act
|
||||
actualRoleName := ref.GetRoleName()
|
||||
actualRoleID := ref.GetRoleID()
|
||||
actualChannel := ref.GetChannel()
|
||||
upstreamBound := rockUpstreamBound(ref)
|
||||
currentBound, bindChangedAt := rockCurrentCycleBinding(&token, ref)
|
||||
|
||||
updates := map[string]interface{}{
|
||||
"updated_at": now,
|
||||
}
|
||||
// 只有上游确实绑定时才同步角色字段;未绑定时 Region 可能只是领取区域,不能当成绑定渠道。
|
||||
if upstreamBound {
|
||||
if currentBound {
|
||||
if actualRoleName != "" {
|
||||
updates["actual_role_name"] = actualRoleName
|
||||
}
|
||||
@@ -139,37 +139,42 @@ func (s *RockService) refreshTokenBinding(ctx context.Context, tokenID uint, act
|
||||
|
||||
result := &TokenBindingRefreshResult{
|
||||
Status: token.Status,
|
||||
Bound: upstreamBound,
|
||||
Bound: currentBound,
|
||||
}
|
||||
if upstreamBound {
|
||||
if currentBound {
|
||||
result.RoleName = actualRoleName
|
||||
result.RoleID = actualRoleID
|
||||
result.Channel = actualChannel
|
||||
}
|
||||
|
||||
redeemedAtRaw := strings.TrimSpace(ref.Token.RedeemedAt)
|
||||
deliveryEvidence := rockCurrentCycleDelivery(&token, ref)
|
||||
baselineUnknown := rockClaimBaseline(&token).IsZero()
|
||||
matchesInitialSnapshot := rockBindingMatchesInitialSnapshot(&token, ref)
|
||||
|
||||
switch {
|
||||
case upstreamStatus == model.RockStatusRedeemed || redeemedAtRaw != "":
|
||||
redeemedAt, ok := parseRockTime(redeemedAtRaw)
|
||||
if !ok {
|
||||
case deliveryEvidence.Ambiguous:
|
||||
result.Warning = true
|
||||
result.Message = "上游显示已发货,但无法确认是否属于本轮,禁止释放,请人工核对"
|
||||
case deliveryEvidence.Current:
|
||||
redeemedAt := deliveryEvidence.At
|
||||
if redeemedAt.IsZero() {
|
||||
redeemedAt = now
|
||||
}
|
||||
updates["status"] = model.RockStatusRedeemed
|
||||
updates["redeemed_at"] = redeemedAt
|
||||
if len(ref.DeliveryList) > 0 {
|
||||
if fulfillID := ref.DeliveryList[len(ref.DeliveryList)-1].FulfillID; fulfillID > 0 {
|
||||
updates["upstream_fulfill_id"] = fulfillID
|
||||
}
|
||||
if deliveryEvidence.FulfillID > 0 {
|
||||
updates["upstream_fulfill_id"] = deliveryEvidence.FulfillID
|
||||
}
|
||||
result.Status = model.RockStatusRedeemed
|
||||
result.RedeemedAt = redeemedAt.Format("2006-01-02 15:04:05")
|
||||
result.Message = "上游已发货,本地状态已同步为已交付"
|
||||
|
||||
case upstreamBound:
|
||||
// 上游已绑定即同步为 bound:不以本地预期 UID 做拦截,
|
||||
// 否则历史 UID 不一致的 Token 永远无法把真实状态刷新回本地。
|
||||
// 发货确认中(有 redeem 标记未完成)时只刷新标记,状态保持 bound。
|
||||
case baselineUnknown && !matchesInitialSnapshot &&
|
||||
(ref.HasBindingSignal() || strings.TrimSpace(rockBindingChangedAt(ref)) != ""):
|
||||
result.Warning = true
|
||||
result.Message = "缺少本轮 using_at,无法判断该绑定是否属于本轮,请人工核对"
|
||||
|
||||
case currentBound:
|
||||
if rockTokenRedeemPending(&token) {
|
||||
updates["redeem_checked_at"] = now
|
||||
if msg := strings.TrimSpace(ref.Message); msg != "" {
|
||||
@@ -179,12 +184,21 @@ func (s *RockService) refreshTokenBinding(ctx context.Context, tokenID uint, act
|
||||
result.Message = "上游仍未确认发货,保持发货确认中"
|
||||
break
|
||||
}
|
||||
updates["bound_at"] = bindChangedAt
|
||||
expectedUID := strings.TrimSpace(token.ExpectedUID)
|
||||
if expectedUID != "" && actualRoleID != "" && expectedUID != actualRoleID {
|
||||
reason := fmt.Sprintf("预期UID(%s) 与 扫码UID(%s/%s) 不一致", expectedUID, actualRoleName, actualRoleID)
|
||||
updates["status"] = model.RockStatusMismatch
|
||||
updates["mismatch_at"] = now
|
||||
updates["mismatch_reason"] = reason
|
||||
result.Status = model.RockStatusMismatch
|
||||
result.Warning = true
|
||||
result.Message = reason
|
||||
break
|
||||
}
|
||||
updates["status"] = model.RockStatusBound
|
||||
updates["mismatch_at"] = nil
|
||||
updates["mismatch_reason"] = ""
|
||||
if token.BoundAt == nil {
|
||||
updates["bound_at"] = resolveRockBoundAt(ref, now)
|
||||
}
|
||||
result.Status = model.RockStatusBound
|
||||
if actualRoleID == "" {
|
||||
result.Warning = true
|
||||
@@ -201,18 +215,38 @@ func (s *RockService) refreshTokenBinding(ctx context.Context, tokenID uint, act
|
||||
updates["redeem_checked_at"] = now
|
||||
result.Status = model.RockStatusBound
|
||||
result.Message = "上游仍未确认发货,保持发货确认中"
|
||||
} else if token.Status == model.RockStatusBound {
|
||||
updates["status"] = model.RockStatusUsing
|
||||
updates["bound_at"] = nil
|
||||
updates["actual_role_name"] = ""
|
||||
updates["actual_role_id"] = ""
|
||||
updates["actual_channel"] = ""
|
||||
updates["mismatch_at"] = nil
|
||||
updates["mismatch_reason"] = ""
|
||||
result.Status = model.RockStatusUsing
|
||||
result.Warning = true
|
||||
result.Message = "未检测到本轮真实绑定事件,已修复为使用中"
|
||||
} else {
|
||||
result.Message = "上游仍未绑定,本地状态保持不变"
|
||||
}
|
||||
}
|
||||
|
||||
result.Changed = result.Status != originalStatus ||
|
||||
(upstreamBound && actualRoleName != "" && actualRoleName != originalRoleName) ||
|
||||
(upstreamBound && actualRoleID != "" && actualRoleID != originalRoleID) ||
|
||||
(upstreamBound && actualChannel != "" && actualChannel != originalChannel)
|
||||
(currentBound && actualRoleName != "" && actualRoleName != originalRoleName) ||
|
||||
(currentBound && actualRoleID != "" && actualRoleID != originalRoleID) ||
|
||||
(currentBound && actualChannel != "" && actualChannel != originalChannel)
|
||||
|
||||
if err := s.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
|
||||
if err := tx.Model(&token).Updates(updates).Error; err != nil {
|
||||
updateResult := tx.Model(&model.RockToken{}).
|
||||
Where("id = ? AND updated_at = ?", token.ID, token.UpdatedAt).
|
||||
Updates(updates)
|
||||
if updateResult.Error != nil {
|
||||
return updateResult.Error
|
||||
}
|
||||
if updateResult.RowsAffected == 0 {
|
||||
return errors.New("Token 状态已被并发更新,请刷新后重试")
|
||||
}
|
||||
if err := tx.First(&token, token.ID).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
writeRockTokenStatusChange(tx, &token, originalStatus, result.Status,
|
||||
@@ -308,12 +342,3 @@ func (s *RockService) markTokenAbnormalByRefresh(ctx context.Context, token *mod
|
||||
Message: message,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func resolveRockBoundAt(ref *RockRefreshResponse, fallback time.Time) time.Time {
|
||||
if ref != nil {
|
||||
if t, ok := parseRockTime(ref.Token.BindChangedAt); ok {
|
||||
return t
|
||||
}
|
||||
}
|
||||
return fallback
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user