getPdo(); $insPerson = $pdo->prepare( "INSERT INTO persons (union_id, full_name, gender, nationality, id_type, id_number, education, graduated_from, hometown, work_location, source_channel, source_detail) VALUES (?,?,?,?,?,?,?,?,?,?,?,?)" ); $insAccount = $pdo->prepare( "INSERT INTO social_accounts (owner_type, owner_id, platform, account_id, is_defult) VALUES ('person', ?, ?, ?, 1)" ); $inserted = 0; $failed = 0; while (($row = fgetcsv($handle)) !== false) { $row = array_map('trim', $row); $rec = []; foreach ($header as $idx => $col) { $col = trim($col); if (isset($row[$idx])) { $rec[$col] = $row[$idx]; } } if (empty($rec['full_name'])) { $failed++; continue; } try { $unionId = !empty($rec['union_id']) ? $rec['union_id'] : ('P' . date('YmdHis') . substr(uniqid(), -6)); // 跳过重复 union_id $chk = $pdo->prepare("SELECT COUNT(*) FROM persons WHERE union_id = ?"); $chk->execute([$unionId]); if ((int)$chk->fetchColumn() > 0) { $failed++; continue; } $insPerson->execute([ $unionId, $rec['full_name'], $rec['gender'] ?? '未知', $rec['nationality'] ?? null, $rec['id_type'] ?? null, $rec['id_number'] ?? null, $rec['education'] ?? null, $rec['graduated_from'] ?? null, $rec['hometown'] ?? null, $rec['work_location'] ?? null, $rec['source_channel'] ?? null, $rec['source_detail'] ?? null, ]); $newId = (int)$pdo->lastInsertId(); if (!empty($rec['phone'])) { $insAccount->execute([$newId, 'phone', $rec['phone']]); } if (!empty($rec['email'])) { $insAccount->execute([$newId, 'email', $rec['email']]); } // 完整度检查(导入每行入库后) updateIncomplete($pdo, 'persons', $newId); updateAccountsIncomplete($pdo, 'person', $newId); $inserted++; } catch (Exception $e) { $failed++; } } fclose($handle); logCurrent('import', 'person', 'persons', null, ['inserted' => $inserted, 'failed' => $failed]); Response::success(['inserted' => $inserted, 'failed' => $failed], "导入完成:成功 $inserted 条,失败 $failed 条");