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:
@@ -408,7 +408,7 @@ foreach ($devicesall as $deviceall) {
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<?php
|
<?php
|
||||||
if ($devicesconfig->success == "true" && $devicesconfig->data > 0) {
|
if ($devicesconfig->success == "true" && $devicesconfig->data) {
|
||||||
?>
|
?>
|
||||||
<div>
|
<div>
|
||||||
<table class="table table-sm">
|
<table class="table table-sm">
|
||||||
@@ -711,7 +711,7 @@ foreach ($devicesall as $deviceall) {
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<?php if ($devices->devicetype->devicemanufactor->config_backup > 0):
|
<?php if ($devices->devicetype->devicemanufactor->config_backup > count()):
|
||||||
|
|
||||||
$year = date("Y", time());
|
$year = date("Y", time());
|
||||||
$month = date("n", time());
|
$month = date("n", time());
|
||||||
|
|||||||
@@ -14,9 +14,32 @@ $pgPort = '5432';
|
|||||||
$pgDb = QGIS_DBNAME;
|
$pgDb = QGIS_DBNAME;
|
||||||
$pgUser = QGIS_DBUSER;
|
$pgUser = QGIS_DBUSER;
|
||||||
$pgPass = QGIS_DBPASS;
|
$pgPass = QGIS_DBPASS;
|
||||||
$targetSchema = '"ON Leibnitz"';
|
|
||||||
$targetTable = 'Preorders';
|
$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);
|
define("INTERNAL_USER_ID", 154);
|
||||||
|
|
||||||
class PreorderSyncWrapper extends PreorderController {
|
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 {
|
try {
|
||||||
$dsn = "pgsql:host=$pgHost;port=$pgPort;dbname=$pgDb";
|
$dsn = "pgsql:host=$pgHost;port=$pgPort;dbname=$pgDb";
|
||||||
$pdo = new PDO($dsn, $pgUser, $pgPass, [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]);
|
$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");
|
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" (
|
CREATE TABLE IF NOT EXISTS $targetSchema."$targetTable" (
|
||||||
id INTEGER PRIMARY KEY,
|
id INTEGER PRIMARY KEY,
|
||||||
type VARCHAR(50),
|
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);
|
CREATE INDEX IF NOT EXISTS idx_preorders_geom ON $targetSchema."$targetTable" USING GIST (geom);
|
||||||
SQL;
|
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 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 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 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 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 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_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 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 oaid VARCHAR(255)");
|
||||||
|
|
||||||
$sqlUpsert = <<<SQL
|
$sqlUpsert = <<<SQL
|
||||||
INSERT INTO $targetSchema."$targetTable"
|
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)
|
(id, type, type_label, strasse, hausnummer, plz, ort, geom, company, firstname, lastname, phone, email, status_code, status_id, oaid, updated_at)
|
||||||
VALUES
|
VALUES
|
||||||
@@ -133,72 +162,67 @@ WHERE
|
|||||||
"$targetTable".oaid IS DISTINCT FROM EXCLUDED.oaid;
|
"$targetTable".oaid IS DISTINCT FROM EXCLUDED.oaid;
|
||||||
SQL;
|
SQL;
|
||||||
|
|
||||||
$stmt = $pdo->prepare($sqlUpsert);
|
$stmt = $pdo->prepare($sqlUpsert);
|
||||||
|
|
||||||
$processedIds = [];
|
$processedIds = [];
|
||||||
$countUpsert = 0;
|
$countUpsert = 0;
|
||||||
$countUnchanged = 0;
|
$countUnchanged = 0;
|
||||||
$countSkipped = 0;
|
$countSkipped = 0;
|
||||||
|
|
||||||
$pdo->beginTransaction();
|
$pdo->beginTransaction();
|
||||||
|
|
||||||
foreach ($preorders as $po) {
|
foreach ($preorders as $po) {
|
||||||
$id = $po->id;
|
$id = $po->id;
|
||||||
$gps_lat = $po->gps_lat;
|
$gps_lat = $po->gps_lat;
|
||||||
$gps_long = $po->gps_long;
|
$gps_long = $po->gps_long;
|
||||||
|
|
||||||
if (empty($gps_lat) || empty($gps_long)) {
|
if (empty($gps_lat) || empty($gps_long)) {
|
||||||
$countSkipped++;
|
$countSkipped++;
|
||||||
continue;
|
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);
|
$deletedCount = 0;
|
||||||
$lonVal = str_replace(',', '.', $gps_long);
|
if (!empty($processedIds)) {
|
||||||
|
$inQuery = implode(',', array_map('intval', $processedIds));
|
||||||
$params = [
|
$deleteSql = "DELETE FROM $targetSchema.\"$targetTable\" WHERE id NOT IN ($inQuery)";
|
||||||
':id' => $id,
|
$deletedCount = $pdo->exec($deleteSql);
|
||||||
':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 {
|
} 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