TextbookApiService.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666
  1. <?php
  2. namespace App\Services;
  3. use App\Models\Textbook;
  4. use App\Models\TextbookCatalog;
  5. use App\Models\TextbookSeries;
  6. use Illuminate\Support\Facades\DB;
  7. use Illuminate\Support\Facades\Http;
  8. use Illuminate\Support\Facades\Log;
  9. class TextbookApiService
  10. {
  11. protected string $baseUrl;
  12. protected bool $useDatabase;
  13. public function __construct()
  14. {
  15. $baseUrl = (string) config('services.textbook_api.base_url', '');
  16. $this->baseUrl = rtrim($baseUrl, '/');
  17. $this->useDatabase = true;
  18. }
  19. /**
  20. * 通用HTTP请求方法 - 减少重复代码,无缓存
  21. */
  22. protected function request(string $method, string $endpoint, array $data = []): array
  23. {
  24. try {
  25. $httpMethod = strtolower($method);
  26. $response = match($httpMethod) {
  27. 'get' => Http::timeout(30)->get($this->baseUrl . $endpoint, $data),
  28. 'post' => Http::timeout(300)->post($this->baseUrl . $endpoint, $data),
  29. 'put' => Http::timeout(30)->put($this->baseUrl . $endpoint, $data),
  30. 'delete' => Http::timeout(30)->delete($this->baseUrl . $endpoint, $data),
  31. default => throw new \InvalidArgumentException("Unsupported HTTP method: {$method}")
  32. };
  33. // 处理文件上传
  34. if (isset($data['file']) && $data['file'] instanceof \Illuminate\Http\UploadedFile) {
  35. $response = Http::timeout(300)
  36. ->attach('file', file_get_contents($data['file']->getPathname()), $data['file']->getClientOriginalName())
  37. ->{$httpMethod}($this->baseUrl . $endpoint, $data);
  38. }
  39. if ($response->successful()) {
  40. return $response->json();
  41. }
  42. // 记录错误并抛出异常
  43. $error = $this->handleErrorResponse($response, $endpoint);
  44. throw new \Exception($error);
  45. } catch (\Exception $e) {
  46. Log::error("API request failed: {$method} {$endpoint}", [
  47. 'error' => $e->getMessage(),
  48. 'data' => $data
  49. ]);
  50. throw $e;
  51. }
  52. }
  53. /**
  54. * 处理错误响应
  55. */
  56. private function handleErrorResponse($response, string $endpoint): string
  57. {
  58. $status = $response->status();
  59. $body = $response->body();
  60. // 常见错误类型的友好提示
  61. return match($status) {
  62. 404 => "未找到资源: {$endpoint}",
  63. 422 => "数据验证失败: {$body}",
  64. 500 => "服务器内部错误: {$body}",
  65. default => "HTTP {$status}: {$body}"
  66. };
  67. }
  68. /**
  69. * 获取教材系列列表
  70. */
  71. public function getTextbookSeries(array $params = []): array
  72. {
  73. if ($this->useDatabase) {
  74. $series = TextbookSeries::query()->orderBy('sort_order')->get();
  75. return ['data' => $series->toArray(), 'meta' => ['total' => $series->count()]];
  76. }
  77. try {
  78. return $this->request('GET', '/textbooks/series', $params);
  79. } catch (\Exception $e) {
  80. // 失败时返回空数据而不是抛出异常,保持向后兼容
  81. Log::warning('Failed to fetch textbook series, returning empty result', ['error' => $e->getMessage()]);
  82. return ['data' => [], 'meta' => []];
  83. }
  84. }
  85. /**
  86. * 根据ID获取单个教材系列
  87. */
  88. public function getTextbookSeriesById(int $seriesId): ?array
  89. {
  90. if ($this->useDatabase) {
  91. return TextbookSeries::query()->find($seriesId)?->toArray();
  92. }
  93. try {
  94. $result = $this->request('GET', "/textbooks/series/{$seriesId}");
  95. return $result['data'] ?? null;
  96. } catch (\Exception $e) {
  97. Log::warning('Series not found or error occurred', [
  98. 'series_id' => $seriesId,
  99. 'error' => $e->getMessage()
  100. ]);
  101. return null;
  102. }
  103. }
  104. /**
  105. * 创建教材系列
  106. */
  107. public function createTextbookSeries(array $data): array
  108. {
  109. if ($this->useDatabase) {
  110. $series = TextbookSeries::query()->create($data);
  111. return ['data' => $series->toArray()];
  112. }
  113. try {
  114. return $this->request('POST', '/textbooks/series', $data);
  115. } catch (\Exception $e) {
  116. // 检查是否是series不存在的错误(虽然这里不太可能,但保持向后兼容)
  117. if (strpos($e->getMessage(), 'Series not found') !== false) {
  118. $seriesId = $data['id'] ?? 'unknown';
  119. throw new \Exception("系列ID {$seriesId} 不存在");
  120. }
  121. throw $e;
  122. }
  123. }
  124. /**
  125. * 更新教材系列
  126. */
  127. public function updateTextbookSeries(int $seriesId, array $data): array
  128. {
  129. if ($this->useDatabase) {
  130. $series = TextbookSeries::query()->findOrFail($seriesId);
  131. $series->fill($data);
  132. $series->save();
  133. return ['data' => $series->toArray()];
  134. }
  135. try {
  136. return $this->request('PUT', "/textbooks/series/{$seriesId}", $data);
  137. } catch (\Exception $e) {
  138. Log::error('Error updating textbook series', ['error' => $e->getMessage()]);
  139. throw $e;
  140. }
  141. }
  142. /**
  143. * 删除教材系列
  144. */
  145. public function deleteTextbookSeries(int $seriesId): bool
  146. {
  147. if ($this->useDatabase) {
  148. $series = TextbookSeries::query()->find($seriesId);
  149. return $series ? (bool) $series->delete() : false;
  150. }
  151. try {
  152. $this->request('DELETE', "/textbooks/series/{$seriesId}");
  153. return true;
  154. } catch (\Exception $e) {
  155. Log::error('Error deleting textbook series', ['error' => $e->getMessage()]);
  156. return false;
  157. }
  158. }
  159. /**
  160. * 删除教材
  161. */
  162. public function deleteTextbook(int $textbookId): bool
  163. {
  164. if ($this->useDatabase) {
  165. $textbook = Textbook::query()->find($textbookId);
  166. return $textbook ? (bool) $textbook->delete() : false;
  167. }
  168. try {
  169. $this->request('DELETE', "/textbooks/by-id/{$textbookId}");
  170. return true;
  171. } catch (\Exception $e) {
  172. Log::error('Error deleting textbook', ['error' => $e->getMessage(), 'textbook_id' => $textbookId]);
  173. return false;
  174. }
  175. }
  176. /**
  177. * 获取教材列表
  178. */
  179. public function getTextbooks(array $params = []): array
  180. {
  181. if ($this->useDatabase) {
  182. $query = Textbook::query();
  183. if (isset($params['series_id'])) {
  184. $query->where('series_id', $params['series_id']);
  185. }
  186. if (isset($params['stage'])) {
  187. $query->where('stage', $params['stage']);
  188. }
  189. $textbooks = $query->orderBy('id')->get();
  190. return ['data' => $textbooks->toArray(), 'meta' => ['total' => $textbooks->count()]];
  191. }
  192. try {
  193. return $this->request('GET', '/textbooks', $params);
  194. } catch (\Exception $e) {
  195. Log::warning('Failed to fetch textbooks, returning empty result', ['error' => $e->getMessage()]);
  196. return ['data' => [], 'meta' => []];
  197. }
  198. }
  199. /**
  200. * 获取单个教材
  201. */
  202. public function getTextbook(int $textbookId): ?array
  203. {
  204. if ($this->useDatabase) {
  205. return Textbook::query()->find($textbookId)?->toArray();
  206. }
  207. try {
  208. $result = $this->request('GET', "/textbooks/by-id/{$textbookId}");
  209. return $result['data'] ?? null;
  210. } catch (\Exception $e) {
  211. Log::warning('Textbook not found or error occurred', [
  212. 'textbook_id' => $textbookId,
  213. 'error' => $e->getMessage()
  214. ]);
  215. return null;
  216. }
  217. }
  218. /**
  219. * 创建教材
  220. */
  221. public function createTextbook(array $data): array
  222. {
  223. if ($this->useDatabase) {
  224. $textbook = Textbook::query()->create($data);
  225. return ['data' => $textbook->toArray()];
  226. }
  227. try {
  228. return $this->request('POST', '/textbooks', $data);
  229. } catch (\Exception $e) {
  230. // 检查是否是series不存在的错误
  231. if (strpos($e->getMessage(), 'Series not found') !== false) {
  232. $seriesId = $data['series_id'] ?? 'unknown';
  233. throw new \Exception("系列ID {$seriesId} 不存在,请先创建教材系列或检查ID是否正确");
  234. }
  235. throw $e;
  236. }
  237. }
  238. /**
  239. * 更新教材
  240. */
  241. public function updateTextbook(int $textbookId, array $data): array
  242. {
  243. if ($this->useDatabase) {
  244. $textbook = Textbook::query()->findOrFail($textbookId);
  245. $textbook->fill($data);
  246. $textbook->save();
  247. return ['data' => $textbook->toArray()];
  248. }
  249. try {
  250. return $this->request('PUT', "/textbooks/by-id/{$textbookId}", $data);
  251. } catch (\Exception $e) {
  252. Log::error('Error updating textbook', ['error' => $e->getMessage()]);
  253. throw $e;
  254. }
  255. }
  256. /**
  257. * 创建或更新教材(upsert模式)
  258. * 根据系列、学段、年级、学期、官方书名判断是否已存在
  259. */
  260. public function createOrUpdateTextbook(array $data): array
  261. {
  262. if ($this->useDatabase) {
  263. if (isset($data['id'])) {
  264. return $this->updateTextbook((int) $data['id'], $data);
  265. }
  266. $textbook = Textbook::query()->create($data);
  267. return ['data' => $textbook->toArray()];
  268. }
  269. try {
  270. return $this->request('POST', '/textbooks/upsert', $data);
  271. } catch (\Exception $e) {
  272. // 检查是否是series不存在的错误
  273. if (strpos($e->getMessage(), 'Series not found') !== false) {
  274. $seriesId = $data['series_id'] ?? 'unknown';
  275. throw new \Exception("系列ID {$seriesId} 不存在,请先创建教材系列或检查ID是否正确");
  276. }
  277. throw $e;
  278. }
  279. }
  280. /**
  281. * 删除教材目录节点
  282. */
  283. public function deleteTextbookCatalog(int $catalogId): bool
  284. {
  285. if ($this->useDatabase) {
  286. $catalog = TextbookCatalog::query()->find($catalogId);
  287. return $catalog ? (bool) $catalog->delete() : false;
  288. }
  289. try {
  290. $this->request('DELETE', "/textbooks/catalog/{$catalogId}");
  291. return true;
  292. } catch (\Exception $e) {
  293. Log::error('Error deleting textbook catalog', ['error' => $e->getMessage(), 'catalog_id' => $catalogId]);
  294. return false;
  295. }
  296. }
  297. /**
  298. * 获取教材目录树
  299. */
  300. public function getTextbookCatalog(int $textbookId, string $format = 'tree'): array
  301. {
  302. if ($this->useDatabase) {
  303. $nodes = TextbookCatalog::query()
  304. ->where('textbook_id', $textbookId)
  305. ->orderBy('sort_order')
  306. ->get();
  307. if ($format === 'tree') {
  308. return $this->buildCatalogTree($nodes->toArray());
  309. }
  310. return $nodes->toArray();
  311. }
  312. try {
  313. $result = $this->request('GET', "/textbooks/by-id/{$textbookId}/catalog", ['format' => $format]);
  314. return $result['data'] ?? [];
  315. } catch (\Exception $e) {
  316. Log::warning('Failed to fetch textbook catalog, returning empty result', ['error' => $e->getMessage()]);
  317. return [];
  318. }
  319. }
  320. /**
  321. * 预览教材命名
  322. */
  323. public function previewTextbookNaming(array $textbookData, array $seriesData): array
  324. {
  325. if ($this->useDatabase) {
  326. $title = $textbookData['official_title'] ?? $textbookData['display_title'] ?? '';
  327. return ['data' => ['official_title' => $title]];
  328. }
  329. try {
  330. $result = $this->request('POST', '/textbooks/naming-preview', [
  331. 'textbook' => $textbookData,
  332. 'series' => $seriesData
  333. ]);
  334. return $result['data'] ?? [];
  335. } catch (\Exception $e) {
  336. Log::warning('Failed to preview textbook naming, returning empty result', ['error' => $e->getMessage()]);
  337. return [];
  338. }
  339. }
  340. /**
  341. * 导入教材元信息
  342. */
  343. public function importTextbookMetadata($file, string $commitMode = 'overwrite'): array
  344. {
  345. if ($this->useDatabase) {
  346. return [
  347. 'data' => [],
  348. 'meta' => [
  349. 'status' => 'pending',
  350. 'message' => 'Local import is pending implementation.',
  351. ],
  352. ];
  353. }
  354. try {
  355. return $this->request('POST', '/textbooks/import/meta', [
  356. 'file' => $file,
  357. 'commit_mode' => $commitMode
  358. ]);
  359. } catch (\Exception $e) {
  360. Log::error('Error importing textbook metadata', ['error' => $e->getMessage()]);
  361. throw $e;
  362. }
  363. }
  364. /**
  365. * 导入教材目录
  366. */
  367. public function importTextbookCatalog(int $textbookId, array $catalogData, string $commitMode = 'overwrite', ?int $seriesId = null): array
  368. {
  369. if ($this->useDatabase) {
  370. if (empty($catalogData)) {
  371. return [
  372. 'success' => false,
  373. 'message' => '目录数据为空',
  374. 'success_count' => 0,
  375. 'error_count' => 0,
  376. 'errors' => [],
  377. ];
  378. }
  379. $successCount = 0;
  380. $errorCount = 0;
  381. $errors = [];
  382. $parentStack = [];
  383. DB::transaction(function () use ($textbookId, $catalogData, $commitMode, $seriesId, &$successCount, &$errorCount, &$errors, &$parentStack) {
  384. if ($commitMode === 'overwrite') {
  385. $textbook = Textbook::query()->find($textbookId);
  386. if ($textbook && $seriesId !== null && (int) $textbook->series_id !== (int) $seriesId) {
  387. throw new \RuntimeException('教材系列不匹配,已阻止覆盖更新');
  388. }
  389. TextbookCatalog::query()->where('textbook_id', $textbookId)->delete();
  390. }
  391. foreach ($catalogData as $index => $node) {
  392. try {
  393. $depth = (int) ($node['depth'] ?? 1);
  394. $parentId = $node['parent_id'] ?? null;
  395. $title = trim((string) ($node['title'] ?? ''));
  396. if ($title === '') {
  397. throw new \Exception('目录标题不能为空');
  398. }
  399. $tags = $node['tags'] ?? null;
  400. if (is_array($tags)) {
  401. $tags = empty($tags) ? null : json_encode($tags, JSON_UNESCAPED_UNICODE);
  402. } elseif (is_string($tags) && $tags !== '') {
  403. json_decode($tags, true);
  404. if (json_last_error() !== JSON_ERROR_NONE) {
  405. $tags = null;
  406. }
  407. }
  408. $meta = $node['meta'] ?? null;
  409. if (is_array($meta)) {
  410. $meta = empty($meta) ? null : json_encode($meta, JSON_UNESCAPED_UNICODE);
  411. } elseif (is_string($meta) && $meta !== '') {
  412. json_decode($meta, true);
  413. if (json_last_error() !== JSON_ERROR_NONE) {
  414. $meta = null;
  415. }
  416. }
  417. if (!$parentId && $depth > 1) {
  418. $parentId = $parentStack[$depth - 1] ?? null;
  419. }
  420. $payload = [
  421. 'textbook_id' => $textbookId,
  422. 'parent_id' => $parentId,
  423. 'node_type' => $node['node_type'] ?? 'chapter',
  424. 'title' => $title,
  425. 'display_no' => $node['display_no'] ?? null,
  426. 'depth' => $depth,
  427. 'sort_order' => (int) ($node['sort_order'] ?? 0),
  428. 'path_key' => $node['path_key'] ?? null,
  429. 'is_required' => (bool) ($node['is_required'] ?? false),
  430. 'is_elective' => (bool) ($node['is_elective'] ?? false),
  431. 'tags' => $tags,
  432. 'page_start' => $node['page_start'] ?? null,
  433. 'page_end' => $node['page_end'] ?? null,
  434. 'meta' => $meta,
  435. ];
  436. $record = TextbookCatalog::create($payload);
  437. $parentStack[$depth] = $record->id;
  438. foreach (array_keys($parentStack) as $stackDepth) {
  439. if ($stackDepth > $depth) {
  440. unset($parentStack[$stackDepth]);
  441. }
  442. }
  443. $successCount++;
  444. } catch (\Throwable $e) {
  445. $errorCount++;
  446. $errors[] = '第' . ($index + 2) . '行: ' . $e->getMessage();
  447. }
  448. }
  449. });
  450. return [
  451. 'success' => $errorCount === 0,
  452. 'success_count' => $successCount,
  453. 'error_count' => $errorCount,
  454. 'errors' => $errors,
  455. 'message' => $errorCount === 0 ? '导入完成' : '部分导入失败',
  456. ];
  457. }
  458. try {
  459. // 将数组转换为JSON字符串
  460. $jsonData = json_encode($catalogData, JSON_UNESCAPED_UNICODE);
  461. // 直接发送 JSON 数据
  462. return $this->request('POST', '/textbooks/import/catalog', [
  463. 'textbook_id' => $textbookId,
  464. 'data' => $jsonData,
  465. 'commit_mode' => $commitMode
  466. ]);
  467. } catch (\Exception $e) {
  468. Log::error('Error importing textbook catalog', ['error' => $e->getMessage()]);
  469. throw $e;
  470. }
  471. }
  472. /**
  473. * 提交导入任务
  474. */
  475. public function commitImportJob(int $jobId): array
  476. {
  477. if ($this->useDatabase) {
  478. return [
  479. 'data' => [],
  480. 'meta' => [
  481. 'status' => 'noop',
  482. 'message' => 'Local import jobs are not tracked yet.',
  483. ],
  484. ];
  485. }
  486. try {
  487. return $this->request('POST', "/api/textbooks/import/{$jobId}/commit");
  488. } catch (\Exception $e) {
  489. Log::error('Error committing import job', ['error' => $e->getMessage()]);
  490. throw $e;
  491. }
  492. }
  493. /**
  494. * 获取导入任务列表
  495. */
  496. public function getImportJobs(array $params = []): array
  497. {
  498. if ($this->useDatabase) {
  499. return ['data' => [], 'meta' => ['total' => 0]];
  500. }
  501. try {
  502. return $this->request('GET', '/textbooks/import/jobs', $params);
  503. } catch (\Exception $e) {
  504. Log::warning('Failed to fetch import jobs, returning empty result', ['error' => $e->getMessage()]);
  505. return ['data' => [], 'meta' => []];
  506. }
  507. }
  508. /**
  509. * 获取单个导入任务
  510. */
  511. public function getImportJob(int $jobId): ?array
  512. {
  513. if ($this->useDatabase) {
  514. return null;
  515. }
  516. try {
  517. $result = $this->request('GET', "/api/textbooks/import/jobs/{$jobId}");
  518. return $result['data'] ?? null;
  519. } catch (\Exception $e) {
  520. Log::warning('Import job not found or error occurred', ['error' => $e->getMessage()]);
  521. return null;
  522. }
  523. }
  524. /**
  525. * 完全同步教材系列到题库服务(清空+重新插入)
  526. */
  527. public function syncTextbookSeriesToQuestionBank(): array
  528. {
  529. if ($this->useDatabase) {
  530. return ['data' => [], 'meta' => ['status' => 'noop']];
  531. }
  532. try {
  533. // 获取MySQL中的所有系列
  534. $mysqlSeries = DB::connection('mysql')
  535. ->table('textbook_series')
  536. ->orderBy('id')
  537. ->get();
  538. if ($mysqlSeries->isEmpty()) {
  539. return [
  540. 'success' => false,
  541. 'message' => 'MySQL中没有找到教材系列数据'
  542. ];
  543. }
  544. // 准备数据
  545. $seriesData = [];
  546. foreach ($mysqlSeries as $series) {
  547. $seriesData[] = [
  548. 'id' => $series->id,
  549. 'name' => $series->name,
  550. 'slug' => $series->slug,
  551. 'publisher' => $series->publisher,
  552. 'region' => $series->region,
  553. 'stages' => json_decode($series->stages, true),
  554. 'is_active' => (bool)$series->is_active,
  555. 'sort_order' => (int)$series->sort_order,
  556. 'meta' => json_decode($series->meta, true),
  557. ];
  558. }
  559. // 调用API进行完全同步
  560. $result = $this->request('POST', '/textbooks/series/sync-all', [
  561. 'series' => $seriesData
  562. ]);
  563. return [
  564. 'success' => true,
  565. 'synced_count' => count($seriesData),
  566. 'data' => $result
  567. ];
  568. } catch (\Exception $e) {
  569. Log::error('Error syncing textbook series', ['error' => $e->getMessage()]);
  570. return [
  571. 'success' => false,
  572. 'message' => '同步失败: ' . $e->getMessage()
  573. ];
  574. }
  575. }
  576. private function buildCatalogTree(array $nodes): array
  577. {
  578. $indexed = [];
  579. foreach ($nodes as $node) {
  580. $node['children'] = [];
  581. $indexed[$node['id']] = $node;
  582. }
  583. $tree = [];
  584. foreach ($indexed as $id => $node) {
  585. $parentId = $node['parent_id'] ?? null;
  586. if ($parentId && isset($indexed[$parentId])) {
  587. $indexed[$parentId]['children'][] = $node;
  588. } else {
  589. $tree[] = $node;
  590. }
  591. }
  592. return $tree;
  593. }
  594. }