mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-08-30 11:33:49 +00:00
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:
@@ -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,
|
||||
]);
|
||||
});
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user