MathRecSysService.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. <?php
  2. namespace App\Services;
  3. use Illuminate\Support\Facades\Http;
  4. use Illuminate\Support\Facades\Log;
  5. class MathRecSysService
  6. {
  7. private string $baseUrl;
  8. private int $timeout;
  9. private string $apiKey;
  10. public function __construct()
  11. {
  12. $this->baseUrl = config('services.mathrecsys.base_url', 'http://localhost:5010');
  13. $this->timeout = config('services.mathrecsys.timeout', 30);
  14. $this->apiKey = config('services.mathrecsys.api_key', '');
  15. }
  16. /**
  17. * 智能融合分析 - 核心智能分析引擎
  18. *
  19. * @param array $data 分析数据
  20. * @return array
  21. */
  22. public function smartAnalyze(array $data): array
  23. {
  24. try {
  25. Log::info('调用MathRecSys智能分析', ['data' => $data]);
  26. $response = Http::timeout($this->timeout)
  27. ->withHeaders([
  28. 'Content-Type' => 'application/json',
  29. 'Accept' => 'application/json',
  30. ])
  31. ->post($this->baseUrl . '/api/fusion/smart-analyze', $data);
  32. if ($response->failed()) {
  33. Log::error('MathRecSys智能分析失败', [
  34. 'status' => $response->status(),
  35. 'body' => $response->body()
  36. ]);
  37. throw new \Exception('智能分析失败: ' . $response->body());
  38. }
  39. $result = $response->json();
  40. Log::info('MathRecSys智能分析成功', ['result' => $result]);
  41. return $result;
  42. } catch (\Exception $e) {
  43. Log::error('MathRecSys智能分析异常', ['error' => $e->getMessage()]);
  44. throw $e;
  45. }
  46. }
  47. /**
  48. * 获取学生能力画像
  49. *
  50. * @param string $studentId 学生ID
  51. * @return array
  52. */
  53. public function getStudentProfile(string $studentId): array
  54. {
  55. try {
  56. Log::info('获取学生能力画像', ['student_id' => $studentId]);
  57. $response = Http::timeout($this->timeout)
  58. ->get($this->baseUrl . '/api/student/profile/' . $studentId);
  59. if ($response->failed()) {
  60. Log::error('获取学生画像失败', [
  61. 'status' => $response->status(),
  62. 'body' => $response->body()
  63. ]);
  64. throw new \Exception('获取学生画像失败');
  65. }
  66. $result = $response->json();
  67. Log::info('获取学生画像成功', ['result' => $result]);
  68. return $result;
  69. } catch (\Exception $e) {
  70. Log::error('获取学生画像异常', ['error' => $e->getMessage()]);
  71. throw $e;
  72. }
  73. }
  74. /**
  75. * 获取班级整体分析
  76. *
  77. * @param string $classId 班级ID
  78. * @return array
  79. */
  80. public function getClassAnalysis(string $classId): array
  81. {
  82. try {
  83. Log::info('获取班级整体分析', ['class_id' => $classId]);
  84. $response = Http::timeout($this->timeout)
  85. ->get($this->baseUrl . '/api/analysis/class/' . $classId);
  86. if ($response->failed()) {
  87. throw new \Exception('获取班级分析失败');
  88. }
  89. $result = $response->json();
  90. Log::info('获取班级分析成功', ['result' => $result]);
  91. return $result;
  92. } catch (\Exception $e) {
  93. Log::error('获取班级分析异常', ['error' => $e->getMessage()]);
  94. throw $e;
  95. }
  96. }
  97. /**
  98. * 获取知识图谱
  99. *
  100. * @param string|null $focus 焦点知识点
  101. * @return array
  102. */
  103. public function getKnowledgeGraph(?string $focus = null): array
  104. {
  105. try {
  106. $params = [];
  107. if ($focus) {
  108. $params['focus'] = $focus;
  109. }
  110. Log::info('获取知识图谱', $params);
  111. $response = Http::timeout($this->timeout)
  112. ->get($this->baseUrl . '/api/knowledge/graph', $params);
  113. if ($response->failed()) {
  114. throw new \Exception('获取知识图谱失败');
  115. }
  116. $result = $response->json();
  117. Log::info('获取知识图谱成功', ['nodes_count' => count($result['nodes'] ?? [])]);
  118. return $result;
  119. } catch (\Exception $e) {
  120. Log::error('获取知识图谱异常', ['error' => $e->getMessage()]);
  121. throw $e;
  122. }
  123. }
  124. /**
  125. * 获取知识点详情
  126. *
  127. * @param string $kpId 知识点ID
  128. * @return array
  129. */
  130. public function getKnowledgePoint(string $kpId): array
  131. {
  132. try {
  133. Log::info('获取知识点详情', ['kp_id' => $kpId]);
  134. $response = Http::timeout($this->timeout)
  135. ->get($this->baseUrl . '/api/knowledge/point/' . $kpId);
  136. if ($response->failed()) {
  137. throw new \Exception('获取知识点详情失败');
  138. }
  139. return $response->json();
  140. } catch (\Exception $e) {
  141. Log::error('获取知识点详情异常', ['error' => $e->getMessage()]);
  142. throw $e;
  143. }
  144. }
  145. /**
  146. * 获取题目推荐
  147. *
  148. * @param string $studentId 学生ID
  149. * @param array $options 选项
  150. * @return array
  151. */
  152. public function getRecommendations(string $studentId, array $options = []): array
  153. {
  154. try {
  155. $data = array_merge(['student_id' => $studentId], $options);
  156. Log::info('获取题目推荐', $data);
  157. $response = Http::timeout($this->timeout)
  158. ->post($this->baseUrl . '/api/recommend', $data);
  159. if ($response->failed()) {
  160. throw new \Exception('获取推荐失败');
  161. }
  162. $result = $response->json();
  163. Log::info('获取推荐成功', ['count' => count($result['data'] ?? [])]);
  164. return $result;
  165. } catch (\Exception $e) {
  166. Log::error('获取推荐异常', ['error' => $e->getMessage()]);
  167. throw $e;
  168. }
  169. }
  170. /**
  171. * 更新学生掌握度
  172. *
  173. * @param string $studentId 学生ID
  174. * @param array $masteryData 掌握度数据
  175. * @return array
  176. */
  177. public function updateMastery(string $studentId, array $masteryData): array
  178. {
  179. try {
  180. Log::info('更新学生掌握度', [
  181. 'student_id' => $studentId,
  182. 'data' => $masteryData
  183. ]);
  184. $response = Http::timeout($this->timeout)
  185. ->put($this->baseUrl . '/api/student/mastery/' . $studentId, $masteryData);
  186. if ($response->failed()) {
  187. throw new \Exception('更新掌握度失败');
  188. }
  189. return $response->json();
  190. } catch (\Exception $e) {
  191. Log::error('更新掌握度异常', ['error' => $e->getMessage()]);
  192. throw $e;
  193. }
  194. }
  195. /**
  196. * 获取学习轨迹
  197. *
  198. * @param string $studentId 学生ID
  199. * @param string|null $startDate 开始日期
  200. * @param string|null $endDate 结束日期
  201. * @return array
  202. */
  203. public function getLearningTrajectory(string $studentId, ?string $startDate = null, ?string $endDate = null): array
  204. {
  205. try {
  206. $params = ['student_id' => $studentId];
  207. if ($startDate) {
  208. $params['start_date'] = $startDate;
  209. }
  210. if ($endDate) {
  211. $params['end_date'] = $endDate;
  212. }
  213. Log::info('获取学习轨迹', $params);
  214. $response = Http::timeout($this->timeout)
  215. ->get($this->baseUrl . '/api/student/trajectory', $params);
  216. if ($response->failed()) {
  217. throw new \Exception('获取学习轨迹失败');
  218. }
  219. return $response->json();
  220. } catch (\Exception $e) {
  221. Log::error('获取学习轨迹异常', ['error' => $e->getMessage()]);
  222. throw $e;
  223. }
  224. }
  225. /**
  226. * 获取薄弱知识点
  227. *
  228. * @param string $studentId 学生ID
  229. * @return array
  230. */
  231. public function getWeakPoints(string $studentId): array
  232. {
  233. try {
  234. Log::info('获取薄弱知识点', ['student_id' => $studentId]);
  235. $response = Http::timeout($this->timeout)
  236. ->get($this->baseUrl . '/api/student/weak-points/' . $studentId);
  237. if ($response->failed()) {
  238. throw new \Exception('获取薄弱知识点失败');
  239. }
  240. return $response->json();
  241. } catch (\Exception $e) {
  242. Log::error('获取薄弱知识点异常', ['error' => $e->getMessage()]);
  243. throw $e;
  244. }
  245. }
  246. /**
  247. * 获取学习建议
  248. *
  249. * @param string $studentId 学生ID
  250. * @param array $options 选项
  251. * @return array
  252. */
  253. public function getLearningSuggestions(string $studentId, array $options = []): array
  254. {
  255. try {
  256. $data = array_merge(['student_id' => $studentId], $options);
  257. Log::info('获取学习建议', $data);
  258. $response = Http::timeout($this->timeout)
  259. ->post($this->baseUrl . '/api/suggestions', $data);
  260. if ($response->failed()) {
  261. throw new \Exception('获取学习建议失败');
  262. }
  263. return $response->json();
  264. } catch (\Exception $e) {
  265. Log::error('获取学习建议异常', ['error' => $e->getMessage()]);
  266. throw $e;
  267. }
  268. }
  269. /**
  270. * 检查服务健康状态
  271. *
  272. * @return bool
  273. */
  274. public function isHealthy(): bool
  275. {
  276. try {
  277. $response = Http::timeout(5)
  278. ->get($this->baseUrl . '/health');
  279. return $response->successful() && ($response->json()['status'] ?? '') === 'ok';
  280. } catch (\Exception $e) {
  281. Log::warning('MathRecSys服务健康检查失败', ['error' => $e->getMessage()]);
  282. return false;
  283. }
  284. }
  285. }