MistakeBookService.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. <?php
  2. namespace App\Services;
  3. use Illuminate\Support\Arr;
  4. use Illuminate\Support\Facades\Http;
  5. use Illuminate\Support\Facades\Log;
  6. class MistakeBookService
  7. {
  8. protected string $learningAnalyticsBase;
  9. protected string $questionBankBase;
  10. protected int $timeout;
  11. public function __construct(
  12. ?string $learningAnalyticsBase = null,
  13. ?string $questionBankBase = null,
  14. ?int $timeout = null
  15. ) {
  16. $this->learningAnalyticsBase = rtrim(
  17. $learningAnalyticsBase
  18. ?: config('services.learning_analytics.url', env('LEARNING_ANALYTICS_API_BASE', 'http://localhost:5016')),
  19. '/'
  20. );
  21. $this->questionBankBase = rtrim(
  22. $questionBankBase
  23. ?: config('services.question_bank.base_url', env('QUESTION_BANK_API_BASE', 'http://localhost:5015')),
  24. '/'
  25. );
  26. $this->timeout = $timeout ?? (int) config('services.learning_analytics.timeout', 20);
  27. }
  28. /**
  29. * 获取错题列表(支持多维筛选)
  30. */
  31. public function listMistakes(array $params = []): array
  32. {
  33. $query = array_filter([
  34. 'student_id' => $params['student_id'] ?? null,
  35. 'kp_ids' => $this->implodeIfArray($params['kp_ids'] ?? null),
  36. 'skill_ids' => $this->implodeIfArray($params['skill_ids'] ?? null),
  37. 'error_types' => $this->implodeIfArray($params['error_types'] ?? null),
  38. 'time_range' => $params['time_range'] ?? null,
  39. 'start_date' => $params['start_date'] ?? null,
  40. 'end_date' => $params['end_date'] ?? null,
  41. 'page' => $params['page'] ?? 1,
  42. 'per_page' => $params['per_page'] ?? 20,
  43. ], fn ($value) => filled($value));
  44. try {
  45. $response = Http::timeout($this->timeout)
  46. ->get($this->learningAnalyticsBase . '/api/mistake-book', $query);
  47. if ($response->successful()) {
  48. $body = $response->json();
  49. return is_array($body) ? $body : ['data' => $body];
  50. }
  51. Log::warning('MistakeBook list failed', [
  52. 'status' => $response->status(),
  53. 'body' => $response->body(),
  54. 'query' => $query,
  55. ]);
  56. } catch (\Throwable $e) {
  57. Log::error('MistakeBook list exception', [
  58. 'error' => $e->getMessage(),
  59. 'query' => $query,
  60. ]);
  61. }
  62. return [
  63. 'data' => [],
  64. 'meta' => ['total' => 0, 'page' => 1, 'per_page' => $query['per_page'] ?? 20],
  65. ];
  66. }
  67. /**
  68. * 获取错题统计概要
  69. */
  70. public function summarize(string $studentId): array
  71. {
  72. try {
  73. $response = Http::timeout($this->timeout)
  74. ->get($this->learningAnalyticsBase . '/api/mistake-book/summary', [
  75. 'student_id' => $studentId,
  76. ]);
  77. if ($response->successful()) {
  78. $body = $response->json();
  79. return is_array($body) ? $body : [];
  80. }
  81. Log::warning('MistakeBook summary failed', [
  82. 'status' => $response->status(),
  83. 'body' => $response->body(),
  84. 'student_id' => $studentId,
  85. ]);
  86. } catch (\Throwable $e) {
  87. Log::error('MistakeBook summary exception', [
  88. 'student_id' => $studentId,
  89. 'error' => $e->getMessage(),
  90. ]);
  91. }
  92. // fallback 返回一个空结构,避免前端崩溃
  93. return [
  94. 'total' => 0,
  95. 'this_week' => 0,
  96. 'pending_review' => 0,
  97. 'mastery_rate' => null,
  98. ];
  99. }
  100. /**
  101. * 获取错误模式与推荐路径
  102. */
  103. public function getMistakePatterns(string $studentId): array
  104. {
  105. try {
  106. $response = Http::timeout($this->timeout)
  107. ->get($this->learningAnalyticsBase . '/api/analytics/mistake-pattern', [
  108. 'student_id' => $studentId,
  109. ]);
  110. if ($response->successful()) {
  111. $body = $response->json();
  112. return is_array($body) ? $body : [];
  113. }
  114. Log::warning('Mistake pattern request failed', [
  115. 'status' => $response->status(),
  116. 'body' => $response->body(),
  117. 'student_id' => $studentId,
  118. ]);
  119. } catch (\Throwable $e) {
  120. Log::error('Mistake pattern request exception', [
  121. 'student_id' => $studentId,
  122. 'error' => $e->getMessage(),
  123. ]);
  124. }
  125. return [];
  126. }
  127. /**
  128. * 收藏/取消收藏错题
  129. */
  130. public function toggleFavorite(string $mistakeId, bool $favorite = true): bool
  131. {
  132. try {
  133. $response = Http::timeout($this->timeout)
  134. ->post($this->learningAnalyticsBase . '/api/mistake-book/' . $mistakeId . '/favorite', [
  135. 'favorite' => $favorite,
  136. ]);
  137. return $response->successful();
  138. } catch (\Throwable $e) {
  139. Log::error('Toggle favorite failed', [
  140. 'mistake_id' => $mistakeId,
  141. 'favorite' => $favorite,
  142. 'error' => $e->getMessage(),
  143. ]);
  144. }
  145. return false;
  146. }
  147. /**
  148. * 标记已复习
  149. */
  150. public function markReviewed(string $mistakeId): bool
  151. {
  152. try {
  153. $response = Http::timeout($this->timeout)
  154. ->post($this->learningAnalyticsBase . '/api/mistake-book/' . $mistakeId . '/review');
  155. return $response->successful();
  156. } catch (\Throwable $e) {
  157. Log::error('Mark reviewed failed', [
  158. 'mistake_id' => $mistakeId,
  159. 'error' => $e->getMessage(),
  160. ]);
  161. }
  162. return false;
  163. }
  164. /**
  165. * 添加到重练清单
  166. */
  167. public function addToRetryList(string $mistakeId): bool
  168. {
  169. try {
  170. $response = Http::timeout($this->timeout)
  171. ->post($this->learningAnalyticsBase . '/api/mistake-book/' . $mistakeId . '/retry-list');
  172. return $response->successful();
  173. } catch (\Throwable $e) {
  174. Log::error('Add to retry list failed', [
  175. 'mistake_id' => $mistakeId,
  176. 'error' => $e->getMessage(),
  177. ]);
  178. }
  179. return false;
  180. }
  181. /**
  182. * 基于错题推荐练习题
  183. */
  184. public function recommendPractice(string $studentId, array $kpIds = [], array $skillIds = []): array
  185. {
  186. $payload = array_filter([
  187. 'student_id' => $studentId,
  188. 'kp_ids' => array_values(array_unique($kpIds)),
  189. 'skill_ids' => array_values(array_unique($skillIds)),
  190. ], fn ($value) => $value !== null);
  191. try {
  192. $response = Http::timeout($this->timeout)
  193. ->post($this->questionBankBase . '/api/questions/recommend', $payload);
  194. if ($response->successful()) {
  195. $body = $response->json();
  196. return is_array($body) ? $body : ['data' => $body];
  197. }
  198. Log::warning('Recommend practice failed', [
  199. 'status' => $response->status(),
  200. 'body' => $response->body(),
  201. 'payload' => $payload,
  202. ]);
  203. } catch (\Throwable $e) {
  204. Log::error('Recommend practice exception', [
  205. 'payload' => $payload,
  206. 'error' => $e->getMessage(),
  207. ]);
  208. }
  209. return ['data' => []];
  210. }
  211. /**
  212. * 为学生仪表板提供快照数据
  213. */
  214. public function getPanelSnapshot(string $studentId, int $limit = 5): array
  215. {
  216. $list = $this->listMistakes([
  217. 'student_id' => $studentId,
  218. 'per_page' => $limit,
  219. ]);
  220. $patterns = $this->getMistakePatterns($studentId);
  221. $summary = $this->summarize($studentId);
  222. return [
  223. 'recent' => $list['data'] ?? [],
  224. 'weak_skills' => $patterns['top_skills'] ?? [],
  225. 'weak_kps' => $patterns['top_kps'] ?? [],
  226. 'error_types' => $patterns['error_types'] ?? [],
  227. 'recommend_path' => $patterns['recommend_path'] ?? [],
  228. 'stats' => [
  229. 'total' => $summary['total'] ?? Arr::get($list, 'meta.total', 0),
  230. 'this_week' => $summary['this_week'] ?? null,
  231. 'pending_review' => $summary['pending_review'] ?? null,
  232. 'mastery_rate' => $summary['mastery_rate'] ?? null,
  233. ],
  234. ];
  235. }
  236. private function implodeIfArray($value): ?string
  237. {
  238. if (is_array($value)) {
  239. return implode(',', array_filter($value));
  240. }
  241. return $value;
  242. }
  243. }