直接换货流程
All checks were successful
构建并部署到测试环境(无 SSH) / build-and-deploy (push) Successful in 8m28s

This commit is contained in:
Break
2026-06-03 16:55:32 +08:00
parent 46ede81aef
commit 5f57429fb0
13 changed files with 718 additions and 167 deletions

View File

@@ -12,67 +12,20 @@ import (
"gorm.io/gorm/clause"
)
func (s *Service) executeMigration(ctx context.Context, order *model.ExchangeOrder) (int64, error) {
var migrationBalance int64
err := s.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
if order.NewAssetID == nil || *order.NewAssetID == 0 {
return errors.New(errors.CodeInvalidParam, "新资产信息缺失")
}
oldAsset, err := s.resolveAssetByIdentifier(ctx, order.OldAssetType, order.OldAssetIdentifier)
if err != nil {
return err
}
newAsset, err := s.resolveAssetByIdentifier(ctx, order.OldAssetType, order.NewAssetIdentifier)
if err != nil {
return err
}
migrationBalance, err = s.transferWalletBalanceWithTx(ctx, tx, order, oldAsset, newAsset)
if err != nil {
return err
}
if err = s.migratePackageUsageWithTx(ctx, tx, oldAsset, newAsset); err != nil {
return err
}
if err = s.copyAccumulatedFieldsWithTx(tx, oldAsset, newAsset); err != nil {
return err
}
if err = s.copyResourceTagsWithTx(ctx, tx, oldAsset, newAsset); err != nil {
return err
}
if oldAsset.VirtualNo != "" && newAsset.VirtualNo != "" {
if err = tx.Model(&model.PersonalCustomerDevice{}).
Where("virtual_no = ?", oldAsset.VirtualNo).
Updates(map[string]any{"virtual_no": newAsset.VirtualNo, "updated_at": time.Now()}).Error; err != nil {
return errors.Wrap(errors.CodeDatabaseError, err, "更新客户绑定关系失败")
}
}
if err = s.updateOldAssetStatusWithTx(tx, oldAsset); err != nil {
return err
}
if err = tx.Model(&model.ExchangeOrder{}).Where("id = ?", order.ID).Updates(map[string]any{
"migration_completed": true,
"migration_balance": migrationBalance,
"updater": middleware.GetUserIDFromContext(ctx),
"updated_at": time.Now(),
}).Error; err != nil {
return errors.Wrap(errors.CodeDatabaseError, err, "更新换货单迁移状态失败")
}
return nil
})
func (s *Service) executeMigrationWithTx(ctx context.Context, tx *gorm.DB, order *model.ExchangeOrder, oldAsset, newAsset *resolvedExchangeAsset) (int64, error) {
migrationBalance, err := s.transferWalletBalanceWithTx(ctx, tx, order, oldAsset, newAsset)
if err != nil {
return 0, errors.Wrap(errors.CodeExchangeMigrationFailed, err, "执行全量迁移失败")
return 0, errors.Wrap(errors.CodeExchangeMigrationFailed, err, "执行钱包迁移失败")
}
if err = s.migratePackageUsageWithTx(ctx, tx, oldAsset, newAsset); err != nil {
return 0, errors.Wrap(errors.CodeExchangeMigrationFailed, err, "迁移套餐使用记录失败")
}
if err = s.copyAccumulatedFieldsWithTx(tx, oldAsset, newAsset); err != nil {
return 0, errors.Wrap(errors.CodeExchangeMigrationFailed, err, "复制累计充值字段失败")
}
if err = s.copyResourceTagsWithTx(ctx, tx, oldAsset, newAsset); err != nil {
return 0, errors.Wrap(errors.CodeExchangeMigrationFailed, err, "复制资产标签失败")
}
return migrationBalance, nil
}
@@ -83,6 +36,9 @@ func (s *Service) transferWalletBalanceWithTx(ctx context.Context, tx *gorm.DB,
return 0, errors.Wrap(errors.CodeDatabaseError, err, "查询旧资产钱包失败")
}
}
if oldWallet.ID > 0 && oldWallet.FrozenBalance > 0 {
return 0, errors.New(errors.CodeExchangeMigrationFailed, "旧资产钱包存在冻结余额,请先处理冻结业务")
}
var newWallet model.AssetWallet
if err := tx.WithContext(ctx).Where("resource_type = ? AND resource_id = ?", newAsset.AssetType, newAsset.AssetID).First(&newWallet).Error; err != nil {
@@ -94,7 +50,7 @@ func (s *Service) transferWalletBalanceWithTx(ctx context.Context, tx *gorm.DB,
if newAsset.ShopID != nil {
shopTag = *newAsset.ShopID
}
newWallet = model.AssetWallet{ResourceType: newAsset.AssetType, ResourceID: newAsset.AssetID, Balance: 0, FrozenBalance: 0, Currency: "CNY", Status: 1, Version: 0, ShopIDTag: shopTag}
newWallet = model.AssetWallet{ResourceType: newAsset.AssetType, ResourceID: newAsset.AssetID, Balance: 0, FrozenBalance: 0, Currency: "CNY", Status: constants.AssetWalletStatusNormal, Version: 0, ShopIDTag: shopTag}
if err = tx.WithContext(ctx).Create(&newWallet).Error; err != nil {
return 0, errors.Wrap(errors.CodeDatabaseError, err, "创建新资产钱包失败")
}
@@ -114,19 +70,21 @@ func (s *Service) transferWalletBalanceWithTx(ctx context.Context, tx *gorm.DB,
return 0, errors.Wrap(errors.CodeDatabaseError, err, "增加新资产钱包余额失败")
}
refType := "exchange"
refType := constants.ReferenceTypeExchange
remark := "换货余额迁移"
if err := tx.WithContext(ctx).Create(&model.AssetWalletTransaction{
AssetWalletID: newWallet.ID,
ResourceType: newAsset.AssetType,
ResourceID: newAsset.AssetID,
UserID: middleware.GetUserIDFromContext(ctx),
TransactionType: "refund",
TransactionType: constants.AssetTransactionTypeExchange,
Amount: migrationBalance,
BalanceBefore: beforeBalance,
BalanceAfter: beforeBalance + migrationBalance,
Status: 1,
Status: constants.TransactionStatusSuccess,
ReferenceType: &refType,
ReferenceNo: &order.ExchangeNo,
Remark: &remark,
Creator: middleware.GetUserIDFromContext(ctx),
ShopIDTag: newWallet.ShopIDTag,
EnterpriseIDTag: newWallet.EnterpriseIDTag,
@@ -224,16 +182,3 @@ func (s *Service) copyResourceTagsWithTx(ctx context.Context, tx *gorm.DB, oldAs
}
return nil
}
func (s *Service) updateOldAssetStatusWithTx(tx *gorm.DB, oldAsset *resolvedExchangeAsset) error {
if oldAsset.AssetType == constants.ExchangeAssetTypeIotCard {
if err := tx.Model(&model.IotCard{}).Where("id = ?", oldAsset.AssetID).Updates(map[string]any{"asset_status": 3, "updated_at": time.Now()}).Error; err != nil {
return errors.Wrap(errors.CodeDatabaseError, err, "更新旧卡状态失败")
}
return nil
}
if err := tx.Model(&model.Device{}).Where("id = ?", oldAsset.AssetID).Updates(map[string]any{"asset_status": 3, "updated_at": time.Now()}).Error; err != nil {
return errors.Wrap(errors.CodeDatabaseError, err, "更新旧设备状态失败")
}
return nil
}