Optimizacion de queries a cargar de una sola vez
This commit is contained in:
@ -243,6 +243,72 @@ class Promotion extends Common\Ideal\Repository
|
||||
return $this->fetchMany($query, ['contract_id' => $contract_id, 'unit_id' => $unit_id]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $contract_id
|
||||
* @param array $unit_ids
|
||||
* @return array
|
||||
* @throws Common\Implement\Exception\EmptyResult
|
||||
*/
|
||||
public function fetchByContractAndUnits(int $contract_id, array $unit_ids): array
|
||||
{
|
||||
$interrogations = implode(',', array_map(fn($k) => ":id{$k}", array_keys($unit_ids)));
|
||||
$query = $this->connection->getQueryBuilder()
|
||||
->select('DISTINCT a.*, GROUP_CONCAT(COALESCE(pu.unit_id, u1.id, u2.id, u3.id) SEPARATOR "|") as unit_id')
|
||||
->from("{$this->getTable()} a")
|
||||
->joined('LEFT OUTER JOIN promotion_brokers pb ON pb.promotion_id = a.id')
|
||||
->joined('LEFT OUTER JOIN broker_contracts bc ON bc.broker_rut = pb.broker_rut')
|
||||
->joined('LEFT OUTER JOIN promotion_units pu ON pu.promotion_id = a.id')
|
||||
->joined('LEFT OUTER JOIN promotion_projects pp ON pp.promotion_id = a.id')
|
||||
->joined('LEFT OUTER JOIN proyecto_tipo_unidad ptu1 ON ptu1.proyecto = pp.project_id')
|
||||
->joined('LEFT OUTER JOIN unidad u1 ON u1.pt = ptu1.id')
|
||||
->joined('LEFT OUTER JOIN promotion_unit_types put ON put.promotion_id = a.id')
|
||||
->joined('LEFT OUTER JOIN proyecto_tipo_unidad ptu2 ON ptu2.tipo = put.unit_type_id AND ptu2.proyecto = put.project_id')
|
||||
->joined('LEFT OUTER JOIN unidad u2 ON u2.pt = ptu2.id')
|
||||
->joined('LEFT OUTER JOIN promotion_unit_lines pul ON pul.promotion_id = a.id')
|
||||
->joined('LEFT OUTER JOIN proyecto_tipo_unidad ptu3 ON ptu3.id = pul.unit_line_id')
|
||||
->joined('LEFT OUTER JOIN unidad u3 ON u3.pt = ptu3.id')
|
||||
->where("bc.id = :contract_id OR pu.unit_id IN ({$interrogations}) OR u1.id IN ({$interrogations}) OR u2.id IN ({$interrogations}) OR u3.id IN ({$interrogations})")
|
||||
->group('a.id');
|
||||
$unitParams = array_combine(array_map(fn($k) => "id{$k}", array_keys($unit_ids)), $unit_ids);
|
||||
try {
|
||||
$results = $this->connection->execute($query, array_merge(['contract_id' => $contract_id], $unitParams))->fetchAll(PDO::FETCH_ASSOC);
|
||||
if (empty($results)) {
|
||||
throw new Common\Implement\Exception\EmptyResult($query);
|
||||
}
|
||||
$temp = new class()
|
||||
{
|
||||
protected array $promotions = [];
|
||||
public function addItem(int $id, Model\Venta\Promotion $promotion): void
|
||||
{
|
||||
if (!array_key_exists($id, $this->promotions)) {
|
||||
$this->promotions[$id] = [
|
||||
'id' => $id,
|
||||
'promotions' => []
|
||||
];
|
||||
}
|
||||
$this->promotions[$id]['promotions'] []= $promotion;
|
||||
}
|
||||
public function toArray(): array
|
||||
{
|
||||
return array_values($this->promotions);
|
||||
}
|
||||
};
|
||||
foreach ($results as $result) {
|
||||
if (str_contains($result['unit_id'], '|')) {
|
||||
$ids = explode('|', $result['unit_id']);
|
||||
foreach ($ids as $id) {
|
||||
$temp->addItem((int) $id, $this->load($result));
|
||||
}
|
||||
continue;
|
||||
}
|
||||
$temp->addItem((int) $result['unit_id'], $this->load($result));
|
||||
}
|
||||
return $temp->toArray();
|
||||
} catch (PDOException $exception) {
|
||||
throw new Common\Implement\Exception\EmptyResult($query, $exception);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Model\Venta\Promotion $promotion
|
||||
* @param int $project_id
|
||||
|
Reference in New Issue
Block a user