This commit is contained in:
@@ -2,12 +2,10 @@ package wallet
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strconv"
|
||||
|
||||
"github.com/bytedance/sonic"
|
||||
"gorm.io/gorm"
|
||||
|
||||
notificationapp "github.com/break/junhong_cmp_fiber/internal/application/notification"
|
||||
walletapp "github.com/break/junhong_cmp_fiber/internal/application/wallet"
|
||||
"github.com/break/junhong_cmp_fiber/internal/infrastructure/messaging/outbox"
|
||||
"github.com/break/junhong_cmp_fiber/internal/model"
|
||||
@@ -16,20 +14,18 @@ import (
|
||||
)
|
||||
|
||||
// DebitEventConsumer 校验已投递的代理主钱包扣款事件具有对应权威资金流水。
|
||||
// 后续余额预警等消费者在此稳定接缝上扩展,不需要回读或改写订单扣款事务。
|
||||
type DebitEventConsumer struct {
|
||||
db *gorm.DB
|
||||
outbox *outbox.Repository
|
||||
db *gorm.DB
|
||||
}
|
||||
|
||||
// NewDebitEventConsumer 创建代理主钱包扣款事件消费者。
|
||||
func NewDebitEventConsumer(db *gorm.DB) *DebitEventConsumer {
|
||||
return &DebitEventConsumer{db: db, outbox: outbox.NewRepository()}
|
||||
return &DebitEventConsumer{db: db}
|
||||
}
|
||||
|
||||
// Consume 校验事件载荷及不可变流水,保证未知或损坏事件不会被静默确认。
|
||||
func (c *DebitEventConsumer) Consume(ctx context.Context, envelope outbox.DeliveryEnvelope) error {
|
||||
if c == nil || c.db == nil || c.outbox == nil {
|
||||
if c == nil || c.db == nil {
|
||||
return errors.New(errors.CodeInternalError, "代理主钱包扣款事件消费者未配置")
|
||||
}
|
||||
if envelope.EventType != constants.OutboxEventTypeAgentMainWalletDebited ||
|
||||
@@ -51,7 +47,7 @@ func (c *DebitEventConsumer) Consume(ctx context.Context, envelope outbox.Delive
|
||||
})
|
||||
}
|
||||
|
||||
// consumeInTx 复核权威扣款流水,并在余额首次跨过固定阈值时追加业务员通知事件。
|
||||
// consumeInTx 复核权威扣款流水。
|
||||
func (c *DebitEventConsumer) consumeInTx(ctx context.Context, tx *gorm.DB, event walletapp.DebitedEvent) error {
|
||||
var transaction model.AgentWalletTransaction
|
||||
err := tx.WithContext(ctx).Unscoped().
|
||||
@@ -68,57 +64,5 @@ func (c *DebitEventConsumer) consumeInTx(ctx context.Context, tx *gorm.DB, event
|
||||
transaction.BalanceAfter != event.BalanceAfter {
|
||||
return errors.New(errors.CodeInternalError, "代理主钱包扣款事件与权威资金流水不一致")
|
||||
}
|
||||
if event.BalanceBefore < constants.AgentMainWalletLowBalanceThreshold ||
|
||||
event.BalanceAfter >= constants.AgentMainWalletLowBalanceThreshold {
|
||||
return nil
|
||||
}
|
||||
|
||||
recipientID, err := resolveBusinessOwnerAccountID(ctx, tx, event.ShopID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if recipientID == 0 {
|
||||
return nil
|
||||
}
|
||||
shopID := strconv.FormatUint(uint64(event.ShopID), 10)
|
||||
notificationEventID := "wallet-low:order:" + strconv.FormatUint(uint64(event.ReferenceID), 10)
|
||||
_, err = c.outbox.AppendIdempotent(ctx, tx, outbox.Envelope{
|
||||
EventID: notificationEventID, EventType: constants.OutboxEventTypeAdminDirectNotification,
|
||||
PayloadVersion: constants.NotificationPayloadVersionV1,
|
||||
AggregateType: "agent_wallet", AggregateID: strconv.FormatUint(uint64(event.WalletID), 10),
|
||||
ResourceType: constants.NotificationRefTypeShopFund, ResourceID: shopID,
|
||||
BusinessKey: notificationEventID, RequestID: event.RequestID, CorrelationID: event.CorrelationID,
|
||||
Payload: notificationapp.AdminDirectPayload{
|
||||
RecipientID: recipientID, NotificationType: constants.NotificationTypeAgentMainWalletLowBalance,
|
||||
RefType: constants.NotificationRefTypeShopFund, RefID: shopID,
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
return errors.Wrap(errors.CodeDatabaseError, err, "写入主钱包低余额通知事件失败")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// resolveBusinessOwnerAccountID 返回店铺当前启用的平台业务员账号;无有效归属时返回零值。
|
||||
func resolveBusinessOwnerAccountID(ctx context.Context, tx *gorm.DB, shopID uint) (uint, error) {
|
||||
var shop model.Shop
|
||||
err := tx.WithContext(ctx).Select("business_owner_account_id").Where("id = ?", shopID).Take(&shop).Error
|
||||
if err == gorm.ErrRecordNotFound || shop.BusinessOwnerAccountID == nil {
|
||||
return 0, nil
|
||||
}
|
||||
if err != nil {
|
||||
return 0, errors.Wrap(errors.CodeDatabaseError, err, "查询店铺业务员归属失败")
|
||||
}
|
||||
|
||||
var account model.Account
|
||||
err = tx.WithContext(ctx).Select("id").
|
||||
Where("id = ? AND status = ? AND user_type = ?", *shop.BusinessOwnerAccountID, constants.StatusEnabled, constants.UserTypePlatform).
|
||||
Take(&account).Error
|
||||
if err == gorm.ErrRecordNotFound {
|
||||
return 0, nil
|
||||
}
|
||||
if err != nil {
|
||||
return 0, errors.Wrap(errors.CodeDatabaseError, err, "校验店铺业务员账号失败")
|
||||
}
|
||||
return account.ID, nil
|
||||
}
|
||||
|
||||
@@ -5,9 +5,11 @@ import (
|
||||
"context"
|
||||
"strconv"
|
||||
|
||||
notificationapp "github.com/break/junhong_cmp_fiber/internal/application/notification"
|
||||
walletapp "github.com/break/junhong_cmp_fiber/internal/application/wallet"
|
||||
"github.com/break/junhong_cmp_fiber/internal/infrastructure/audit"
|
||||
"github.com/break/junhong_cmp_fiber/internal/infrastructure/messaging/outbox"
|
||||
"github.com/break/junhong_cmp_fiber/internal/model"
|
||||
"github.com/break/junhong_cmp_fiber/pkg/constants"
|
||||
"github.com/break/junhong_cmp_fiber/pkg/errors"
|
||||
"gorm.io/gorm"
|
||||
@@ -39,5 +41,54 @@ func (w *DebitEventWriter) Append(ctx context.Context, tx *gorm.DB, event wallet
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return w.audit.WriteAgentWalletDebit(ctx, tx, event)
|
||||
if err := w.audit.WriteAgentWalletDebit(ctx, tx, event); err != nil {
|
||||
return err
|
||||
}
|
||||
if event.BalanceBefore < constants.AgentMainWalletLowBalanceThreshold || event.BalanceAfter >= constants.AgentMainWalletLowBalanceThreshold {
|
||||
return nil
|
||||
}
|
||||
recipientID, err := resolveBusinessOwnerAccountID(ctx, tx, event.ShopID)
|
||||
if err != nil || recipientID == 0 {
|
||||
return err
|
||||
}
|
||||
shopID := strconv.FormatUint(uint64(event.ShopID), 10)
|
||||
notificationEventID := "wallet-low:order:" + strconv.FormatUint(uint64(event.ReferenceID), 10)
|
||||
_, err = w.outbox.AppendIdempotent(ctx, tx, outbox.Envelope{
|
||||
EventID: notificationEventID, EventType: constants.OutboxEventTypeAdminDirectNotification,
|
||||
PayloadVersion: constants.NotificationPayloadVersionV1,
|
||||
AggregateType: "agent_wallet", AggregateID: strconv.FormatUint(uint64(event.WalletID), 10),
|
||||
ResourceType: constants.NotificationRefTypeShopFund, ResourceID: shopID,
|
||||
BusinessKey: notificationEventID, RequestID: event.RequestID, CorrelationID: event.CorrelationID,
|
||||
Payload: notificationapp.AdminDirectPayload{
|
||||
RecipientID: recipientID, NotificationType: constants.NotificationTypeAgentMainWalletLowBalance,
|
||||
RefType: constants.NotificationRefTypeShopFund, RefID: shopID,
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
return errors.Wrap(errors.CodeDatabaseError, err, "写入主钱包低余额通知事件失败")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// resolveBusinessOwnerAccountID 返回店铺当前启用的平台业务员账号;无有效归属时返回零值。
|
||||
func resolveBusinessOwnerAccountID(ctx context.Context, tx *gorm.DB, shopID uint) (uint, error) {
|
||||
var shop model.Shop
|
||||
err := tx.WithContext(ctx).Select("business_owner_account_id").Where("id = ?", shopID).Take(&shop).Error
|
||||
if err == gorm.ErrRecordNotFound || shop.BusinessOwnerAccountID == nil {
|
||||
return 0, nil
|
||||
}
|
||||
if err != nil {
|
||||
return 0, errors.Wrap(errors.CodeDatabaseError, err, "查询店铺业务员归属失败")
|
||||
}
|
||||
var account model.Account
|
||||
err = tx.WithContext(ctx).Select("id").
|
||||
Where("id = ? AND status = ? AND user_type = ?", *shop.BusinessOwnerAccountID, constants.StatusEnabled, constants.UserTypePlatform).
|
||||
Take(&account).Error
|
||||
if err == gorm.ErrRecordNotFound {
|
||||
return 0, nil
|
||||
}
|
||||
if err != nil {
|
||||
return 0, errors.Wrap(errors.CodeDatabaseError, err, "校验店铺业务员账号失败")
|
||||
}
|
||||
return account.ID, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user