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.
 
 
 
 
 
 

189 lines
7.8 KiB

<?php
namespace service;
use app\Controllers\MessageController;
use app\Controllers\SystemMessageController;
use app\Core\Commands;
use app\Interfaces\IApiMedia;
use app\Models\Atendimento;
use app\Models\Evento;
use app\Models\Message;
use app\Models\NotificaMedia;
use app\Models\SupervisorModel;
use websocket\WsInterface;
class ServiceTimeout implements IService
{
/** @var String[] $mensagem lista de mensagens para o sistema*/
private $mensagem;
/** @var IApiMedia $api provider api*/
private $api;
/** @var MessageController $message controller de message*/
private $message;
/** @var NotificaMedia $notificaMedia model de NotificaMedia*/
private $notificaMedia;
/** @var Message $mensages model de Message*/
private $mensages;
/** @var Atendimento $atendimentos model de atendimento*/
private $atendimentos;
/** @var Evento $eventosModel model de eventos */
protected $eventosModel;
/** @var SystemMessageController $systemController controller de mensagens de sistemas */
protected $systemController;
private $timeout_agent_alert = (CONF_WHATSAPP_TIMEOUT_AGENT_AVISO / 60);
private $timeout_agent_desconexao = (CONF_WHATSAPP_TIMEOUT_AGENT_DESCONEXAO / 60);
function __construct()
{
$this->eventosModel = new Evento();
$this->message = new MessageController();
$this->notificaMedia = new NotificaMedia();
$this->mensages = new Message();
$this->atendimentos = new Atendimento();
$this->systemController = new SystemMessageController();
$this->mensagem = [
"ALERTA_INATIVIDADE" => "Você será desconectado em " . ($this->timeout_agent_desconexao - $this->timeout_agent_alert) . " minutos, para permanecer em atendimento clique em '/presente'",
"ALERTA_DESCONECTADO" => "Você foi desconectado por inatividade!",
"ALERTA_CLIENTE" => "Olá, como podemos lhe ajudar!",
"TIMEOUT_CLIENT_INATIVIDADE" => "O tempo da conversa foi expirado, por favor volte novamente a fila para um novo atendimento!",
"TALK_FINISHED" => "O atendimento foi finalizado!",
"TALK_ALERT_FINISH" => "Por favor, volte a comunicação com nosso atendimento em 1 minuto ou atendimento será encerrado!"
];
}
public function run()
{
$atendiss = $this->atendimentos->findAtenEmAberto();
foreach ($atendiss as $value) {
$this->api = returnChannel($value->context);
$this->timeoutCliente($value->uniqueid, $value->cliente_id);
$this->timeoutSessao($value->uniqueid, $value->cliente_id, $value->data_reg);
}
}
private function timeoutCliente($uniqueid, $client)
{
// echo "unique: $uniqueid - client: $client \n";
if ($this->message->timeoutTalk($uniqueid, $client) == 'FINISH') {
$this->api->enviarMsg($client, $this->mensagem['TIMEOUT_CLIENT_INATIVIDADE']);
$this->finalizar($client);
return null;
}
if ($this->message->timeoutTalk($uniqueid, $client) == 'ALERT') {
$msg = $this->mensages->findLastMessage($uniqueid);
$retorno = $this->notificaMedia->verificaNotifica(
$uniqueid,
$client,
utf8_encode($this->mensagem['TALK_ALERT_FINISH']) . $msg->id
);
if ($retorno->quant == 0) {
$this->notificaMedia->addNotifica(
$uniqueid,
$client,
utf8_encode($this->mensagem['TALK_ALERT_FINISH']) . $msg->id
);
$this->api->enviarMsg(
$client,
$this->mensagem['TALK_ALERT_FINISH']
);
}
}
}
private function timeoutSessao($uniqueid, $client, $data_atendimento)
{
$date1 = date_create($data_atendimento);
$date2 = date_create();
echo $date1->format('Y-m-d H:i:s') . "\n";
echo $date2->format('Y-m-d H:i:s') . "\n";
$diff = date_diff($date1, $date2);
echo $diff->h . "\n";
if ($diff->h > 23) {
$this->notificaMedia->addNotifica(
$uniqueid,
$client,
utf8_encode($this->mensagem['TIMEOUT_CLIENT_INATIVIDADE']) . $uniqueid
);
$this->api->enviarMsg(
$client,
$this->mensagem['TIMEOUT_CLIENT_INATIVIDADE']
);
$this->finalizar($client);
}
}
public function finalizar($numero)
{
try {
$this->atendimentos->begin();
$supervisorModel = new SupervisorModel();
$atendimento = $this->atendimentos->getAtendimentoByCliente($numero, CONF_EVENT_START);
//cria o evento pra finalizar
$ret = $this->eventosModel->createEvento(
$atendimento->uniqueid,
CONF_EVENT_TIMEOUT_CLIENTE,
date('Y-m-d H:i:s'),
date('Y-m-d H:i:s'),
$atendimento->fila
);
if ($ret) {
$mensagemW = 'Atendimento finalizado pelo sistema por inatividade do cliente';
$ws = new WsInterface();
$agente = $supervisorModel->findAgentByMatricula($atendimento->matricula);
$messegeModel = new Message();
$messegeModel->addMessage(
$atendimento->uniqueid,
$numero,
$agente->matricula,
'finish',
$mensagemW,
$atendimento->nome,
$atendimento->context,
'read'
);
//verifica se está como indisponivel e caso não tenha atendimento ele entra em pausa
if ($agente->status == CONF_AGENT_STATUS_INDISPONIVEL) {
$atends = $this->atendimentos->getAtendimentoAbertoByAgente($atendimento->matricula);
if (empty($atends)) {
$agente->enterPause($atendimento->matricula, $agente->motivo_pausa);
}
}
//coloca o agente como livre caso esteja ocupado e com menos atendimento que o maximo de atendimento
if ($agente->status == CONF_AGENT_STATUS_OCUPADO) {
$param = $this->atendimentos->getQuantiAtendimentSimultaneos();
$atendimentosAbertos = $this->atendimentos->getAtendimentoAbertoByAgente($agente->matricula);
if (count($atendimentosAbertos) < $param->prm_media_simultaneo) {
$supervisorModel->updateAgent($agente->matricula, CONF_AGENT_STATUS_LIVRE);
}
}
//envia as mensagens do sistema do momento atual para o cliente
$this->systemController->sendMessageSystem(
CONF_MOMENT_FINALIZAR_ATENDIMENTO,
[["nome" => "@autor_name", "valor" => 'inatividade']],
$this->api,
$numero
);
$this->atendimentos->commit();
//notifica o agente da mudança
$ws->enviaMsg($ws->enviaActions($mensagemW, 'finish', $agente->matricula, $atendimento->uniqueid));
return true;
} else {
$this->atendimentos->rollback();
$this->api->enviarMsg($numero, CONF_NAME_REPONSE . " : Erro ao finalizar!");
return true;
}
} catch (\Exception $th) {
$this->atendimentos->rollback();
$this->api->enviarMsg($numero, CONF_NAME_REPONSE . " : " . $th->getMessage());
return true;
}
}
}