import { Controller, Get, Logger, Param, Query } from '@nestjs/common'; import { ApiOperation, ApiResponse, ApiTags, ApiParam, ApiQuery, } from '@nestjs/swagger'; import { RecommendationService } from './recommendation.service'; import { YouMayAlsoLikeVideoResponseDto, YouMayAlsoLikeAdResponseDto, } from './dto/enriched-recommendation.dto'; import { AdType as PrismaAdType } from '@prisma/mongo/client'; @ApiTags('推荐') @Controller('recommend') export class RecommendPublicController { private readonly logger = new Logger(RecommendPublicController.name); constructor(private readonly recommendationService: RecommendationService) {} @Get('video/:videoId') @ApiOperation({ summary: '获取视频推荐 - 猜你喜欢', description: '基于当前视频的标签和Redis分数,返回带有完整元数据的相似视频推荐列表。仅返回上架状态的视频。', }) @ApiParam({ name: 'videoId', description: '当前视频ID', example: '6756abc123def', }) @ApiQuery({ name: 'limit', required: false, description: '返回推荐数量(默认6)', example: 6, }) @ApiResponse({ status: 200, description: '成功返回推荐列表', type: YouMayAlsoLikeVideoResponseDto, }) async getVideoRecommendations( @Param('videoId') videoId: string, @Query('limit') limit?: string, ): Promise { const limitNum = limit ? parseInt(limit, 10) : 6; this.logger.debug( `GET /api/v1/recommend/video/${videoId}?limit=${limitNum}`, ); const recommendations = await this.recommendationService.getEnrichedVideoRecommendations( videoId, limitNum, ); return { currentVideoId: videoId, recommendations, count: recommendations.length, }; } @Get('ad/:adId') @ApiOperation({ summary: '获取广告推荐 - 猜你喜欢', description: '基于渠道和广告模块过滤,返回带有完整元数据的相似广告推荐列表。仅返回同渠道、同模块、状态启用且在有效期内的广告。', }) @ApiParam({ name: 'adId', description: '当前广告ID', example: '6756def456ghi', }) // @ApiQuery({ // name: 'channelId', // required: true, // description: '渠道ID(必须匹配)', // example: '6756channel123', // }) @ApiQuery({ name: 'adType', required: true, enum: PrismaAdType, description: '广告类型/模块 (AdType enum)', }) @ApiQuery({ name: 'limit', required: false, description: '返回推荐数量(默认3)', example: 3, }) @ApiResponse({ status: 200, description: '成功返回推荐列表', type: YouMayAlsoLikeAdResponseDto, }) async getAdRecommendations( @Param('adId') adId: string, @Query('adType') adType: PrismaAdType, @Query('limit') limit?: string, ): Promise { const limitNum = limit ? parseInt(limit, 10) : 3; this.logger.debug( `GET /api/v1/recommend/ad/${adId}?adType=${adType}&limit=${limitNum}`, ); const recommendations = await this.recommendationService.getEnrichedAdRecommendations(adId, { adType, limit: limitNum, }); return { currentAdId: adId, recommendations, count: recommendations.length, }; } }