TextbookApiService.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  1. <?php
  2. namespace App\Services;
  3. use Illuminate\Support\Facades\Http;
  4. use Illuminate\Support\Facades\Log;
  5. class TextbookApiService
  6. {
  7. protected string $baseUrl;
  8. public function __construct()
  9. {
  10. // QuestionBankService API 地址
  11. $baseUrl = config('services.question_bank.base_url', 'http://localhost:5015/api');
  12. // 移除末尾的 /,保持 baseUrl 以 /api 结尾
  13. $this->baseUrl = rtrim($baseUrl, '/');
  14. }
  15. /**
  16. * 获取教材系列列表
  17. */
  18. public function getTextbookSeries(array $params = []): array
  19. {
  20. try {
  21. $response = Http::timeout(30)->get($this->baseUrl . '/textbooks/series', $params);
  22. if ($response->successful()) {
  23. return $response->json();
  24. }
  25. Log::error('Failed to fetch textbook series', [
  26. 'status' => $response->status(),
  27. 'body' => $response->body()
  28. ]);
  29. return ['data' => [], 'meta' => []];
  30. } catch (\Exception $e) {
  31. Log::error('Error fetching textbook series', ['error' => $e->getMessage()]);
  32. return ['data' => [], 'meta' => []];
  33. }
  34. }
  35. /**
  36. * 创建教材系列
  37. */
  38. public function createTextbookSeries(array $data): array
  39. {
  40. try {
  41. $response = Http::timeout(30)->post($this->baseUrl . '/textbooks/series', $data);
  42. if ($response->successful()) {
  43. return $response->json();
  44. }
  45. Log::error('Failed to create textbook series', [
  46. 'status' => $response->status(),
  47. 'body' => $response->body()
  48. ]);
  49. throw new \Exception('Failed to create textbook series');
  50. } catch (\Exception $e) {
  51. Log::error('Error creating textbook series', ['error' => $e->getMessage()]);
  52. throw $e;
  53. }
  54. }
  55. /**
  56. * 更新教材系列
  57. */
  58. public function updateTextbookSeries(int $seriesId, array $data): array
  59. {
  60. try {
  61. $response = Http::timeout(30)->put($this->baseUrl . "/textbooks/series/{$seriesId}", $data);
  62. if ($response->successful()) {
  63. return $response->json();
  64. }
  65. Log::error('Failed to update textbook series', [
  66. 'status' => $response->status(),
  67. 'body' => $response->body()
  68. ]);
  69. throw new \Exception('Failed to update textbook series');
  70. } catch (\Exception $e) {
  71. Log::error('Error updating textbook series', ['error' => $e->getMessage()]);
  72. throw $e;
  73. }
  74. }
  75. /**
  76. * 删除教材系列
  77. */
  78. public function deleteTextbookSeries(int $seriesId): bool
  79. {
  80. try {
  81. $response = Http::timeout(30)->delete($this->baseUrl . "/textbooks/series/{$seriesId}");
  82. if ($response->successful()) {
  83. return true;
  84. }
  85. Log::error('Failed to delete textbook series', [
  86. 'status' => $response->status(),
  87. 'body' => $response->body()
  88. ]);
  89. return false;
  90. } catch (\Exception $e) {
  91. Log::error('Error deleting textbook series', ['error' => $e->getMessage()]);
  92. return false;
  93. }
  94. }
  95. /**
  96. * 删除教材
  97. */
  98. public function deleteTextbook(int $textbookId): bool
  99. {
  100. try {
  101. $response = Http::timeout(30)->delete($this->baseUrl . "/textbooks/{$textbookId}");
  102. if ($response->successful()) {
  103. return true;
  104. }
  105. Log::error('Failed to delete textbook', [
  106. 'textbook_id' => $textbookId,
  107. 'status' => $response->status(),
  108. 'body' => $response->body()
  109. ]);
  110. return false;
  111. } catch (\Exception $e) {
  112. Log::error('Error deleting textbook', ['error' => $e->getMessage(), 'textbook_id' => $textbookId]);
  113. return false;
  114. }
  115. }
  116. /**
  117. * 获取教材列表
  118. */
  119. public function getTextbooks(array $params = []): array
  120. {
  121. try {
  122. $response = Http::timeout(30)->get($this->baseUrl . '/textbooks', $params);
  123. if ($response->successful()) {
  124. return $response->json();
  125. }
  126. Log::error('Failed to fetch textbooks', [
  127. 'status' => $response->status(),
  128. 'body' => $response->body()
  129. ]);
  130. return ['data' => [], 'meta' => []];
  131. } catch (\Exception $e) {
  132. Log::error('Error fetching textbooks', ['error' => $e->getMessage()]);
  133. return ['data' => [], 'meta' => []];
  134. }
  135. }
  136. /**
  137. * 获取单个教材
  138. */
  139. public function getTextbook(int $textbookId): ?array
  140. {
  141. try {
  142. $response = Http::timeout(30)->get($this->baseUrl . "/textbooks/{$textbookId}");
  143. if ($response->successful()) {
  144. return $response->json('data');
  145. }
  146. Log::error('Failed to fetch textbook', [
  147. 'status' => $response->status(),
  148. 'body' => $response->body()
  149. ]);
  150. return null;
  151. } catch (\Exception $e) {
  152. Log::error('Error fetching textbook', ['error' => $e->getMessage()]);
  153. return null;
  154. }
  155. }
  156. /**
  157. * 创建教材
  158. */
  159. public function createTextbook(array $data): array
  160. {
  161. try {
  162. $response = Http::timeout(30)->post($this->baseUrl . '/textbooks', $data);
  163. if ($response->successful()) {
  164. return $response->json();
  165. }
  166. Log::error('Failed to create textbook', [
  167. 'status' => $response->status(),
  168. 'body' => $response->body()
  169. ]);
  170. throw new \Exception('Failed to create textbook');
  171. } catch (\Exception $e) {
  172. Log::error('Error creating textbook', ['error' => $e->getMessage()]);
  173. throw $e;
  174. }
  175. }
  176. /**
  177. * 更新教材
  178. */
  179. public function updateTextbook(int $textbookId, array $data): array
  180. {
  181. try {
  182. $response = Http::timeout(30)->put($this->baseUrl . "/textbooks/{$textbookId}", $data);
  183. if ($response->successful()) {
  184. return $response->json();
  185. }
  186. Log::error('Failed to update textbook', [
  187. 'status' => $response->status(),
  188. 'body' => $response->body()
  189. ]);
  190. throw new \Exception('Failed to update textbook');
  191. } catch (\Exception $e) {
  192. Log::error('Error updating textbook', ['error' => $e->getMessage()]);
  193. throw $e;
  194. }
  195. }
  196. /**
  197. * 删除教材目录节点
  198. */
  199. public function deleteTextbookCatalog(int $catalogId): bool
  200. {
  201. try {
  202. $response = Http::timeout(30)->delete($this->baseUrl . "/textbooks/catalog/{$catalogId}");
  203. if ($response->successful()) {
  204. return true;
  205. }
  206. Log::error('Failed to delete textbook catalog', [
  207. 'catalog_id' => $catalogId,
  208. 'status' => $response->status(),
  209. 'body' => $response->body()
  210. ]);
  211. return false;
  212. } catch (\Exception $e) {
  213. Log::error('Error deleting textbook catalog', ['error' => $e->getMessage(), 'catalog_id' => $catalogId]);
  214. return false;
  215. }
  216. }
  217. /**
  218. * 获取教材目录树
  219. */
  220. public function getTextbookCatalog(int $textbookId, string $format = 'tree'): array
  221. {
  222. try {
  223. $response = Http::timeout(30)->get($this->baseUrl . "/textbooks/{$textbookId}/catalog", [
  224. 'format' => $format
  225. ]);
  226. if ($response->successful()) {
  227. return $response->json('data');
  228. }
  229. Log::error('Failed to fetch textbook catalog', [
  230. 'status' => $response->status(),
  231. 'body' => $response->body()
  232. ]);
  233. return [];
  234. } catch (\Exception $e) {
  235. Log::error('Error fetching textbook catalog', ['error' => $e->getMessage()]);
  236. return [];
  237. }
  238. }
  239. /**
  240. * 预览教材命名
  241. */
  242. public function previewTextbookNaming(array $textbookData, array $seriesData): array
  243. {
  244. try {
  245. $response = Http::timeout(30)->post($this->baseUrl . '/textbooks/naming-preview', [
  246. 'textbook' => $textbookData,
  247. 'series' => $seriesData
  248. ]);
  249. if ($response->successful()) {
  250. return $response->json('data');
  251. }
  252. Log::error('Failed to preview textbook naming', [
  253. 'status' => $response->status(),
  254. 'body' => $response->body()
  255. ]);
  256. return [];
  257. } catch (\Exception $e) {
  258. Log::error('Error previewing textbook naming', ['error' => $e->getMessage()]);
  259. return [];
  260. }
  261. }
  262. /**
  263. * 导入教材元信息
  264. */
  265. public function importTextbookMetadata($file, string $commitMode = 'overwrite'): array
  266. {
  267. try {
  268. $response = Http::timeout(300)
  269. ->attach('file', file_get_contents($file->getPathname()), $file->getClientOriginalName())
  270. ->post($this->baseUrl . '/textbooks/import/meta', [
  271. 'commit_mode' => $commitMode
  272. ]);
  273. if ($response->successful()) {
  274. return $response->json();
  275. }
  276. Log::error('Failed to import textbook metadata', [
  277. 'status' => $response->status(),
  278. 'body' => $response->body()
  279. ]);
  280. throw new \Exception('Failed to import textbook metadata');
  281. } catch (\Exception $e) {
  282. Log::error('Error importing textbook metadata', ['error' => $e->getMessage()]);
  283. throw $e;
  284. }
  285. }
  286. /**
  287. * 导入教材目录
  288. */
  289. public function importTextbookCatalog(int $textbookId, $file, string $commitMode = 'overwrite'): array
  290. {
  291. try {
  292. $response = Http::timeout(300)
  293. ->attach('file', file_get_contents($file->getPathname()), $file->getClientOriginalName())
  294. ->post($this->baseUrl . '/textbooks/import/catalog', [
  295. 'textbook_id' => $textbookId,
  296. 'commit_mode' => $commitMode
  297. ]);
  298. if ($response->successful()) {
  299. return $response->json();
  300. }
  301. Log::error('Failed to import textbook catalog', [
  302. 'status' => $response->status(),
  303. 'body' => $response->body()
  304. ]);
  305. throw new \Exception('Failed to import textbook catalog');
  306. } catch (\Exception $e) {
  307. Log::error('Error importing textbook catalog', ['error' => $e->getMessage()]);
  308. throw $e;
  309. }
  310. }
  311. /**
  312. * 提交导入任务
  313. */
  314. public function commitImportJob(int $jobId): array
  315. {
  316. try {
  317. $response = Http::timeout(300)->post($this->baseUrl . "/api/textbooks/import/{$jobId}/commit");
  318. if ($response->successful()) {
  319. return $response->json();
  320. }
  321. Log::error('Failed to commit import job', [
  322. 'status' => $response->status(),
  323. 'body' => $response->body()
  324. ]);
  325. throw new \Exception('Failed to commit import job');
  326. } catch (\Exception $e) {
  327. Log::error('Error committing import job', ['error' => $e->getMessage()]);
  328. throw $e;
  329. }
  330. }
  331. /**
  332. * 获取导入任务列表
  333. */
  334. public function getImportJobs(array $params = []): array
  335. {
  336. try {
  337. $response = Http::timeout(30)->get($this->baseUrl . '/textbooks/import/jobs', $params);
  338. if ($response->successful()) {
  339. return $response->json();
  340. }
  341. Log::error('Failed to fetch import jobs', [
  342. 'status' => $response->status(),
  343. 'body' => $response->body()
  344. ]);
  345. return ['data' => [], 'meta' => []];
  346. } catch (\Exception $e) {
  347. Log::error('Error fetching import jobs', ['error' => $e->getMessage()]);
  348. return ['data' => [], 'meta' => []];
  349. }
  350. }
  351. /**
  352. * 获取单个导入任务
  353. */
  354. public function getImportJob(int $jobId): ?array
  355. {
  356. try {
  357. $response = Http::timeout(30)->get($this->baseUrl . "/api/textbooks/import/jobs/{$jobId}");
  358. if ($response->successful()) {
  359. return $response->json('data');
  360. }
  361. Log::error('Failed to fetch import job', [
  362. 'status' => $response->status(),
  363. 'body' => $response->body()
  364. ]);
  365. return null;
  366. } catch (\Exception $e) {
  367. Log::error('Error fetching import job', ['error' => $e->getMessage()]);
  368. return null;
  369. }
  370. }
  371. }