getPdo(); // 唯一性校验:企业名称 / 注册号 / 联系方式,收集全部重复 $dups = []; $dup = findDuplicate($pdo, 'company_name', $data['name_zh'] ?? $data['display_name'] ?? ''); if ($dup) $dups[] = $dup; if (!empty($data['registration_number'])) { $dup = findDuplicate($pdo, 'registration_number', $data['registration_number']); if ($dup) $dups[] = $dup; } if (isset($_POST['accounts'])) { $accounts = json_decode($_POST['accounts'], true); if (!is_array($accounts)) { Response::error('联系方式格式错误', 400); } $dups = array_merge($dups, checkAccountsDuplicates($pdo, $accounts)); } if (!empty($dups)) Response::duplicates($dups); [$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) || array_key_exists('accounts', $_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); } if (array_key_exists('accounts', $_POST)) { saveCompanyAccounts($pdo, $newId, $accounts ?? []); } $pdo->commit(); } catch (Exception $e) { $pdo->rollBack(); Response::error('关联信息保存失败:' . $e->getMessage()); } } // 完整度检查:企业主表 + 其 social_accounts updateIncomplete($pdo, 'companies', $newId); updateAccountsIncomplete($pdo, 'company', $newId); logCurrent('add', 'company', 'companies', $newId, $data); Response::success(['id' => $newId], '新增成功');