updated rimotype map

This commit is contained in:
2025-09-17 13:23:03 +02:00
parent 7614fead8f
commit 7a71c9fdd8
14 changed files with 1258 additions and 425 deletions
+58 -58
View File
@@ -15,66 +15,66 @@ class ADBRimoFcp extends TTCrudBaseModel
public int $create;
public int $edit;
public static function getRimoFcpStatistics(): array {
$db = self::getDB();
$fronkDbName = defined('FRONKDB_DBNAME') ? FRONKDB_DBNAME : 'thetool';
$addressDbName = defined('ADDRESSDB_DBNAME') ? ADDRESSDB_DBNAME : 'addressdb';
public static function getRimoFcpStatistics(array $fcpIds = []): array {
$fronkDbName = FRONKDB_DBNAME ?? 'thetool';
$addressDbName = ADDRESSDB_DBNAME ?? 'addressdb';
$fcps = self::getAll($fcpIds ? ['id' => $fcpIds] : [], null, 0, ['key' => 'name', 'order' => 'ASC']);
if (!$fcps) return [];
$fcpResultMap = [];
foreach ($fcps as $fcp) {
$fcpResultMap[$fcp->id] = [
'fcp_id' => $fcp->id,
'fcp_name' => $fcp->name,
'fcp_rimo_id' => $fcp->rimo_id,
'total_hausnummer_count' => 0,
'total_wohneinheit_count' => 0,
'total_active_preorders' => 0,
'counts_by_rimo_type' => new stdClass(),
];
}
$idList = implode(',', array_keys($fcpResultMap));
if (empty($idList)) return array_values($fcpResultMap);
$sql = "
-- Use a Common Table Expression (CTE) to pre-calculate counts for each combination of FCP and rimo_type.
WITH RimoTypeCounts AS (
SELECT
hn.fcp_id,
-- Group NULL rimo_types into an 'UNKNOWN' category for clarity.
COALESCE(hn.rimo_type, 'UNKNOWN') AS rimo_type,
COUNT(DISTINCT hn.id) AS hausnummer_count,
COUNT(DISTINCT we.id) AS wohneinheit_count,
COUNT(DISTINCT CASE WHEN ps.code < 899 THEN p.id ELSE NULL END) AS preorder_count
FROM
`{$addressDbName}`.`Hausnummer` AS hn
LEFT JOIN
`{$addressDbName}`.`Wohneinheit` AS we ON hn.id = we.hausnummer_id
LEFT JOIN
`{$fronkDbName}`.`Preorder` AS p ON hn.id = p.adb_hausnummer_id
LEFT JOIN
`{$fronkDbName}`.`Preorderstatus` AS ps ON p.status_id = ps.id
WHERE
hn.fcp_id IS NOT NULL
GROUP BY
hn.fcp_id,
COALESCE(hn.rimo_type, 'UNKNOWN')
)
-- Final SELECT statement to assemble the data for each FCP.
SELECT
fcp.id AS fcp_id,
fcp.name AS fcp_name,
fcp.rimo_id AS fcp_rimo_id,
-- Aggregate total counts for the entire FCP.
SUM(rtc.hausnummer_count) AS total_hausnummer_count,
SUM(rtc.wohneinheit_count) AS total_wohneinheit_count,
SUM(rtc.preorder_count) AS total_active_preorders,
-- Create a single JSON object from all the rimo_type groups for the current FCP.
JSON_OBJECTAGG(
rtc.rimo_type,
JSON_OBJECT(
'hausnummer_count', rtc.hausnummer_count,
'wohneinheit_count', rtc.wohneinheit_count,
'preorder_count', rtc.preorder_count
)
) AS counts_by_rimo_type
FROM
`{$addressDbName}`.`RimoFcp` AS fcp
LEFT JOIN
RimoTypeCounts AS rtc ON fcp.id = rtc.fcp_id
WHERE
rtc.fcp_id IS NOT NULL
GROUP BY
fcp.id, fcp.name, fcp.rimo_id
ORDER BY
fcp.name;
";
SELECT
hn.fcp_id,
COALESCE(hn.rimo_type, 'UNKNOWN') AS rimo_type,
COUNT(DISTINCT hn.id) AS hausnummer_count,
COUNT(DISTINCT we.id) AS wohneinheit_count,
COUNT(DISTINCT CASE WHEN ps.code < 899 THEN p.id END) AS preorder_count
FROM `{$addressDbName}`.`Hausnummer` AS hn
LEFT JOIN `{$addressDbName}`.`Wohneinheit` AS we ON hn.id = we.hausnummer_id
LEFT JOIN `{$fronkDbName}`.`Preorder` AS p ON p.adb_hausnummer_id = hn.id
LEFT JOIN `{$fronkDbName}`.`Preorderstatus` AS ps ON p.status_id = ps.id
WHERE hn.fcp_id IN ({$idList})
GROUP BY hn.fcp_id, rimo_type
";
$result = $db->query($sql);
return $result ? $result->fetch_all(MYSQLI_ASSOC) : [];
$result = self::getDB()->query($sql);
if ($result) {
while ($row = $result->fetch_assoc()) {
$fcpId = $row['fcp_id'];
$rimoType = $row['rimo_type'];
$hCount = (int)$row['hausnummer_count'];
$wCount = (int)$row['wohneinheit_count'];
$pCount = (int)$row['preorder_count'];
$fcpResultMap[$fcpId]['counts_by_rimo_type']->{$rimoType} = (object)[
'hausnummer_count' => $hCount,
'wohneinheit_count' => $wCount,
'preorder_count' => $pCount,
];
$fcpResultMap[$fcpId]['total_hausnummer_count'] += $hCount;
$fcpResultMap[$fcpId]['total_wohneinheit_count'] += $wCount;
$fcpResultMap[$fcpId]['total_active_preorders'] += $pCount;
}
}
return array_values($fcpResultMap);
}
}