TextbookExcelImporter.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659
  1. <?php
  2. namespace App\Services\Import;
  3. use PhpOffice\PhpSpreadsheet\IOFactory;
  4. use PhpOffice\PhpSpreadsheet\Spreadsheet;
  5. use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
  6. use Illuminate\Support\Facades\Storage;
  7. use Illuminate\Support\Facades\Log;
  8. use App\Services\TextbookApiService;
  9. class TextbookExcelImporter
  10. {
  11. protected $apiService;
  12. public function __construct(TextbookApiService $apiService)
  13. {
  14. $this->apiService = $apiService;
  15. }
  16. /**
  17. * 生成教材系列Excel模板
  18. */
  19. public function generateTextbookSeriesTemplate(): string
  20. {
  21. $spreadsheet = new Spreadsheet();
  22. $sheet = $spreadsheet->getActiveSheet();
  23. // 设置列标题
  24. $headers = [
  25. 'A1' => '系列名称',
  26. 'B1' => '别名',
  27. 'C1' => '出版社',
  28. 'D1' => '适用地区',
  29. 'E1' => '适用学段',
  30. 'F1' => '是否启用',
  31. 'G1' => '排序',
  32. 'H1' => '扩展信息(JSON)',
  33. ];
  34. foreach ($headers as $cell => $header) {
  35. $sheet->setCellValue($cell, $header);
  36. }
  37. // 设置表头样式
  38. $headerStyle = [
  39. 'font' => ['bold' => true, 'color' => ['rgb' => 'FFFFFF']],
  40. 'fill' => ['fillType' => \PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID, 'color' => ['rgb' => '0EA5E9']],
  41. 'alignment' => ['horizontal' => \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER],
  42. ];
  43. $sheet->getStyle('A1:H1')->applyFromArray($headerStyle);
  44. // 设置列宽
  45. $sheet->getColumnDimension('A')->setWidth(20);
  46. $sheet->getColumnDimension('B')->setWidth(15);
  47. $sheet->getColumnDimension('C')->setWidth(25);
  48. $sheet->getColumnDimension('D')->setWidth(20);
  49. $sheet->getColumnDimension('E')->setWidth(20);
  50. $sheet->getColumnDimension('F')->setWidth(12);
  51. $sheet->getColumnDimension('G')->setWidth(10);
  52. $sheet->getColumnDimension('H')->setWidth(30);
  53. // 添加示例数据
  54. $examples = [
  55. ['人教版', 'pep', '人民教育出版社', '全国', '["primary","junior","senior"]', '是', '1', '{"website":"http://www.pep.com.cn"}'],
  56. ['北师大版', 'bsd', '北京师范大学出版社', '全国', '["primary","junior","senior"]', '是', '2', '{}'],
  57. ['苏教版', 'js', '江苏教育出版社', '江苏省', '["primary","junior"]', '是', '3', '{}'],
  58. ];
  59. for ($i = 0; $i < count($examples); $i++) {
  60. $row = $i + 2;
  61. for ($j = 0; $j < count($examples[$i]); $j++) {
  62. $column = chr(65 + $j); // A, B, C...
  63. $sheet->setCellValue($column . $row, $examples[$i][$j]);
  64. }
  65. }
  66. // 添加说明信息
  67. $sheet->setCellValue('A6', '填写说明:');
  68. $sheet->setCellValue('A7', '1. 系列名称: 必填,如"人教版"、"北师大版"');
  69. $sheet->setCellValue('A8', '2. 别名: 必填,唯一标识,如"pep"、"bsd"');
  70. $sheet->setCellValue('A9', '3. 出版社: 必填,如"人民教育出版社"');
  71. $sheet->setCellValue('A10', '4. 适用地区: 可选,如"全国"、"江苏省"');
  72. $sheet->setCellValue('A11', '5. 适用学段: 必填,JSON格式["primary","junior","senior"]');
  73. $sheet->setCellValue('A12', '6. 是否启用: 填"是"或"否"');
  74. $sheet->setCellValue('A13', '7. 排序: 数字,越小越靠前');
  75. $sheet->setCellValue('A14', '8. 扩展信息: JSON格式,可选');
  76. // 保存文件
  77. $fileName = 'textbook_series_template_' . date('Ymd_His') . '.xlsx';
  78. $filePath = storage_path('app/templates/' . $fileName);
  79. // 确保目录存在
  80. if (!is_dir(dirname($filePath))) {
  81. mkdir(dirname($filePath), 0755, true);
  82. }
  83. $writer = new Xlsx($spreadsheet);
  84. $writer->save($filePath);
  85. return $filePath;
  86. }
  87. /**
  88. * 生成教材Excel模板
  89. */
  90. public function generateTextbookTemplate(): string
  91. {
  92. $spreadsheet = new Spreadsheet();
  93. $sheet = $spreadsheet->getActiveSheet();
  94. // 设置列标题
  95. $headers = [
  96. 'A1' => '系列ID',
  97. 'B1' => '学段',
  98. 'C1' => '年级',
  99. 'D1' => '学期',
  100. 'E1' => '命名体系',
  101. 'F1' => '版本',
  102. 'G1' => '模块类型',
  103. 'H1' => '册次',
  104. 'I1' => '旧体系编码',
  105. 'J1' => '课标年代',
  106. 'K1' => '修订年份',
  107. 'L1' => '审定年份',
  108. 'M1' => '版次标识',
  109. 'N1' => 'ISBN',
  110. 'O1' => '封面路径',
  111. 'P1' => '官方书名',
  112. 'Q1' => '展示名称',
  113. 'R1' => '别名(JSON)',
  114. 'S1' => '状态',
  115. 'T1' => '扩展信息(JSON)',
  116. ];
  117. foreach ($headers as $cell => $header) {
  118. $sheet->setCellValue($cell, $header);
  119. }
  120. // 设置表头样式
  121. $headerStyle = [
  122. 'font' => ['bold' => true, 'color' => ['rgb' => 'FFFFFF']],
  123. 'fill' => ['fillType' => \PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID, 'color' => ['rgb' => '0EA5E9']],
  124. 'alignment' => ['horizontal' => \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER],
  125. ];
  126. $sheet->getStyle('A1:T1')->applyFromArray($headerStyle);
  127. // 设置列宽
  128. $columnWidths = [10, 12, 10, 10, 12, 10, 15, 10, 15, 12, 12, 12, 15, 15, 20, 30, 30, 20, 12, 30];
  129. foreach ($columnWidths as $i => $width) {
  130. $sheet->getColumnDimensionByColumn($i + 1)->setWidth($width);
  131. }
  132. // 添加示例数据
  133. $examples = [
  134. ['1', '小学', '1', '上册', '', '', '', '', '', '2011', '', '', '', '', '', '人教版小学数学一年级上册', '', '[]', '草稿', '{}'],
  135. ['1', '小学', '1', '下册', '', '', '', '', '', '2011', '', '', '', '', '', '人教版小学数学一年级下册', '', '[]', '草稿', '{}'],
  136. ['1', '初中', '7', '上册', '', '', '', '', '', '2022', '', '', '', '', '', '人教版初中数学七年级上册', '', '[]', '草稿', '{}'],
  137. ['1', '高中', '10', '', '新体系', 'A版', '必修', '1', '', '2017', '2020', '', '', '', '', '人教A版高中数学必修第一册', '', '[]', '草稿', '{}'],
  138. ];
  139. for ($i = 0; $i < count($examples); $i++) {
  140. $row = $i + 2;
  141. for ($j = 0; $j < count($examples[$i]); $j++) {
  142. $column = chr(65 + $j); // A, B, C...
  143. $sheet->setCellValue($column . $row, $examples[$i][$j]);
  144. }
  145. }
  146. // 添加说明信息
  147. $sheet->setCellValue('A7', '填写说明:');
  148. $sheet->setCellValue('A8', '1. 系列ID: 必填,对应教材系列的ID(可在教材系列列表查看)');
  149. $sheet->setCellValue('A9', '2. 学段: 必填,小学/初中/高中');
  150. $sheet->setCellValue('A10', '3. 年级: 数字,小学1-6,初中7-9,高中10-12');
  151. $sheet->setCellValue('A11', '4. 学期: 上册/下册(高中可留空)');
  152. $sheet->setCellValue('A12', '5. 命名体系: 新体系/旧体系,高中适用');
  153. $sheet->setCellValue('A13', '6. 版本: A版/B版,高中新体系适用');
  154. $sheet->setCellValue('A14', '7. 模块类型: 必修/选择性必修/选修,高中适用');
  155. $sheet->setCellValue('A15', '8. 册次: 数字,高中新体系适用');
  156. $sheet->setCellValue('A16', '9. 旧体系编码: 高中旧体系适用,如"必修1"');
  157. $sheet->setCellValue('A17', '10. 课标年代: 义务教育2011/2022,高中2017');
  158. $sheet->setCellValue('A18', '11. 修订年份: 高中为2020');
  159. $sheet->setCellValue('A19', '12. 审定年份: 如2024');
  160. $sheet->setCellValue('A20', '13. 版次标识: 如"2024秋版"');
  161. $sheet->setCellValue('A21', '14. ISBN: 可选');
  162. $sheet->setCellValue('A22', '15. 封面路径: 可选');
  163. $sheet->setCellValue('A23', '16. 官方书名: 可选,系统自动生成');
  164. $sheet->setCellValue('A24', '17. 展示名称: 可选,站内显示');
  165. $sheet->setCellValue('A25', '18. 别名: JSON格式,可选');
  166. $sheet->setCellValue('A26', '19. 状态: 草稿/已发布/已归档');
  167. $sheet->setCellValue('A27', '20. 扩展信息: JSON格式,可选');
  168. // 保存文件
  169. $fileName = 'textbook_template_' . date('Ymd_His') . '.xlsx';
  170. $filePath = storage_path('app/templates/' . $fileName);
  171. if (!is_dir(dirname($filePath))) {
  172. mkdir(dirname($filePath), 0755, true);
  173. }
  174. $writer = new Xlsx($spreadsheet);
  175. $writer->save($filePath);
  176. return $filePath;
  177. }
  178. /**
  179. * 导入教材系列Excel文件
  180. */
  181. public function importTextbookSeries(string $filePath): array
  182. {
  183. try {
  184. $spreadsheet = IOFactory::load($filePath);
  185. $sheet = $spreadsheet->getActiveSheet();
  186. $data = $sheet->toArray();
  187. // 跳过标题行
  188. $rows = array_slice($data, 1);
  189. $successCount = 0;
  190. $errorCount = 0;
  191. $errors = [];
  192. foreach ($rows as $index => $row) {
  193. try {
  194. // 验证必填字段
  195. if (empty($row[0]) || empty($row[1]) || empty($row[2]) || empty($row[4])) {
  196. throw new \Exception('必填字段不能为空');
  197. }
  198. // 解析适用学段
  199. $stages = json_decode($row[4], true);
  200. if (!is_array($stages)) {
  201. throw new \Exception('适用学段必须是JSON数组格式');
  202. }
  203. // 解析是否启用
  204. $isActive = $row[5] === '是' || $row[5] === 'true' || $row[5] === '1';
  205. // 解析扩展信息
  206. $meta = !empty($row[7]) ? json_decode($row[7], true) : [];
  207. if (!is_array($meta)) {
  208. $meta = [];
  209. }
  210. $textbookSeriesData = [
  211. 'name' => $row[0],
  212. 'slug' => $row[1],
  213. 'publisher' => $row[2],
  214. 'region' => $row[3] ?: null,
  215. 'stages' => json_encode($stages),
  216. 'is_active' => $isActive,
  217. 'sort_order' => (int)($row[6] ?: 0),
  218. 'meta' => json_encode($meta),
  219. ];
  220. // 通过API创建教材系列
  221. $result = $this->apiService->createTextbookSeries($textbookSeriesData);
  222. if ($result && isset($result['data'])) {
  223. $successCount++;
  224. } else {
  225. throw new \Exception('API创建失败');
  226. }
  227. } catch (\Exception $e) {
  228. $errorCount++;
  229. $errors[] = "第" . ($index + 2) . "行: " . $e->getMessage();
  230. }
  231. }
  232. return [
  233. 'success' => true,
  234. 'success_count' => $successCount,
  235. 'error_count' => $errorCount,
  236. 'errors' => $errors,
  237. ];
  238. } catch (\Exception $e) {
  239. Log::error('Excel导入失败', ['error' => $e->getMessage()]);
  240. return [
  241. 'success' => false,
  242. 'message' => '文件解析失败: ' . $e->getMessage(),
  243. ];
  244. }
  245. }
  246. /**
  247. * 导入教材Excel文件
  248. */
  249. public function importTextbook(string $filePath): array
  250. {
  251. // 中文到英文的映射
  252. $stageMap = [
  253. '小学' => 'primary',
  254. '初中' => 'junior',
  255. '高中' => 'senior',
  256. // 兼容英文输入
  257. 'primary' => 'primary',
  258. 'junior' => 'junior',
  259. 'senior' => 'senior',
  260. ];
  261. $semesterMap = [
  262. '上册' => 1,
  263. '下册' => 2,
  264. '1' => 1,
  265. '2' => 2,
  266. ];
  267. $namingSchemeMap = [
  268. '新体系' => 'new',
  269. '旧体系' => 'old',
  270. 'new' => 'new',
  271. 'old' => 'old',
  272. ];
  273. $trackMap = [
  274. 'A版' => 'A',
  275. 'B版' => 'B',
  276. 'A' => 'A',
  277. 'B' => 'B',
  278. ];
  279. $statusMap = [
  280. '草稿' => 'draft',
  281. '已发布' => 'published',
  282. '已归档' => 'archived',
  283. 'draft' => 'draft',
  284. 'published' => 'published',
  285. 'archived' => 'archived',
  286. ];
  287. try {
  288. $spreadsheet = IOFactory::load($filePath);
  289. $sheet = $spreadsheet->getActiveSheet();
  290. $data = $sheet->toArray();
  291. // 跳过标题行
  292. $rows = array_slice($data, 1);
  293. $successCount = 0;
  294. $errorCount = 0;
  295. $errors = [];
  296. foreach ($rows as $index => $row) {
  297. try {
  298. // 跳过空行
  299. if (empty($row[0]) && empty($row[1])) {
  300. continue;
  301. }
  302. // 验证必填字段
  303. if (empty($row[0]) || empty($row[1])) {
  304. throw new \Exception('系列ID和学段不能为空');
  305. }
  306. // 转换学段
  307. $stageInput = trim($row[1]);
  308. $stage = $stageMap[$stageInput] ?? null;
  309. if (!$stage) {
  310. throw new \Exception("无效的学段: {$stageInput},请填写:小学/初中/高中");
  311. }
  312. // 转换学期
  313. $semester = null;
  314. if (!empty($row[3])) {
  315. $semesterInput = trim($row[3]);
  316. $semester = $semesterMap[$semesterInput] ?? null;
  317. if ($semester === null) {
  318. throw new \Exception("无效的学期: {$semesterInput},请填写:上册/下册");
  319. }
  320. }
  321. // 转换命名体系
  322. $namingScheme = null;
  323. if (!empty($row[4])) {
  324. $namingInput = trim($row[4]);
  325. $namingScheme = $namingSchemeMap[$namingInput] ?? null;
  326. }
  327. // 转换版本
  328. $track = null;
  329. if (!empty($row[5])) {
  330. $trackInput = trim($row[5]);
  331. $track = $trackMap[$trackInput] ?? $trackInput;
  332. }
  333. // 转换状态
  334. $statusInput = trim($row[18] ?? 'draft');
  335. $status = $statusMap[$statusInput] ?? 'draft';
  336. // 解析别名
  337. $aliases = !empty($row[17]) ? json_decode($row[17], true) : [];
  338. if (!is_array($aliases)) {
  339. $aliases = [];
  340. }
  341. // 解析扩展信息
  342. $meta = !empty($row[19]) ? json_decode($row[19], true) : [];
  343. if (!is_array($meta)) {
  344. $meta = [];
  345. }
  346. $textbookData = [
  347. 'series_id' => (int)$row[0],
  348. 'stage' => $stage,
  349. 'grade' => $row[2] ?: null,
  350. 'semester' => $semester,
  351. 'naming_scheme' => $namingScheme,
  352. 'track' => $track,
  353. 'module_type' => $row[6] ?: null,
  354. 'volume_no' => $row[7] ? (int)$row[7] : null,
  355. 'legacy_code' => $row[8] ?: null,
  356. 'curriculum_standard_year' => $row[9] ?: null,
  357. 'curriculum_revision_year' => $row[10] ?: null,
  358. 'approval_year' => $row[11] ?: null,
  359. 'edition_label' => $row[12] ?: null,
  360. 'isbn' => $row[13] ?: null,
  361. 'cover_path' => $row[14] ?: null,
  362. 'official_title' => $row[15] ?: null,
  363. 'display_title' => $row[16] ?: null,
  364. 'aliases' => json_encode($aliases),
  365. 'status' => $status,
  366. 'meta' => json_encode($meta),
  367. ];
  368. // 通过API创建教材
  369. $result = $this->apiService->createTextbook($textbookData);
  370. if ($result && isset($result['data'])) {
  371. $successCount++;
  372. } else {
  373. throw new \Exception('API创建失败');
  374. }
  375. } catch (\Exception $e) {
  376. $errorCount++;
  377. $errors[] = "第" . ($index + 2) . "行: " . $e->getMessage();
  378. }
  379. }
  380. return [
  381. 'success' => true,
  382. 'success_count' => $successCount,
  383. 'error_count' => $errorCount,
  384. 'errors' => $errors,
  385. ];
  386. } catch (\Exception $e) {
  387. Log::error('Excel导入失败', ['error' => $e->getMessage()]);
  388. return [
  389. 'success' => false,
  390. 'message' => '文件解析失败: ' . $e->getMessage(),
  391. ];
  392. }
  393. }
  394. /**
  395. * 生成教材目录Excel模板
  396. */
  397. public function generateTextbookCatalogTemplate(): string
  398. {
  399. $spreadsheet = new Spreadsheet();
  400. $sheet = $spreadsheet->getActiveSheet();
  401. // 设置列标题
  402. $headers = [
  403. 'A1' => '教材ID',
  404. 'B1' => '目录标题',
  405. 'C1' => '显示编号',
  406. 'D1' => '节点类型',
  407. 'E1' => '层级深度',
  408. 'F1' => '排序',
  409. 'G1' => '父级ID',
  410. 'H1' => '路径键',
  411. 'I1' => '起始页码',
  412. 'J1' => '结束页码',
  413. 'K1' => '是否必修',
  414. 'L1' => '是否选修',
  415. 'M1' => '标签(JSON)',
  416. 'N1' => '扩展信息(JSON)',
  417. ];
  418. foreach ($headers as $cell => $header) {
  419. $sheet->setCellValue($cell, $header);
  420. }
  421. // 设置表头样式
  422. $headerStyle = [
  423. 'font' => ['bold' => true, 'color' => ['rgb' => 'FFFFFF']],
  424. 'fill' => ['fillType' => \PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID, 'color' => ['rgb' => '0EA5E9']],
  425. 'alignment' => ['horizontal' => \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER],
  426. ];
  427. $sheet->getStyle('A1:N1')->applyFromArray($headerStyle);
  428. // 设置列宽
  429. $columnWidths = [12, 30, 15, 15, 12, 10, 12, 25, 12, 12, 12, 12, 20, 30];
  430. foreach ($columnWidths as $i => $width) {
  431. $sheet->getColumnDimensionByColumn($i + 1)->setWidth($width);
  432. }
  433. // 添加示例数据(使用中文节点类型)
  434. $examples = [
  435. ['1', '第一章 有理数', '1', '章', '1', '1', '', 'chapter-1', '1', '20', '是', '否', '[]', '{}'],
  436. ['1', '1.1 正数和负数', '1.1', '节', '2', '1', '', 'chapter-1-section-1', '2', '5', '是', '否', '[]', '{}'],
  437. ['1', '1.2 有理数', '1.2', '节', '2', '2', '', 'chapter-1-section-2', '6', '10', '是', '否', '[]', '{}'],
  438. ['1', '1.2.1 有理数的概念', '1.2.1', '小节', '3', '1', '', 'chapter-1-section-2-sub-1', '6', '7', '是', '否', '[]', '{}'],
  439. ['1', '第二章 整式的加减', '2', '章', '1', '2', '', 'chapter-2', '21', '40', '是', '否', '[]', '{}'],
  440. ];
  441. for ($i = 0; $i < count($examples); $i++) {
  442. $row = $i + 2;
  443. for ($j = 0; $j < count($examples[$i]); $j++) {
  444. $column = chr(65 + $j); // A, B, C...
  445. $sheet->setCellValue($column . $row, $examples[$i][$j]);
  446. }
  447. }
  448. // 添加说明信息
  449. $sheet->setCellValue('A8', '填写说明:');
  450. $sheet->setCellValue('A9', '1. 教材ID: 必填,对应教材的ID(可在教材列表查看)');
  451. $sheet->setCellValue('A10', '2. 目录标题: 必填,如"第一章 有理数"');
  452. $sheet->setCellValue('A11', '3. 显示编号: 可选,如"1"、"1.1"、"1.2.1"');
  453. $sheet->setCellValue('A12', '4. 节点类型: 章/节/小节/条目/项目学习');
  454. $sheet->setCellValue('A13', '5. 层级深度: 数字,1=章,2=节,3=小节');
  455. $sheet->setCellValue('A14', '6. 排序: 数字,同级内排序');
  456. $sheet->setCellValue('A15', '7. 父级ID: 可选,上级节点的ID(首次导入可留空,系统自动处理)');
  457. $sheet->setCellValue('A16', '8. 路径键: 可选,层级路径标识');
  458. $sheet->setCellValue('A17', '9. 起始页码: 数字,可选');
  459. $sheet->setCellValue('A18', '10. 结束页码: 数字,可选');
  460. $sheet->setCellValue('A19', '11. 是否必修: 是/否');
  461. $sheet->setCellValue('A20', '12. 是否选修: 是/否');
  462. $sheet->setCellValue('A21', '13. 标签: JSON格式,可选');
  463. $sheet->setCellValue('A22', '14. 扩展信息: JSON格式,可选');
  464. // 保存文件
  465. $fileName = 'textbook_catalog_template_' . date('Ymd_His') . '.xlsx';
  466. $filePath = storage_path('app/templates/' . $fileName);
  467. if (!is_dir(dirname($filePath))) {
  468. mkdir(dirname($filePath), 0755, true);
  469. }
  470. $writer = new Xlsx($spreadsheet);
  471. $writer->save($filePath);
  472. return $filePath;
  473. }
  474. /**
  475. * 导入教材目录Excel文件
  476. */
  477. public function importTextbookCatalog(string $filePath, int $textbookId): array
  478. {
  479. // 中文到英文的映射
  480. $nodeTypeMap = [
  481. '章' => 'chapter',
  482. '节' => 'section',
  483. '小节' => 'subsection',
  484. '条目' => 'item',
  485. '项目学习' => 'project',
  486. // 兼容英文输入
  487. 'chapter' => 'chapter',
  488. 'section' => 'section',
  489. 'subsection' => 'subsection',
  490. 'item' => 'item',
  491. 'project' => 'project',
  492. ];
  493. try {
  494. // 首先验证Excel文件
  495. $spreadsheet = IOFactory::load($filePath);
  496. $sheet = $spreadsheet->getActiveSheet();
  497. $data = $sheet->toArray();
  498. // 跳过标题行
  499. $rows = array_slice($data, 1);
  500. $successCount = 0;
  501. $errorCount = 0;
  502. $errors = [];
  503. foreach ($rows as $index => $row) {
  504. try {
  505. // 跳过空行
  506. if (empty($row[0]) && empty($row[1])) {
  507. continue;
  508. }
  509. // 验证必填字段
  510. if (empty($row[0]) || empty($row[1])) {
  511. throw new \Exception('教材ID和目录标题不能为空');
  512. }
  513. // 验证教材ID是否匹配
  514. if ((int)$row[0] !== $textbookId) {
  515. throw new \Exception('教材ID必须与选择的教材ID一致');
  516. }
  517. // 转换节点类型
  518. $nodeTypeInput = trim($row[3] ?? '章');
  519. $nodeType = $nodeTypeMap[$nodeTypeInput] ?? 'chapter';
  520. // 解析是否必修/选修
  521. $isRequired = $row[10] === '是' || $row[10] === 'true' || $row[10] === '1';
  522. $isElective = $row[11] === '是' || $row[11] === 'true' || $row[11] === '1';
  523. // 解析标签和扩展信息
  524. $tags = !empty($row[12]) ? json_decode($row[12], true) : [];
  525. if (!is_array($tags)) {
  526. $tags = [];
  527. }
  528. $meta = !empty($row[13]) ? json_decode($row[13], true) : [];
  529. if (!is_array($meta)) {
  530. $meta = [];
  531. }
  532. // 构建目录数据(这里我们只是验证,实际导入通过API完成)
  533. $catalogData = [
  534. 'textbook_id' => (int)$row[0],
  535. 'title' => $row[1],
  536. 'display_no' => $row[2] ?: null,
  537. 'node_type' => $nodeType,
  538. 'depth' => $row[4] ? (int)$row[4] : 1,
  539. 'sort_order' => $row[5] ? (int)$row[5] : 0,
  540. 'parent_id' => $row[6] ?: null,
  541. 'path_key' => $row[7] ?: null,
  542. 'page_start' => $row[8] ? (int)$row[8] : null,
  543. 'page_end' => $row[9] ? (int)$row[9] : null,
  544. 'is_required' => $isRequired,
  545. 'is_elective' => $isElective,
  546. 'tags' => json_encode($tags),
  547. 'meta' => json_encode($meta),
  548. ];
  549. // 验证数据(实际创建通过API完成)
  550. $successCount++;
  551. } catch (\Exception $e) {
  552. $errorCount++;
  553. $errors[] = "第" . ($index + 2) . "行: " . $e->getMessage();
  554. }
  555. }
  556. return [
  557. 'success' => true,
  558. 'success_count' => $successCount,
  559. 'error_count' => $errorCount,
  560. 'errors' => $errors,
  561. ];
  562. } catch (\Exception $e) {
  563. Log::error('Excel导入失败', ['error' => $e->getMessage()]);
  564. return [
  565. 'success' => false,
  566. 'message' => '文件解析失败: ' . $e->getMessage(),
  567. ];
  568. }
  569. }
  570. }