All checks were successful
构建并部署到测试环境(无 SSH) / build-and-deploy (push) Successful in 9m20s
- 新增 6 张表与成对迁移 000212,扩展企业微信审批场景业务类型白名单 - 后台线下套餐订单与两条代理线下充值入账路径在来源成功事务内建账,来源唯一键幂等 - 核销申请、审批尝试记录、账单分摊预占与驳回重提,审批业务类型 employee_collection_approval - 企业微信终态消费幂等:通过转已核销、驳回释放预占、通过后撤销不回滚并转异常终态 - 退款成功事务内按 bill_id+refund_id 幂等冲销账单或仅写退款关联提示 - 线下收款方式字典、账单查询/统计/关闭、申请查询与代办权限,均写入事务内审计 OpenSpec Change: add-employee-collection-bills
171 lines
8.1 KiB
Go
171 lines
8.1 KiB
Go
package agentrecharge
|
|
|
|
import (
|
|
"context"
|
|
"strings"
|
|
|
|
"gorm.io/gorm"
|
|
"gorm.io/gorm/clause"
|
|
|
|
approvalapp "github.com/break/junhong_cmp_fiber/internal/application/approval"
|
|
employeecollectionapp "github.com/break/junhong_cmp_fiber/internal/application/employeecollection"
|
|
walletapp "github.com/break/junhong_cmp_fiber/internal/application/wallet"
|
|
"github.com/break/junhong_cmp_fiber/internal/model"
|
|
"github.com/break/junhong_cmp_fiber/pkg/auditcontext"
|
|
"github.com/break/junhong_cmp_fiber/pkg/constants"
|
|
"github.com/break/junhong_cmp_fiber/pkg/errors"
|
|
)
|
|
|
|
// ApprovalDecisionHandler 将渠道无关审批终态应用到员工线下代充值业务。
|
|
type ApprovalDecisionHandler struct {
|
|
db *gorm.DB
|
|
posting *walletapp.PostingService
|
|
audit RechargeAuditWriter
|
|
billCreation *employeecollectionapp.BillCreationService
|
|
}
|
|
|
|
// NewApprovalDecisionHandler 创建员工线下代充值审批终态消费者。
|
|
func NewApprovalDecisionHandler(
|
|
db *gorm.DB,
|
|
posting *walletapp.PostingService,
|
|
audit RechargeAuditWriter,
|
|
billCreation *employeecollectionapp.BillCreationService,
|
|
) *ApprovalDecisionHandler {
|
|
return &ApprovalDecisionHandler{db: db, posting: posting, audit: audit, billCreation: billCreation}
|
|
}
|
|
|
|
// Handle 幂等处理标准审批终态;只有 approved 首次入账,其他终态不修改钱包。
|
|
func (h *ApprovalDecisionHandler) Handle(ctx context.Context, event approvalapp.TerminalDecisionEvent) error {
|
|
if h == nil || h.db == nil || h.posting == nil || h.audit == nil {
|
|
return errors.New(errors.CodeInternalError, "员工线下代充值审批终态能力未配置")
|
|
}
|
|
if h.billCreation == nil {
|
|
return errors.New(errors.CodeInternalError, "员工代收款建账能力未配置")
|
|
}
|
|
if event.BusinessType != constants.ApprovalBusinessTypeOfflineRecharge || event.BusinessID == 0 || event.InstanceID == 0 {
|
|
return errors.New(errors.CodeInvalidParam, "员工线下代充值审批终态参数无效")
|
|
}
|
|
ctx = auditcontext.With(ctx, auditcontext.Context{CorrelationID: event.CorrelationID, ParentEventID: event.EventID})
|
|
return h.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
|
|
var record model.AgentRechargeRecord
|
|
if err := tx.WithContext(ctx).Clauses(clause.Locking{Strength: "UPDATE"}).
|
|
Where("id = ? AND payment_method = ?", event.BusinessID, constants.RechargeMethodOffline).
|
|
First(&record).Error; err != nil {
|
|
if err == gorm.ErrRecordNotFound {
|
|
return errors.New(errors.CodeForbidden, "无权限操作该资源或资源不存在")
|
|
}
|
|
return errors.Wrap(errors.CodeDatabaseError, err, "锁定员工线下代充值申请失败")
|
|
}
|
|
if record.ApprovalInstanceID == nil || *record.ApprovalInstanceID != event.InstanceID {
|
|
return errors.New(errors.CodeConflict, "线下代充值申请关联的审批实例不一致")
|
|
}
|
|
switch event.Decision {
|
|
case constants.ApprovalDecisionApproved:
|
|
return h.applyApproved(ctx, tx, &record, event)
|
|
case constants.ApprovalDecisionRejected:
|
|
return h.closeOfflineRecharge(ctx, tx, &record, event, constants.RechargeStatusRejected, "企业微信审批已拒绝")
|
|
case constants.ApprovalDecisionCancelled:
|
|
return h.closeOfflineRecharge(ctx, tx, &record, event, constants.RechargeStatusClosed, "企业微信审批已撤销")
|
|
case constants.ApprovalDecisionDeleted:
|
|
return h.closeOfflineRecharge(ctx, tx, &record, event, constants.RechargeStatusClosed, "企业微信审批已删除")
|
|
case constants.ApprovalDecisionRevokedAfterApproved:
|
|
return nil
|
|
default:
|
|
return errors.New(errors.CodeInvalidParam, "不支持的线下代充值审批终态")
|
|
}
|
|
})
|
|
}
|
|
|
|
func (h *ApprovalDecisionHandler) applyApproved(
|
|
ctx context.Context,
|
|
tx *gorm.DB,
|
|
record *model.AgentRechargeRecord,
|
|
event approvalapp.TerminalDecisionEvent,
|
|
) error {
|
|
if record.Status != constants.RechargeStatusPending && record.Status != constants.RechargeStatusCompleted {
|
|
return errors.New(errors.CodeInvalidStatus, "线下代充值申请状态不允许审批入账")
|
|
}
|
|
if record.Status == constants.RechargeStatusPending {
|
|
result := tx.WithContext(ctx).Model(&model.AgentRechargeRecord{}).
|
|
Where("id = ? AND status = ?", record.ID, constants.RechargeStatusPending).
|
|
Updates(map[string]any{
|
|
"status": constants.RechargeStatusCompleted, "paid_at": event.OccurredAt,
|
|
"completed_at": event.OccurredAt, "updated_at": event.OccurredAt,
|
|
})
|
|
if result.Error != nil {
|
|
return errors.Wrap(errors.CodeDatabaseError, result.Error, "完成线下代充值审批申请失败")
|
|
}
|
|
if result.RowsAffected != 1 {
|
|
return errors.New(errors.CodeConflict, "线下代充值申请状态已变化")
|
|
}
|
|
}
|
|
posting, err := h.posting.PostInTx(ctx, tx, walletapp.PostingCommand{
|
|
ShopID: record.ShopID, WalletID: record.AgentWalletID, Amount: record.Amount,
|
|
ReferenceType: constants.ReferenceTypeTopup, ReferenceID: record.ID,
|
|
TransactionType: constants.AgentTransactionTypeRecharge,
|
|
UserID: event.SubmitterAccountID, Creator: event.SubmitterAccountID,
|
|
Remark: "企业微信审批通过线下充值", CorrelationID: event.CorrelationID,
|
|
})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
// 员工代收款建账:锚点为“平台账号发起的线下充值入账成功”,按来源唯一键 recharge:{id} 幂等。
|
|
if _, err := h.billCreation.CreateFromRechargeInTx(ctx, tx, record); err != nil {
|
|
return err
|
|
}
|
|
if record.Status == constants.RechargeStatusCompleted && posting.AlreadyApplied {
|
|
return nil
|
|
}
|
|
return h.appendTerminalAudit(ctx, tx, record, event, constants.AuditActionAgentRechargeCredited, "企业微信审批通过,代理充值已入账", constants.RechargeStatusCompleted, true)
|
|
}
|
|
|
|
func (h *ApprovalDecisionHandler) closeOfflineRecharge(ctx context.Context, tx *gorm.DB, record *model.AgentRechargeRecord, event approvalapp.TerminalDecisionEvent, status int, reason string) error {
|
|
if record.Status == status {
|
|
return nil
|
|
}
|
|
if record.Status != constants.RechargeStatusPending {
|
|
return errors.New(errors.CodeInvalidStatus, "线下代充值申请状态不允许结束审批")
|
|
}
|
|
reason = strings.TrimSpace(reason)
|
|
result := tx.WithContext(ctx).Model(&model.AgentRechargeRecord{}).
|
|
Where("id = ? AND status = ?", record.ID, constants.RechargeStatusPending).
|
|
Updates(map[string]any{"status": status, "rejection_reason": reason})
|
|
if result.Error != nil {
|
|
return errors.Wrap(errors.CodeDatabaseError, result.Error, "结束线下代充值审批申请失败")
|
|
}
|
|
if result.RowsAffected != 1 {
|
|
return errors.New(errors.CodeConflict, "线下代充值申请状态已变化")
|
|
}
|
|
return h.appendTerminalAudit(ctx, tx, record, event, constants.AuditActionAgentRechargeClosed, reason, status, false)
|
|
}
|
|
|
|
func (h *ApprovalDecisionHandler) appendTerminalAudit(ctx context.Context, tx *gorm.DB, record *model.AgentRechargeRecord, event approvalapp.TerminalDecisionEvent, actionCode, summary string, status int, withWallet bool) error {
|
|
after := *record
|
|
after.Status = status
|
|
change := RechargeAudit{
|
|
ActionCode: actionCode, Summary: summary, Record: &after,
|
|
BeforeData: map[string]any{"status": record.Status}, AfterData: map[string]any{"status": status},
|
|
}
|
|
var approval model.ApprovalInstance
|
|
if err := tx.WithContext(ctx).First(&approval, event.InstanceID).Error; err != nil {
|
|
return errors.Wrap(errors.CodeDatabaseError, err, "查询线下代充值审批审计快照失败")
|
|
}
|
|
change.Approval = &approval
|
|
if withWallet {
|
|
var wallet model.AgentWallet
|
|
if err := tx.WithContext(ctx).First(&wallet, record.AgentWalletID).Error; err != nil {
|
|
return errors.Wrap(errors.CodeDatabaseError, err, "查询代理充值钱包审计快照失败")
|
|
}
|
|
var transaction model.AgentWalletTransaction
|
|
if err := tx.WithContext(ctx).Where("reference_type = ? AND reference_id = ? AND transaction_type = ? AND status = ?",
|
|
constants.ReferenceTypeTopup, record.ID, constants.AgentTransactionTypeRecharge, constants.TransactionStatusSuccess).
|
|
First(&transaction).Error; err != nil {
|
|
return errors.Wrap(errors.CodeDatabaseError, err, "查询代理充值入账流水审计快照失败")
|
|
}
|
|
change.Wallet, change.Transaction = &wallet, &transaction
|
|
}
|
|
return h.audit.WriteAgentRecharge(ctx, tx, change)
|
|
}
|
|
|
|
var _ approvalapp.BusinessDecisionHandler = (*ApprovalDecisionHandler)(nil)
|