feat: 更新AI教练控制器,增加用户聊天次数管理功能
- 在AI教练控制器中引入用户聊天次数的检查,确保用户在进行对话前有足够的聊天次数。 - 新增用户服务方法以获取和扣减用户的聊天次数,优化用户体验。 - 调整默认免费聊天次数为5次,提升系统的使用限制管理。
This commit is contained in:
@@ -317,99 +317,4 @@ export class UsersController {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取营养汇总分析
|
||||
*/
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@Get('nutrition-summary')
|
||||
@HttpCode(HttpStatus.OK)
|
||||
@ApiOperation({ summary: '获取最近饮食的营养汇总分析' })
|
||||
@ApiQuery({ name: 'mealCount', required: false, description: '分析最近几顿饮食,默认10顿' })
|
||||
@ApiResponse({ status: 200, description: '成功获取营养汇总', type: DietAnalysisResponseDto })
|
||||
async getNutritionSummary(
|
||||
@Query('mealCount') mealCount: string = '10',
|
||||
@CurrentUser() user: AccessTokenPayload,
|
||||
): Promise<DietAnalysisResponseDto> {
|
||||
this.logger.log(`获取营养汇总 - 用户ID: ${user.sub}, 分析${mealCount}顿饮食`);
|
||||
|
||||
const count = Math.min(20, Math.max(1, parseInt(mealCount) || 10));
|
||||
const nutritionSummary = await this.usersService.getRecentNutritionSummary(user.sub, count);
|
||||
|
||||
// 获取最近的饮食记录用于分析
|
||||
const recentRecords = await this.usersService.getDietHistory(user.sub, { limit: count });
|
||||
|
||||
// 简单的营养评分算法(可以后续优化)
|
||||
const nutritionScore = this.calculateNutritionScore(nutritionSummary);
|
||||
|
||||
// 生成基础建议(后续可以接入AI分析)
|
||||
const recommendations = this.generateBasicRecommendations(nutritionSummary);
|
||||
|
||||
return {
|
||||
nutritionSummary,
|
||||
recentRecords: recentRecords.records,
|
||||
healthAnalysis: '基于您最近的饮食记录,我将为您提供个性化的营养分析和健康建议。',
|
||||
nutritionScore,
|
||||
recommendations,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* 简单的营养评分算法
|
||||
*/
|
||||
private calculateNutritionScore(summary: any): number {
|
||||
let score = 50; // 基础分数
|
||||
|
||||
// 基于热量是否合理调整分数
|
||||
const dailyCalories = summary.totalCalories / (summary.recordCount / 3); // 假设一天3餐
|
||||
if (dailyCalories >= 1500 && dailyCalories <= 2500) score += 20;
|
||||
else if (dailyCalories < 1200 || dailyCalories > 3000) score -= 20;
|
||||
|
||||
// 基于蛋白质摄入调整分数
|
||||
const dailyProtein = summary.totalProtein / (summary.recordCount / 3);
|
||||
if (dailyProtein >= 50 && dailyProtein <= 150) score += 15;
|
||||
else if (dailyProtein < 30) score -= 15;
|
||||
|
||||
// 基于膳食纤维调整分数
|
||||
const dailyFiber = summary.totalFiber / (summary.recordCount / 3);
|
||||
if (dailyFiber >= 25) score += 15;
|
||||
else if (dailyFiber < 10) score -= 10;
|
||||
|
||||
return Math.max(0, Math.min(100, score));
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成基础营养建议
|
||||
*/
|
||||
private generateBasicRecommendations(summary: any): string[] {
|
||||
const recommendations: string[] = [];
|
||||
|
||||
const dailyCalories = summary.totalCalories / (summary.recordCount / 3);
|
||||
const dailyProtein = summary.totalProtein / (summary.recordCount / 3);
|
||||
const dailyFiber = summary.totalFiber / (summary.recordCount / 3);
|
||||
const dailySodium = summary.totalSodium / (summary.recordCount / 3);
|
||||
|
||||
if (dailyCalories < 1200) {
|
||||
recommendations.push('您的日均热量摄入偏低,建议适当增加营养密度高的食物。');
|
||||
} else if (dailyCalories > 2500) {
|
||||
recommendations.push('您的日均热量摄入偏高,建议控制portion size或选择低热量食物。');
|
||||
}
|
||||
|
||||
if (dailyProtein < 50) {
|
||||
recommendations.push('建议增加优质蛋白质摄入,如鸡胸肉、鱼类、豆制品等。');
|
||||
}
|
||||
|
||||
if (dailyFiber < 25) {
|
||||
recommendations.push('建议增加膳食纤维摄入,多吃蔬菜、水果和全谷物。');
|
||||
}
|
||||
|
||||
if (dailySodium > 2000) {
|
||||
recommendations.push('钠摄入偏高,建议减少加工食品和调味料的使用。');
|
||||
}
|
||||
|
||||
if (recommendations.length === 0) {
|
||||
recommendations.push('您的饮食结构相对均衡,继续保持良好的饮食习惯!');
|
||||
}
|
||||
|
||||
return recommendations;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user