feat: 更新AI教练控制器,增加用户聊天次数管理功能
- 在AI教练控制器中引入用户聊天次数的检查,确保用户在进行对话前有足够的聊天次数。 - 新增用户服务方法以获取和扣减用户的聊天次数,优化用户体验。 - 调整默认免费聊天次数为5次,提升系统的使用限制管理。
This commit is contained in:
@@ -36,7 +36,7 @@ import { ActivityLogsService } from '../activity-logs/activity-logs.service';
|
||||
import { ActivityActionType, ActivityEntityType } from '../activity-logs/models/activity-log.model';
|
||||
import { CreateDietRecordDto, UpdateDietRecordDto, GetDietHistoryQueryDto, DietRecordResponseDto, DietHistoryResponseDto, NutritionSummaryDto } from './dto/diet-record.dto';
|
||||
|
||||
const DEFAULT_FREE_USAGE_COUNT = 10;
|
||||
const DEFAULT_FREE_USAGE_COUNT = 5;
|
||||
|
||||
@Injectable()
|
||||
export class UsersService {
|
||||
@@ -126,6 +126,49 @@ export class UsersService {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @desc 获取用户剩余的聊天次数
|
||||
*/
|
||||
async getUserUsageCount(userId: string): Promise<number> {
|
||||
try {
|
||||
const user = await this.userModel.findOne({ where: { id: userId } });
|
||||
|
||||
if (!user) {
|
||||
this.logger.log(`getUserUsageCount: ${userId} not found, return 0`);
|
||||
return 0
|
||||
}
|
||||
|
||||
if (user.isVip) {
|
||||
// 会员用户无限次
|
||||
this.logger.log(`getUserUsageCount: ${userId} is vip, return 999`);
|
||||
return 999
|
||||
}
|
||||
|
||||
this.logger.log(`getUserUsageCount: ${userId} freeUsageCount: ${user.freeUsageCount}`);
|
||||
|
||||
return user.freeUsageCount || 0;
|
||||
} catch (error) {
|
||||
this.logger.error(`getUserUsageCount error: ${error instanceof Error ? error.message : String(error)}`);
|
||||
return 0
|
||||
}
|
||||
}
|
||||
|
||||
// 扣减用户免费次数
|
||||
async deductUserUsageCount(userId: string, count: number = 1): Promise<void> {
|
||||
try {
|
||||
this.logger.log(`deductUserUsageCount: ${userId} deduct ${count} times`);
|
||||
const user = await this.userModel.findOne({ where: { id: userId } });
|
||||
if (!user) {
|
||||
throw new NotFoundException(`ID为${userId}的用户不存在`);
|
||||
}
|
||||
user.freeUsageCount -= count;
|
||||
await user.save();
|
||||
} catch (error) {
|
||||
this.logger.error(`deductUserUsageCount error: ${error instanceof Error ? error.message : String(error)}`);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// 更新用户昵称、头像
|
||||
async updateUser(updateUserDto: UpdateUserDto): Promise<UpdateUserResponseDto> {
|
||||
@@ -453,8 +496,8 @@ export class UsersService {
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取最近N顿饮食的营养汇总
|
||||
*/
|
||||
* 获取最近N顿饮食的营养汇总
|
||||
*/
|
||||
async getRecentNutritionSummary(userId: string, mealCount: number = 10): Promise<NutritionSummaryDto> {
|
||||
const records = await this.userDietHistoryModel.findAll({
|
||||
where: { userId, deleted: false },
|
||||
|
||||
Reference in New Issue
Block a user