Feature: [address reference] write logic to match existing addresses with updates

Feature: [address reference] match addresses with reference after import

fix sql query for address reference matcher
This commit is contained in:
2023-03-07 22:16:16 +01:00
parent c9fe5a393f
commit c56ae08fae
4 changed files with 232 additions and 5 deletions

View File

@@ -24,12 +24,18 @@ class AddressReferenceBEFromBestAddress
private AddressReferenceBaseImporter $baseImporter;
private AddressToReferenceMatcher $addressToReferenceMatcher;
private HttpClientInterface $client;
public function __construct(HttpClientInterface $client, AddressReferenceBaseImporter $baseImporter)
{
public function __construct(
HttpClientInterface $client,
AddressReferenceBaseImporter $baseImporter,
AddressToReferenceMatcher $addressToReferenceMatcher
) {
$this->client = $client;
$this->baseImporter = $baseImporter;
$this->addressToReferenceMatcher = $addressToReferenceMatcher;
}
public function import(string $lang, array $lists): void
@@ -89,16 +95,18 @@ class AddressReferenceBEFromBestAddress
$record['municipality_objectid'],
$record['postal_info_objectid'],
$record['streetname'],
$record['housenumber'] . $record['boxnumber'],
$record['housenumber'] .($record['boxnumber'] !== '' ? ' bte '. $record['boxnumber'] : ''),
'bestaddress.' . $list,
(float) $record['X'],
(float) $record['Y'],
(float) $record['X'],
3812
);
}
$this->baseImporter->finalize();
$this->addressToReferenceMatcher->checkAddressesMatchingReferences();
gzclose($uncompressedStream);
}
}

View File

@@ -22,12 +22,15 @@ class AddressReferenceFromBano
{
private AddressReferenceBaseImporter $baseImporter;
private AddressToReferenceMatcher $addressToReferenceMatcher;
private HttpClientInterface $client;
public function __construct(HttpClientInterface $client, AddressReferenceBaseImporter $baseImporter)
public function __construct(HttpClientInterface $client, AddressReferenceBaseImporter $baseImporter, AddressToReferenceMatcher $addressToReferenceMatcher)
{
$this->client = $client;
$this->baseImporter = $baseImporter;
$this->addressToReferenceMatcher = $addressToReferenceMatcher;
}
public function import(string $departementNo): void
@@ -82,6 +85,8 @@ class AddressReferenceFromBano
$this->baseImporter->finalize();
$this->addressToReferenceMatcher->checkAddressesMatchingReferences();
fclose($file);
}
}

View File

@@ -0,0 +1,87 @@
<?php
namespace Chill\MainBundle\Service\Import;
use Chill\MainBundle\Entity\Address;
use Doctrine\DBAL\Connection;
use Psr\Log\LoggerInterface;
/**
* Mark existing addresses as to be reviewed regarding the
* address reference
*/
final class AddressToReferenceMatcher
{
private Connection $connection;
private LoggerInterface $logger;
private const LOG_PREFIX = '[address_to_reference_matcher] ';
private const SQL_MARK_TO_REVIEW_ADDRESS_UNMATCHING = <<<SQL
UPDATE chill_main_address a SET refstatus = '{{ to_review }}', refstatuslastupdate = NOW()
FROM chill_main_address_reference ar
WHERE
a.addressreference_id = ar.id
-- restrict only on active addresses
AND (a.validto IS NULL OR a.validto >= NOW())
-- only addresses that are marked matching or "to review", but before the update
AND
(a.refstatus LIKE '{{ matching }}'
OR (a.refstatus LIKE '{{ reviewed }}' AND a.refstatuslastupdate < ar.updatedat))
AND (
a.postcode_id != ar.postcode_id
OR a.street != ar.street
OR a.streetnumber != ar.streetnumber
OR ROUND(ST_X(a.point) * 1000000) <> ROUND(ST_X(ar.point) * 1000000)
OR ROUND(ST_Y(a.point) * 1000000) <> ROUND(ST_Y(ar.point) * 1000000)
)
SQL;
private const SQL_MARK_MATCHING_ADDRESSES_REVIEWED_OR_TO_REVIEW = <<<SQL
UPDATE chill_main_address a SET refstatus = '{{ matching }}', refstatuslastupdate = NOW()
FROM chill_main_address_reference ar
WHERE
a.addressreference_id = ar.id
-- restrict only on active addresses
AND (a.validto IS NULL OR a.validto >= NOW())
AND a.refstatus IN ('{{ to_review }}', '{{ reviewed }}')
AND a.postcode_id = ar.postcode_id
AND a.street = ar.street
AND a.streetnumber = ar.streetnumber
AND ROUND(ST_X(a.point) * 1000000) = ROUND(ST_X(ar.point) * 1000000)
AND ROUND(ST_Y(a.point) * 1000000) = ROUND(ST_Y(ar.point) * 1000000)
SQL;
private const SUBSTITUTES = [
'{{ to_review }}' => Address::ADDR_REFERENCE_STATUS_TO_REVIEW,
'{{ matching }}' => Address::ADDR_REFERENCE_STATUS_MATCH,
'{{ reviewed }}' => Address::ADDR_REFERENCE_STATUS_REVIEWED
];
public function __construct(Connection $connection, LoggerInterface $logger)
{
$this->connection = $connection;
$this->logger = $logger;
}
public function checkAddressesMatchingReferences(): void
{
$this->logger->notice(self::LOG_PREFIX.'Starting addresses matching');
$this->connection->transactional(function () {
$markedAsMatching = $this->connection->executeStatement(
strtr(self::SQL_MARK_MATCHING_ADDRESSES_REVIEWED_OR_TO_REVIEW, self::SUBSTITUTES)
);
$markedAsToReview = $this->connection->executeStatement(
strtr(self::SQL_MARK_TO_REVIEW_ADDRESS_UNMATCHING, self::SUBSTITUTES)
);
$this->logger->info(self::LOG_PREFIX.'Executed address matching', [
'marked_as_matching' => $markedAsMatching,
'marked_as_to_review' => $markedAsToReview,
]);
});
}
}