getPdo(); [$sql, $params] = buildInsert($data); $pdo->prepare("INSERT INTO companies $sql")->execute($params); $newId = (int)$pdo->lastInsertId(); // 财务信息 / 资质认证(可选,整表替换) if (array_key_exists('financials', $_POST) || array_key_exists('certifications', $_POST)) { $pdo->beginTransaction(); try { if (array_key_exists('financials', $_POST)) { $financials = json_decode($_POST['financials'], true); if (!is_array($financials)) { $pdo->rollBack(); Response::error('财务信息格式错误', 400); } saveCompanyFinancials($pdo, $newId, $financials); } if (array_key_exists('certifications', $_POST)) { $certs = json_decode($_POST['certifications'], true); if (!is_array($certs)) { $pdo->rollBack(); Response::error('资质认证格式错误', 400); } saveCompanyCertifications($pdo, $newId, $certs); } $pdo->commit(); } catch (Exception $e) { $pdo->rollBack(); Response::error('关联信息保存失败:' . $e->getMessage()); } } logCurrent('add', 'company', 'companies', $newId, $data); Response::success(['id' => $newId], '新增成功');