TextbookApiService.php 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972
  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.*', 'textbook_series.name as series_name')
  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. // 关键词筛选:教材名称 / ISBN / 系列名
  224. if (isset($params['keyword']) && trim((string) $params['keyword']) !== '') {
  225. $keyword = trim((string) $params['keyword']);
  226. $query->where(function ($q) use ($keyword) {
  227. $q->where('textbooks.official_title', 'like', "%{$keyword}%")
  228. ->orWhere('textbooks.isbn', 'like', "%{$keyword}%")
  229. ->orWhere('textbook_series.name', 'like', "%{$keyword}%");
  230. });
  231. }
  232. // 命名方案筛选
  233. if (isset($params['naming_scheme']) && $params['naming_scheme'] !== '') {
  234. $query->where('textbooks.naming_scheme', $params['naming_scheme']);
  235. }
  236. $query->orderBy('textbooks.id');
  237. $page = max(1, (int) ($params['page'] ?? 1));
  238. $perPage = isset($params['per_page']) && is_numeric($params['per_page'])
  239. ? max(1, (int) $params['per_page'])
  240. : null;
  241. if ($perPage !== null) {
  242. $total = (clone $query)->count('textbooks.id');
  243. $textbooks = $query
  244. ->forPage($page, $perPage)
  245. ->get();
  246. return [
  247. 'data' => $textbooks->toArray(),
  248. 'meta' => [
  249. 'total' => $total,
  250. 'current_page' => $page,
  251. 'per_page' => $perPage,
  252. 'last_page' => (int) ceil($total / $perPage),
  253. ],
  254. ];
  255. }
  256. $textbooks = $query->get();
  257. return ['data' => $textbooks->toArray(), 'meta' => ['total' => $textbooks->count()]];
  258. }
  259. try {
  260. return $this->request('GET', '/textbooks', $params);
  261. } catch (\Exception $e) {
  262. Log::warning('Failed to fetch textbooks, returning empty result', ['error' => $e->getMessage()]);
  263. return ['data' => [], 'meta' => []];
  264. }
  265. }
  266. /**
  267. * 获取单个教材
  268. */
  269. public function getTextbook(int $textbookId): ?array
  270. {
  271. if ($this->useDatabase) {
  272. return Textbook::query()->find($textbookId)?->toArray();
  273. }
  274. try {
  275. $result = $this->request('GET', "/textbooks/by-id/{$textbookId}");
  276. return $result['data'] ?? null;
  277. } catch (\Exception $e) {
  278. Log::warning('Textbook not found or error occurred', [
  279. 'textbook_id' => $textbookId,
  280. 'error' => $e->getMessage()
  281. ]);
  282. return null;
  283. }
  284. }
  285. /**
  286. * 创建教材
  287. */
  288. public function createTextbook(array $data): array
  289. {
  290. if ($this->useDatabase) {
  291. $textbook = Textbook::query()->create($data);
  292. return ['data' => $textbook->toArray()];
  293. }
  294. try {
  295. return $this->request('POST', '/textbooks', $data);
  296. } catch (\Exception $e) {
  297. // 检查是否是series不存在的错误
  298. if (strpos($e->getMessage(), 'Series not found') !== false) {
  299. $seriesId = $data['series_id'] ?? 'unknown';
  300. throw new \Exception("系列ID {$seriesId} 不存在,请先创建教材系列或检查ID是否正确");
  301. }
  302. throw $e;
  303. }
  304. }
  305. /**
  306. * 更新教材
  307. */
  308. public function updateTextbook(int $textbookId, array $data): array
  309. {
  310. if ($this->useDatabase) {
  311. $textbook = Textbook::query()->findOrFail($textbookId);
  312. $textbook->fill($data);
  313. $textbook->save();
  314. return ['data' => $textbook->toArray()];
  315. }
  316. try {
  317. return $this->request('PUT', "/textbooks/by-id/{$textbookId}", $data);
  318. } catch (\Exception $e) {
  319. Log::error('Error updating textbook', ['error' => $e->getMessage()]);
  320. throw $e;
  321. }
  322. }
  323. /**
  324. * 创建或更新教材(upsert模式)
  325. * 根据系列、学段、年级、学期、官方书名判断是否已存在
  326. */
  327. public function createOrUpdateTextbook(array $data): array
  328. {
  329. if ($this->useDatabase) {
  330. if (isset($data['id'])) {
  331. return $this->updateTextbook((int) $data['id'], $data);
  332. }
  333. $textbook = Textbook::query()->create($data);
  334. return ['data' => $textbook->toArray()];
  335. }
  336. try {
  337. return $this->request('POST', '/textbooks/upsert', $data);
  338. } catch (\Exception $e) {
  339. // 检查是否是series不存在的错误
  340. if (strpos($e->getMessage(), 'Series not found') !== false) {
  341. $seriesId = $data['series_id'] ?? 'unknown';
  342. throw new \Exception("系列ID {$seriesId} 不存在,请先创建教材系列或检查ID是否正确");
  343. }
  344. throw $e;
  345. }
  346. }
  347. /**
  348. * 删除教材目录节点
  349. */
  350. public function deleteTextbookCatalog(int $catalogId): bool
  351. {
  352. if ($this->useDatabase) {
  353. $catalog = TextbookCatalog::query()->find($catalogId);
  354. return $catalog ? (bool) $catalog->delete() : false;
  355. }
  356. try {
  357. $this->request('DELETE', "/textbooks/catalog/{$catalogId}");
  358. return true;
  359. } catch (\Exception $e) {
  360. Log::error('Error deleting textbook catalog', ['error' => $e->getMessage(), 'catalog_id' => $catalogId]);
  361. return false;
  362. }
  363. }
  364. /**
  365. * 获取教材目录树
  366. */
  367. public function getTextbookCatalog(int $textbookId, string $format = 'tree'): array
  368. {
  369. if ($this->useDatabase) {
  370. // 先验证教材是否存在且已发布,且其系列处于启用状态
  371. $textbook = Textbook::query()
  372. ->select('textbooks.*')
  373. ->join('textbook_series', 'textbooks.series_id', '=', 'textbook_series.id')
  374. ->where('textbooks.id', $textbookId)
  375. ->where('textbooks.status', 'published')
  376. ->where('textbook_series.is_active', true)
  377. ->first();
  378. if (!$textbook) {
  379. Log::warning('Textbook not found or not published or series inactive', [
  380. 'textbook_id' => $textbookId
  381. ]);
  382. return [];
  383. }
  384. $nodes = TextbookCatalog::query()
  385. ->where('textbook_id', $textbookId)
  386. ->orderBy('sort_order')
  387. ->get();
  388. if ($format === 'tree') {
  389. return $this->buildCatalogTree($nodes->toArray());
  390. }
  391. return $nodes->toArray();
  392. }
  393. try {
  394. $result = $this->request('GET', "/textbooks/by-id/{$textbookId}/catalog", ['format' => $format]);
  395. return $result['data'] ?? [];
  396. } catch (\Exception $e) {
  397. Log::warning('Failed to fetch textbook catalog, returning empty result', ['error' => $e->getMessage()]);
  398. return [];
  399. }
  400. }
  401. /**
  402. * 预览教材命名
  403. */
  404. public function previewTextbookNaming(array $textbookData, array $seriesData): array
  405. {
  406. if ($this->useDatabase) {
  407. $title = $textbookData['official_title'] ?? $textbookData['display_title'] ?? '';
  408. return ['data' => ['official_title' => $title]];
  409. }
  410. try {
  411. $result = $this->request('POST', '/textbooks/naming-preview', [
  412. 'textbook' => $textbookData,
  413. 'series' => $seriesData
  414. ]);
  415. return $result['data'] ?? [];
  416. } catch (\Exception $e) {
  417. Log::warning('Failed to preview textbook naming, returning empty result', ['error' => $e->getMessage()]);
  418. return [];
  419. }
  420. }
  421. /**
  422. * 导入教材元信息
  423. */
  424. public function importTextbookMetadata($file, string $commitMode = 'overwrite'): array
  425. {
  426. if ($this->useDatabase) {
  427. return [
  428. 'data' => [],
  429. 'meta' => [
  430. 'status' => 'pending',
  431. 'message' => 'Local import is pending implementation.',
  432. ],
  433. ];
  434. }
  435. try {
  436. return $this->request('POST', '/textbooks/import/meta', [
  437. 'file' => $file,
  438. 'commit_mode' => $commitMode
  439. ]);
  440. } catch (\Exception $e) {
  441. Log::error('Error importing textbook metadata', ['error' => $e->getMessage()]);
  442. throw $e;
  443. }
  444. }
  445. /**
  446. * 导入教材目录
  447. */
  448. public function importTextbookCatalog(int $textbookId, array $catalogData, string $commitMode = 'overwrite', ?int $seriesId = null): array
  449. {
  450. if ($this->useDatabase) {
  451. if (empty($catalogData)) {
  452. return [
  453. 'success' => false,
  454. 'message' => '目录数据为空',
  455. 'success_count' => 0,
  456. 'error_count' => 0,
  457. 'errors' => [],
  458. ];
  459. }
  460. $successCount = 0;
  461. $errorCount = 0;
  462. $errors = [];
  463. $parentStack = [];
  464. DB::transaction(function () use ($textbookId, $catalogData, $commitMode, $seriesId, &$successCount, &$errorCount, &$errors, &$parentStack) {
  465. if ($commitMode === 'overwrite') {
  466. $textbook = Textbook::query()->find($textbookId);
  467. if ($textbook && $seriesId !== null && (int) $textbook->series_id !== (int) $seriesId) {
  468. throw new \RuntimeException('教材系列不匹配,已阻止覆盖更新');
  469. }
  470. TextbookCatalog::query()->where('textbook_id', $textbookId)->delete();
  471. }
  472. foreach ($catalogData as $index => $node) {
  473. try {
  474. $depth = (int) ($node['depth'] ?? 1);
  475. $parentId = $node['parent_id'] ?? null;
  476. $title = trim((string) ($node['title'] ?? ''));
  477. if ($title === '') {
  478. throw new \Exception('目录标题不能为空');
  479. }
  480. $tags = $node['tags'] ?? null;
  481. if (is_array($tags)) {
  482. $tags = empty($tags) ? null : json_encode($tags, JSON_UNESCAPED_UNICODE);
  483. } elseif (is_string($tags) && $tags !== '') {
  484. json_decode($tags, true);
  485. if (json_last_error() !== JSON_ERROR_NONE) {
  486. $tags = null;
  487. }
  488. }
  489. $meta = $node['meta'] ?? null;
  490. if (is_array($meta)) {
  491. $meta = empty($meta) ? null : json_encode($meta, JSON_UNESCAPED_UNICODE);
  492. } elseif (is_string($meta) && $meta !== '') {
  493. json_decode($meta, true);
  494. if (json_last_error() !== JSON_ERROR_NONE) {
  495. $meta = null;
  496. }
  497. }
  498. if (!$parentId && $depth > 1) {
  499. $parentId = $parentStack[$depth - 1] ?? null;
  500. }
  501. $payload = [
  502. 'textbook_id' => $textbookId,
  503. 'parent_id' => $parentId,
  504. 'node_type' => $node['node_type'] ?? 'chapter',
  505. 'title' => $title,
  506. 'display_no' => $node['display_no'] ?? null,
  507. 'depth' => $depth,
  508. 'sort_order' => (int) ($node['sort_order'] ?? 0),
  509. 'path_key' => $node['path_key'] ?? null,
  510. 'is_required' => (bool) ($node['is_required'] ?? false),
  511. 'is_elective' => (bool) ($node['is_elective'] ?? false),
  512. 'tags' => $tags,
  513. 'page_start' => $node['page_start'] ?? null,
  514. 'page_end' => $node['page_end'] ?? null,
  515. 'meta' => $meta,
  516. ];
  517. $record = TextbookCatalog::create($payload);
  518. $parentStack[$depth] = $record->id;
  519. foreach (array_keys($parentStack) as $stackDepth) {
  520. if ($stackDepth > $depth) {
  521. unset($parentStack[$stackDepth]);
  522. }
  523. }
  524. $successCount++;
  525. } catch (\Throwable $e) {
  526. $errorCount++;
  527. $errors[] = '第' . ($index + 2) . '行: ' . $e->getMessage();
  528. }
  529. }
  530. });
  531. return [
  532. 'success' => $errorCount === 0,
  533. 'success_count' => $successCount,
  534. 'error_count' => $errorCount,
  535. 'errors' => $errors,
  536. 'message' => $errorCount === 0 ? '导入完成' : '部分导入失败',
  537. ];
  538. }
  539. try {
  540. // 将数组转换为JSON字符串
  541. $jsonData = json_encode($catalogData, JSON_UNESCAPED_UNICODE);
  542. // 直接发送 JSON 数据
  543. return $this->request('POST', '/textbooks/import/catalog', [
  544. 'textbook_id' => $textbookId,
  545. 'data' => $jsonData,
  546. 'commit_mode' => $commitMode
  547. ]);
  548. } catch (\Exception $e) {
  549. Log::error('Error importing textbook catalog', ['error' => $e->getMessage()]);
  550. throw $e;
  551. }
  552. }
  553. /**
  554. * 提交导入任务
  555. */
  556. public function commitImportJob(int $jobId): array
  557. {
  558. if ($this->useDatabase) {
  559. return [
  560. 'data' => [],
  561. 'meta' => [
  562. 'status' => 'noop',
  563. 'message' => 'Local import jobs are not tracked yet.',
  564. ],
  565. ];
  566. }
  567. try {
  568. return $this->request('POST', "/api/textbooks/import/{$jobId}/commit");
  569. } catch (\Exception $e) {
  570. Log::error('Error committing import job', ['error' => $e->getMessage()]);
  571. throw $e;
  572. }
  573. }
  574. /**
  575. * 获取导入任务列表
  576. */
  577. public function getImportJobs(array $params = []): array
  578. {
  579. if ($this->useDatabase) {
  580. return ['data' => [], 'meta' => ['total' => 0]];
  581. }
  582. try {
  583. return $this->request('GET', '/textbooks/import/jobs', $params);
  584. } catch (\Exception $e) {
  585. Log::warning('Failed to fetch import jobs, returning empty result', ['error' => $e->getMessage()]);
  586. return ['data' => [], 'meta' => []];
  587. }
  588. }
  589. /**
  590. * 获取单个导入任务
  591. */
  592. public function getImportJob(int $jobId): ?array
  593. {
  594. if ($this->useDatabase) {
  595. return null;
  596. }
  597. try {
  598. $result = $this->request('GET', "/api/textbooks/import/jobs/{$jobId}");
  599. return $result['data'] ?? null;
  600. } catch (\Exception $e) {
  601. Log::warning('Import job not found or error occurred', ['error' => $e->getMessage()]);
  602. return null;
  603. }
  604. }
  605. /**
  606. * 完全同步教材系列到题库服务(清空+重新插入)
  607. */
  608. public function syncTextbookSeriesToQuestionBank(): array
  609. {
  610. if ($this->useDatabase) {
  611. return ['data' => [], 'meta' => ['status' => 'noop']];
  612. }
  613. try {
  614. // 获取MySQL中的所有系列
  615. $mysqlSeries = DB::connection('mysql')
  616. ->table('textbook_series')
  617. ->orderBy('id')
  618. ->get();
  619. if ($mysqlSeries->isEmpty()) {
  620. return [
  621. 'success' => false,
  622. 'message' => 'MySQL中没有找到教材系列数据'
  623. ];
  624. }
  625. // 准备数据
  626. $seriesData = [];
  627. foreach ($mysqlSeries as $series) {
  628. $seriesData[] = [
  629. 'id' => $series->id,
  630. 'name' => $series->name,
  631. 'slug' => $series->slug,
  632. 'publisher' => $series->publisher,
  633. 'region' => $series->region,
  634. 'stages' => json_decode($series->stages, true),
  635. 'is_active' => (bool)$series->is_active,
  636. 'sort_order' => (int)$series->sort_order,
  637. 'meta' => json_decode($series->meta, true),
  638. ];
  639. }
  640. // 调用API进行完全同步
  641. $result = $this->request('POST', '/textbooks/series/sync-all', [
  642. 'series' => $seriesData
  643. ]);
  644. return [
  645. 'success' => true,
  646. 'synced_count' => count($seriesData),
  647. 'data' => $result
  648. ];
  649. } catch (\Exception $e) {
  650. Log::error('Error syncing textbook series', ['error' => $e->getMessage()]);
  651. return [
  652. 'success' => false,
  653. 'message' => '同步失败: ' . $e->getMessage()
  654. ];
  655. }
  656. }
  657. /**
  658. * 根据教材系列、年级和学期获取教材及其目录结构
  659. *
  660. * @param int $seriesId 教材系列ID
  661. * @param int $grade 年级(1-12)
  662. * @param int $semesterCode 学期代码(1=上册,2=下册)
  663. * @param string $catalogFormat 目录格式(tree 或 flat)
  664. * @return array 包含教材信息和目录结构的数组
  665. */
  666. public function getTextbookByFilter(int $seriesId, int $grade, int $semesterCode, string $catalogFormat = 'tree'): array
  667. {
  668. if ($this->useDatabase) {
  669. try {
  670. // 先查找符合条件且已发布的教材
  671. $query = Textbook::query()
  672. ->select('textbooks.*')
  673. ->join('textbook_series', 'textbooks.series_id', '=', 'textbook_series.id')
  674. ->where('textbooks.series_id', $seriesId)
  675. ->where('textbooks.grade', $grade)
  676. ->where('textbooks.semester', $semesterCode)
  677. ->where('textbooks.status', 'published')
  678. ->where('textbook_series.is_active', true);
  679. $textbook = $query->first();
  680. if (!$textbook) {
  681. return [
  682. 'success' => false,
  683. 'message' => '未找到符合条件的教材',
  684. 'data' => null,
  685. 'meta' => [
  686. 'series_id' => $seriesId,
  687. 'grade' => $grade,
  688. 'semester_code' => $semesterCode,
  689. 'semester_label' => $this->getSemesterLabel($semesterCode),
  690. ]
  691. ];
  692. }
  693. // 获取教材目录
  694. $catalog = $this->getTextbookCatalog($textbook->id, $catalogFormat);
  695. // 格式化教材信息
  696. $textbookData = [
  697. 'id' => $textbook->id,
  698. 'name' => $textbook->official_title ?? '',
  699. 'display_name' => $textbook->official_title ?? '',
  700. 'cover' => $this->formatCoverUrl($textbook->cover_path ?? ''),
  701. 'series_id' => $textbook->series_id,
  702. 'series_name' => $textbook->series->name ?? '',
  703. 'publisher' => $textbook->series->publisher ?? '',
  704. 'stage' => $this->getStageLabel($textbook->stage ?? ''),
  705. 'stage_code' => $textbook->stage ?? '',
  706. 'grade' => $textbook->grade,
  707. 'grade_label' => $this->getGradeLabel($textbook->grade, $textbook->stage ?? ''),
  708. 'semester' => $this->getSemesterLabel($textbook->semester),
  709. 'semester_code' => $textbook->semester,
  710. 'module_type' => $textbook->module_type ?? null,
  711. 'volume_no' => $textbook->volume_no ?? null,
  712. 'isbn' => $textbook->isbn ?? '',
  713. 'approval_year' => $textbook->approval_year ?? null,
  714. 'curriculum_standard_year' => $textbook->curriculum_standard_year ?? null,
  715. 'status' => $textbook->status ?? 'draft',
  716. 'sort_order' => $textbook->sort_order ?? 0,
  717. ];
  718. return [
  719. 'success' => true,
  720. 'data' => [
  721. 'textbook' => $textbookData,
  722. 'catalog' => $catalog,
  723. ],
  724. 'meta' => [
  725. 'series_id' => $seriesId,
  726. 'grade' => $grade,
  727. 'grade_label' => $textbookData['grade_label'],
  728. 'semester_code' => $semesterCode,
  729. 'semester_label' => $textbookData['semester'],
  730. 'catalog_format' => $catalogFormat,
  731. ]
  732. ];
  733. } catch (\Exception $e) {
  734. Log::error('获取教材及目录失败', [
  735. 'series_id' => $seriesId,
  736. 'grade' => $grade,
  737. 'semester_code' => $semesterCode,
  738. 'error' => $e->getMessage()
  739. ]);
  740. return [
  741. 'success' => false,
  742. 'message' => '获取教材信息失败: ' . $e->getMessage(),
  743. 'data' => null,
  744. ];
  745. }
  746. }
  747. // 外部API调用(如果需要)
  748. try {
  749. $result = $this->request('GET', '/textbooks/filter', [
  750. 'series_id' => $seriesId,
  751. 'grade' => $grade,
  752. 'semester_code' => $semesterCode,
  753. 'catalog_format' => $catalogFormat,
  754. ]);
  755. return [
  756. 'success' => true,
  757. 'data' => $result['data'] ?? null,
  758. 'meta' => $result['meta'] ?? []
  759. ];
  760. } catch (\Exception $e) {
  761. Log::warning('Failed to fetch textbook by filter, returning empty result', ['error' => $e->getMessage()]);
  762. return [
  763. 'success' => false,
  764. 'message' => '获取教材信息失败: ' . $e->getMessage(),
  765. 'data' => null,
  766. ];
  767. }
  768. }
  769. /**
  770. * 获取学期标签
  771. */
  772. private function getSemesterLabel(?int $semester): string
  773. {
  774. return match ($semester) {
  775. 1 => '上册',
  776. 2 => '下册',
  777. default => '',
  778. };
  779. }
  780. /**
  781. * 获取学段标签
  782. */
  783. private function getStageLabel(string $stage): string
  784. {
  785. return match ($stage) {
  786. 'primary' => '小学',
  787. 'junior' => '初中',
  788. 'senior' => '高中',
  789. default => $stage,
  790. };
  791. }
  792. /**
  793. * 格式化封面URL
  794. */
  795. private function formatCoverUrl(?string $coverPath): string
  796. {
  797. if (empty($coverPath)) {
  798. return '';
  799. }
  800. // cover_path 可能存储为多图逗号拼接,这里默认返回第一张作为封面图。
  801. $coverParts = array_values(array_filter(array_map('trim', explode(',', (string) $coverPath))));
  802. $coverPath = $coverParts[0] ?? '';
  803. if ($coverPath === '') {
  804. return '';
  805. }
  806. // 如果已经是完整URL,直接返回
  807. if (str_starts_with($coverPath, 'http://') || str_starts_with($coverPath, 'https://')) {
  808. return $coverPath;
  809. }
  810. // 本地存储路径,添加域名
  811. return url('/storage/' . ltrim($coverPath, '/'));
  812. }
  813. /**
  814. * 获取年级标签
  815. */
  816. private function getGradeLabel(?int $grade, string $stage): string
  817. {
  818. if ($grade === null) {
  819. return '';
  820. }
  821. return match ($stage) {
  822. 'primary' => $grade . '年级',
  823. 'junior' => match ($grade) {
  824. 7 => '七年级',
  825. 8 => '八年级',
  826. 9 => '九年级',
  827. default => $grade . '年级',
  828. },
  829. 'senior' => match ($grade) {
  830. 10 => '高一',
  831. 11 => '高二',
  832. 12 => '高三',
  833. default => '高' . ($grade - 9),
  834. },
  835. default => $grade . '年级',
  836. };
  837. }
  838. private function buildCatalogTree(array $nodes): array
  839. {
  840. // 建立ID索引,包含children数组
  841. $indexed = [];
  842. foreach ($nodes as &$node) { // 使用引用
  843. $node['children'] = [];
  844. $indexed[$node['id']] = &$node;
  845. }
  846. unset($node); // 释放引用
  847. $tree = [];
  848. foreach ($nodes as &$node) { // 使用引用
  849. $parentId = $node['parent_id'] ?? null;
  850. if ($parentId && isset($indexed[$parentId])) {
  851. // 父节点存在,将当前节点添加到父节点的children中
  852. $indexed[$parentId]['children'][] = &$node;
  853. } else {
  854. // 根节点,直接添加到树中
  855. $tree[] = &$node;
  856. }
  857. }
  858. unset($node); // 释放引用
  859. return $tree;
  860. }
  861. }