fix: 第1批 M8、M13、M14 审批提交失败终态与退款关闭类终态定位

- 审批核心在实例进入提交失败的同一事务内写 submission_failed 决策投递与终态事件,消费入口单独放行
- 核销(4)、退款(7)、扫码注册(3)、资料资格(4)、线下充值(7)、佣金提现(5)新增“审批提交失败”终态并释放预占/冻结
- 三处恢复入口不再重放提交失败;退款关闭类终态按审批尝试定位退款单(M13)
- 核销列表与详情批量返回审批提交状态;筛选、字段说明、状态名称、退款导出加入新状态
- 迁移 000239 放宽五张业务表状态约束与决策投递取值约束(测试库已执行 up/down/up)
- OpenSpec Change fix-approval-submission-failure-terminal:18/26,测试部署后端到端验证与存量补数据待完成
This commit is contained in:
邹进
2026-10-12 00:03:46 +09:00
parent 25c081fe60
commit 6298a9061f
42 changed files with 810 additions and 87 deletions

View File

@@ -28,7 +28,8 @@ func (s *Service) Handle(ctx context.Context, event approvalapp.TerminalDecision
switch event.Decision {
case constants.ApprovalDecisionApproved:
return s.applyApprovedDecision(ctx, event)
case constants.ApprovalDecisionRejected, constants.ApprovalDecisionCancelled, constants.ApprovalDecisionDeleted:
case constants.ApprovalDecisionRejected, constants.ApprovalDecisionCancelled, constants.ApprovalDecisionDeleted,
constants.ApprovalDecisionSubmissionFailed:
return s.applyClosedDecision(ctx, event)
case constants.ApprovalDecisionRevokedAfterApproved:
return s.applyRevokedAfterApproved(ctx, event)
@@ -214,6 +215,8 @@ func (s *Service) prepareChannelRefundInTx(ctx context.Context, tx *gorm.DB, ref
return s.channelRefund.PrepareInTx(ctx, tx, refund, attempt)
}
// applyClosedDecision 处理企微拒绝、撤销、删除(退款单→已拒绝)与审批提交失败(退款单→审批提交失败),均为钱未动的终态。
// 业务标识按审批尝试优先解析(BusinessID 在尝试模式下是尝试 ID),再按退款单 ID 加行锁。
func (s *Service) applyClosedDecision(ctx context.Context, event approvalapp.TerminalDecisionEvent) error {
ctx = auditcontext.With(ctx, auditcontext.Context{CorrelationID: event.CorrelationID, ParentEventID: event.EventID})
reason := map[string]string{
@@ -221,18 +224,35 @@ func (s *Service) applyClosedDecision(ctx context.Context, event approvalapp.Ter
constants.ApprovalDecisionCancelled: "企业微信审批已撤销",
constants.ApprovalDecisionDeleted: "企业微信审批已删除",
}[event.Decision]
submissionFailed := event.Decision == constants.ApprovalDecisionSubmissionFailed
targetStatus, actionSummary, auditSuffix, auditResult := model.RefundStatusRejected, "拒绝退款审批", ":rejected", "退款已拒绝"
if submissionFailed {
targetStatus, actionSummary, auditSuffix, auditResult = model.RefundStatusSubmissionFailed, "退款审批提交失败", ":submission_failed", "退款审批提交失败"
}
var refund model.RefundRequest
var order model.Order
err := s.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
if err := tx.WithContext(ctx).Clauses(clause.Locking{Strength: "UPDATE"}).Where("id = ?", event.BusinessID).First(&refund).Error; err != nil {
target, _, err := refundapproval.ResolveRefundInTx(ctx, tx, event.BusinessID, event.InstanceID)
if err != nil {
return err
}
if err := tx.WithContext(ctx).Clauses(clause.Locking{Strength: "UPDATE"}).First(&refund, target.ID).Error; err != nil {
return errors.Wrap(errors.CodeDatabaseError, err, "锁定退款申请失败")
}
if refund.ApprovalInstanceID == nil || *refund.ApprovalInstanceID != event.InstanceID {
return errors.New(errors.CodeConflict, "退款申请关联的审批实例不一致")
}
if refund.Status == model.RefundStatusRejected {
// 已被重新发起取代的历史审批不再改变退款单事实。
if refund.LatestApprovalInstanceID != 0 && refund.LatestApprovalInstanceID != event.InstanceID {
return nil
}
if refund.Status == targetStatus {
return nil
}
if submissionFailed {
failureReason, failed, err := approvalapp.ConfirmSubmissionFailed(ctx, tx, event.InstanceID)
if err != nil || !failed {
return err
}
reason = failureReason
}
if refund.Status != model.RefundStatusPending {
return errors.New(errors.CodeInvalidStatus, "退款申请状态不允许结束审批")
}
@@ -243,7 +263,7 @@ func (s *Service) applyClosedDecision(ctx context.Context, event approvalapp.Ter
result := tx.WithContext(ctx).Model(&model.RefundRequest{}).
Where("id = ? AND status = ?", refund.ID, model.RefundStatusPending).
Updates(map[string]any{
"status": model.RefundStatusRejected, "processed_at": event.OccurredAt,
"status": targetStatus, "processed_at": event.OccurredAt,
"reject_reason": strings.TrimSpace(reason), "updated_at": event.OccurredAt,
})
if result.Error != nil {
@@ -252,8 +272,9 @@ func (s *Service) applyClosedDecision(ctx context.Context, event approvalapp.Ter
if result.RowsAffected != 1 {
return errors.New(errors.CodeConflict, "退款申请状态已变化")
}
return s.appendRefundAudit(ctx, tx, refund.ID, constants.AuditActionRefundRejected, "拒绝退款审批",
"refund:"+strconv.FormatUint(uint64(refund.ID), 10)+":rejected", beforeRefund, nil, "退款已拒绝")
return s.appendRefundAudit(ctx, tx, refund.ID, constants.AuditActionRefundRejected, actionSummary,
"refund:"+strconv.FormatUint(uint64(refund.ID), 10)+auditSuffix+":"+strconv.FormatUint(uint64(event.InstanceID), 10),
beforeRefund, nil, auditResult)
})
if err != nil {
s.recordRefundFailure(ctx, constants.AuditActionRefundRejected, "拒绝退款审批失败", &refund, &order, err)