feat: 完善 Rock 发货异常闭环与绑定判定
This commit is contained in:
@@ -7,6 +7,7 @@ import (
|
||||
"time"
|
||||
|
||||
"affiliate_dash/internal/model"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type CheckBindingResult struct {
|
||||
@@ -46,16 +47,27 @@ func (s *RockService) CheckBindingAndVerifyUID(ctx context.Context, tokenID uint
|
||||
token.MismatchReason = ""
|
||||
token.MismatchAt = nil
|
||||
token.BoundAt = &now
|
||||
_ = s.db.WithContext(ctx).Unscoped().Model(&token).Updates(map[string]interface{}{
|
||||
"expected_uid": trimmedExpected,
|
||||
"status": model.RockStatusBound,
|
||||
"mismatch_reason": "",
|
||||
"mismatch_at": nil,
|
||||
"bound_at": now,
|
||||
"updated_at": now,
|
||||
}).Error
|
||||
writeRockTokenStatusChange(s.db.WithContext(ctx), &token, model.RockStatusMismatch, model.RockStatusBound,
|
||||
rockTokenSourceBindCheck, "买家确认正确 UID 后解除绑定错误", false, nil)
|
||||
if err := s.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
|
||||
if err := tx.Unscoped().Model(&token).Updates(map[string]interface{}{
|
||||
"expected_uid": trimmedExpected,
|
||||
"status": model.RockStatusBound,
|
||||
"mismatch_reason": "",
|
||||
"mismatch_at": nil,
|
||||
"bound_at": now,
|
||||
"updated_at": now,
|
||||
}).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
writeRockTokenStatusChange(tx, &token, model.RockStatusMismatch, model.RockStatusBound,
|
||||
rockTokenSourceBindCheck, "买家确认正确 UID 后解除绑定错误", false, nil)
|
||||
return writeAudit(tx, nil, nil, nil, "rock_token.bind_confirm", "rock_token",
|
||||
fmt.Sprintf("%d", token.ID), map[string]interface{}{
|
||||
"expected_uid": trimmedExpected,
|
||||
"actual_uid": actualUID,
|
||||
})
|
||||
}); err != nil {
|
||||
return nil, fmt.Errorf("更新扫码绑定状态失败: %w", err)
|
||||
}
|
||||
return &CheckBindingResult{
|
||||
Bound: true,
|
||||
RoleName: token.ActualRoleName,
|
||||
@@ -155,7 +167,7 @@ func (s *RockService) CheckBindingAndVerifyUID(ctx context.Context, tokenID uint
|
||||
}
|
||||
|
||||
// 1. 检查上游是否已绑定 (上游状态 bound 或 bound_account_id 有值)
|
||||
if !ref.IsBound() || strings.TrimSpace(ref.GetRoleID()) == "" {
|
||||
if !ref.HasBindingSignal() || strings.TrimSpace(ref.GetRoleID()) == "" {
|
||||
return &CheckBindingResult{
|
||||
Bound: false,
|
||||
Message: "等待买家微信/QQ扫码授权",
|
||||
@@ -165,9 +177,10 @@ func (s *RockService) CheckBindingAndVerifyUID(ctx context.Context, tokenID uint
|
||||
actualRoleName := ref.GetRoleName()
|
||||
actualRoleID := ref.GetRoleID()
|
||||
actualChannel := ref.GetChannel()
|
||||
currentBound, bindChangedAt := rockCurrentCycleBinding(&token, ref)
|
||||
|
||||
// 2. 历史残留不算本次扫码:角色和绑定时间与分配快照一致,或发货记录都早于本次领取。
|
||||
if !rockBuyerNewlyBound(&token, ref) {
|
||||
// 2. 历史残留不算本次扫码:只认晚于本轮 using_at 的 bind_changed_at。
|
||||
if !currentBound {
|
||||
return &CheckBindingResult{
|
||||
Bound: false,
|
||||
Message: "等待买家扫码(检测到历史绑定残留,已自动忽略)",
|
||||
@@ -185,16 +198,30 @@ func (s *RockService) CheckBindingAndVerifyUID(ctx context.Context, tokenID uint
|
||||
// 严禁发货!跃迁为 mismatch
|
||||
fromStatus := token.Status
|
||||
mismatchReason := fmt.Sprintf("预期UID(%s) 与 扫码UID(%s/%s) 不一致", trimmedExpected, actualRoleName, actualRoleID)
|
||||
_ = s.db.WithContext(ctx).Unscoped().Model(&token).Updates(map[string]interface{}{
|
||||
"status": model.RockStatusMismatch,
|
||||
"actual_role_name": actualRoleName,
|
||||
"actual_role_id": actualRoleID,
|
||||
"actual_channel": actualChannel,
|
||||
"mismatch_at": now,
|
||||
"mismatch_reason": mismatchReason,
|
||||
}).Error
|
||||
writeRockTokenStatusChange(s.db.WithContext(ctx), &token, fromStatus, model.RockStatusMismatch,
|
||||
rockTokenSourceBindCheck, mismatchReason, false, nil)
|
||||
if err := s.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
|
||||
if err := tx.Unscoped().Model(&token).Updates(map[string]interface{}{
|
||||
"status": model.RockStatusMismatch,
|
||||
"actual_role_name": actualRoleName,
|
||||
"actual_role_id": actualRoleID,
|
||||
"actual_channel": actualChannel,
|
||||
"mismatch_at": now,
|
||||
"mismatch_reason": mismatchReason,
|
||||
"bound_at": bindChangedAt,
|
||||
"updated_at": now,
|
||||
}).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
writeRockTokenStatusChange(tx, &token, fromStatus, model.RockStatusMismatch,
|
||||
rockTokenSourceBindCheck, mismatchReason, false, nil)
|
||||
return writeAudit(tx, nil, nil, nil, "rock_token.bind_mismatch", "rock_token",
|
||||
fmt.Sprintf("%d", token.ID), map[string]interface{}{
|
||||
"expected_uid": trimmedExpected,
|
||||
"actual_uid": actualRoleID,
|
||||
"reason": mismatchReason,
|
||||
})
|
||||
}); err != nil {
|
||||
return nil, fmt.Errorf("记录绑定 UID 不一致失败: %w", err)
|
||||
}
|
||||
|
||||
roleDesc := actualRoleName
|
||||
if roleDesc == "" {
|
||||
@@ -213,15 +240,32 @@ func (s *RockService) CheckBindingAndVerifyUID(ctx context.Context, tokenID uint
|
||||
|
||||
// 4. UID 一致,更新为 bound 成功状态并保存玩家角色名称
|
||||
fromStatus := token.Status
|
||||
_ = s.db.WithContext(ctx).Unscoped().Model(&token).Updates(map[string]interface{}{
|
||||
"status": model.RockStatusBound,
|
||||
"actual_role_name": actualRoleName,
|
||||
"actual_role_id": actualRoleID,
|
||||
"actual_channel": actualChannel,
|
||||
"bound_at": now,
|
||||
}).Error
|
||||
writeRockTokenStatusChange(s.db.WithContext(ctx), &token, fromStatus, model.RockStatusBound,
|
||||
rockTokenSourceBindCheck, fmt.Sprintf("扫码绑定成功 UID %s", actualRoleID), false, nil)
|
||||
boundAt := bindChangedAt
|
||||
if boundAt.IsZero() {
|
||||
boundAt = now
|
||||
}
|
||||
if err := s.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
|
||||
if err := tx.Unscoped().Model(&token).Updates(map[string]interface{}{
|
||||
"status": model.RockStatusBound,
|
||||
"actual_role_name": actualRoleName,
|
||||
"actual_role_id": actualRoleID,
|
||||
"actual_channel": actualChannel,
|
||||
"bound_at": boundAt,
|
||||
"updated_at": now,
|
||||
}).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
writeRockTokenStatusChange(tx, &token, fromStatus, model.RockStatusBound,
|
||||
rockTokenSourceBindCheck, fmt.Sprintf("扫码绑定成功 UID %s", actualRoleID), false, nil)
|
||||
return writeAudit(tx, nil, nil, nil, "rock_token.bind_confirm", "rock_token",
|
||||
fmt.Sprintf("%d", token.ID), map[string]interface{}{
|
||||
"expected_uid": trimmedExpected,
|
||||
"actual_uid": actualRoleID,
|
||||
"bound_at": boundAt.Format(time.RFC3339),
|
||||
})
|
||||
}); err != nil {
|
||||
return nil, fmt.Errorf("保存扫码绑定状态失败: %w", err)
|
||||
}
|
||||
|
||||
return &CheckBindingResult{
|
||||
Bound: true,
|
||||
|
||||
Reference in New Issue
Block a user