You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

180 lines
6.6 KiB

<?php
namespace app\Repositories;
use app\Core\Repository;
use app\Models\SupervisorModel;
use Exception;
use app\Models\EventosAtendimentosModel;
class SupervisorRepository extends Repository
{
function config(): void
{
$this->table = SupervisorModel::$table;
}
public function listaAgentesDisponivel($status = null, $somenteLivre = true)
{
$table = $this->table;
$tableEventosAtendimento = EventosAtendimentosModel::$table;
$data = [];
$query = " SELECT *,
(
SELECT
count(*)
FROM
atendimento ma
WHERE 'START' = (SELECT m2.evento FROM $tableEventosAtendimento m2
WHERE ma.uniqueid = m2.uniqueid
ORDER BY m2.id DESC LIMIT 1)
AND ma.matricula = ms.matricula
) AS countAtendimentos,
(
SELECT
count(*)
FROM
atendimento ma
WHERE ma.data_reg >= current_date
AND ma.matricula = ms.matricula
) AS numero_atendimento_dia
FROM $table ms
WHERE 1=1
";
if ($somenteLivre) {
$query .= " AND (
SELECT
count(*)
FROM
atendimento ma
WHERE 'START' = (SELECT m2.evento FROM $tableEventosAtendimento m2
WHERE ma.uniqueid = m2.uniqueid
ORDER BY m2.id DESC LIMIT 1)
AND ma.matricula = ms.matricula
) < (SELECT prm_media_simultaneo FROM pbx_parametros pp LIMIT 1 )";
}
if ($status) {
$query .= " AND ms.status = :status ";
$data['status'] = $status;
}
$query .= " ORDER BY countAtendimentos ";
return $this->db->read($query, $data)->fetchAll();
}
public function statusAgente($matricula)
{
$table = $this->table;
$tableEventosAtendimento = EventosAtendimentosModel::$table;
$query = "SELECT
*,
(
SELECT
count(*)
FROM
atendimento ma
WHERE 'START' = (SELECT m2.evento FROM $tableEventosAtendimento m2
WHERE ma.uniqueid = m2.uniqueid
ORDER BY m2.id DESC LIMIT 1)
AND ma.matricula = ms.matricula
) AS numero_atendimento
FROM $table ms
WHERE ms.matricula = :matricula ";
return $this->db->read($query, ['matricula' => $matricula])->fetch();
}
public function findAgentByQueue($queue, $status)
{
$table = $this->table;
$tableEventosAtendimento = EventosAtendimentosModel::$table;
$data = [];
$query = "SELECT *
FROM $table ms
WHERE ms.fila = :queue
AND ms.status = :status
ORDER BY (
SELECT
count(*)
FROM
atendimento ma
WHERE 'START' = (SELECT m2.evento FROM $tableEventosAtendimento m2
WHERE ma.uniqueid = m2.uniqueid
ORDER BY m2.id DESC LIMIT 1)
AND ma.matricula = ms.matricula
)
";
$data['queue'] = $queue;
$data['status'] = $status;
return $this->db->read($query, $data)->fetchAll();
}
function list(array $params = []): array
{
$table = $this->table;
$query = "SELECT * FROM $table WHERE 1=1";
if ($params['matricula']) {
$query .= " AND matricula = :matricula ";
$dados['matricula'] = $params['matricula'];
}
if ($params['fila']) {
$query .= " AND fila = :fila ";
$dados['fila'] = $params['fila'];
}
if (empty($params['id_empresa'])) {
throw new Exception("Parâmetro id_empresa é obrigatório, $table");
}
$query .= " AND id_empresa = :id_empresa ";
$dados['id_empresa'] = $params['id_empresa'];
return $this->db->read($query, $dados)->fetchAll();
}
function update(array $params): int
{
$table = $this->table;
$params['duracao'] = 'now()';
$query = "UPDATE $table SET ";
foreach ($params as $key => $value) {
if (array_key_last($params) == $key) {
$query .= " $key = :$key";
}
else {
$query .= " $key = :$key, ";
}
}
$query .= " WHERE matricula = :matricula AND tempo_login = (SELECT MAX(tempo_login) FROM $table WHERE matricula = :matricula);";
if (empty($params['matricula'])) {
throw new Exception("Parâmetro matricula é obrigatório, $table");
}
return $this->db->update($query, $params);
}
function delete(array $params): int
{
$table = $this->table;
$query = "DELETE FROM $table WHERE matricula = :matricula";
if (empty($params['matricula'])) {
throw new Exception('Parâmetro matricula é obrigatório');
}
return $this->db->delete($query, $params);
}
function get(array $params)
{
$table = $this->table;
$query = "SELECT * FROM $table WHERE 1=1";
if ($params['matricula']) {
$query .= " AND matricula = :matricula ";
$dados['matricula'] = $params['matricula'];
}
if (empty($params['matricula'])) {
throw new Exception('Parâmetro matricula é obrigatório');
}
return $this->db->read($query, $dados)->fetch();
}
}