MistakeBookService.php 8.8 KB

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