Search update and optimization
This commit is contained in:
@ -16,6 +16,24 @@ class Unidades
|
||||
{
|
||||
use withJson, withRedis;
|
||||
|
||||
public function get(ServerRequestInterface $request, ResponseInterface $response, Service\Redis $redisService,
|
||||
Service\Venta\Unidad $unidadService, int $unidad_id): ResponseInterface
|
||||
{
|
||||
$output = [
|
||||
'unidad_id' => $unidad_id,
|
||||
'unidad' => null
|
||||
];
|
||||
$redisKey = "unidad:{$unidad_id}";
|
||||
try {
|
||||
$output['unidad'] = $this->fetchRedis($redisService, $redisKey);
|
||||
} catch (EmptyRedis) {
|
||||
try {
|
||||
$output['unidad'] = $unidadService->getById($unidad_id);
|
||||
$this->saveRedis($redisService, $redisKey, $output['unidad']);
|
||||
} catch (EmptyResult) {}
|
||||
}
|
||||
return $this->withJson($response, $output);
|
||||
}
|
||||
public function disponibles(ServerRequestInterface $request, ResponseInterface $response, Repository\Venta\Unidad $unidadRepository, Repository\Proyecto\TipoUnidad $tipoUnidadRepository, Service\Redis $redisService): ResponseInterface
|
||||
{
|
||||
$body = $request->getBody();
|
||||
|
@ -20,7 +20,7 @@ class Ventas
|
||||
{
|
||||
$redisKey = "proyectos:vendibles";
|
||||
try {
|
||||
$proyectos = $proyectoService->process($proyectoRepository->load((array) $this->fetchRedis($redisService, $redisKey)));
|
||||
$proyectos = $this->fetchRedis($redisService, $redisKey);
|
||||
} catch (EmptyRedis) {
|
||||
$proyectos = array_map(function(Model\Proyecto $proyecto) {
|
||||
return ['id' => $proyecto->id, 'descripcion' => $proyecto->descripcion];
|
||||
|
@ -66,7 +66,7 @@ class Venta extends Ideal\Model
|
||||
|
||||
public function proyecto(): Proyecto
|
||||
{
|
||||
return $this->propiedad()->departamentos()[0]->proyectoTipoUnidad->proyecto;
|
||||
return $this->propiedad()->proyecto();
|
||||
}
|
||||
|
||||
protected float $valor_util;
|
||||
|
@ -2,22 +2,40 @@
|
||||
namespace Incoviba\Model\Venta;
|
||||
|
||||
use Incoviba\Common\Ideal;
|
||||
use Incoviba\Model;
|
||||
|
||||
class Propiedad extends Ideal\Model
|
||||
{
|
||||
public array $unidades;
|
||||
|
||||
protected array $departamentos;
|
||||
public function departamentos(): array
|
||||
{
|
||||
return array_values(array_filter($this->unidades, function(Unidad $unidad) {return $unidad->proyectoTipoUnidad->tipoUnidad->descripcion === 'departamento';}));
|
||||
if (!isset($this->departamentos)) {
|
||||
$this->departamentos = array_values(array_filter($this->unidades, function(Unidad $unidad) {return $unidad->proyectoTipoUnidad->tipoUnidad->descripcion === 'departamento';}));
|
||||
}
|
||||
return $this->departamentos;
|
||||
}
|
||||
protected array $estacionamientos;
|
||||
public function estacionamientos(): array
|
||||
{
|
||||
return array_values(array_filter($this->unidades, function(Unidad $unidad) {return $unidad->proyectoTipoUnidad->tipoUnidad->descripcion === 'estacionamiento';}));
|
||||
if (!isset($this->estacionamientos)) {
|
||||
$this->estacionamientos = array_values(array_filter($this->unidades, function(Unidad $unidad) {return $unidad->proyectoTipoUnidad->tipoUnidad->descripcion === 'estacionamiento';}));
|
||||
}
|
||||
return $this->estacionamientos;
|
||||
}
|
||||
protected array $bodegas;
|
||||
public function bodegas(): array
|
||||
{
|
||||
return array_values(array_filter($this->unidades, function(Unidad $unidad) {return $unidad->proyectoTipoUnidad->tipoUnidad->descripcion === 'bodega';}));
|
||||
if (!isset($this->bodegas)) {
|
||||
$this->bodegas = array_values(array_filter($this->unidades, function(Unidad $unidad) {return $unidad->proyectoTipoUnidad->tipoUnidad->descripcion === 'bodega';}));
|
||||
}
|
||||
return $this->bodegas;
|
||||
}
|
||||
|
||||
public function proyecto(): Model\Proyecto
|
||||
{
|
||||
return $this->unidades[0]->proyectoTipoUnidad->proyecto;
|
||||
}
|
||||
|
||||
protected float $vendible;
|
||||
@ -46,6 +64,7 @@ class Propiedad extends Ideal\Model
|
||||
public function jsonSerialize(): mixed
|
||||
{
|
||||
return [
|
||||
'unidades' => $this->unidades,
|
||||
'departamentos' => $this->departamentos(),
|
||||
'estacionamientos' => $this->estacionamientos(),
|
||||
'bodegas' => $this->bodegas(),
|
||||
|
@ -46,7 +46,7 @@ class Unidad extends Ideal\Model
|
||||
|
||||
public function jsonSerialize(): mixed
|
||||
{
|
||||
return array_merge(parent::jsonSerialize(), [
|
||||
$output = array_merge(parent::jsonSerialize(), [
|
||||
'subtipo' => $this->subtipo,
|
||||
'piso' => $this->piso,
|
||||
'descripcion' => $this->descripcion,
|
||||
@ -54,5 +54,10 @@ class Unidad extends Ideal\Model
|
||||
'proyecto_tipo_unidad' => $this->proyectoTipoUnidad,
|
||||
'prorrateo' => $this->prorrateo
|
||||
]);
|
||||
if (isset($this->precios)) {
|
||||
$output['precios'] = $this->precios;
|
||||
$output['current_precio'] = $this->currentPrecio;
|
||||
}
|
||||
return $output;
|
||||
}
|
||||
}
|
||||
|
@ -1,7 +1,7 @@
|
||||
<?php
|
||||
namespace Incoviba\Repository;
|
||||
|
||||
use DateTimeImmutable;
|
||||
use PDO;
|
||||
use Incoviba\Common\Ideal;
|
||||
use Incoviba\Common\Define;
|
||||
use Incoviba\Common\Implement;
|
||||
@ -155,6 +155,19 @@ WHERE ptu.`proyecto` = ? AND tev.`activa`
|
||||
GROUP BY a.`id`";
|
||||
return $this->fetchMany($query, [$proyecto_id]);
|
||||
}
|
||||
public function fetchIdsByProyecto(int $proyecto_id): array
|
||||
{
|
||||
$query = "SELECT a.`id`
|
||||
FROM `{$this->getTable()}` a
|
||||
JOIN `propiedad_unidad` pu ON pu.`propiedad` = a.`propiedad`
|
||||
JOIN `unidad` ON `unidad`.`id` = pu.`unidad` AND pu.`principal` = 1
|
||||
JOIN `proyecto_tipo_unidad` ptu ON ptu.`id` = `unidad`.`pt`
|
||||
JOIN (SELECT e1.* FROM `estado_venta` e1 JOIN (SELECT MAX(`id`) AS 'id', `venta` FROM `estado_venta` GROUP BY `venta`) e0 ON e0.`id` = e1.`id`) ev ON ev.`venta` = a.`id`
|
||||
JOIN `tipo_estado_venta` tev ON tev.`id` = ev.`estado`
|
||||
WHERE ptu.`proyecto` = ? AND tev.`activa`
|
||||
GROUP BY a.`id`";
|
||||
return $this->connection->execute($query, [$proyecto_id])->fetchAll(PDO::FETCH_ASSOC);
|
||||
}
|
||||
public function fetchActivaByProyecto(int $proyecto_id): array
|
||||
{
|
||||
$query = "SELECT a.*
|
||||
@ -197,11 +210,27 @@ FROM `{$this->getTable()}` a
|
||||
WHERE `unidad`.`descripcion` LIKE ? AND tu.`descripcion` = ?";
|
||||
return $this->fetchMany($query, [$unidad, $tipo]);
|
||||
}
|
||||
public function fetchIdsByUnidad(string $unidad, string $tipo): array
|
||||
{
|
||||
$query = "SELECT a.id
|
||||
FROM `{$this->getTable()}` a
|
||||
JOIN `propiedad_unidad` pu ON pu.`propiedad` = a.`propiedad`
|
||||
JOIN `unidad` ON `unidad`.`id` = pu.`unidad`
|
||||
JOIN `proyecto_tipo_unidad` ptu ON ptu.`id` = `unidad`.`pt`
|
||||
JOIN `tipo_unidad` tu ON tu.`id` = ptu.`tipo`
|
||||
WHERE `unidad`.`descripcion` LIKE ? AND tu.`descripcion` = ?";
|
||||
return $this->connection->execute($query, [$unidad, $tipo])->fetchAll(PDO::FETCH_ASSOC);
|
||||
}
|
||||
public function fetchByPrecio(string $precio): array
|
||||
{
|
||||
$query = "SELECT * FROM `{$this->getTable()}` WHERE `valor_uf` = ?";
|
||||
return $this->fetchMany($query, [$precio]);
|
||||
}
|
||||
public function fetchIdsByPrecio(string $precio): array
|
||||
{
|
||||
$query = "SELECT `id` FROM `{$this->getTable()}` WHERE `valor_uf` = ?";
|
||||
return $this->connection->execute($query, [$precio])->fetchAll(PDO::FETCH_ASSOC);
|
||||
}
|
||||
public function fetchByPropietario(string $propietario): array
|
||||
{
|
||||
$query = "SELECT a.*
|
||||
@ -212,6 +241,16 @@ WHERE CONCAT_WS('-', `propietario`.`rut`, `propietario`.`dv`) LIKE :propietario
|
||||
OR CONCAT_WS(' ', `propietario`.`nombres`, `propietario`.`apellido_paterno`, `propietario`.`apellido_materno`) LIKE :propietario";
|
||||
return $this->fetchMany($query, [':propietario' => "%{$propietario}%"]);
|
||||
}
|
||||
public function fetchIdsByPropietario(string $propietario): array
|
||||
{
|
||||
$query = "SELECT a.id
|
||||
FROM `{$this->getTable()}` a
|
||||
JOIN `propietario` ON `propietario`.`rut` = a.`propietario`
|
||||
WHERE CONCAT_WS('-', `propietario`.`rut`, `propietario`.`dv`) LIKE :propietario OR `propietario`.`nombres` LIKE :propietario
|
||||
OR `propietario`.`apellido_paterno` LIKE :propietario OR `propietario`.`apellido_materno` LIKE :propietario
|
||||
OR CONCAT_WS(' ', `propietario`.`nombres`, `propietario`.`apellido_paterno`, `propietario`.`apellido_materno`) LIKE :propietario";
|
||||
return $this->connection->execute($query, [':propietario' => "%{$propietario}%"])->fetchAll(PDO::FETCH_ASSOC);
|
||||
}
|
||||
public function fetchByPropietarioNombreCompleto(string $propietario): array
|
||||
{
|
||||
$query = "SELECT a.*
|
||||
|
@ -1,6 +1,7 @@
|
||||
<?php
|
||||
namespace Incoviba\Repository\Venta;
|
||||
|
||||
use PDO;
|
||||
use Incoviba\Common\Ideal;
|
||||
use Incoviba\Common\Define;
|
||||
use Incoviba\Common\Implement;
|
||||
@ -117,6 +118,20 @@ class Unidad extends Ideal\Repository
|
||||
->where("a.`descripcion` LIKE ? AND tu.`descripcion` = ? AND (pu.`id` IS NULL OR `venta`.`id` IS NULL OR tev.`activa` = 0)");
|
||||
return $this->fetchMany($query, [$descripcion, $tipo]);
|
||||
}
|
||||
public function fetchDisponiblesIdsByDescripcionAndTipo(string $descripcion, string $tipo): array
|
||||
{
|
||||
$query = $this->connection->getQueryBuilder()
|
||||
->select('a.id')
|
||||
->from("{$this->getTable()} a")
|
||||
->joined("JOIN `proyecto_tipo_unidad` ptu ON ptu.`id` = a.`pt`
|
||||
JOIN `tipo_unidad` tu ON tu.`id` = ptu.`tipo`
|
||||
LEFT OUTER JOIN `propiedad_unidad` pu ON pu.`unidad` = a.`id`
|
||||
LEFT OUTER JOIN `venta` ON `venta`.`propiedad` = pu.`propiedad`
|
||||
LEFT OUTER JOIN (SELECT ev1.* FROM `estado_venta` ev1 JOIN (SELECT MAX(`id`) as 'id', `venta` FROM `estado_venta`) ev0 ON ev0.`id` = ev1.`id`) ev ON ev.`venta` = `venta`.`id`
|
||||
LEFT OUTER JOIN `tipo_estado_venta` tev ON tev.`id` = ev.`estado`")
|
||||
->where("a.`descripcion` LIKE ? AND tu.`descripcion` = ? AND (pu.`id` IS NULL OR `venta`.`id` IS NULL OR tev.`activa` = 0)");
|
||||
return $this->connection->execute($query, [$descripcion, $tipo])->fetchAll(PDO::FETCH_ASSOC);
|
||||
}
|
||||
|
||||
protected function joinProrrateo(): string
|
||||
{
|
||||
|
@ -22,7 +22,8 @@ class Search
|
||||
} else {
|
||||
$results = $this->find($query, $tipo);
|
||||
}
|
||||
return $this->sort($results);
|
||||
return $results;
|
||||
//return $this->sort($results);
|
||||
}
|
||||
|
||||
protected function findCualquiera(string $query): array
|
||||
@ -45,7 +46,7 @@ class Search
|
||||
}
|
||||
protected function find(string $query, string $tipo): array
|
||||
{
|
||||
preg_match_all('/["\']([\s\w]+)["\']|(\w+)/i', $query, $matches, PREG_SET_ORDER | PREG_UNMATCHED_AS_NULL);
|
||||
preg_match_all('/["\']([\s\w]+)["\']|(\w+)/iu', $query, $matches, PREG_SET_ORDER | PREG_UNMATCHED_AS_NULL);
|
||||
$queries = array_map(function($match) {
|
||||
array_shift($match);
|
||||
$valid = array_filter($match, function($line) {return $line !== null;});
|
||||
@ -56,7 +57,7 @@ class Search
|
||||
foreach ($queries as $q) {
|
||||
$this->add($results, $this->findVentas($q, $tipo));
|
||||
if (in_array($tipo, $tiposUnidades)) {
|
||||
$this->add($results, $this->findUnidadesDisponibles($q, $tipo));
|
||||
$this->add($results, $this->findUnidadesDisponibles($q, $tipo), false);
|
||||
}
|
||||
}
|
||||
return $results;
|
||||
@ -91,7 +92,7 @@ class Search
|
||||
protected function findUnidadesDisponibles(string $query, string $tipo): array
|
||||
{
|
||||
try {
|
||||
return $this->unidadRepository->fetchDisponiblesByDescripcionAndTipo($query, $tipo);
|
||||
return $this->unidadRepository->fetchDisponiblesIdsByDescripcionAndTipo($query, $tipo);
|
||||
} catch (EmptyResponse) {
|
||||
return [];
|
||||
}
|
||||
@ -99,7 +100,7 @@ class Search
|
||||
protected function findUnidad(string $query, string $tipo): array
|
||||
{
|
||||
try {
|
||||
return $this->ventaService->getByUnidad($query, $tipo);
|
||||
return $this->ventaRepository->fetchIdsByUnidad($query, $tipo);
|
||||
} catch (EmptyResult) {
|
||||
return [];
|
||||
}
|
||||
@ -107,7 +108,7 @@ class Search
|
||||
protected function findPropietario(string $query): array
|
||||
{
|
||||
try {
|
||||
return $this->ventaService->getByPropietario($query);
|
||||
return $this->ventaRepository->fetchIdsByPropietario($query);
|
||||
} catch (EmptyResult) {
|
||||
return [];
|
||||
}
|
||||
@ -116,7 +117,7 @@ class Search
|
||||
{
|
||||
try {
|
||||
$precio = str_replace(['$', '.', ','], ['', '', '.'], $query);
|
||||
return $this->ventaService->getByPrecio($precio);
|
||||
return $this->ventaRepository->fetchIdsByPrecio($precio);
|
||||
} catch (EmptyResult) {
|
||||
return [];
|
||||
}
|
||||
@ -125,7 +126,7 @@ class Search
|
||||
{
|
||||
try {
|
||||
$proyecto = $this->proyectoService->getByName($query);
|
||||
return $this->ventaService->getByProyecto($proyecto->id);
|
||||
return $this->ventaRepository->fetchIdsByProyecto($proyecto->id);
|
||||
} catch (EmptyResult) {
|
||||
return [];
|
||||
}
|
||||
@ -145,22 +146,21 @@ class Search
|
||||
}
|
||||
return $this->tipos;
|
||||
}
|
||||
protected function add(array &$results, array $found): void
|
||||
protected function add(array &$results, array $found, bool $is_venta = true): void
|
||||
{
|
||||
foreach ($found as $item) {
|
||||
if (!$this->inResults($item, $results)) {
|
||||
$item['tipo'] = 'venta';
|
||||
if (!$is_venta) {
|
||||
$item['tipo'] = 'unidad';
|
||||
}
|
||||
$results []= $item;
|
||||
}
|
||||
}
|
||||
}
|
||||
protected function inResults($item, array $results): bool
|
||||
{
|
||||
foreach ($results as $result) {
|
||||
if (get_class($item) === get_class($result) and $item->id === $result->id) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
return in_array($item, $results, true);
|
||||
}
|
||||
protected function sort(&$results): array
|
||||
{
|
||||
|
Reference in New Issue
Block a user