TextbookApiService.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680
  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. if (isset($params['grade'])) {
  190. $query->where('grade', $params['grade']);
  191. }
  192. if (array_key_exists('semester', $params) && $params['semester'] !== null) {
  193. $query->where('semester', $params['semester']);
  194. }
  195. if (isset($params['status'])) {
  196. $query->where('status', $params['status']);
  197. }
  198. $textbooks = $query->orderBy('id')->get();
  199. return ['data' => $textbooks->toArray(), 'meta' => ['total' => $textbooks->count()]];
  200. }
  201. try {
  202. return $this->request('GET', '/textbooks', $params);
  203. } catch (\Exception $e) {
  204. Log::warning('Failed to fetch textbooks, returning empty result', ['error' => $e->getMessage()]);
  205. return ['data' => [], 'meta' => []];
  206. }
  207. }
  208. /**
  209. * 获取单个教材
  210. */
  211. public function getTextbook(int $textbookId): ?array
  212. {
  213. if ($this->useDatabase) {
  214. return Textbook::query()->find($textbookId)?->toArray();
  215. }
  216. try {
  217. $result = $this->request('GET', "/textbooks/by-id/{$textbookId}");
  218. return $result['data'] ?? null;
  219. } catch (\Exception $e) {
  220. Log::warning('Textbook not found or error occurred', [
  221. 'textbook_id' => $textbookId,
  222. 'error' => $e->getMessage()
  223. ]);
  224. return null;
  225. }
  226. }
  227. /**
  228. * 创建教材
  229. */
  230. public function createTextbook(array $data): array
  231. {
  232. if ($this->useDatabase) {
  233. $textbook = Textbook::query()->create($data);
  234. return ['data' => $textbook->toArray()];
  235. }
  236. try {
  237. return $this->request('POST', '/textbooks', $data);
  238. } catch (\Exception $e) {
  239. // 检查是否是series不存在的错误
  240. if (strpos($e->getMessage(), 'Series not found') !== false) {
  241. $seriesId = $data['series_id'] ?? 'unknown';
  242. throw new \Exception("系列ID {$seriesId} 不存在,请先创建教材系列或检查ID是否正确");
  243. }
  244. throw $e;
  245. }
  246. }
  247. /**
  248. * 更新教材
  249. */
  250. public function updateTextbook(int $textbookId, array $data): array
  251. {
  252. if ($this->useDatabase) {
  253. $textbook = Textbook::query()->findOrFail($textbookId);
  254. $textbook->fill($data);
  255. $textbook->save();
  256. return ['data' => $textbook->toArray()];
  257. }
  258. try {
  259. return $this->request('PUT', "/textbooks/by-id/{$textbookId}", $data);
  260. } catch (\Exception $e) {
  261. Log::error('Error updating textbook', ['error' => $e->getMessage()]);
  262. throw $e;
  263. }
  264. }
  265. /**
  266. * 创建或更新教材(upsert模式)
  267. * 根据系列、学段、年级、学期、官方书名判断是否已存在
  268. */
  269. public function createOrUpdateTextbook(array $data): array
  270. {
  271. if ($this->useDatabase) {
  272. if (isset($data['id'])) {
  273. return $this->updateTextbook((int) $data['id'], $data);
  274. }
  275. $textbook = Textbook::query()->create($data);
  276. return ['data' => $textbook->toArray()];
  277. }
  278. try {
  279. return $this->request('POST', '/textbooks/upsert', $data);
  280. } catch (\Exception $e) {
  281. // 检查是否是series不存在的错误
  282. if (strpos($e->getMessage(), 'Series not found') !== false) {
  283. $seriesId = $data['series_id'] ?? 'unknown';
  284. throw new \Exception("系列ID {$seriesId} 不存在,请先创建教材系列或检查ID是否正确");
  285. }
  286. throw $e;
  287. }
  288. }
  289. /**
  290. * 删除教材目录节点
  291. */
  292. public function deleteTextbookCatalog(int $catalogId): bool
  293. {
  294. if ($this->useDatabase) {
  295. $catalog = TextbookCatalog::query()->find($catalogId);
  296. return $catalog ? (bool) $catalog->delete() : false;
  297. }
  298. try {
  299. $this->request('DELETE', "/textbooks/catalog/{$catalogId}");
  300. return true;
  301. } catch (\Exception $e) {
  302. Log::error('Error deleting textbook catalog', ['error' => $e->getMessage(), 'catalog_id' => $catalogId]);
  303. return false;
  304. }
  305. }
  306. /**
  307. * 获取教材目录树
  308. */
  309. public function getTextbookCatalog(int $textbookId, string $format = 'tree'): array
  310. {
  311. if ($this->useDatabase) {
  312. $nodes = TextbookCatalog::query()
  313. ->where('textbook_id', $textbookId)
  314. ->orderBy('sort_order')
  315. ->get();
  316. if ($format === 'tree') {
  317. return $this->buildCatalogTree($nodes->toArray());
  318. }
  319. return $nodes->toArray();
  320. }
  321. try {
  322. $result = $this->request('GET', "/textbooks/by-id/{$textbookId}/catalog", ['format' => $format]);
  323. return $result['data'] ?? [];
  324. } catch (\Exception $e) {
  325. Log::warning('Failed to fetch textbook catalog, returning empty result', ['error' => $e->getMessage()]);
  326. return [];
  327. }
  328. }
  329. /**
  330. * 预览教材命名
  331. */
  332. public function previewTextbookNaming(array $textbookData, array $seriesData): array
  333. {
  334. if ($this->useDatabase) {
  335. $title = $textbookData['official_title'] ?? $textbookData['display_title'] ?? '';
  336. return ['data' => ['official_title' => $title]];
  337. }
  338. try {
  339. $result = $this->request('POST', '/textbooks/naming-preview', [
  340. 'textbook' => $textbookData,
  341. 'series' => $seriesData
  342. ]);
  343. return $result['data'] ?? [];
  344. } catch (\Exception $e) {
  345. Log::warning('Failed to preview textbook naming, returning empty result', ['error' => $e->getMessage()]);
  346. return [];
  347. }
  348. }
  349. /**
  350. * 导入教材元信息
  351. */
  352. public function importTextbookMetadata($file, string $commitMode = 'overwrite'): array
  353. {
  354. if ($this->useDatabase) {
  355. return [
  356. 'data' => [],
  357. 'meta' => [
  358. 'status' => 'pending',
  359. 'message' => 'Local import is pending implementation.',
  360. ],
  361. ];
  362. }
  363. try {
  364. return $this->request('POST', '/textbooks/import/meta', [
  365. 'file' => $file,
  366. 'commit_mode' => $commitMode
  367. ]);
  368. } catch (\Exception $e) {
  369. Log::error('Error importing textbook metadata', ['error' => $e->getMessage()]);
  370. throw $e;
  371. }
  372. }
  373. /**
  374. * 导入教材目录
  375. */
  376. public function importTextbookCatalog(int $textbookId, array $catalogData, string $commitMode = 'overwrite', ?int $seriesId = null): array
  377. {
  378. if ($this->useDatabase) {
  379. if (empty($catalogData)) {
  380. return [
  381. 'success' => false,
  382. 'message' => '目录数据为空',
  383. 'success_count' => 0,
  384. 'error_count' => 0,
  385. 'errors' => [],
  386. ];
  387. }
  388. $successCount = 0;
  389. $errorCount = 0;
  390. $errors = [];
  391. $parentStack = [];
  392. DB::transaction(function () use ($textbookId, $catalogData, $commitMode, $seriesId, &$successCount, &$errorCount, &$errors, &$parentStack) {
  393. if ($commitMode === 'overwrite') {
  394. $textbook = Textbook::query()->find($textbookId);
  395. if ($textbook && $seriesId !== null && (int) $textbook->series_id !== (int) $seriesId) {
  396. throw new \RuntimeException('教材系列不匹配,已阻止覆盖更新');
  397. }
  398. TextbookCatalog::query()->where('textbook_id', $textbookId)->delete();
  399. }
  400. foreach ($catalogData as $index => $node) {
  401. try {
  402. $depth = (int) ($node['depth'] ?? 1);
  403. $parentId = $node['parent_id'] ?? null;
  404. $title = trim((string) ($node['title'] ?? ''));
  405. if ($title === '') {
  406. throw new \Exception('目录标题不能为空');
  407. }
  408. $tags = $node['tags'] ?? null;
  409. if (is_array($tags)) {
  410. $tags = empty($tags) ? null : json_encode($tags, JSON_UNESCAPED_UNICODE);
  411. } elseif (is_string($tags) && $tags !== '') {
  412. json_decode($tags, true);
  413. if (json_last_error() !== JSON_ERROR_NONE) {
  414. $tags = null;
  415. }
  416. }
  417. $meta = $node['meta'] ?? null;
  418. if (is_array($meta)) {
  419. $meta = empty($meta) ? null : json_encode($meta, JSON_UNESCAPED_UNICODE);
  420. } elseif (is_string($meta) && $meta !== '') {
  421. json_decode($meta, true);
  422. if (json_last_error() !== JSON_ERROR_NONE) {
  423. $meta = null;
  424. }
  425. }
  426. if (!$parentId && $depth > 1) {
  427. $parentId = $parentStack[$depth - 1] ?? null;
  428. }
  429. $payload = [
  430. 'textbook_id' => $textbookId,
  431. 'parent_id' => $parentId,
  432. 'node_type' => $node['node_type'] ?? 'chapter',
  433. 'title' => $title,
  434. 'display_no' => $node['display_no'] ?? null,
  435. 'depth' => $depth,
  436. 'sort_order' => (int) ($node['sort_order'] ?? 0),
  437. 'path_key' => $node['path_key'] ?? null,
  438. 'is_required' => (bool) ($node['is_required'] ?? false),
  439. 'is_elective' => (bool) ($node['is_elective'] ?? false),
  440. 'tags' => $tags,
  441. 'page_start' => $node['page_start'] ?? null,
  442. 'page_end' => $node['page_end'] ?? null,
  443. 'meta' => $meta,
  444. ];
  445. $record = TextbookCatalog::create($payload);
  446. $parentStack[$depth] = $record->id;
  447. foreach (array_keys($parentStack) as $stackDepth) {
  448. if ($stackDepth > $depth) {
  449. unset($parentStack[$stackDepth]);
  450. }
  451. }
  452. $successCount++;
  453. } catch (\Throwable $e) {
  454. $errorCount++;
  455. $errors[] = '第' . ($index + 2) . '行: ' . $e->getMessage();
  456. }
  457. }
  458. });
  459. return [
  460. 'success' => $errorCount === 0,
  461. 'success_count' => $successCount,
  462. 'error_count' => $errorCount,
  463. 'errors' => $errors,
  464. 'message' => $errorCount === 0 ? '导入完成' : '部分导入失败',
  465. ];
  466. }
  467. try {
  468. // 将数组转换为JSON字符串
  469. $jsonData = json_encode($catalogData, JSON_UNESCAPED_UNICODE);
  470. // 直接发送 JSON 数据
  471. return $this->request('POST', '/textbooks/import/catalog', [
  472. 'textbook_id' => $textbookId,
  473. 'data' => $jsonData,
  474. 'commit_mode' => $commitMode
  475. ]);
  476. } catch (\Exception $e) {
  477. Log::error('Error importing textbook catalog', ['error' => $e->getMessage()]);
  478. throw $e;
  479. }
  480. }
  481. /**
  482. * 提交导入任务
  483. */
  484. public function commitImportJob(int $jobId): array
  485. {
  486. if ($this->useDatabase) {
  487. return [
  488. 'data' => [],
  489. 'meta' => [
  490. 'status' => 'noop',
  491. 'message' => 'Local import jobs are not tracked yet.',
  492. ],
  493. ];
  494. }
  495. try {
  496. return $this->request('POST', "/api/textbooks/import/{$jobId}/commit");
  497. } catch (\Exception $e) {
  498. Log::error('Error committing import job', ['error' => $e->getMessage()]);
  499. throw $e;
  500. }
  501. }
  502. /**
  503. * 获取导入任务列表
  504. */
  505. public function getImportJobs(array $params = []): array
  506. {
  507. if ($this->useDatabase) {
  508. return ['data' => [], 'meta' => ['total' => 0]];
  509. }
  510. try {
  511. return $this->request('GET', '/textbooks/import/jobs', $params);
  512. } catch (\Exception $e) {
  513. Log::warning('Failed to fetch import jobs, returning empty result', ['error' => $e->getMessage()]);
  514. return ['data' => [], 'meta' => []];
  515. }
  516. }
  517. /**
  518. * 获取单个导入任务
  519. */
  520. public function getImportJob(int $jobId): ?array
  521. {
  522. if ($this->useDatabase) {
  523. return null;
  524. }
  525. try {
  526. $result = $this->request('GET', "/api/textbooks/import/jobs/{$jobId}");
  527. return $result['data'] ?? null;
  528. } catch (\Exception $e) {
  529. Log::warning('Import job not found or error occurred', ['error' => $e->getMessage()]);
  530. return null;
  531. }
  532. }
  533. /**
  534. * 完全同步教材系列到题库服务(清空+重新插入)
  535. */
  536. public function syncTextbookSeriesToQuestionBank(): array
  537. {
  538. if ($this->useDatabase) {
  539. return ['data' => [], 'meta' => ['status' => 'noop']];
  540. }
  541. try {
  542. // 获取MySQL中的所有系列
  543. $mysqlSeries = DB::connection('mysql')
  544. ->table('textbook_series')
  545. ->orderBy('id')
  546. ->get();
  547. if ($mysqlSeries->isEmpty()) {
  548. return [
  549. 'success' => false,
  550. 'message' => 'MySQL中没有找到教材系列数据'
  551. ];
  552. }
  553. // 准备数据
  554. $seriesData = [];
  555. foreach ($mysqlSeries as $series) {
  556. $seriesData[] = [
  557. 'id' => $series->id,
  558. 'name' => $series->name,
  559. 'slug' => $series->slug,
  560. 'publisher' => $series->publisher,
  561. 'region' => $series->region,
  562. 'stages' => json_decode($series->stages, true),
  563. 'is_active' => (bool)$series->is_active,
  564. 'sort_order' => (int)$series->sort_order,
  565. 'meta' => json_decode($series->meta, true),
  566. ];
  567. }
  568. // 调用API进行完全同步
  569. $result = $this->request('POST', '/textbooks/series/sync-all', [
  570. 'series' => $seriesData
  571. ]);
  572. return [
  573. 'success' => true,
  574. 'synced_count' => count($seriesData),
  575. 'data' => $result
  576. ];
  577. } catch (\Exception $e) {
  578. Log::error('Error syncing textbook series', ['error' => $e->getMessage()]);
  579. return [
  580. 'success' => false,
  581. 'message' => '同步失败: ' . $e->getMessage()
  582. ];
  583. }
  584. }
  585. private function buildCatalogTree(array $nodes): array
  586. {
  587. // 建立ID索引,包含children数组
  588. $indexed = [];
  589. foreach ($nodes as &$node) { // 使用引用
  590. $node['children'] = [];
  591. $indexed[$node['id']] = &$node;
  592. }
  593. unset($node); // 释放引用
  594. $tree = [];
  595. foreach ($nodes as &$node) { // 使用引用
  596. $parentId = $node['parent_id'] ?? null;
  597. if ($parentId && isset($indexed[$parentId])) {
  598. // 父节点存在,将当前节点添加到父节点的children中
  599. $indexed[$parentId]['children'][] = &$node;
  600. } else {
  601. // 根节点,直接添加到树中
  602. $tree[] = &$node;
  603. }
  604. }
  605. unset($node); // 释放引用
  606. return $tree;
  607. }
  608. }