diff --git a/backend/services/trading-service/src/application/services/c2c.service.ts b/backend/services/trading-service/src/application/services/c2c.service.ts index 4f0f69ef..3f3516fa 100644 --- a/backend/services/trading-service/src/application/services/c2c.service.ts +++ b/backend/services/trading-service/src/application/services/c2c.service.ts @@ -7,6 +7,7 @@ import { RedisService } from '../../infrastructure/redis/redis.service'; import { PrismaService } from '../../infrastructure/persistence/prisma/prisma.service'; import { IdentityClient } from '../../infrastructure/identity/identity.client'; import { Money } from '../../domain/value-objects/money.vo'; +import { TradingAccountAggregate } from '../../domain/aggregates/trading-account.aggregate'; import Decimal from 'decimal.js'; // C2C 订单类型常量 @@ -132,10 +133,16 @@ export class C2cService { // 计算总金额 const totalAmount = priceDecimal.mul(quantityDecimal); - // 获取用户交易账户 - const account = await this.tradingAccountRepository.findByAccountSequence(accountSequence); + // 获取用户交易账户,不存在则自动创建零余额账户 + let account = await this.tradingAccountRepository.findByAccountSequence(accountSequence); if (!account) { - throw new NotFoundException('交易账户不存在'); + this.logger.log(`C2C: 用户 ${accountSequence} 无交易账户,自动创建`); + const newAccount = TradingAccountAggregate.create(accountSequence); + await this.tradingAccountRepository.save(newAccount); + account = await this.tradingAccountRepository.findByAccountSequence(accountSequence); + if (!account) { + throw new NotFoundException('交易账户创建失败'); + } } // 检查余额并冻结资产 @@ -232,10 +239,16 @@ export class C2cService { } } - // 获取接单方账户 - const takerAccount = await this.tradingAccountRepository.findByAccountSequence(takerAccountSequence); + // 获取接单方账户,不存在则自动创建零余额账户 + let takerAccount = await this.tradingAccountRepository.findByAccountSequence(takerAccountSequence); if (!takerAccount) { - throw new NotFoundException('交易账户不存在'); + this.logger.log(`C2C: 接单方 ${takerAccountSequence} 无交易账户,自动创建`); + const newAccount = TradingAccountAggregate.create(takerAccountSequence); + await this.tradingAccountRepository.save(newAccount); + takerAccount = await this.tradingAccountRepository.findByAccountSequence(takerAccountSequence); + if (!takerAccount) { + throw new NotFoundException('交易账户创建失败'); + } } // #13: 支持部分成交,taker可指定数量