'S01', 'name' => '甲', 'score' => 98.0, 'submit_ts' => 1000, 'last_submit_ts' => 5000, 'last_submit_at' => '2026-09-10 12:00:00'], ['sid' => 'S02', 'name' => '乙', 'score' => 90.0, 'submit_ts' => 2000, 'last_submit_ts' => 2000, 'last_submit_at' => '2026-09-10 10:00:00'], ]; $sortTs = static fn(array $r): int => (int)($r['last_submit_ts'] ?? 0) ?: (int)($r['submit_ts'] ?? 0); usort($aggRows, static function (array $x, array $y) use ($sortTs): int { $d = $sortTs($y) <=> $sortTs($x); return $d !== 0 ? $d : strcmp((string)($x['sid'] ?? ''), (string)($y['sid'] ?? '')); }); check('重考后的甲(last_submit_ts=5000)排在乙前', $aggRows[0]['sid'] === 'S01', json_encode($aggRows, JSON_UNESCAPED_UNICODE)); /* ============ 2) XlsxWriter 多工作表 ============ */ echo "\n== XlsxWriter 多工作表 ==\n"; $sheets = [ ['name' => '1班', 'rows' => [['学号', '姓名'], ['S01', '甲']], 'widths' => [16, 14]], ['name' => '2班', 'rows' => [['学号', '姓名'], ['S02', '乙']], 'widths' => [16, 14]], ['name' => '成绩明细', 'rows' => [['学号', '成绩'], ['S03', 90]], 'widths' => [16, 11]], ['name' => '成绩明细', 'rows' => [['学号', '成绩'], ['S04', 80]], 'widths' => [16, 11]], // 重名 → 应去重 ['name' => str_repeat('长', 40), 'rows' => [['X'], [1]], 'widths' => [10]], // 超长名 → 截断 ]; $bin = XlsxWriter::renderMulti($sheets); check('renderMulti 产出 PK ZIP', substr($bin, 0, 2) === 'PK'); $names = []; if (class_exists('ZipArchive')) { $tmp = __DIR__ . '/_multi.xlsx'; file_put_contents($tmp, $bin); $z = new ZipArchive(); if ($z->open($tmp) === true) { for ($i = 0; $i < $z->numFiles; $i++) { $n = $z->getNameIndex($i); if (strpos($n, 'xl/worksheets/sheet') === 0) { $names[] = $n; } } $z->close(); } @unlink($tmp); } check('产出 5 个工作表文件', count($names) === 5, 'got ' . count($names) . ': ' . json_encode($names)); check('sheet 文件名连续 sheet1..sheet5', in_array('xl/worksheets/sheet5.xml', $names, true)); /* ============ 3) 名单导入列识别 ============ */ echo "\n== 名单导入列识别 ==\n"; $meta = []; $parsed = Roster::rowsToStudents([ ['学号', '姓名', '班级', '组别', '登陆账号'], ['2024012001.0', '张三', '1班', 'A', 'stu001'], ['2024012002', '李四', '1班', 'B', 'stu002'], ['2024012001', '张三重复', '1班', 'A', 'stu001'], // 学号重复 → 跳过 ['', ''], // 空行 → 跳过 ], $meta); check('识别到 2 人(重复学号与空行被跳过)', count($parsed) === 2, json_encode($parsed, JSON_UNESCAPED_UNICODE)); check('学号去掉 Excel 数字尾巴 .0', ($parsed[0]['sid'] ?? '') === '2024012001', (string)($parsed[0]['sid'] ?? '')); check('识别班级 / 组别 / 登陆账号列', ($parsed[0]['cls'] ?? '') === '1班' && ($parsed[0]['grp'] ?? '') === 'A' && ($parsed[0]['login'] ?? '') === 'stu001', json_encode($parsed[0] ?? [], JSON_UNESCAPED_UNICODE)); check('重复学号记入 duplicates', (int)($meta['duplicates'] ?? 0) === 1, (string)($meta['duplicates'] ?? '')); check('空行记入 skipped', (int)($meta['skipped'] ?? 0) === 1, (string)($meta['skipped'] ?? '')); echo "\n========================================\n"; echo "通过 {$pass} · 失败 {$fail}\n"; exit($fail === 0 ? 0 : 1);