All checks were successful
构建并部署到测试环境(无 SSH) / build-and-deploy (push) Successful in 8m30s
95 lines
4.1 KiB
Go
95 lines
4.1 KiB
Go
// Package wallet 提供代理主钱包持久化与可靠事件 Adapter。
|
|
package wallet
|
|
|
|
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"
|
|
)
|
|
|
|
// DebitEventWriter 将代理主钱包扣款事实写入公共 Outbox 和统一审计。
|
|
type DebitEventWriter struct {
|
|
outbox *outbox.Repository
|
|
audit *audit.Writer
|
|
}
|
|
|
|
// NewDebitEventWriter 创建代理主钱包扣款事件 Writer。
|
|
func NewDebitEventWriter(repository *outbox.Repository, auditWriter *audit.Writer) *DebitEventWriter {
|
|
return &DebitEventWriter{outbox: repository, audit: auditWriter}
|
|
}
|
|
|
|
// Append 在调用方业务事务中追加代理主钱包扣款 Outbox 与审计事件。
|
|
func (w *DebitEventWriter) Append(ctx context.Context, tx *gorm.DB, event walletapp.DebitedEvent) error {
|
|
if w == nil || w.outbox == nil || w.audit == nil {
|
|
return errors.New(errors.CodeInternalError, "代理主钱包扣款事件 Writer 未配置")
|
|
}
|
|
_, err := w.outbox.Append(ctx, tx, outbox.Envelope{
|
|
EventID: event.EventID, EventType: constants.OutboxEventTypeAgentMainWalletDebited,
|
|
PayloadVersion: constants.AgentMainWalletDebitedPayloadVersionV1,
|
|
AggregateType: "agent_wallet", AggregateID: strconv.FormatUint(uint64(event.WalletID), 10),
|
|
ResourceType: event.ReferenceType, ResourceID: strconv.FormatUint(uint64(event.ReferenceID), 10),
|
|
BusinessKey: event.EventID, RequestID: event.RequestID, CorrelationID: event.CorrelationID, Payload: event,
|
|
})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
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
|
|
}
|