| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345 |
- <?php
- namespace App\Http\Controllers\Api;
- use App\Http\Controllers\Controller;
- use App\Services\MathRecSysService;
- use Illuminate\Http\Request;
- use Illuminate\Http\JsonResponse;
- class StudentController extends Controller
- {
- protected MathRecSysService $mathRecSys;
- public function __construct(MathRecSysService $mathRecSys)
- {
- $this->mathRecSys = $mathRecSys;
- }
- /**
- * 获取学生完整信息(集成MathRecSys数据)
- *
- * @param string $studentId 学生ID
- * @return JsonResponse
- */
- public function show(string $studentId): JsonResponse
- {
- try {
- // 获取Laravel本地数据
- $localStudent = $this->getLocalStudent($studentId);
- // 获取MathRecSys能力画像
- $mathRecSysProfile = $this->mathRecSys->getStudentProfile($studentId);
- // 获取薄弱知识点
- $weakPoints = $this->mathRecSys->getWeakPoints($studentId);
- // 合并数据
- $studentData = [
- 'basic_info' => $localStudent,
- 'ability_profile' => $mathRecSysProfile['data'] ?? [],
- 'mastery_levels' => $this->formatMasteryLevels($mathRecSysProfile),
- 'weak_points' => $weakPoints['data'] ?? [],
- 'learning_progress' => $mathRecSysProfile['progress'] ?? [],
- 'recommendations' => $this->getStudentRecommendations($studentId),
- 'last_updated' => now()->toISOString()
- ];
- return response()->json([
- 'success' => true,
- 'data' => $studentData
- ]);
- } catch (\Exception $e) {
- \Log::error('获取学生信息失败', [
- 'student_id' => $studentId,
- 'error' => $e->getMessage()
- ]);
- return response()->json([
- 'success' => false,
- 'message' => '获取学生信息失败: ' . $e->getMessage()
- ], 500);
- }
- }
- /**
- * 获取班级整体分析
- *
- * @param string $classId 班级ID
- * @return JsonResponse
- */
- public function classAnalysis(string $classId): JsonResponse
- {
- try {
- $analysis = $this->mathRecSys->getClassAnalysis($classId);
- return response()->json([
- 'success' => true,
- 'data' => $analysis['data'] ?? []
- ]);
- } catch (\Exception $e) {
- \Log::error('获取班级分析失败', [
- 'class_id' => $classId,
- 'error' => $e->getMessage()
- ]);
- return response()->json([
- 'success' => false,
- 'message' => '获取班级分析失败'
- ], 500);
- }
- }
- /**
- * 获取学生个性化推荐
- *
- * @param string $studentId 学生ID
- * @param Request $request
- * @return JsonResponse
- */
- public function getRecommendations(string $studentId, Request $request): JsonResponse
- {
- try {
- $options = $request->only(['count', 'difficulty', 'focus_kp']);
- $recommendations = $this->mathRecSys->getRecommendations($studentId, $options);
- return response()->json([
- 'success' => true,
- 'data' => $recommendations['data'] ?? []
- ]);
- } catch (\Exception $e) {
- \Log::error('获取推荐失败', [
- 'student_id' => $studentId,
- 'error' => $e->getMessage()
- ]);
- return response()->json([
- 'success' => false,
- 'message' => '获取推荐失败'
- ], 500);
- }
- }
- /**
- * 智能分析题目
- *
- * @param Request $request
- * @param string $studentId 学生ID
- * @return JsonResponse
- */
- public function analyzeQuestion(Request $request, string $studentId): JsonResponse
- {
- $request->validate([
- 'question' => 'required|string',
- 'student_answer' => 'required|string',
- 'correct_answer' => 'required|string',
- 'focus_kp' => 'nullable|string',
- 'question_type' => 'nullable|string',
- 'difficulty' => 'nullable|numeric|min:0|max:1',
- ]);
- try {
- $analysisData = [
- 'student_id' => $studentId,
- 'question' => $request->question,
- 'student_answer' => $request->student_answer,
- 'correct_answer' => $request->correct_answer,
- 'focus_kp' => $request->focus_kp,
- 'question_type' => $request->question_type ?? '计算题',
- 'difficulty' => $request->difficulty ?? 0.5,
- ];
- $analysis = $this->mathRecSys->smartAnalyze($analysisData);
- return response()->json([
- 'success' => true,
- 'data' => $analysis['data'] ?? []
- ]);
- } catch (\Exception $e) {
- \Log::error('题目分析失败', [
- 'student_id' => $studentId,
- 'error' => $e->getMessage()
- ]);
- return response()->json([
- 'success' => false,
- 'message' => '题目分析失败'
- ], 500);
- }
- }
- /**
- * 获取学习轨迹
- *
- * @param string $studentId 学生ID
- * @param Request $request
- * @return JsonResponse
- */
- public function getTrajectory(string $studentId, Request $request): JsonResponse
- {
- try {
- $startDate = $request->input('start_date');
- $endDate = $request->input('end_date');
- $trajectory = $this->mathRecSys->getLearningTrajectory($studentId, $startDate, $endDate);
- return response()->json([
- 'success' => true,
- 'data' => $trajectory['data'] ?? []
- ]);
- } catch (\Exception $e) {
- \Log::error('获取学习轨迹失败', [
- 'student_id' => $studentId,
- 'error' => $e->getMessage()
- ]);
- return response()->json([
- 'success' => false,
- 'message' => '获取学习轨迹失败'
- ], 500);
- }
- }
- /**
- * 获取学习建议
- *
- * @param string $studentId 学生ID
- * @return JsonResponse
- */
- public function getSuggestions(string $studentId): JsonResponse
- {
- try {
- $suggestions = $this->mathRecSys->getLearningSuggestions($studentId);
- return response()->json([
- 'success' => true,
- 'data' => $suggestions['data'] ?? []
- ]);
- } catch (\Exception $e) {
- \Log::error('获取学习建议失败', [
- 'student_id' => $studentId,
- 'error' => $e->getMessage()
- ]);
- return response()->json([
- 'success' => false,
- 'message' => '获取学习建议失败'
- ], 500);
- }
- }
- /**
- * 更新学生掌握度
- *
- * @param Request $request
- * @param string $studentId 学生ID
- * @return JsonResponse
- */
- public function updateMastery(Request $request, string $studentId): JsonResponse
- {
- $request->validate([
- 'mastery_data' => 'required|array',
- 'mastery_data.*.kp' => 'required|string',
- 'mastery_data.*.level' => 'required|numeric|min:0|max:1',
- ]);
- try {
- $result = $this->mathRecSys->updateMastery($studentId, $request->mastery_data);
- return response()->json([
- 'success' => true,
- 'data' => $result['data'] ?? []
- ]);
- } catch (\Exception $e) {
- \Log::error('更新掌握度失败', [
- 'student_id' => $studentId,
- 'error' => $e->getMessage()
- ]);
- return response()->json([
- 'success' => false,
- 'message' => '更新掌握度失败'
- ], 500);
- }
- }
- /**
- * 检查MathRecSys服务状态
- *
- * @return JsonResponse
- */
- public function checkServiceHealth(): JsonResponse
- {
- $isHealthy = $this->mathRecSys->isHealthy();
- return response()->json([
- 'success' => true,
- 'healthy' => $isHealthy,
- 'timestamp' => now()->toISOString()
- ]);
- }
- /**
- * 从本地数据库获取学生基本信息
- *
- * @param string $studentId
- * @return array
- */
- private function getLocalStudent(string $studentId): array
- {
- // 这里应该从Laravel数据库查询学生信息
- // 示例返回数据,实际应该查询数据库
- return [
- 'id' => $studentId,
- 'name' => '学生_' . substr($studentId, -3),
- 'class' => '五年级一班',
- 'grade' => '五年级',
- 'created_at' => now()->subMonths(6)->toISOString(),
- ];
- }
- /**
- * 格式化掌握度数据
- *
- * @param array $profile
- * @return array
- */
- private function formatMasteryLevels(array $profile): array
- {
- $masteryData = $profile['data']['mastery'] ?? $profile['mastery'] ?? [];
- return array_map(function ($item) {
- return [
- 'kp' => $item['kp'] ?? $item['kp_id'] ?? '',
- 'level' => floatval($item['level'] ?? $item['mastery_level'] ?? 0),
- 'practice_count' => $item['practice_count'] ?? 0,
- 'success_rate' => floatval($item['success_rate'] ?? 0),
- 'last_practice' => $item['last_practice'] ?? null
- ];
- }, $masteryData);
- }
- /**
- * 获取推荐(内部方法)
- *
- * @param string $studentId
- * @return array
- */
- private function getStudentRecommendations(string $studentId): array
- {
- try {
- $recommendations = $this->mathRecSys->getRecommendations($studentId, ['count' => 5]);
- return $recommendations['data'] ?? [];
- } catch (\Exception $e) {
- \Log::warning('获取推荐失败', ['error' => $e->getMessage()]);
- return [];
- }
- }
- }
|