Script Preorder Sync erweitert auf Netzgebiete:
ON Leibnitz ON Bad Gleichenberg ON Semriach ON Straden ON St.Anna am Aigen
This commit is contained in:
@@ -14,9 +14,32 @@ $pgPort = '5432';
|
||||
$pgDb = QGIS_DBNAME;
|
||||
$pgUser = QGIS_DBUSER;
|
||||
$pgPass = QGIS_DBPASS;
|
||||
$targetSchema = '"ON Leibnitz"';
|
||||
|
||||
$targetTable = 'Preorders';
|
||||
|
||||
$campaigns = [
|
||||
[
|
||||
'targetSchema' => '"ON Leibnitz"',
|
||||
'campaignId' => 99
|
||||
],
|
||||
[
|
||||
'targetSchema' => '"ON Semriach"',
|
||||
'campaignId' => 101
|
||||
],
|
||||
[
|
||||
'targetSchema' => '"ON Bad Gleichenberg"',
|
||||
'campaignId' => 108
|
||||
],
|
||||
[
|
||||
'targetSchema' => '"ON Straden"',
|
||||
'campaignId' => 107
|
||||
],
|
||||
[
|
||||
'targetSchema' => '"ON St.Anna am Aigen"',
|
||||
'campaignId' => 106
|
||||
]
|
||||
];
|
||||
|
||||
define("INTERNAL_USER_ID", 154);
|
||||
|
||||
class PreorderSyncWrapper extends PreorderController {
|
||||
@@ -32,24 +55,6 @@ class PreorderSyncWrapper extends PreorderController {
|
||||
}
|
||||
}
|
||||
|
||||
$apiParams = [
|
||||
'mod' => 'Preorder',
|
||||
'action' => 'api',
|
||||
'do' => 'getFilteredPreorders',
|
||||
'filter' => [
|
||||
'preordercampaign_id' => 99
|
||||
]
|
||||
];
|
||||
|
||||
new PreorderSyncWrapper($apiParams);
|
||||
$response = PreorderSyncWrapper::$capturedResult;
|
||||
|
||||
if (!$response || !isset($response['status']) || $response['status'] !== 'OK') {
|
||||
die("Fehler beim Abrufen der Daten oder keine Daten erhalten.\n");
|
||||
}
|
||||
|
||||
$preorders = $response['result']['preorders'] ?? [];
|
||||
|
||||
try {
|
||||
$dsn = "pgsql:host=$pgHost;port=$pgPort;dbname=$pgDb";
|
||||
$pdo = new PDO($dsn, $pgUser, $pgPass, [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]);
|
||||
@@ -57,9 +62,33 @@ try {
|
||||
die("Verbindung zu PostgreSQL fehlgeschlagen: " . $e->getMessage() . "\n");
|
||||
}
|
||||
|
||||
$pdo->exec("CREATE SCHEMA IF NOT EXISTS $targetSchema");
|
||||
foreach ($campaigns as $campaign) {
|
||||
$targetSchema = $campaign['targetSchema'];
|
||||
$campaignId = $campaign['campaignId'];
|
||||
|
||||
$createTableSql = <<<SQL
|
||||
$apiParams = [
|
||||
'mod' => 'Preorder',
|
||||
'action' => 'api',
|
||||
'do' => 'getFilteredPreorders',
|
||||
'filter' => [
|
||||
'preordercampaign_id' => $campaignId
|
||||
]
|
||||
];
|
||||
|
||||
PreorderSyncWrapper::$capturedResult = null;
|
||||
new PreorderSyncWrapper($apiParams);
|
||||
$response = PreorderSyncWrapper::$capturedResult;
|
||||
|
||||
if (!$response || !isset($response['status']) || $response['status'] !== 'OK') {
|
||||
echo "Fehler beim Abrufen der Daten oder keine Daten erhalten fuer Schema $targetSchema (ID: $campaignId).\n";
|
||||
continue;
|
||||
}
|
||||
|
||||
$preorders = $response['result']['preorders'] ?? [];
|
||||
|
||||
$pdo->exec("CREATE SCHEMA IF NOT EXISTS $targetSchema");
|
||||
|
||||
$createTableSql = <<<SQL
|
||||
CREATE TABLE IF NOT EXISTS $targetSchema."$targetTable" (
|
||||
id INTEGER PRIMARY KEY,
|
||||
type VARCHAR(50),
|
||||
@@ -82,18 +111,18 @@ CREATE TABLE IF NOT EXISTS $targetSchema."$targetTable" (
|
||||
CREATE INDEX IF NOT EXISTS idx_preorders_geom ON $targetSchema."$targetTable" USING GIST (geom);
|
||||
SQL;
|
||||
|
||||
$pdo->exec($createTableSql);
|
||||
$pdo->exec($createTableSql);
|
||||
|
||||
$pdo->exec("ALTER TABLE $targetSchema.\"$targetTable\" ADD COLUMN IF NOT EXISTS company VARCHAR(255)");
|
||||
$pdo->exec("ALTER TABLE $targetSchema.\"$targetTable\" ADD COLUMN IF NOT EXISTS firstname VARCHAR(255)");
|
||||
$pdo->exec("ALTER TABLE $targetSchema.\"$targetTable\" ADD COLUMN IF NOT EXISTS lastname VARCHAR(255)");
|
||||
$pdo->exec("ALTER TABLE $targetSchema.\"$targetTable\" ADD COLUMN IF NOT EXISTS phone VARCHAR(100)");
|
||||
$pdo->exec("ALTER TABLE $targetSchema.\"$targetTable\" ADD COLUMN IF NOT EXISTS email VARCHAR(255)");
|
||||
$pdo->exec("ALTER TABLE $targetSchema.\"$targetTable\" ADD COLUMN IF NOT EXISTS status_code INTEGER");
|
||||
$pdo->exec("ALTER TABLE $targetSchema.\"$targetTable\" ADD COLUMN IF NOT EXISTS status_id INTEGER");
|
||||
$pdo->exec("ALTER TABLE $targetSchema.\"$targetTable\" ADD COLUMN IF NOT EXISTS oaid VARCHAR(255)");
|
||||
$pdo->exec("ALTER TABLE $targetSchema.\"$targetTable\" ADD COLUMN IF NOT EXISTS company VARCHAR(255)");
|
||||
$pdo->exec("ALTER TABLE $targetSchema.\"$targetTable\" ADD COLUMN IF NOT EXISTS firstname VARCHAR(255)");
|
||||
$pdo->exec("ALTER TABLE $targetSchema.\"$targetTable\" ADD COLUMN IF NOT EXISTS lastname VARCHAR(255)");
|
||||
$pdo->exec("ALTER TABLE $targetSchema.\"$targetTable\" ADD COLUMN IF NOT EXISTS phone VARCHAR(100)");
|
||||
$pdo->exec("ALTER TABLE $targetSchema.\"$targetTable\" ADD COLUMN IF NOT EXISTS email VARCHAR(255)");
|
||||
$pdo->exec("ALTER TABLE $targetSchema.\"$targetTable\" ADD COLUMN IF NOT EXISTS status_code INTEGER");
|
||||
$pdo->exec("ALTER TABLE $targetSchema.\"$targetTable\" ADD COLUMN IF NOT EXISTS status_id INTEGER");
|
||||
$pdo->exec("ALTER TABLE $targetSchema.\"$targetTable\" ADD COLUMN IF NOT EXISTS oaid VARCHAR(255)");
|
||||
|
||||
$sqlUpsert = <<<SQL
|
||||
$sqlUpsert = <<<SQL
|
||||
INSERT INTO $targetSchema."$targetTable"
|
||||
(id, type, type_label, strasse, hausnummer, plz, ort, geom, company, firstname, lastname, phone, email, status_code, status_id, oaid, updated_at)
|
||||
VALUES
|
||||
@@ -133,72 +162,67 @@ WHERE
|
||||
"$targetTable".oaid IS DISTINCT FROM EXCLUDED.oaid;
|
||||
SQL;
|
||||
|
||||
$stmt = $pdo->prepare($sqlUpsert);
|
||||
$stmt = $pdo->prepare($sqlUpsert);
|
||||
|
||||
$processedIds = [];
|
||||
$countUpsert = 0;
|
||||
$countUnchanged = 0;
|
||||
$countSkipped = 0;
|
||||
$processedIds = [];
|
||||
$countUpsert = 0;
|
||||
$countUnchanged = 0;
|
||||
$countSkipped = 0;
|
||||
|
||||
$pdo->beginTransaction();
|
||||
$pdo->beginTransaction();
|
||||
|
||||
foreach ($preorders as $po) {
|
||||
$id = $po->id;
|
||||
$gps_lat = $po->gps_lat;
|
||||
$gps_long = $po->gps_long;
|
||||
foreach ($preorders as $po) {
|
||||
$id = $po->id;
|
||||
$gps_lat = $po->gps_lat;
|
||||
$gps_long = $po->gps_long;
|
||||
|
||||
if (empty($gps_lat) || empty($gps_long)) {
|
||||
$countSkipped++;
|
||||
continue;
|
||||
if (empty($gps_lat) || empty($gps_long)) {
|
||||
$countSkipped++;
|
||||
continue;
|
||||
}
|
||||
|
||||
$latVal = str_replace(',', '.', $gps_lat);
|
||||
$lonVal = str_replace(',', '.', $gps_long);
|
||||
|
||||
$params = [
|
||||
':id' => $id,
|
||||
':type' => $po->type,
|
||||
':type_label' => $po->type_label,
|
||||
':strasse' => $po->adb_strasse,
|
||||
':hausnummer' => $po->adb_hausnummer,
|
||||
':plz' => $po->adb_plz,
|
||||
':ort' => $po->adb_ort,
|
||||
':company' => $po->company,
|
||||
':firstname' => $po->firstname,
|
||||
':lastname' => $po->lastname,
|
||||
':phone' => $po->phone,
|
||||
':email' => $po->email,
|
||||
':status_code' => $po->status_code,
|
||||
':status_id' => $po->status_id,
|
||||
':oaid' => $po->oaid,
|
||||
':lat' => $latVal,
|
||||
':lon' => $lonVal
|
||||
];
|
||||
|
||||
$stmt->execute($params);
|
||||
$processedIds[] = $id;
|
||||
if ($stmt->rowCount() > 0) {
|
||||
$countUpsert++;
|
||||
} else {
|
||||
$countUnchanged++;
|
||||
}
|
||||
}
|
||||
|
||||
$latVal = str_replace(',', '.', $gps_lat);
|
||||
$lonVal = str_replace(',', '.', $gps_long);
|
||||
|
||||
$params = [
|
||||
':id' => $id,
|
||||
':type' => $po->type,
|
||||
':type_label' => $po->type_label,
|
||||
':strasse' => $po->adb_strasse,
|
||||
':hausnummer' => $po->adb_hausnummer,
|
||||
':plz' => $po->adb_plz,
|
||||
':ort' => $po->adb_ort,
|
||||
':company' => $po->company,
|
||||
':firstname' => $po->firstname,
|
||||
':lastname' => $po->lastname,
|
||||
':phone' => $po->phone,
|
||||
':email' => $po->email,
|
||||
':status_code' => $po->status_code,
|
||||
':status_id' => $po->status_id,
|
||||
':oaid' => $po->oaid,
|
||||
':lat' => $latVal,
|
||||
':lon' => $lonVal
|
||||
];
|
||||
|
||||
$stmt->execute($params);
|
||||
$processedIds[] = $id;
|
||||
if ($stmt->rowCount() > 0) {
|
||||
$countUpsert++;
|
||||
$deletedCount = 0;
|
||||
if (!empty($processedIds)) {
|
||||
$inQuery = implode(',', array_map('intval', $processedIds));
|
||||
$deleteSql = "DELETE FROM $targetSchema.\"$targetTable\" WHERE id NOT IN ($inQuery)";
|
||||
$deletedCount = $pdo->exec($deleteSql);
|
||||
} else {
|
||||
$countUnchanged++;
|
||||
|
||||
if (count($preorders) == 0) {
|
||||
}
|
||||
}
|
||||
|
||||
$pdo->commit();
|
||||
}
|
||||
|
||||
$deletedCount = 0;
|
||||
if (!empty($processedIds)) {
|
||||
$inQuery = implode(',', array_map('intval', $processedIds));
|
||||
$deleteSql = "DELETE FROM $targetSchema.\"$targetTable\" WHERE id NOT IN ($inQuery)";
|
||||
$deletedCount = $pdo->exec($deleteSql);
|
||||
} else {
|
||||
|
||||
if (count($preorders) == 0) {
|
||||
}
|
||||
}
|
||||
|
||||
$pdo->commit();
|
||||
|
||||
//echo "Sync fertig.\n";
|
||||
//echo "Neu erstellt oder aktualisiert: $countUpsert\n";
|
||||
//echo "Unverändert (kein Update nötig): $countUnchanged\n";
|
||||
//echo "Ohne Koordinaten (übersprungen): $countSkipped\n";
|
||||
//echo "Gelöscht (nicht mehr in Quelle): $deletedCount\n";
|
||||
Reference in New Issue
Block a user