From 8caa80459e1e82e8ed8147063a3a080b9afcbe1e Mon Sep 17 00:00:00 2001 From: Juan Pablo Vial Date: Wed, 13 Mar 2024 16:17:14 -0300 Subject: [PATCH] Accelerar listado ventas --- app/resources/routes/api/ventas.php | 1 + app/resources/views/ventas/list.blade.php | 26 ++++++++++++++++------- app/src/Controller/API/Ventas.php | 26 +++++++++++++++++++++++ app/src/Repository/Venta.php | 21 ++++++++++++++++++ app/src/Service/Venta.php | 4 ++++ 5 files changed, 70 insertions(+), 8 deletions(-) diff --git a/app/resources/routes/api/ventas.php b/app/resources/routes/api/ventas.php index 1c23c8b..9ff578f 100644 --- a/app/resources/routes/api/ventas.php +++ b/app/resources/routes/api/ventas.php @@ -22,6 +22,7 @@ $app->group('/ventas', function($app) { $app->group('/by', function($app) { $app->get('/unidad/{unidad_id}', [Ventas::class, 'unidad']); }); + $app->post('/get[/]', [Ventas::class, 'getMany']); $app->post('[/]', [Ventas::class, 'proyecto']); }); $app->group('/venta/{venta_id}', function($app) { diff --git a/app/resources/views/ventas/list.blade.php b/app/resources/views/ventas/list.blade.php index b4dd7b0..bf6bfa0 100644 --- a/app/resources/views/ventas/list.blade.php +++ b/app/resources/views/ventas/list.blade.php @@ -116,10 +116,15 @@ this.data.id = data.proyecto.id this.data.proyecto = data.proyecto.descripcion this.data.venta_ids = data.ventas + const chunkSize = 50 + const chunks = [] + for (let i = 0; i < data.ventas.length; i += chunkSize) { + chunks.push(data.ventas.splice(i, i + chunkSize)) + } const promises = [] - data.ventas.forEach(venta_id => { - const promise = this.get().venta(venta_id).then(() => { - progress.progress('increment') + chunks.forEach(chunk => { + const promise = this.get().venta(chunk).then(count => { + progress.progress('increment', count) }) promises.push(promise) }) @@ -129,17 +134,22 @@ } }) }, - venta: venta_id => { - return fetchAPI('{{$urls->api}}/venta/' + venta_id).then(response => { + venta: chunk => { + const body = new FormData() + body.set('ventas', chunk.join(',')) + return fetchAPI('{{$urls->api}}/ventas/get', {method: 'post', body}).then(response => { if (response.ok) { return response.json() } }).then(data => { - if (typeof data.venta === 'undefined') { - console.error(venta_id, data.error) + if (data.ventas.length === 0) { + console.error(chunk, data.error) return } - this.add().venta(data.venta) + data.ventas.forEach(venta_id => { + this.add().venta(venta_id) + }) + return data.ventas.length }) } } diff --git a/app/src/Controller/API/Ventas.php b/app/src/Controller/API/Ventas.php index 507f405..94fc0c9 100644 --- a/app/src/Controller/API/Ventas.php +++ b/app/src/Controller/API/Ventas.php @@ -74,6 +74,32 @@ class Ventas extends Controller } return $this->withJson($response, $output); } + public function getMany(ServerRequestInterface $request, ResponseInterface $response, Service\Redis $redisService, + Service\Venta $service): ResponseInterface + { + $body = $request->getParsedBody(); + $output = [ + 'input' => $body, + 'ventas' => [] + ]; + $ventas = explode(',', $body['ventas']); + foreach ($ventas as $venta_id) { + $redisKey = "venta:{$venta_id}"; + try { + $venta = $this->fetchRedis($redisService, $redisKey); + $output['ventas'] []= $venta; + } catch (EmptyRedis) { + try { + $venta = $service->getById($venta_id); + $output['ventas'] []= $venta; + $this->saveRedis($redisService, $redisKey, $venta); + } catch (EmptyResult $exception) { + $this->logger->notice($exception); + } + } + } + return $this->withJson($response, $output); + } public function porFirmar(ServerRequestInterface $request, ResponseInterface $response, Service\Venta $ventaService, Service\Redis $redisService): ResponseInterface { $body = $request->getBody(); diff --git a/app/src/Repository/Venta.php b/app/src/Repository/Venta.php index 8f0e238..a6a7a0f 100644 --- a/app/src/Repository/Venta.php +++ b/app/src/Repository/Venta.php @@ -376,6 +376,27 @@ class Venta extends Ideal\Repository ->group('venta.id'); return $this->connection->execute($query, [$venta_id])->fetch(PDO::FETCH_ASSOC); } + public function fetchByIdForList(int $venta_id): array + { + $query = $this->connection->getQueryBuilder() + ->select('venta.id AS id, venta.fecha AS fecha, venta.valor_uf AS valor') + ->columns('proyecto.id AS proyecto_id, proyecto.descripcion AS proyecto_descripcion') + ->columns('CONCAT_WS(" ", propietario.nombres, propietario.apellido_paterno, propietario.apellido_materno) AS propietario') + ->columns("GROUP_CONCAT(unidad.descripcion SEPARATOR ' - ') AS unidad_descripcion, tu.descripcion AS tipo_unidad_descripcion, ptu.m2 + ptu.logia + ptu.terraza AS superficie") + ->columns('tev.activa') + ->from($this->getTable()) + ->joined('JOIN propietario ON propietario.rut = venta.propietario') + ->joined('JOIN propiedad_unidad pu ON pu.propiedad = venta.propiedad') + ->joined('JOIN unidad ON unidad.id = pu.unidad') + ->joined('JOIN proyecto_tipo_unidad ptu ON unidad.pt = ptu.id') + ->joined('JOIN proyecto ON proyecto.id = ptu.proyecto') + ->joined('JOIN tipo_unidad tu ON tu.id = ptu.tipo') + ->joined('JOIN (SELECT ev1.* FROM estado_venta ev1 JOIN (SELECT MAX(id) AS id, venta FROM estado_venta GROUP BY venta) ev0 ON ev0.id = ev1.id) ev ON ev.venta = venta.id') + ->joined('JOIN tipo_estado_venta tev ON ev.estado = tev.id') + ->where('venta.id = ?') + ->group('venta.id'); + return $this->connection->execute($query, [$venta_id])->fetch(PDO::FETCH_ASSOC); + } protected function fetchIds(string $query, ?array $data = null): array { diff --git a/app/src/Service/Venta.php b/app/src/Service/Venta.php index 17de561..34bee46 100644 --- a/app/src/Service/Venta.php +++ b/app/src/Service/Venta.php @@ -79,6 +79,10 @@ class Venta extends Service { return $this->ventaRepository->fetchByIdForSearch($venta_id); } + public function getByIdForList(int $venta_id): array + { + return $this->ventaRepository->fetchByIdForList($venta_id); + } protected function process(Model\Venta $venta): Model\Venta {