import { Controller, Get, Post, Body, Param, Query } from '@nestjs/common'; import { ApiTags, ApiOperation, ApiResponse, ApiBearerAuth, ApiQuery } from '@nestjs/swagger'; import { PoolAccountService } from '../../application/services/pool-account.service'; import { AdminOnly } from '../../shared/guards/jwt-auth.guard'; import { PoolAccountType, TransactionType } from '@prisma/client'; import Decimal from 'decimal.js'; class InitializePoolsDto { sharePool: { name: string; initialBalance?: string }; blackHolePool: { name: string; targetBurn: string }; circulationPool: { name: string; initialBalance?: string }; } class BurnToBlackHoleDto { amount: string; fromPoolType: PoolAccountType; counterpartyAccountSeq?: string; counterpartyUserId?: string; referenceId?: string; } @ApiTags('Pool Accounts') @Controller('pool-accounts') @ApiBearerAuth() export class PoolAccountController { constructor(private readonly poolAccountService: PoolAccountService) {} @Get() @AdminOnly() @ApiOperation({ summary: '获取所有池账户' }) @ApiResponse({ status: 200, description: '池账户列表' }) async findAll() { return this.poolAccountService.findAll(); } @Get('stats') @AdminOnly() @ApiOperation({ summary: '获取池账户统计' }) async getStats() { return this.poolAccountService.getPoolStats(); } @Get(':type') @AdminOnly() @ApiOperation({ summary: '获取特定类型池账户' }) async findByType(@Param('type') type: PoolAccountType) { return this.poolAccountService.findByType(type); } @Get(':type/transactions') @AdminOnly() @ApiOperation({ summary: '获取池账户交易记录' }) @ApiQuery({ name: 'transactionType', required: false }) @ApiQuery({ name: 'startDate', required: false }) @ApiQuery({ name: 'endDate', required: false }) @ApiQuery({ name: 'limit', required: false, type: Number }) @ApiQuery({ name: 'offset', required: false, type: Number }) async getTransactions( @Param('type') type: PoolAccountType, @Query('transactionType') transactionType?: TransactionType, @Query('startDate') startDate?: string, @Query('endDate') endDate?: string, @Query('limit') limit?: number, @Query('offset') offset?: number, ) { return this.poolAccountService.getTransactions(type, { transactionType, startDate: startDate ? new Date(startDate) : undefined, endDate: endDate ? new Date(endDate) : undefined, limit: limit ? Number(limit) : undefined, offset: offset ? Number(offset) : undefined, }); } @Post('initialize') @AdminOnly() @ApiOperation({ summary: '初始化池账户' }) @ApiResponse({ status: 201, description: '池账户初始化成功' }) async initialize(@Body() dto: InitializePoolsDto) { return this.poolAccountService.initializePools({ sharePool: { name: dto.sharePool.name, initialBalance: dto.sharePool.initialBalance ? new Decimal(dto.sharePool.initialBalance) : undefined, }, blackHolePool: { name: dto.blackHolePool.name, targetBurn: new Decimal(dto.blackHolePool.targetBurn), }, circulationPool: { name: dto.circulationPool.name, initialBalance: dto.circulationPool.initialBalance ? new Decimal(dto.circulationPool.initialBalance) : undefined, }, }); } @Post('burn') @AdminOnly() @ApiOperation({ summary: '销毁到黑洞池' }) async burnToBlackHole(@Body() dto: BurnToBlackHoleDto) { return this.poolAccountService.burnToBlackHole(new Decimal(dto.amount), { fromPoolType: dto.fromPoolType, counterpartyAccountSeq: dto.counterpartyAccountSeq, counterpartyUserId: dto.counterpartyUserId, referenceId: dto.referenceId, }); } }