mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-07 18:44:08 +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:
parent
c9fe5a393f
commit
c56ae08fae
@ -24,12 +24,18 @@ class AddressReferenceBEFromBestAddress
|
|||||||
|
|
||||||
private AddressReferenceBaseImporter $baseImporter;
|
private AddressReferenceBaseImporter $baseImporter;
|
||||||
|
|
||||||
|
private AddressToReferenceMatcher $addressToReferenceMatcher;
|
||||||
|
|
||||||
private HttpClientInterface $client;
|
private HttpClientInterface $client;
|
||||||
|
|
||||||
public function __construct(HttpClientInterface $client, AddressReferenceBaseImporter $baseImporter)
|
public function __construct(
|
||||||
{
|
HttpClientInterface $client,
|
||||||
|
AddressReferenceBaseImporter $baseImporter,
|
||||||
|
AddressToReferenceMatcher $addressToReferenceMatcher
|
||||||
|
) {
|
||||||
$this->client = $client;
|
$this->client = $client;
|
||||||
$this->baseImporter = $baseImporter;
|
$this->baseImporter = $baseImporter;
|
||||||
|
$this->addressToReferenceMatcher = $addressToReferenceMatcher;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function import(string $lang, array $lists): void
|
public function import(string $lang, array $lists): void
|
||||||
@ -89,16 +95,18 @@ class AddressReferenceBEFromBestAddress
|
|||||||
$record['municipality_objectid'],
|
$record['municipality_objectid'],
|
||||||
$record['postal_info_objectid'],
|
$record['postal_info_objectid'],
|
||||||
$record['streetname'],
|
$record['streetname'],
|
||||||
$record['housenumber'] . $record['boxnumber'],
|
$record['housenumber'] .($record['boxnumber'] !== '' ? ' bte '. $record['boxnumber'] : ''),
|
||||||
'bestaddress.' . $list,
|
'bestaddress.' . $list,
|
||||||
(float) $record['X'],
|
|
||||||
(float) $record['Y'],
|
(float) $record['Y'],
|
||||||
|
(float) $record['X'],
|
||||||
3812
|
3812
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->baseImporter->finalize();
|
$this->baseImporter->finalize();
|
||||||
|
|
||||||
|
$this->addressToReferenceMatcher->checkAddressesMatchingReferences();
|
||||||
|
|
||||||
gzclose($uncompressedStream);
|
gzclose($uncompressedStream);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -22,12 +22,15 @@ class AddressReferenceFromBano
|
|||||||
{
|
{
|
||||||
private AddressReferenceBaseImporter $baseImporter;
|
private AddressReferenceBaseImporter $baseImporter;
|
||||||
|
|
||||||
|
private AddressToReferenceMatcher $addressToReferenceMatcher;
|
||||||
|
|
||||||
private HttpClientInterface $client;
|
private HttpClientInterface $client;
|
||||||
|
|
||||||
public function __construct(HttpClientInterface $client, AddressReferenceBaseImporter $baseImporter)
|
public function __construct(HttpClientInterface $client, AddressReferenceBaseImporter $baseImporter, AddressToReferenceMatcher $addressToReferenceMatcher)
|
||||||
{
|
{
|
||||||
$this->client = $client;
|
$this->client = $client;
|
||||||
$this->baseImporter = $baseImporter;
|
$this->baseImporter = $baseImporter;
|
||||||
|
$this->addressToReferenceMatcher = $addressToReferenceMatcher;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function import(string $departementNo): void
|
public function import(string $departementNo): void
|
||||||
@ -82,6 +85,8 @@ class AddressReferenceFromBano
|
|||||||
|
|
||||||
$this->baseImporter->finalize();
|
$this->baseImporter->finalize();
|
||||||
|
|
||||||
|
$this->addressToReferenceMatcher->checkAddressesMatchingReferences();
|
||||||
|
|
||||||
fclose($file);
|
fclose($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,
|
||||||
|
]);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,127 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Services\Import;
|
||||||
|
|
||||||
|
use Chill\MainBundle\Doctrine\Model\Point;
|
||||||
|
use Chill\MainBundle\Entity\Address;
|
||||||
|
use Chill\MainBundle\Entity\AddressReference;
|
||||||
|
use Chill\MainBundle\Entity\PostalCode;
|
||||||
|
use Chill\MainBundle\Repository\AddressReferenceRepository;
|
||||||
|
use Chill\MainBundle\Repository\CountryRepository;
|
||||||
|
use Chill\MainBundle\Service\Import\AddressReferenceBaseImporter;
|
||||||
|
use Chill\MainBundle\Service\Import\AddressToReferenceMatcher;
|
||||||
|
use Doctrine\ORM\EntityManagerInterface;
|
||||||
|
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @internal
|
||||||
|
* @coversNothing
|
||||||
|
*/
|
||||||
|
class AddressToReferenceMatcherTest extends KernelTestCase
|
||||||
|
{
|
||||||
|
private AddressToReferenceMatcher $addressToReferenceMatcher;
|
||||||
|
|
||||||
|
private AddressReferenceRepository $addressReferenceRepository;
|
||||||
|
|
||||||
|
private CountryRepository $countryRepository;
|
||||||
|
|
||||||
|
private EntityManagerInterface $entityManager;
|
||||||
|
|
||||||
|
private AddressReferenceBaseImporter $addressReferenceBaseImporter;
|
||||||
|
|
||||||
|
protected function setUp(): void
|
||||||
|
{
|
||||||
|
parent::setUp();
|
||||||
|
self::bootKernel();
|
||||||
|
|
||||||
|
$this->addressToReferenceMatcher = self::$container->get(AddressToReferenceMatcher::class);
|
||||||
|
$this->addressReferenceRepository = self::$container->get(AddressReferenceRepository::class);
|
||||||
|
$this->countryRepository = self::$container->get(CountryRepository::class);
|
||||||
|
$this->addressReferenceBaseImporter = self::$container->get(AddressReferenceBaseImporter::class);
|
||||||
|
$this->entityManager = self::$container->get(EntityManagerInterface::class);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testCheckAddressesMatchingReferences(): void
|
||||||
|
{
|
||||||
|
if (null === $belgium = $this->countryRepository->findOneBy(['countryCode' => 'BE'])) {
|
||||||
|
throw new \RuntimeException('Belgium not found');
|
||||||
|
}
|
||||||
|
|
||||||
|
$postalCode = (new PostalCode())
|
||||||
|
->setName('test for matcher')
|
||||||
|
->setPostalCodeSource('test for matcher')
|
||||||
|
->setCode('78910')
|
||||||
|
->setRefPostalCodeId($refPostalCodeId = '78910'.uniqid())
|
||||||
|
->setCountry($belgium)
|
||||||
|
;
|
||||||
|
$this->entityManager->persist($postalCode);
|
||||||
|
$this->entityManager->flush();
|
||||||
|
|
||||||
|
$this->addressReferenceBaseImporter->importAddress(
|
||||||
|
$refAddress = '010203_test'.uniqid(),
|
||||||
|
$refPostalCodeId,
|
||||||
|
'78910',
|
||||||
|
$street = 'Rue Poulet',
|
||||||
|
$streetNumber = '14',
|
||||||
|
'test_matcher',
|
||||||
|
$lat = 50.0123456789,
|
||||||
|
$lon = 5.0123456789,
|
||||||
|
4326
|
||||||
|
);
|
||||||
|
$this->addressReferenceBaseImporter->finalize();
|
||||||
|
|
||||||
|
if (null === $addressReference = $this->addressReferenceRepository->findOneBy(['refId' => $refAddress])) {
|
||||||
|
throw new \RuntimeException('address reference not created');
|
||||||
|
}
|
||||||
|
|
||||||
|
$address = Address::createFromAddressReference($addressReference);
|
||||||
|
|
||||||
|
$this->assertEquals('Rue Poulet', $address->getStreet());
|
||||||
|
|
||||||
|
$this->entityManager->persist($address);
|
||||||
|
$this->entityManager->flush();
|
||||||
|
|
||||||
|
// we update the address
|
||||||
|
$this->addressReferenceBaseImporter->importAddress(
|
||||||
|
$refAddress,
|
||||||
|
$refPostalCodeId,
|
||||||
|
'78910',
|
||||||
|
'Rue Poulet aux amandes',
|
||||||
|
'14',
|
||||||
|
'test_matcher',
|
||||||
|
50.01234456789,
|
||||||
|
5.0123456789,
|
||||||
|
4326
|
||||||
|
);
|
||||||
|
$this->addressReferenceBaseImporter->finalize();
|
||||||
|
|
||||||
|
$this->entityManager->flush();
|
||||||
|
|
||||||
|
$this->addressToReferenceMatcher->checkAddressesMatchingReferences();
|
||||||
|
|
||||||
|
$this->entityManager->refresh($address);
|
||||||
|
|
||||||
|
$this->assertEquals('to_review', $address->getRefStatus());
|
||||||
|
|
||||||
|
// we update the address
|
||||||
|
$this->addressReferenceBaseImporter->importAddress(
|
||||||
|
$refAddress,
|
||||||
|
$refPostalCodeId,
|
||||||
|
'78910',
|
||||||
|
$street,
|
||||||
|
$streetNumber,
|
||||||
|
'test_matcher',
|
||||||
|
$lat,
|
||||||
|
$lon,
|
||||||
|
4326
|
||||||
|
);
|
||||||
|
$this->addressReferenceBaseImporter->finalize();
|
||||||
|
|
||||||
|
$this->addressToReferenceMatcher->checkAddressesMatchingReferences();
|
||||||
|
|
||||||
|
$this->entityManager->refresh($address);
|
||||||
|
|
||||||
|
$this->assertEquals('Rue Poulet', $address->getStreet());
|
||||||
|
$this->assertEquals('match', $address->getRefStatus());
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user