refactor: 适配 asset_wallet 更名,更新订单、充值和购买验证服务
Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-opencode) Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
This commit is contained in:
@@ -31,7 +31,7 @@ type Service struct {
|
|||||||
orderStore *postgres.OrderStore
|
orderStore *postgres.OrderStore
|
||||||
orderItemStore *postgres.OrderItemStore
|
orderItemStore *postgres.OrderItemStore
|
||||||
agentWalletStore *postgres.AgentWalletStore
|
agentWalletStore *postgres.AgentWalletStore
|
||||||
cardWalletStore *postgres.CardWalletStore
|
assetWalletStore *postgres.AssetWalletStore
|
||||||
purchaseValidationService *purchase_validation.Service
|
purchaseValidationService *purchase_validation.Service
|
||||||
shopPackageAllocationStore *postgres.ShopPackageAllocationStore
|
shopPackageAllocationStore *postgres.ShopPackageAllocationStore
|
||||||
shopSeriesAllocationStore *postgres.ShopSeriesAllocationStore
|
shopSeriesAllocationStore *postgres.ShopSeriesAllocationStore
|
||||||
@@ -51,7 +51,7 @@ func New(
|
|||||||
orderStore *postgres.OrderStore,
|
orderStore *postgres.OrderStore,
|
||||||
orderItemStore *postgres.OrderItemStore,
|
orderItemStore *postgres.OrderItemStore,
|
||||||
agentWalletStore *postgres.AgentWalletStore,
|
agentWalletStore *postgres.AgentWalletStore,
|
||||||
cardWalletStore *postgres.CardWalletStore,
|
assetWalletStore *postgres.AssetWalletStore,
|
||||||
purchaseValidationService *purchase_validation.Service,
|
purchaseValidationService *purchase_validation.Service,
|
||||||
shopPackageAllocationStore *postgres.ShopPackageAllocationStore,
|
shopPackageAllocationStore *postgres.ShopPackageAllocationStore,
|
||||||
shopSeriesAllocationStore *postgres.ShopSeriesAllocationStore,
|
shopSeriesAllocationStore *postgres.ShopSeriesAllocationStore,
|
||||||
@@ -70,7 +70,7 @@ func New(
|
|||||||
orderStore: orderStore,
|
orderStore: orderStore,
|
||||||
orderItemStore: orderItemStore,
|
orderItemStore: orderItemStore,
|
||||||
agentWalletStore: agentWalletStore,
|
agentWalletStore: agentWalletStore,
|
||||||
cardWalletStore: cardWalletStore,
|
assetWalletStore: assetWalletStore,
|
||||||
purchaseValidationService: purchaseValidationService,
|
purchaseValidationService: purchaseValidationService,
|
||||||
shopPackageAllocationStore: shopPackageAllocationStore,
|
shopPackageAllocationStore: shopPackageAllocationStore,
|
||||||
shopSeriesAllocationStore: shopSeriesAllocationStore,
|
shopSeriesAllocationStore: shopSeriesAllocationStore,
|
||||||
@@ -326,6 +326,22 @@ func (s *Service) CreateAdminOrder(ctx context.Context, req *dto.CreateAdminOrde
|
|||||||
var validationResult *purchase_validation.PurchaseValidationResult
|
var validationResult *purchase_validation.PurchaseValidationResult
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
|
if req.PaymentMethod == model.PaymentMethodOffline {
|
||||||
|
// offline 订单:绕过代理 Allocation 上架检查,仅验证套餐全局状态
|
||||||
|
if req.OrderType == model.OrderTypeSingleCard {
|
||||||
|
if req.IotCardID == nil {
|
||||||
|
return nil, errors.New(errors.CodeInvalidParam, "单卡购买必须指定IoT卡ID")
|
||||||
|
}
|
||||||
|
validationResult, err = s.purchaseValidationService.ValidateAdminOfflineCardPurchase(ctx, *req.IotCardID, req.PackageIDs)
|
||||||
|
} else if req.OrderType == model.OrderTypeDevice {
|
||||||
|
if req.DeviceID == nil {
|
||||||
|
return nil, errors.New(errors.CodeInvalidParam, "设备购买必须指定设备ID")
|
||||||
|
}
|
||||||
|
validationResult, err = s.purchaseValidationService.ValidateAdminOfflineDevicePurchase(ctx, *req.DeviceID, req.PackageIDs)
|
||||||
|
} else {
|
||||||
|
return nil, errors.New(errors.CodeInvalidParam, "无效的订单类型")
|
||||||
|
}
|
||||||
|
} else {
|
||||||
if req.OrderType == model.OrderTypeSingleCard {
|
if req.OrderType == model.OrderTypeSingleCard {
|
||||||
if req.IotCardID == nil {
|
if req.IotCardID == nil {
|
||||||
return nil, errors.New(errors.CodeInvalidParam, "单卡购买必须指定IoT卡ID")
|
return nil, errors.New(errors.CodeInvalidParam, "单卡购买必须指定IoT卡ID")
|
||||||
@@ -339,6 +355,7 @@ func (s *Service) CreateAdminOrder(ctx context.Context, req *dto.CreateAdminOrde
|
|||||||
} else {
|
} else {
|
||||||
return nil, errors.New(errors.CodeInvalidParam, "无效的订单类型")
|
return nil, errors.New(errors.CodeInvalidParam, "无效的订单类型")
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@@ -362,10 +379,13 @@ func (s *Service) CreateAdminOrder(ctx context.Context, req *dto.CreateAdminOrde
|
|||||||
lockKey := constants.RedisOrderCreateLockKey(carrierType, carrierID)
|
lockKey := constants.RedisOrderCreateLockKey(carrierType, carrierID)
|
||||||
defer s.redis.Del(ctx, lockKey)
|
defer s.redis.Del(ctx, lockKey)
|
||||||
|
|
||||||
|
// offline 订单不检查强充:平台直接操作,不涉及消费者支付门槛,不产生一次性佣金触发条件
|
||||||
|
if req.PaymentMethod != model.PaymentMethodOffline {
|
||||||
forceRechargeCheck := s.checkForceRechargeRequirement(ctx, validationResult)
|
forceRechargeCheck := s.checkForceRechargeRequirement(ctx, validationResult)
|
||||||
if forceRechargeCheck.NeedForceRecharge && validationResult.TotalPrice < forceRechargeCheck.ForceRechargeAmount {
|
if forceRechargeCheck.NeedForceRecharge && validationResult.TotalPrice < forceRechargeCheck.ForceRechargeAmount {
|
||||||
return nil, errors.New(errors.CodeForceRechargeRequired, "首次购买需满足最低充值要求")
|
return nil, errors.New(errors.CodeForceRechargeRequired, "首次购买需满足最低充值要求")
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
userID := middleware.GetUserIDFromContext(ctx)
|
userID := middleware.GetUserIDFromContext(ctx)
|
||||||
|
|
||||||
@@ -1271,12 +1291,12 @@ func (s *Service) unfreezeWalletForCancel(ctx context.Context, tx *gorm.DB, orde
|
|||||||
} else {
|
} else {
|
||||||
return errors.New(errors.CodeInternalError, "无法确定钱包归属")
|
return errors.New(errors.CodeInternalError, "无法确定钱包归属")
|
||||||
}
|
}
|
||||||
wallet, err := s.cardWalletStore.GetByResourceTypeAndID(ctx, resourceType, resourceID)
|
wallet, err := s.assetWalletStore.GetByResourceTypeAndID(ctx, resourceType, resourceID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.Wrap(errors.CodeWalletNotFound, err, "查询卡钱包失败")
|
return errors.Wrap(errors.CodeWalletNotFound, err, "查询资产钱包失败")
|
||||||
}
|
}
|
||||||
// 卡钱包解冻:直接减少冻结余额
|
// 资产钱包解冻:直接减少冻结余额
|
||||||
result := tx.Model(&model.CardWallet{}).
|
result := tx.Model(&model.AssetWallet{}).
|
||||||
Where("id = ? AND frozen_balance >= ?", wallet.ID, order.TotalAmount).
|
Where("id = ? AND frozen_balance >= ?", wallet.ID, order.TotalAmount).
|
||||||
Updates(map[string]any{
|
Updates(map[string]any{
|
||||||
"frozen_balance": gorm.Expr("frozen_balance - ?", order.TotalAmount),
|
"frozen_balance": gorm.Expr("frozen_balance - ?", order.TotalAmount),
|
||||||
@@ -1393,8 +1413,8 @@ func (s *Service) WalletPay(ctx context.Context, orderID uint, buyerType string,
|
|||||||
return s.activatePackage(ctx, tx, order)
|
return s.activatePackage(ctx, tx, order)
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
// 卡钱包系统(iot_card 或 device)
|
// 资产钱包系统(iot_card 或 device)
|
||||||
wallet, err := s.cardWalletStore.GetByResourceTypeAndID(ctx, resourceType, resourceID)
|
wallet, err := s.assetWalletStore.GetByResourceTypeAndID(ctx, resourceType, resourceID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if err == gorm.ErrRecordNotFound {
|
if err == gorm.ErrRecordNotFound {
|
||||||
return errors.New(errors.CodeWalletNotFound, "钱包不存在")
|
return errors.New(errors.CodeWalletNotFound, "钱包不存在")
|
||||||
@@ -1437,7 +1457,10 @@ func (s *Service) WalletPay(ctx context.Context, orderID uint, buyerType string,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
walletResult := tx.Model(&model.CardWallet{}).
|
// 扣款前记录余额快照,用于写入流水
|
||||||
|
balanceBefore := wallet.Balance
|
||||||
|
|
||||||
|
walletResult := tx.Model(&model.AssetWallet{}).
|
||||||
Where("id = ? AND balance >= ? AND version = ?", wallet.ID, order.TotalAmount, wallet.Version).
|
Where("id = ? AND balance >= ? AND version = ?", wallet.ID, order.TotalAmount, wallet.Version).
|
||||||
Updates(map[string]any{
|
Updates(map[string]any{
|
||||||
"balance": gorm.Expr("balance - ?", order.TotalAmount),
|
"balance": gorm.Expr("balance - ?", order.TotalAmount),
|
||||||
@@ -1450,6 +1473,27 @@ func (s *Service) WalletPay(ctx context.Context, orderID uint, buyerType string,
|
|||||||
return errors.New(errors.CodeInsufficientBalance, "余额不足或并发冲突")
|
return errors.New(errors.CodeInsufficientBalance, "余额不足或并发冲突")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 扣款成功后补写扣款流水,填补流水表中扣款记录缺失的问题
|
||||||
|
deductTx := &model.AssetWalletTransaction{
|
||||||
|
AssetWalletID: wallet.ID,
|
||||||
|
ResourceType: resourceType,
|
||||||
|
ResourceID: resourceID,
|
||||||
|
UserID: buyerID,
|
||||||
|
TransactionType: "deduct",
|
||||||
|
Amount: -order.TotalAmount,
|
||||||
|
BalanceBefore: balanceBefore,
|
||||||
|
BalanceAfter: balanceBefore - order.TotalAmount,
|
||||||
|
Status: 1,
|
||||||
|
ReferenceType: strPtr("order"),
|
||||||
|
ReferenceNo: &order.OrderNo,
|
||||||
|
Remark: strPtr("钱包支付套餐"),
|
||||||
|
ShopIDTag: wallet.ShopIDTag,
|
||||||
|
EnterpriseIDTag: wallet.EnterpriseIDTag,
|
||||||
|
}
|
||||||
|
if err := tx.Create(deductTx).Error; err != nil {
|
||||||
|
return errors.Wrap(errors.CodeDatabaseError, err, "创建扣款流水失败")
|
||||||
|
}
|
||||||
|
|
||||||
return s.activatePackage(ctx, tx, order)
|
return s.activatePackage(ctx, tx, order)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -173,3 +173,56 @@ func (s *Service) GetPurchasePrice(ctx context.Context, pkg *model.Package, buye
|
|||||||
return pkg.SuggestedRetailPrice
|
return pkg.SuggestedRetailPrice
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ValidateAdminOfflineCardPurchase 后台 offline 订单专用卡验证
|
||||||
|
// 绕过代理 Allocation 上架检查,仅验证套餐全局状态
|
||||||
|
func (s *Service) ValidateAdminOfflineCardPurchase(ctx context.Context, cardID uint, packageIDs []uint) (*PurchaseValidationResult, error) {
|
||||||
|
card, err := s.iotCardStore.GetByID(ctx, cardID)
|
||||||
|
if err != nil {
|
||||||
|
if err == gorm.ErrRecordNotFound {
|
||||||
|
return nil, errors.New(errors.CodeIotCardNotFound, "IoT卡不存在")
|
||||||
|
}
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if card.SeriesID == nil || *card.SeriesID == 0 {
|
||||||
|
return nil, errors.New(errors.CodeInvalidParam, "该卡未关联套餐系列,无法购买套餐")
|
||||||
|
}
|
||||||
|
|
||||||
|
packages, totalPrice, err := s.validatePackages(ctx, packageIDs, *card.SeriesID, 0)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return &PurchaseValidationResult{
|
||||||
|
Card: card,
|
||||||
|
Packages: packages,
|
||||||
|
TotalPrice: totalPrice,
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// ValidateAdminOfflineDevicePurchase 后台 offline 订单专用设备验证
|
||||||
|
// 绕过代理 Allocation 上架检查,仅验证套餐全局状态
|
||||||
|
func (s *Service) ValidateAdminOfflineDevicePurchase(ctx context.Context, deviceID uint, packageIDs []uint) (*PurchaseValidationResult, error) {
|
||||||
|
device, err := s.deviceStore.GetByID(ctx, deviceID)
|
||||||
|
if err != nil {
|
||||||
|
if err == gorm.ErrRecordNotFound {
|
||||||
|
return nil, errors.New(errors.CodeNotFound, "设备不存在")
|
||||||
|
}
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if device.SeriesID == nil || *device.SeriesID == 0 {
|
||||||
|
return nil, errors.New(errors.CodeInvalidParam, "该设备未关联套餐系列,无法购买套餐")
|
||||||
|
}
|
||||||
|
|
||||||
|
packages, totalPrice, err := s.validatePackages(ctx, packageIDs, *device.SeriesID, 0)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return &PurchaseValidationResult{
|
||||||
|
Device: device,
|
||||||
|
Packages: packages,
|
||||||
|
TotalPrice: totalPrice,
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|||||||
@@ -29,12 +29,12 @@ type ForceRechargeRequirement struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Service 充值服务
|
// Service 充值服务
|
||||||
// 负责卡钱包(IoT卡/设备)的充值订单创建、预检、支付回调处理等业务逻辑
|
// 负责资产钱包(IoT卡/设备)的充值订单创建、预检、支付回调处理等业务逻辑
|
||||||
type Service struct {
|
type Service struct {
|
||||||
db *gorm.DB
|
db *gorm.DB
|
||||||
cardRechargeStore *postgres.CardRechargeStore
|
assetRechargeStore *postgres.AssetRechargeStore
|
||||||
cardWalletStore *postgres.CardWalletStore
|
assetWalletStore *postgres.AssetWalletStore
|
||||||
cardWalletTransactionStore *postgres.CardWalletTransactionStore
|
assetWalletTransactionStore *postgres.AssetWalletTransactionStore
|
||||||
iotCardStore *postgres.IotCardStore
|
iotCardStore *postgres.IotCardStore
|
||||||
deviceStore *postgres.DeviceStore
|
deviceStore *postgres.DeviceStore
|
||||||
shopSeriesAllocationStore *postgres.ShopSeriesAllocationStore
|
shopSeriesAllocationStore *postgres.ShopSeriesAllocationStore
|
||||||
@@ -46,9 +46,9 @@ type Service struct {
|
|||||||
// New 创建充值服务实例
|
// New 创建充值服务实例
|
||||||
func New(
|
func New(
|
||||||
db *gorm.DB,
|
db *gorm.DB,
|
||||||
cardRechargeStore *postgres.CardRechargeStore,
|
assetRechargeStore *postgres.AssetRechargeStore,
|
||||||
cardWalletStore *postgres.CardWalletStore,
|
assetWalletStore *postgres.AssetWalletStore,
|
||||||
cardWalletTransactionStore *postgres.CardWalletTransactionStore,
|
assetWalletTransactionStore *postgres.AssetWalletTransactionStore,
|
||||||
iotCardStore *postgres.IotCardStore,
|
iotCardStore *postgres.IotCardStore,
|
||||||
deviceStore *postgres.DeviceStore,
|
deviceStore *postgres.DeviceStore,
|
||||||
shopSeriesAllocationStore *postgres.ShopSeriesAllocationStore,
|
shopSeriesAllocationStore *postgres.ShopSeriesAllocationStore,
|
||||||
@@ -58,9 +58,9 @@ func New(
|
|||||||
) *Service {
|
) *Service {
|
||||||
return &Service{
|
return &Service{
|
||||||
db: db,
|
db: db,
|
||||||
cardRechargeStore: cardRechargeStore,
|
assetRechargeStore: assetRechargeStore,
|
||||||
cardWalletStore: cardWalletStore,
|
assetWalletStore: assetWalletStore,
|
||||||
cardWalletTransactionStore: cardWalletTransactionStore,
|
assetWalletTransactionStore: assetWalletTransactionStore,
|
||||||
iotCardStore: iotCardStore,
|
iotCardStore: iotCardStore,
|
||||||
deviceStore: deviceStore,
|
deviceStore: deviceStore,
|
||||||
shopSeriesAllocationStore: shopSeriesAllocationStore,
|
shopSeriesAllocationStore: shopSeriesAllocationStore,
|
||||||
@@ -82,13 +82,13 @@ func (s *Service) Create(ctx context.Context, req *dto.CreateRechargeRequest, us
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 2. 获取资源(卡或设备)钱包
|
// 2. 获取资源(卡或设备)钱包
|
||||||
var wallet *model.CardWallet
|
var wallet *model.AssetWallet
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
if req.ResourceType == "iot_card" {
|
if req.ResourceType == "iot_card" {
|
||||||
wallet, err = s.cardWalletStore.GetByResourceTypeAndID(ctx, "iot_card", req.ResourceID)
|
wallet, err = s.assetWalletStore.GetByResourceTypeAndID(ctx, "iot_card", req.ResourceID)
|
||||||
} else if req.ResourceType == "device" {
|
} else if req.ResourceType == "device" {
|
||||||
wallet, err = s.cardWalletStore.GetByResourceTypeAndID(ctx, "device", req.ResourceID)
|
wallet, err = s.assetWalletStore.GetByResourceTypeAndID(ctx, "device", req.ResourceID)
|
||||||
} else {
|
} else {
|
||||||
return nil, errors.New(errors.CodeInvalidParam, "无效的资源类型")
|
return nil, errors.New(errors.CodeInvalidParam, "无效的资源类型")
|
||||||
}
|
}
|
||||||
@@ -115,9 +115,9 @@ func (s *Service) Create(ctx context.Context, req *dto.CreateRechargeRequest, us
|
|||||||
rechargeNo := s.generateRechargeNo()
|
rechargeNo := s.generateRechargeNo()
|
||||||
|
|
||||||
// 5. 创建充值订单
|
// 5. 创建充值订单
|
||||||
recharge := &model.CardRechargeRecord{
|
recharge := &model.AssetRechargeRecord{
|
||||||
UserID: userID,
|
UserID: userID,
|
||||||
CardWalletID: wallet.ID,
|
AssetWalletID: wallet.ID,
|
||||||
ResourceType: req.ResourceType,
|
ResourceType: req.ResourceType,
|
||||||
ResourceID: req.ResourceID,
|
ResourceID: req.ResourceID,
|
||||||
RechargeNo: rechargeNo,
|
RechargeNo: rechargeNo,
|
||||||
@@ -128,7 +128,7 @@ func (s *Service) Create(ctx context.Context, req *dto.CreateRechargeRequest, us
|
|||||||
EnterpriseIDTag: wallet.EnterpriseIDTag,
|
EnterpriseIDTag: wallet.EnterpriseIDTag,
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := s.cardRechargeStore.Create(ctx, recharge); err != nil {
|
if err := s.assetRechargeStore.Create(ctx, recharge); err != nil {
|
||||||
return nil, errors.Wrap(errors.CodeDatabaseError, err, "创建充值订单失败")
|
return nil, errors.Wrap(errors.CodeDatabaseError, err, "创建充值订单失败")
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -173,7 +173,7 @@ func (s *Service) GetRechargeCheck(ctx context.Context, resourceType string, res
|
|||||||
// GetByID 根据ID查询充值订单详情
|
// GetByID 根据ID查询充值订单详情
|
||||||
// 支持数据权限过滤
|
// 支持数据权限过滤
|
||||||
func (s *Service) GetByID(ctx context.Context, id uint, userID uint) (*dto.RechargeResponse, error) {
|
func (s *Service) GetByID(ctx context.Context, id uint, userID uint) (*dto.RechargeResponse, error) {
|
||||||
recharge, err := s.cardRechargeStore.GetByID(ctx, id)
|
recharge, err := s.assetRechargeStore.GetByID(ctx, id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if err == gorm.ErrRecordNotFound {
|
if err == gorm.ErrRecordNotFound {
|
||||||
return nil, errors.New(errors.CodeRechargeNotFound, "充值订单不存在")
|
return nil, errors.New(errors.CodeRechargeNotFound, "充值订单不存在")
|
||||||
@@ -201,10 +201,10 @@ func (s *Service) List(ctx context.Context, req *dto.RechargeListRequest, userID
|
|||||||
pageSize = constants.DefaultPageSize
|
pageSize = constants.DefaultPageSize
|
||||||
}
|
}
|
||||||
|
|
||||||
params := &postgres.ListCardRechargeParams{
|
params := &postgres.ListAssetRechargeParams{
|
||||||
Page: page,
|
Page: page,
|
||||||
PageSize: pageSize,
|
PageSize: pageSize,
|
||||||
UserID: &userID, // 数据权限:只能查看自己的
|
UserID: &userID,
|
||||||
}
|
}
|
||||||
|
|
||||||
if req.Status != nil {
|
if req.Status != nil {
|
||||||
@@ -212,7 +212,7 @@ func (s *Service) List(ctx context.Context, req *dto.RechargeListRequest, userID
|
|||||||
}
|
}
|
||||||
if req.WalletID != nil {
|
if req.WalletID != nil {
|
||||||
walletID := *req.WalletID
|
walletID := *req.WalletID
|
||||||
params.CardWalletID = &walletID
|
params.AssetWalletID = &walletID
|
||||||
}
|
}
|
||||||
if req.StartTime != nil {
|
if req.StartTime != nil {
|
||||||
params.StartTime = req.StartTime
|
params.StartTime = req.StartTime
|
||||||
@@ -221,7 +221,7 @@ func (s *Service) List(ctx context.Context, req *dto.RechargeListRequest, userID
|
|||||||
params.EndTime = req.EndTime
|
params.EndTime = req.EndTime
|
||||||
}
|
}
|
||||||
|
|
||||||
recharges, total, err := s.cardRechargeStore.List(ctx, params)
|
recharges, total, err := s.assetRechargeStore.List(ctx, params)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.Wrap(errors.CodeDatabaseError, err, "查询充值订单列表失败")
|
return nil, errors.Wrap(errors.CodeDatabaseError, err, "查询充值订单列表失败")
|
||||||
}
|
}
|
||||||
@@ -249,7 +249,7 @@ func (s *Service) List(ctx context.Context, req *dto.RechargeListRequest, userID
|
|||||||
// 支持幂等性检查、事务处理、更新余额、触发佣金
|
// 支持幂等性检查、事务处理、更新余额、触发佣金
|
||||||
func (s *Service) HandlePaymentCallback(ctx context.Context, rechargeNo string, paymentMethod string, paymentTransactionID string) error {
|
func (s *Service) HandlePaymentCallback(ctx context.Context, rechargeNo string, paymentMethod string, paymentTransactionID string) error {
|
||||||
// 1. 查询充值订单
|
// 1. 查询充值订单
|
||||||
recharge, err := s.cardRechargeStore.GetByRechargeNo(ctx, rechargeNo)
|
recharge, err := s.assetRechargeStore.GetByRechargeNo(ctx, rechargeNo)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if err == gorm.ErrRecordNotFound {
|
if err == gorm.ErrRecordNotFound {
|
||||||
return errors.New(errors.CodeRechargeNotFound, "充值订单不存在")
|
return errors.New(errors.CodeRechargeNotFound, "充值订单不存在")
|
||||||
@@ -272,7 +272,7 @@ func (s *Service) HandlePaymentCallback(ctx context.Context, rechargeNo string,
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 4. 获取钱包信息
|
// 4. 获取钱包信息
|
||||||
wallet, err := s.cardWalletStore.GetByID(ctx, recharge.CardWalletID)
|
wallet, err := s.assetWalletStore.GetByID(ctx, recharge.AssetWalletID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.Wrap(errors.CodeDatabaseError, err, "查询钱包失败")
|
return errors.Wrap(errors.CodeDatabaseError, err, "查询钱包失败")
|
||||||
}
|
}
|
||||||
@@ -286,7 +286,7 @@ func (s *Service) HandlePaymentCallback(ctx context.Context, rechargeNo string,
|
|||||||
err = s.db.Transaction(func(tx *gorm.DB) error {
|
err = s.db.Transaction(func(tx *gorm.DB) error {
|
||||||
// 6.1 更新充值订单状态(带状态检查,实现乐观锁)
|
// 6.1 更新充值订单状态(带状态检查,实现乐观锁)
|
||||||
oldStatus := constants.RechargeStatusPending
|
oldStatus := constants.RechargeStatusPending
|
||||||
if err := s.cardRechargeStore.UpdateStatusWithOptimisticLock(ctx, recharge.ID, &oldStatus, constants.RechargeStatusPaid, &now, nil); err != nil {
|
if err := s.assetRechargeStore.UpdateStatusWithOptimisticLock(ctx, recharge.ID, &oldStatus, constants.RechargeStatusPaid, &now, nil); err != nil {
|
||||||
if err == gorm.ErrRecordNotFound {
|
if err == gorm.ErrRecordNotFound {
|
||||||
// 状态已变更,幂等处理
|
// 状态已变更,幂等处理
|
||||||
return nil
|
return nil
|
||||||
@@ -295,13 +295,13 @@ func (s *Service) HandlePaymentCallback(ctx context.Context, rechargeNo string,
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 6.2 更新支付信息
|
// 6.2 更新支付信息
|
||||||
if err := s.cardRechargeStore.UpdatePaymentInfo(ctx, recharge.ID, &paymentMethod, &paymentTransactionID); err != nil {
|
if err := s.assetRechargeStore.UpdatePaymentInfo(ctx, recharge.ID, &paymentMethod, &paymentTransactionID); err != nil {
|
||||||
return errors.Wrap(errors.CodeDatabaseError, err, "更新支付信息失败")
|
return errors.Wrap(errors.CodeDatabaseError, err, "更新支付信息失败")
|
||||||
}
|
}
|
||||||
|
|
||||||
// 6.3 增加钱包余额(使用乐观锁)
|
// 6.3 增加钱包余额(使用乐观锁)
|
||||||
balanceBefore := wallet.Balance
|
balanceBefore := wallet.Balance
|
||||||
result := tx.Model(&model.CardWallet{}).
|
result := tx.Model(&model.AssetWallet{}).
|
||||||
Where("id = ? AND version = ?", wallet.ID, wallet.Version).
|
Where("id = ? AND version = ?", wallet.ID, wallet.Version).
|
||||||
Updates(map[string]any{
|
Updates(map[string]any{
|
||||||
"balance": gorm.Expr("balance + ?", recharge.Amount),
|
"balance": gorm.Expr("balance + ?", recharge.Amount),
|
||||||
@@ -314,11 +314,11 @@ func (s *Service) HandlePaymentCallback(ctx context.Context, rechargeNo string,
|
|||||||
return errors.New(errors.CodeInternalError, "钱包版本冲突,请重试")
|
return errors.New(errors.CodeInternalError, "钱包版本冲突,请重试")
|
||||||
}
|
}
|
||||||
|
|
||||||
// 6.4 创建钱包交易记录
|
// 6.4 创建钱包交易记录(reference_no 存储充值单号,便于前端跳转)
|
||||||
remark := "钱包充值"
|
remark := "钱包充值"
|
||||||
refType := "recharge"
|
refType := "recharge"
|
||||||
transaction := &model.CardWalletTransaction{
|
transaction := &model.AssetWalletTransaction{
|
||||||
CardWalletID: wallet.ID,
|
AssetWalletID: wallet.ID,
|
||||||
ResourceType: resourceType,
|
ResourceType: resourceType,
|
||||||
ResourceID: resourceID,
|
ResourceID: resourceID,
|
||||||
UserID: recharge.UserID,
|
UserID: recharge.UserID,
|
||||||
@@ -328,7 +328,7 @@ func (s *Service) HandlePaymentCallback(ctx context.Context, rechargeNo string,
|
|||||||
BalanceAfter: balanceBefore + recharge.Amount,
|
BalanceAfter: balanceBefore + recharge.Amount,
|
||||||
Status: 1,
|
Status: 1,
|
||||||
ReferenceType: &refType,
|
ReferenceType: &refType,
|
||||||
ReferenceID: &recharge.ID,
|
ReferenceNo: &recharge.RechargeNo,
|
||||||
Remark: &remark,
|
Remark: &remark,
|
||||||
ShopIDTag: wallet.ShopIDTag,
|
ShopIDTag: wallet.ShopIDTag,
|
||||||
EnterpriseIDTag: wallet.EnterpriseIDTag,
|
EnterpriseIDTag: wallet.EnterpriseIDTag,
|
||||||
@@ -348,7 +348,7 @@ func (s *Service) HandlePaymentCallback(ctx context.Context, rechargeNo string,
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 6.7 更新充值订单状态为已完成
|
// 6.7 更新充值订单状态为已完成
|
||||||
if err := tx.Model(&model.CardRechargeRecord{}).
|
if err := tx.Model(&model.AssetRechargeRecord{}).
|
||||||
Where("id = ?", recharge.ID).
|
Where("id = ?", recharge.ID).
|
||||||
Update("status", constants.RechargeStatusCompleted).Error; err != nil {
|
Update("status", constants.RechargeStatusCompleted).Error; err != nil {
|
||||||
return errors.Wrap(errors.CodeDatabaseError, err, "更新充值订单完成状态失败")
|
return errors.Wrap(errors.CodeDatabaseError, err, "更新充值订单完成状态失败")
|
||||||
@@ -729,7 +729,7 @@ func (s *Service) generateRechargeNo() string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// buildRechargeResponse 构建充值订单响应
|
// buildRechargeResponse 构建充值订单响应
|
||||||
func (s *Service) buildRechargeResponse(recharge *model.CardRechargeRecord) *dto.RechargeResponse {
|
func (s *Service) buildRechargeResponse(recharge *model.AssetRechargeRecord) *dto.RechargeResponse {
|
||||||
statusText := ""
|
statusText := ""
|
||||||
switch recharge.Status {
|
switch recharge.Status {
|
||||||
case constants.RechargeStatusPending:
|
case constants.RechargeStatusPending:
|
||||||
@@ -748,7 +748,7 @@ func (s *Service) buildRechargeResponse(recharge *model.CardRechargeRecord) *dto
|
|||||||
ID: recharge.ID,
|
ID: recharge.ID,
|
||||||
RechargeNo: recharge.RechargeNo,
|
RechargeNo: recharge.RechargeNo,
|
||||||
UserID: recharge.UserID,
|
UserID: recharge.UserID,
|
||||||
WalletID: recharge.CardWalletID,
|
WalletID: recharge.AssetWalletID,
|
||||||
Amount: recharge.Amount,
|
Amount: recharge.Amount,
|
||||||
PaymentMethod: recharge.PaymentMethod,
|
PaymentMethod: recharge.PaymentMethod,
|
||||||
PaymentChannel: recharge.PaymentChannel,
|
PaymentChannel: recharge.PaymentChannel,
|
||||||
|
|||||||
Reference in New Issue
Block a user