Accelerar listado ventas

This commit is contained in:
Juan Pablo Vial
2024-03-13 16:17:14 -03:00
parent 98953cce42
commit 8caa80459e
5 changed files with 70 additions and 8 deletions

View File

@ -22,6 +22,7 @@ $app->group('/ventas', function($app) {
$app->group('/by', function($app) { $app->group('/by', function($app) {
$app->get('/unidad/{unidad_id}', [Ventas::class, 'unidad']); $app->get('/unidad/{unidad_id}', [Ventas::class, 'unidad']);
}); });
$app->post('/get[/]', [Ventas::class, 'getMany']);
$app->post('[/]', [Ventas::class, 'proyecto']); $app->post('[/]', [Ventas::class, 'proyecto']);
}); });
$app->group('/venta/{venta_id}', function($app) { $app->group('/venta/{venta_id}', function($app) {

View File

@ -116,10 +116,15 @@
this.data.id = data.proyecto.id this.data.id = data.proyecto.id
this.data.proyecto = data.proyecto.descripcion this.data.proyecto = data.proyecto.descripcion
this.data.venta_ids = data.ventas 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 = [] const promises = []
data.ventas.forEach(venta_id => { chunks.forEach(chunk => {
const promise = this.get().venta(venta_id).then(() => { const promise = this.get().venta(chunk).then(count => {
progress.progress('increment') progress.progress('increment', count)
}) })
promises.push(promise) promises.push(promise)
}) })
@ -129,17 +134,22 @@
} }
}) })
}, },
venta: venta_id => { venta: chunk => {
return fetchAPI('{{$urls->api}}/venta/' + venta_id).then(response => { const body = new FormData()
body.set('ventas', chunk.join(','))
return fetchAPI('{{$urls->api}}/ventas/get', {method: 'post', body}).then(response => {
if (response.ok) { if (response.ok) {
return response.json() return response.json()
} }
}).then(data => { }).then(data => {
if (typeof data.venta === 'undefined') { if (data.ventas.length === 0) {
console.error(venta_id, data.error) console.error(chunk, data.error)
return return
} }
this.add().venta(data.venta) data.ventas.forEach(venta_id => {
this.add().venta(venta_id)
})
return data.ventas.length
}) })
} }
} }

View File

@ -74,6 +74,32 @@ class Ventas extends Controller
} }
return $this->withJson($response, $output); 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 public function porFirmar(ServerRequestInterface $request, ResponseInterface $response, Service\Venta $ventaService, Service\Redis $redisService): ResponseInterface
{ {
$body = $request->getBody(); $body = $request->getBody();

View File

@ -376,6 +376,27 @@ class Venta extends Ideal\Repository
->group('venta.id'); ->group('venta.id');
return $this->connection->execute($query, [$venta_id])->fetch(PDO::FETCH_ASSOC); 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 protected function fetchIds(string $query, ?array $data = null): array
{ {

View File

@ -79,6 +79,10 @@ class Venta extends Service
{ {
return $this->ventaRepository->fetchByIdForSearch($venta_id); 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 protected function process(Model\Venta $venta): Model\Venta
{ {