feat: 退款资产后处理逐次尝试记录与重试上限,停复机指令调用前结束写跳过记录

- 新增 tb_refund_post_process_attempt,每次尝试一条(步骤、触发方式、结果、错误码与信息、耗时)
- 退款单增加 asset_post_status/attempts/last_error/next_at;失败按 2^(n-1) 分钟退避(最长 1 小时),10 次后转人工
- 中间失败只写应用日志,成功或转人工写一条结论审计(带总尝试次数、首次失败时间、最后错误码)
- 补偿扫描跳过需人工处理与未到重试时间的退款单,Outbox 不再无限重投
- 新增 POST /refunds/:id/asset-post-processing/retry 人工重试
- 停复机指令执行前结束(已是目标状态、条件不再满足、需相反动作)写 skipped 停复机记录
- 迁移 000271 需维护者在测试库执行后再部署

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
邹进
2026-11-05 18:33:32 +09:00
parent b7a7430cc3
commit 8eb7d403c4
15 changed files with 488 additions and 77 deletions

View File

@@ -77,6 +77,12 @@ type RefundRequest struct {
// 后处理标记
CommissionDeducted bool `gorm:"column:commission_deducted;not null;default:false;comment:佣金是否已回扣" json:"commission_deducted"`
AssetReset bool `gorm:"column:asset_reset;not null;default:false;comment:退款后资产处理是否完成" json:"asset_reset"`
// 资产后处理重试状态(refactor-audit-log-persistence)
AssetPostStatus int `gorm:"column:asset_post_status;type:smallint;not null;default:0;comment:资产后处理状态 0-未开始 1-待重试 2-已完成 3-需人工处理" json:"asset_post_status"`
AssetPostAttempts int `gorm:"column:asset_post_attempts;type:int;not null;default:0;comment:资产后处理已尝试次数" json:"asset_post_attempts"`
AssetPostLastError string `gorm:"column:asset_post_last_error;type:varchar(500);not null;default:'';comment:资产后处理最后一次错误摘要" json:"asset_post_last_error"`
AssetPostNextAt *time.Time `gorm:"column:asset_post_next_at;comment:资产后处理下次自动重试时间" json:"asset_post_next_at,omitempty"`
}
// TableName 指定表名
@@ -136,3 +142,34 @@ func RefundActiveStatuses() []int {
func RefundResubmittableStatuses() []int {
return []int{RefundStatusRejected, RefundStatusReturned, RefundStatusChannelFailed, RefundStatusSubmissionFailed}
}
// 退款资产后处理状态。
const (
RefundAssetPostNotStarted = 0 // 未开始
RefundAssetPostRetryPending = 1 // 待重试
RefundAssetPostCompleted = 2 // 已完成
RefundAssetPostManualRequired = 3 // 需人工处理
)
// RefundPostProcessAttempt 退款资产后处理逐次尝试记录,只新增不修改。
type RefundPostProcessAttempt struct {
ID uint `gorm:"column:id;primaryKey;autoIncrement" json:"id"`
RefundID uint `gorm:"column:refund_id;not null" json:"refund_id"`
RefundNo string `gorm:"column:refund_no;type:varchar(64);not null;default:''" json:"refund_no"`
Step string `gorm:"column:step;type:varchar(32);not null" json:"step"`
AssetType string `gorm:"column:asset_type;type:varchar(16);not null;default:''" json:"asset_type"`
AssetID uint `gorm:"column:asset_id;not null;default:0" json:"asset_id"`
ICCID string `gorm:"column:iccid;type:varchar(30);not null;default:''" json:"iccid"`
Attempt int `gorm:"column:attempt;not null" json:"attempt"`
Trigger string `gorm:"column:trigger;type:varchar(16);not null" json:"trigger"`
Result string `gorm:"column:result;type:varchar(16);not null" json:"result"`
ErrorCode string `gorm:"column:error_code;type:varchar(32);not null;default:''" json:"error_code"`
ErrorMessage string `gorm:"column:error_message;type:varchar(500);not null;default:''" json:"error_message"`
DurationMS int `gorm:"column:duration_ms;not null;default:0" json:"duration_ms"`
CreatedAt time.Time `gorm:"column:created_at;type:timestamptz;not null;autoCreateTime" json:"created_at"`
}
// TableName 指定退款资产后处理尝试记录表名。
func (RefundPostProcessAttempt) TableName() string {
return "tb_refund_post_process_attempt"
}