Browse Source

Add Atendimento Repository

main
lucas cardoso 2 years ago
parent
commit
2bf180d71f
  1. 193
      app/Repositories/AtendimentoRepository.php

193
app/Repositories/AtendimentoRepository.php

@ -0,0 +1,193 @@
<?php
namespace app\Repositories;
use app\Core\Repository;
use Exception;
class AtendimentoRepository extends Repository
{
static $table = 'atendimento';
public function getAtendimentoByEvento($fila, $evento = 'EMESPERA')
{
$table = AtendimentoModel::$table;
$tableEventosAtendimento = EventosAtendimentoRepository::$table;
$query = "SELECT ma.* FROM $table ma
INNER JOIN $tableEventosAtendimento me
ON ma.uniqueid = me.uniqueid
WHERE (SELECT m2.evento FROM $tableEventosAtendimento m2 WHERE ma.uniqueid = m2.uniqueid ORDER BY id DESC LIMIT 1) = :evento
AND me.fila = :fila";
return $this->db->read($query, ['evento' => $evento, 'fila' => $fila])->fetchAll();
}
public function findAtendAgent($matricula, $quantidade = 10)
{
$table = AtendimentoModel::$table;
$tableEventosAtendimento = EventosAtendimentoRepository::$table;
$data = [];
$query = "SELECT ma.*,
ma.nome AS profile_name,
ppr.protocolo as protocolo,
(SELECT m2.evento FROM $tableEventosAtendimento m2 WHERE ma.uniqueid = m2.uniqueid ORDER BY id DESC LIMIT 1) AS evento,
CASE
WHEN (SELECT m2.evento FROM $tableEventosAtendimento m2 WHERE ma.uniqueid = m2.uniqueid ORDER BY id DESC LIMIT 1) = 'START' THEN 1
ELSE 0
END AS status
FROM $table ma
INNER JOIN pbx_protocolo_reg ppr ON ma.uniqueid = ppr.uniqueid ";
if ($matricula) {
$query .= " WHERE ma.matricula = :matricula ";
$data['matricula'] = $matricula;
}
if (empty($quantidade)) {
$data['quantidade'] = 10;
} else {
$data['quantidade'] = $quantidade;
}
$query .= " ORDER BY status DESC, data_reg DESC
LIMIT :quantidade ";
return $this->db->read($query, $data)->fetchAll();
}
public function findAtenEmAberto($cliente_id = null)
{
$table = AtendimentoModel::$table;
$tableEventosAtendimento = EventosAtendimentoRepository::$table;
$query = "SELECT ma.* FROM $table ma
WHERE (SELECT m2.evento FROM $tableEventosAtendimento m2 WHERE ma.uniqueid = m2.uniqueid ORDER BY id DESC LIMIT 1) = 'START'
AND ma.matricula notnull ";
$data = [];
if ($cliente_id) {
$data['cliente_id'] = $cliente_id;
$query .= 'AND ma.cliente_id = :cliente_id';
return $this->db->read($query, $data)->fetch();
}
return $this->db->read($query, $data)->fetchAll();
}
public function getAtendimentoAbertoByAgente($matricula)
{
$table = AtendimentoModel::$table;
$tableEventosAtendimento = EventosAtendimentoRepository::$table;
$query = "SELECT ma.* FROM $table ma
WHERE (SELECT m2.evento FROM $tableEventosAtendimento m2 WHERE ma.uniqueid = m2.uniqueid ORDER BY id DESC LIMIT 1) = 'START'
AND ma.matricula = :matricula ";
return $this->db->read($query, ['matricula' => $matricula])->fetchAll();
}
public function getAtendAbandonado($fila = null)
{
$table = AtendimentoModel::$table;
$tableEventosAtendimento = EventosAtendimentoRepository::$table;
$data = [];
$query = "SELECT ma.*, me.fila as fila FROM $table ma
INNER JOIN $tableEventosAtendimento me
ON ma.uniqueid = me.uniqueid
AND me.evento = 'ABANDON'
WHERE (SELECT m2.evento FROM $tableEventosAtendimento m2 WHERE ma.uniqueid = m2.uniqueid ORDER BY id DESC LIMIT 1) = 'ABANDON'
AND ma.data_reg >= CURRENT_DATE ";
if ($fila) {
$query .= ' AND me.fila = :fila';
$data['fila'] = $fila;
}
return $this->db->read($query, $data)->fetchAll();
}
function update(array $params): int
{
$table = AtendimentoModel::$table;
$query = "UPDATE $table SET matricula = :matricula WHERE uniqueid = :uniqueid;";
if (empty($params['uniqueid'])) {
throw new Exception('Parâmetro uniqueid é obrigatório');
}
return $this->db->update($query, $params);
}
function list(array $params = []): array
{
$table = AtendimentoModel::$table;
$tableProtocoloReg = ProtocoloReg::$table;
$tableEventosAtendimento = EventosAtendimentoRepository::$table;
$dados = [];
$query = "SELECT ma.*,
ma.nome AS profile_name,
ppr.protocolo as protocolo,
(SELECT m2.evento FROM $tableEventosAtendimento m2 WHERE ma.uniqueid = m2.uniqueid ORDER BY id DESC LIMIT 1) AS evento,
CASE
WHEN (SELECT m2.evento FROM $tableEventosAtendimento m2 WHERE ma.uniqueid = m2.uniqueid ORDER BY id DESC LIMIT 1) = 'START' THEN 1
ELSE 0
END AS status
FROM $table ma
INNER JOIN $tableProtocoloReg ppr ON ma.uniqueid = ppr.uniqueid
WHERE 1=1 ";
if ($params['matricula']) {
$query .= " AND ma.matricula = :matricula ";
$dados['matricula'] = $params['matricula'];
}
if ($params['uniqueid']) {
$query .= " AND ma.uniqueid = :uniqueid ";
$dados['uniqueid'] = $params['uniqueid'];
}
if ($params['id']) {
$query .= " AND ma.id = :id ";
$dados['id'] = $params['id'];
}
if ($params['protocolo']) {
$query .= " AND ppr.protocolo::varchar LIKE '%{$params['protocolo']}%' ";
}
if ($params['evento']) {
$query .= " AND (SELECT m2.evento FROM $tableEventosAtendimento m2 WHERE ma.uniqueid = m2.uniqueid ORDER BY id DESC LIMIT 1) = :evento ";
$dados['evento'] = $params['evento'];
}
if ($params['nome']) {
$params['nome'] = strtolower($params['nome']);
$query .= " AND lower(ma.nome) LIKE '%{$params['nome']}%' ";
}
if (empty($params['quantidade'])) {
$dados['quantidade'] = 10;
} else {
$dados['quantidade'] = $params['quantidade'];
}
$query .= " ORDER BY status DESC, data_reg DESC
LIMIT :quantidade ";
return $this->db->read($query, $dados)->fetchAll();
}
function delete(array $params): int
{
$table = AtendimentoModel::$table;
$query = "DELETE FROM $table WHERE matricula = :matricula";
return $this->db->delete($query, $params);
}
public function get(array $params = [])
{
$table = AtendimentoModel::$table;
$tableEventosAtendimento = EventosAtendimentoRepository::$table;
$dados = [];
$query = "SELECT ma.*, me.fila as fila FROM $table ma
INNER JOIN $tableEventosAtendimento me
ON ma.uniqueid = me.uniqueid
WHERE 1=1 ";
if ($params['uniqueid']) {
$query .= " AND ma.uniqueid = :uniqueid ";
$dados['uniqueid'] = $params['uniqueid'];
}
if ($params['id']) {
$query .= " AND ma.id = :id ";
$dados['id'] = $params['id'];
}
return $this->db->read($query, $dados)->fetch();
}
}
Loading…
Cancel
Save