TextbookApiService.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732
  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. $query = TextbookSeries::query()->orderBy('sort_order');
  75. // 默认只返回启用的系列(is_active = true)
  76. // 除非显式传入 include_inactive=true
  77. if (!isset($params['include_inactive']) || $params['include_inactive'] !== 'true') {
  78. $query->where('is_active', true);
  79. }
  80. // 支持按学段筛选
  81. if (isset($params['stage'])) {
  82. $query->where('stages', 'like', '%' . $params['stage'] . '%');
  83. }
  84. $series = $query->get();
  85. return ['data' => $series->toArray(), 'meta' => ['total' => $series->count()]];
  86. }
  87. try {
  88. return $this->request('GET', '/textbooks/series', $params);
  89. } catch (\Exception $e) {
  90. // 失败时返回空数据而不是抛出异常,保持向后兼容
  91. Log::warning('Failed to fetch textbook series, returning empty result', ['error' => $e->getMessage()]);
  92. return ['data' => [], 'meta' => []];
  93. }
  94. }
  95. /**
  96. * 根据ID获取单个教材系列
  97. */
  98. public function getTextbookSeriesById(int $seriesId): ?array
  99. {
  100. if ($this->useDatabase) {
  101. return TextbookSeries::query()->find($seriesId)?->toArray();
  102. }
  103. try {
  104. $result = $this->request('GET', "/textbooks/series/{$seriesId}");
  105. return $result['data'] ?? null;
  106. } catch (\Exception $e) {
  107. Log::warning('Series not found or error occurred', [
  108. 'series_id' => $seriesId,
  109. 'error' => $e->getMessage()
  110. ]);
  111. return null;
  112. }
  113. }
  114. /**
  115. * 创建教材系列
  116. */
  117. public function createTextbookSeries(array $data): array
  118. {
  119. if ($this->useDatabase) {
  120. $series = TextbookSeries::query()->create($data);
  121. return ['data' => $series->toArray()];
  122. }
  123. try {
  124. return $this->request('POST', '/textbooks/series', $data);
  125. } catch (\Exception $e) {
  126. // 检查是否是series不存在的错误(虽然这里不太可能,但保持向后兼容)
  127. if (strpos($e->getMessage(), 'Series not found') !== false) {
  128. $seriesId = $data['id'] ?? 'unknown';
  129. throw new \Exception("系列ID {$seriesId} 不存在");
  130. }
  131. throw $e;
  132. }
  133. }
  134. /**
  135. * 更新教材系列
  136. */
  137. public function updateTextbookSeries(int $seriesId, array $data): array
  138. {
  139. if ($this->useDatabase) {
  140. $series = TextbookSeries::query()->findOrFail($seriesId);
  141. $series->fill($data);
  142. $series->save();
  143. return ['data' => $series->toArray()];
  144. }
  145. try {
  146. return $this->request('PUT', "/textbooks/series/{$seriesId}", $data);
  147. } catch (\Exception $e) {
  148. Log::error('Error updating textbook series', ['error' => $e->getMessage()]);
  149. throw $e;
  150. }
  151. }
  152. /**
  153. * 删除教材系列
  154. */
  155. public function deleteTextbookSeries(int $seriesId): bool
  156. {
  157. if ($this->useDatabase) {
  158. $series = TextbookSeries::query()->find($seriesId);
  159. return $series ? (bool) $series->delete() : false;
  160. }
  161. try {
  162. $this->request('DELETE', "/textbooks/series/{$seriesId}");
  163. return true;
  164. } catch (\Exception $e) {
  165. Log::error('Error deleting textbook series', ['error' => $e->getMessage()]);
  166. return false;
  167. }
  168. }
  169. /**
  170. * 删除教材
  171. */
  172. public function deleteTextbook(int $textbookId): bool
  173. {
  174. if ($this->useDatabase) {
  175. $textbook = Textbook::query()->find($textbookId);
  176. return $textbook ? (bool) $textbook->delete() : false;
  177. }
  178. try {
  179. $this->request('DELETE', "/textbooks/by-id/{$textbookId}");
  180. return true;
  181. } catch (\Exception $e) {
  182. Log::error('Error deleting textbook', ['error' => $e->getMessage(), 'textbook_id' => $textbookId]);
  183. return false;
  184. }
  185. }
  186. /**
  187. * 获取教材列表
  188. */
  189. public function getTextbooks(array $params = []): array
  190. {
  191. if ($this->useDatabase) {
  192. $query = Textbook::query()
  193. ->select('textbooks.*')
  194. ->join('textbook_series', 'textbooks.series_id', '=', 'textbook_series.id');
  195. // 默认只返回已发布且系列启用的教材
  196. // 除非显式传入 include_unpublished=true 或 include_inactive_series=true
  197. if (!isset($params['include_unpublished']) || $params['include_unpublished'] !== 'true') {
  198. $query->where('textbooks.status', 'published');
  199. }
  200. if (!isset($params['include_inactive_series']) || $params['include_inactive_series'] !== 'true') {
  201. $query->where('textbook_series.is_active', true);
  202. }
  203. // 支持按系列ID筛选
  204. if (isset($params['series_id'])) {
  205. $query->where('textbooks.series_id', $params['series_id']);
  206. }
  207. // 支持按学段筛选
  208. if (isset($params['stage'])) {
  209. $query->where('textbooks.stage', $params['stage']);
  210. }
  211. // 支持按年级筛选
  212. if (isset($params['grade'])) {
  213. $query->where('textbooks.grade', $params['grade']);
  214. }
  215. // 支持按学期筛选
  216. if (array_key_exists('semester', $params) && $params['semester'] !== null) {
  217. $query->where('textbooks.semester', $params['semester']);
  218. }
  219. // 显式按状态筛选(会覆盖默认的已发布筛选)
  220. if (isset($params['status'])) {
  221. $query->where('textbooks.status', $params['status']);
  222. }
  223. $textbooks = $query->orderBy('textbooks.id')->get();
  224. return ['data' => $textbooks->toArray(), 'meta' => ['total' => $textbooks->count()]];
  225. }
  226. try {
  227. return $this->request('GET', '/textbooks', $params);
  228. } catch (\Exception $e) {
  229. Log::warning('Failed to fetch textbooks, returning empty result', ['error' => $e->getMessage()]);
  230. return ['data' => [], 'meta' => []];
  231. }
  232. }
  233. /**
  234. * 获取单个教材
  235. */
  236. public function getTextbook(int $textbookId): ?array
  237. {
  238. if ($this->useDatabase) {
  239. return Textbook::query()->find($textbookId)?->toArray();
  240. }
  241. try {
  242. $result = $this->request('GET', "/textbooks/by-id/{$textbookId}");
  243. return $result['data'] ?? null;
  244. } catch (\Exception $e) {
  245. Log::warning('Textbook not found or error occurred', [
  246. 'textbook_id' => $textbookId,
  247. 'error' => $e->getMessage()
  248. ]);
  249. return null;
  250. }
  251. }
  252. /**
  253. * 创建教材
  254. */
  255. public function createTextbook(array $data): array
  256. {
  257. if ($this->useDatabase) {
  258. $textbook = Textbook::query()->create($data);
  259. return ['data' => $textbook->toArray()];
  260. }
  261. try {
  262. return $this->request('POST', '/textbooks', $data);
  263. } catch (\Exception $e) {
  264. // 检查是否是series不存在的错误
  265. if (strpos($e->getMessage(), 'Series not found') !== false) {
  266. $seriesId = $data['series_id'] ?? 'unknown';
  267. throw new \Exception("系列ID {$seriesId} 不存在,请先创建教材系列或检查ID是否正确");
  268. }
  269. throw $e;
  270. }
  271. }
  272. /**
  273. * 更新教材
  274. */
  275. public function updateTextbook(int $textbookId, array $data): array
  276. {
  277. if ($this->useDatabase) {
  278. $textbook = Textbook::query()->findOrFail($textbookId);
  279. $textbook->fill($data);
  280. $textbook->save();
  281. return ['data' => $textbook->toArray()];
  282. }
  283. try {
  284. return $this->request('PUT', "/textbooks/by-id/{$textbookId}", $data);
  285. } catch (\Exception $e) {
  286. Log::error('Error updating textbook', ['error' => $e->getMessage()]);
  287. throw $e;
  288. }
  289. }
  290. /**
  291. * 创建或更新教材(upsert模式)
  292. * 根据系列、学段、年级、学期、官方书名判断是否已存在
  293. */
  294. public function createOrUpdateTextbook(array $data): array
  295. {
  296. if ($this->useDatabase) {
  297. if (isset($data['id'])) {
  298. return $this->updateTextbook((int) $data['id'], $data);
  299. }
  300. $textbook = Textbook::query()->create($data);
  301. return ['data' => $textbook->toArray()];
  302. }
  303. try {
  304. return $this->request('POST', '/textbooks/upsert', $data);
  305. } catch (\Exception $e) {
  306. // 检查是否是series不存在的错误
  307. if (strpos($e->getMessage(), 'Series not found') !== false) {
  308. $seriesId = $data['series_id'] ?? 'unknown';
  309. throw new \Exception("系列ID {$seriesId} 不存在,请先创建教材系列或检查ID是否正确");
  310. }
  311. throw $e;
  312. }
  313. }
  314. /**
  315. * 删除教材目录节点
  316. */
  317. public function deleteTextbookCatalog(int $catalogId): bool
  318. {
  319. if ($this->useDatabase) {
  320. $catalog = TextbookCatalog::query()->find($catalogId);
  321. return $catalog ? (bool) $catalog->delete() : false;
  322. }
  323. try {
  324. $this->request('DELETE', "/textbooks/catalog/{$catalogId}");
  325. return true;
  326. } catch (\Exception $e) {
  327. Log::error('Error deleting textbook catalog', ['error' => $e->getMessage(), 'catalog_id' => $catalogId]);
  328. return false;
  329. }
  330. }
  331. /**
  332. * 获取教材目录树
  333. */
  334. public function getTextbookCatalog(int $textbookId, string $format = 'tree'): array
  335. {
  336. if ($this->useDatabase) {
  337. // 先验证教材是否存在且已发布,且其系列处于启用状态
  338. $textbook = Textbook::query()
  339. ->select('textbooks.*')
  340. ->join('textbook_series', 'textbooks.series_id', '=', 'textbook_series.id')
  341. ->where('textbooks.id', $textbookId)
  342. ->where('textbooks.status', 'published')
  343. ->where('textbook_series.is_active', true)
  344. ->first();
  345. if (!$textbook) {
  346. Log::warning('Textbook not found or not published or series inactive', [
  347. 'textbook_id' => $textbookId
  348. ]);
  349. return [];
  350. }
  351. $nodes = TextbookCatalog::query()
  352. ->where('textbook_id', $textbookId)
  353. ->orderBy('sort_order')
  354. ->get();
  355. if ($format === 'tree') {
  356. return $this->buildCatalogTree($nodes->toArray());
  357. }
  358. return $nodes->toArray();
  359. }
  360. try {
  361. $result = $this->request('GET', "/textbooks/by-id/{$textbookId}/catalog", ['format' => $format]);
  362. return $result['data'] ?? [];
  363. } catch (\Exception $e) {
  364. Log::warning('Failed to fetch textbook catalog, returning empty result', ['error' => $e->getMessage()]);
  365. return [];
  366. }
  367. }
  368. /**
  369. * 预览教材命名
  370. */
  371. public function previewTextbookNaming(array $textbookData, array $seriesData): array
  372. {
  373. if ($this->useDatabase) {
  374. $title = $textbookData['official_title'] ?? $textbookData['display_title'] ?? '';
  375. return ['data' => ['official_title' => $title]];
  376. }
  377. try {
  378. $result = $this->request('POST', '/textbooks/naming-preview', [
  379. 'textbook' => $textbookData,
  380. 'series' => $seriesData
  381. ]);
  382. return $result['data'] ?? [];
  383. } catch (\Exception $e) {
  384. Log::warning('Failed to preview textbook naming, returning empty result', ['error' => $e->getMessage()]);
  385. return [];
  386. }
  387. }
  388. /**
  389. * 导入教材元信息
  390. */
  391. public function importTextbookMetadata($file, string $commitMode = 'overwrite'): array
  392. {
  393. if ($this->useDatabase) {
  394. return [
  395. 'data' => [],
  396. 'meta' => [
  397. 'status' => 'pending',
  398. 'message' => 'Local import is pending implementation.',
  399. ],
  400. ];
  401. }
  402. try {
  403. return $this->request('POST', '/textbooks/import/meta', [
  404. 'file' => $file,
  405. 'commit_mode' => $commitMode
  406. ]);
  407. } catch (\Exception $e) {
  408. Log::error('Error importing textbook metadata', ['error' => $e->getMessage()]);
  409. throw $e;
  410. }
  411. }
  412. /**
  413. * 导入教材目录
  414. */
  415. public function importTextbookCatalog(int $textbookId, array $catalogData, string $commitMode = 'overwrite', ?int $seriesId = null): array
  416. {
  417. if ($this->useDatabase) {
  418. if (empty($catalogData)) {
  419. return [
  420. 'success' => false,
  421. 'message' => '目录数据为空',
  422. 'success_count' => 0,
  423. 'error_count' => 0,
  424. 'errors' => [],
  425. ];
  426. }
  427. $successCount = 0;
  428. $errorCount = 0;
  429. $errors = [];
  430. $parentStack = [];
  431. DB::transaction(function () use ($textbookId, $catalogData, $commitMode, $seriesId, &$successCount, &$errorCount, &$errors, &$parentStack) {
  432. if ($commitMode === 'overwrite') {
  433. $textbook = Textbook::query()->find($textbookId);
  434. if ($textbook && $seriesId !== null && (int) $textbook->series_id !== (int) $seriesId) {
  435. throw new \RuntimeException('教材系列不匹配,已阻止覆盖更新');
  436. }
  437. TextbookCatalog::query()->where('textbook_id', $textbookId)->delete();
  438. }
  439. foreach ($catalogData as $index => $node) {
  440. try {
  441. $depth = (int) ($node['depth'] ?? 1);
  442. $parentId = $node['parent_id'] ?? null;
  443. $title = trim((string) ($node['title'] ?? ''));
  444. if ($title === '') {
  445. throw new \Exception('目录标题不能为空');
  446. }
  447. $tags = $node['tags'] ?? null;
  448. if (is_array($tags)) {
  449. $tags = empty($tags) ? null : json_encode($tags, JSON_UNESCAPED_UNICODE);
  450. } elseif (is_string($tags) && $tags !== '') {
  451. json_decode($tags, true);
  452. if (json_last_error() !== JSON_ERROR_NONE) {
  453. $tags = null;
  454. }
  455. }
  456. $meta = $node['meta'] ?? null;
  457. if (is_array($meta)) {
  458. $meta = empty($meta) ? null : json_encode($meta, JSON_UNESCAPED_UNICODE);
  459. } elseif (is_string($meta) && $meta !== '') {
  460. json_decode($meta, true);
  461. if (json_last_error() !== JSON_ERROR_NONE) {
  462. $meta = null;
  463. }
  464. }
  465. if (!$parentId && $depth > 1) {
  466. $parentId = $parentStack[$depth - 1] ?? null;
  467. }
  468. $payload = [
  469. 'textbook_id' => $textbookId,
  470. 'parent_id' => $parentId,
  471. 'node_type' => $node['node_type'] ?? 'chapter',
  472. 'title' => $title,
  473. 'display_no' => $node['display_no'] ?? null,
  474. 'depth' => $depth,
  475. 'sort_order' => (int) ($node['sort_order'] ?? 0),
  476. 'path_key' => $node['path_key'] ?? null,
  477. 'is_required' => (bool) ($node['is_required'] ?? false),
  478. 'is_elective' => (bool) ($node['is_elective'] ?? false),
  479. 'tags' => $tags,
  480. 'page_start' => $node['page_start'] ?? null,
  481. 'page_end' => $node['page_end'] ?? null,
  482. 'meta' => $meta,
  483. ];
  484. $record = TextbookCatalog::create($payload);
  485. $parentStack[$depth] = $record->id;
  486. foreach (array_keys($parentStack) as $stackDepth) {
  487. if ($stackDepth > $depth) {
  488. unset($parentStack[$stackDepth]);
  489. }
  490. }
  491. $successCount++;
  492. } catch (\Throwable $e) {
  493. $errorCount++;
  494. $errors[] = '第' . ($index + 2) . '行: ' . $e->getMessage();
  495. }
  496. }
  497. });
  498. return [
  499. 'success' => $errorCount === 0,
  500. 'success_count' => $successCount,
  501. 'error_count' => $errorCount,
  502. 'errors' => $errors,
  503. 'message' => $errorCount === 0 ? '导入完成' : '部分导入失败',
  504. ];
  505. }
  506. try {
  507. // 将数组转换为JSON字符串
  508. $jsonData = json_encode($catalogData, JSON_UNESCAPED_UNICODE);
  509. // 直接发送 JSON 数据
  510. return $this->request('POST', '/textbooks/import/catalog', [
  511. 'textbook_id' => $textbookId,
  512. 'data' => $jsonData,
  513. 'commit_mode' => $commitMode
  514. ]);
  515. } catch (\Exception $e) {
  516. Log::error('Error importing textbook catalog', ['error' => $e->getMessage()]);
  517. throw $e;
  518. }
  519. }
  520. /**
  521. * 提交导入任务
  522. */
  523. public function commitImportJob(int $jobId): array
  524. {
  525. if ($this->useDatabase) {
  526. return [
  527. 'data' => [],
  528. 'meta' => [
  529. 'status' => 'noop',
  530. 'message' => 'Local import jobs are not tracked yet.',
  531. ],
  532. ];
  533. }
  534. try {
  535. return $this->request('POST', "/api/textbooks/import/{$jobId}/commit");
  536. } catch (\Exception $e) {
  537. Log::error('Error committing import job', ['error' => $e->getMessage()]);
  538. throw $e;
  539. }
  540. }
  541. /**
  542. * 获取导入任务列表
  543. */
  544. public function getImportJobs(array $params = []): array
  545. {
  546. if ($this->useDatabase) {
  547. return ['data' => [], 'meta' => ['total' => 0]];
  548. }
  549. try {
  550. return $this->request('GET', '/textbooks/import/jobs', $params);
  551. } catch (\Exception $e) {
  552. Log::warning('Failed to fetch import jobs, returning empty result', ['error' => $e->getMessage()]);
  553. return ['data' => [], 'meta' => []];
  554. }
  555. }
  556. /**
  557. * 获取单个导入任务
  558. */
  559. public function getImportJob(int $jobId): ?array
  560. {
  561. if ($this->useDatabase) {
  562. return null;
  563. }
  564. try {
  565. $result = $this->request('GET', "/api/textbooks/import/jobs/{$jobId}");
  566. return $result['data'] ?? null;
  567. } catch (\Exception $e) {
  568. Log::warning('Import job not found or error occurred', ['error' => $e->getMessage()]);
  569. return null;
  570. }
  571. }
  572. /**
  573. * 完全同步教材系列到题库服务(清空+重新插入)
  574. */
  575. public function syncTextbookSeriesToQuestionBank(): array
  576. {
  577. if ($this->useDatabase) {
  578. return ['data' => [], 'meta' => ['status' => 'noop']];
  579. }
  580. try {
  581. // 获取MySQL中的所有系列
  582. $mysqlSeries = DB::connection('mysql')
  583. ->table('textbook_series')
  584. ->orderBy('id')
  585. ->get();
  586. if ($mysqlSeries->isEmpty()) {
  587. return [
  588. 'success' => false,
  589. 'message' => 'MySQL中没有找到教材系列数据'
  590. ];
  591. }
  592. // 准备数据
  593. $seriesData = [];
  594. foreach ($mysqlSeries as $series) {
  595. $seriesData[] = [
  596. 'id' => $series->id,
  597. 'name' => $series->name,
  598. 'slug' => $series->slug,
  599. 'publisher' => $series->publisher,
  600. 'region' => $series->region,
  601. 'stages' => json_decode($series->stages, true),
  602. 'is_active' => (bool)$series->is_active,
  603. 'sort_order' => (int)$series->sort_order,
  604. 'meta' => json_decode($series->meta, true),
  605. ];
  606. }
  607. // 调用API进行完全同步
  608. $result = $this->request('POST', '/textbooks/series/sync-all', [
  609. 'series' => $seriesData
  610. ]);
  611. return [
  612. 'success' => true,
  613. 'synced_count' => count($seriesData),
  614. 'data' => $result
  615. ];
  616. } catch (\Exception $e) {
  617. Log::error('Error syncing textbook series', ['error' => $e->getMessage()]);
  618. return [
  619. 'success' => false,
  620. 'message' => '同步失败: ' . $e->getMessage()
  621. ];
  622. }
  623. }
  624. private function buildCatalogTree(array $nodes): array
  625. {
  626. // 建立ID索引,包含children数组
  627. $indexed = [];
  628. foreach ($nodes as &$node) { // 使用引用
  629. $node['children'] = [];
  630. $indexed[$node['id']] = &$node;
  631. }
  632. unset($node); // 释放引用
  633. $tree = [];
  634. foreach ($nodes as &$node) { // 使用引用
  635. $parentId = $node['parent_id'] ?? null;
  636. if ($parentId && isset($indexed[$parentId])) {
  637. // 父节点存在,将当前节点添加到父节点的children中
  638. $indexed[$parentId]['children'][] = &$node;
  639. } else {
  640. // 根节点,直接添加到树中
  641. $tree[] = &$node;
  642. }
  643. }
  644. unset($node); // 释放引用
  645. return $tree;
  646. }
  647. }