解耦订单和申诉模块外部依赖
This commit is contained in:
@@ -172,14 +172,14 @@ func (r *Repository) AdminRefund(ctx context.Context, orderID uint64) (*RefundSt
|
||||
if order.RefundStatus == refundStatusRefunded {
|
||||
return r.buildRefundStatusDTO(&order), nil
|
||||
}
|
||||
if r.refundFunc == nil {
|
||||
if r.refundStarter == nil {
|
||||
return nil, ErrDependencyUnavailable
|
||||
}
|
||||
totalCent := order.RentAmountCent + order.DepositAmountCent
|
||||
if totalCent <= 0 {
|
||||
return nil, ErrInvalidCheckoutAmount
|
||||
}
|
||||
status, err := r.refundFunc(ctx, orderID, totalCent, refundBizAdmin, "后台人工退款")
|
||||
status, err := r.refundStarter.StartRefund(ctx, orderID, totalCent, refundBizAdmin, "后台人工退款")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -195,8 +195,8 @@ func (r *Repository) ConfirmPaidFromChannel(ctx context.Context, orderID uint64,
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if newConvID > 0 && r.chatRepo != nil {
|
||||
r.chatRepo.NotifyNewConversation(newConvID)
|
||||
if newConvID > 0 && r.chatNotifier != nil {
|
||||
r.chatNotifier.NotifyNewConversation(newConvID)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -11,7 +11,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 = refundStatusPending
|
||||
@@ -26,10 +26,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
|
||||
}
|
||||
if _, err := r.refundFunc(ctx, action.OrderID, action.RefundAmountCent, action.BizType, action.Remark); err != nil {
|
||||
if _, err := r.refundStarter.StartRefund(ctx, action.OrderID, action.RefundAmountCent, action.BizType, action.Remark); err != nil {
|
||||
log.Printf("[order] start refund failed order_id=%d biz_type=%s amount_cent=%d err=%v", action.OrderID, action.BizType, action.RefundAmountCent, err)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,13 +3,29 @@ package order
|
||||
import (
|
||||
"context"
|
||||
|
||||
"hfb_sys/backend/internal/modules/chat"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// RefundFunc 由 payment 模块注入,避免 order 与 payment 形成循环依赖。
|
||||
type RefundFunc func(ctx context.Context, orderID uint64, refundAmountCent int64, bizType string, remark string) (status string, err error)
|
||||
// RefundStarter 由支付模块适配实现,避免 order 直接依赖 payment。
|
||||
type RefundStarter interface {
|
||||
StartRefund(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 OrderChatNotifier interface {
|
||||
NotifyNewConversation(conversationID uint64)
|
||||
}
|
||||
|
||||
type Dependencies struct {
|
||||
ChatNotifier OrderChatNotifier
|
||||
RefundStarter RefundStarter
|
||||
}
|
||||
|
||||
type refundAction struct {
|
||||
OrderID uint64
|
||||
RefundAmountCent int64
|
||||
@@ -18,21 +34,18 @@ type refundAction struct {
|
||||
}
|
||||
|
||||
type Repository struct {
|
||||
db *gorm.DB
|
||||
chatRepo *chat.Repository
|
||||
refundFunc RefundFunc
|
||||
db *gorm.DB
|
||||
chatNotifier OrderChatNotifier
|
||||
refundStarter RefundStarter
|
||||
}
|
||||
|
||||
const defaultPendingPaymentTimeoutMinutes = 15
|
||||
|
||||
func NewRepository(db *gorm.DB) *Repository {
|
||||
return &Repository{db: db}
|
||||
}
|
||||
|
||||
func (r *Repository) SetChatRepo(cr *chat.Repository) {
|
||||
r.chatRepo = cr
|
||||
}
|
||||
|
||||
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.chatNotifier = deps[0].ChatNotifier
|
||||
repo.refundStarter = deps[0].RefundStarter
|
||||
}
|
||||
return repo
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user