解耦订单和申诉模块外部依赖

This commit is contained in:
yml2213
2026-06-10 15:41:31 +08:00
parent ca50b103ec
commit 28c4efc669
8 changed files with 112 additions and 85 deletions
@@ -256,7 +256,7 @@ func (r *Repository) prepareRefund(order *model.RentalOrder, amountCent int64, b
if amountCent <= 0 {
return nil, nil
}
if r.refundFunc == nil {
if r.refundStarter == nil {
return nil, ErrDependencyUnavailable
}
order.RefundStatus = "pending"
@@ -271,10 +271,10 @@ func (r *Repository) prepareRefund(order *model.RentalOrder, amountCent int64, b
}
func (r *Repository) startRefundBestEffort(ctx context.Context, action *refundAction) {
if action == nil || r.refundFunc == nil {
if action == nil || r.refundStarter == nil {
return
}
_, _ = r.refundFunc(ctx, action.OrderID, action.RefundAmountCent, action.BizType, action.Remark)
_, _ = r.refundStarter.StartRefund(ctx, action.OrderID, action.RefundAmountCent, action.BizType, action.Remark)
}
func renterFrozenBalance(tx *gorm.DB, renterID uint64) (int64, error) {
+21 -11
View File
@@ -8,14 +8,24 @@ import (
)
type Repository struct {
db *gorm.DB
refundFunc RefundFunc
db *gorm.DB
refundStarter RefundStarter
}
// RefundFunc 由 payment 模块注入,避免 dispute payment 形成循环依赖
// RefundStarter 由支付模块适配实现,避免 dispute 直接依赖 payment。
type RefundStarter interface {
StartRefund(ctx context.Context, orderID uint64, refundAmountCent int64, bizType string, remark string) (status string, err error)
}
// RefundFunc 由 payment 模块注入,避免 dispute 与 payment 形成循环依赖。
type RefundFunc func(ctx context.Context, orderID uint64, refundAmountCent int64, bizType string, remark string) (status string, err error)
type RefundStarterFunc func(ctx context.Context, orderID uint64, refundAmountCent int64, bizType string, remark string) (status string, err error)
func (fn RefundStarterFunc) StartRefund(ctx context.Context, orderID uint64, refundAmountCent int64, bizType string, remark string) (string, error) {
return fn(ctx, orderID, refundAmountCent, bizType, remark)
}
type Dependencies struct {
RefundStarter RefundStarter
}
type refundAction struct {
OrderID uint64
@@ -24,12 +34,12 @@ type refundAction struct {
Remark string
}
func NewRepository(db *gorm.DB) *Repository {
return &Repository{db: db}
}
func (r *Repository) SetRefundFunc(fn RefundFunc) {
r.refundFunc = fn
func NewRepository(db *gorm.DB, deps ...Dependencies) *Repository {
repo := &Repository{db: db}
if len(deps) > 0 {
repo.refundStarter = deps[0].RefundStarter
}
return repo
}
func IsNotFound(err error) bool {