Browse Source

Ajuste na funcao pois as consultas para o update estavam sendo realizadas em dois momentos distintos, para corrigir mudei a logica da consulta de WITH para criacao de tabela temporaria

ajuste_importa_bilhetes
douglas.strappasson 12 months ago
parent
commit
01a97c6a5d
  1. 111
      asterisk/var_lib_asterisk/scripts/manutencaoDB/importaBilhetes.php

111
asterisk/var_lib_asterisk/scripts/manutencaoDB/importaBilhetes.php

@ -285,60 +285,75 @@ function CorrigeNumeroDestino($dbcon) {
function CorrigeNumeroDestinoImportados($dbcon) function CorrigeNumeroDestinoImportados($dbcon)
{ {
$precisaCorrecao = "WITH importado_sem_corrigir AS ( // Apagar a tabela temporária
SELECT uniqueid FROM ast_bilhetes ab $dropTempTable = "DROP TABLE IF EXISTS temp_table_precisa_corrigir;";
WHERE corrige_num_destino = 0
AND EXISTS (SELECT '' FROM pbx_bilhetes WHERE id_bilhetes = ab.id_bilhetes) if (!$result = pg_query($dbcon, $dropTempTable)) {
), ibRaiseExcept("Não foi possível apagar a tabela temporária precisa_corrigir!");
precisa_corrigir AS ( }
SELECT DISTINCT a.uniqueid, a.id_bilhetes, MIN(
CASE // Criar a tabela temporária precisa_corrigir
WHEN POSITION('/' IN b.agente) > 0 THEN SUBSTRING(b.agente, POSITION('/' IN b.agente) + 1) $createTempTable = "CREATE TEMP TABLE temp_table_precisa_corrigir AS
ELSE soNumero(b.agente) WITH importado_sem_corrigir AS (
END SELECT uniqueid FROM ast_bilhetes ab
) AS dst WHERE corrige_num_destino = 0
FROM ast_bilhetes a AND EXISTS (SELECT '' FROM pbx_bilhetes WHERE id_bilhetes = ab.id_bilhetes)
INNER JOIN ast_eventos_dacs b ON b.uid2 = a.uniqueid ),
INNER JOIN importado_sem_corrigir c ON c.uniqueid = a.uniqueid precisa_corrigir AS (
WHERE b.evento IN ('COMPLETEAGENT', 'COMPLETECALLER', 'TRANSFER') SELECT DISTINCT a.uniqueid, a.id_bilhetes, MIN(
AND POSITION('Agent/' IN b.agente) = 0 CASE
AND a.corrige_num_destino = 0 WHEN POSITION('/' IN b.agente) > 0 THEN SUBSTRING(b.agente, POSITION('/' IN b.agente) + 1)
GROUP BY a.id_bilhetes, a.uniqueid ELSE soNumero(b.agente)
UNION END
SELECT DISTINCT a.uniqueid, a.id_bilhetes, MIN( ) AS dst
CASE FROM ast_bilhetes a
WHEN POSITION('Local' IN a.dstchannel) > 0 THEN SUBSTRING(a.dstchannel, 7, POSITION('@' IN a.dstchannel) - 7) INNER JOIN ast_eventos_dacs b ON b.uid2 = a.uniqueid
WHEN POSITION('SIP' IN a.dstchannel) > 0 THEN SUBSTRING(a.dstchannel, 5, POSITION('-' IN a.dstchannel) - 5) INNER JOIN importado_sem_corrigir c ON c.uniqueid = a.uniqueid
ELSE soNumero(a.dstchannel) WHERE b.evento IN ('COMPLETEAGENT', 'COMPLETECALLER', 'TRANSFER')
END AND POSITION('Agent/' IN b.agente) = 0
) AS dst AND a.corrige_num_destino = 0
FROM ast_bilhetes a GROUP BY a.id_bilhetes, a.uniqueid
INNER JOIN ast_eventos_dacs b ON b.uid2 = a.uniqueid UNION
INNER JOIN importado_sem_corrigir c ON c.uniqueid = a.uniqueid SELECT DISTINCT a.uniqueid, a.id_bilhetes, MIN(
WHERE b.evento IN ('COMPLETEAGENT', 'COMPLETECALLER', 'TRANSFER') CASE
AND POSITION('Agent/' IN b.agente) > 0 WHEN POSITION('Local' IN a.dstchannel) > 0 THEN SUBSTRING(a.dstchannel, 7, POSITION('@' IN a.dstchannel) - 7)
AND a.corrige_num_destino = 0 WHEN POSITION('SIP' IN a.dstchannel) > 0 THEN SUBSTRING(a.dstchannel, 5, POSITION('-' IN a.dstchannel) - 5)
GROUP BY a.id_bilhetes, a.uniqueid ELSE soNumero(a.dstchannel)
)"; END
) AS dst
$updatePbxBilhete = $precisaCorrecao . "UPDATE pbx_bilhetes FROM ast_bilhetes a
SET dst = pc.dst INNER JOIN ast_eventos_dacs b ON b.uid2 = a.uniqueid
FROM precisa_corrigir pc INNER JOIN importado_sem_corrigir c ON c.uniqueid = a.uniqueid
WHERE pbx_bilhetes.uniqueid = pc.uniqueid"; WHERE b.evento IN ('COMPLETEAGENT', 'COMPLETECALLER', 'TRANSFER')
AND POSITION('Agent/' IN b.agente) > 0
$updateAstBilhete = $precisaCorrecao . "UPDATE ast_bilhetes AND a.corrige_num_destino = 0
SET dst = pc.dst, amaflags = 99, corrige_num_destino = 1 GROUP BY a.id_bilhetes, a.uniqueid
FROM precisa_corrigir pc )SELECT * FROM precisa_corrigir;";
WHERE ast_bilhetes.uniqueid = pc.uniqueid";
if (!$result = pg_query($dbcon, $createTempTable)) {
ibRaiseExcept("Não foi possível criar a tabela temporária precisa_corrigir!");
}
// Atualizar a tabela pbx_bilhetes usando a tabela temporária
$updatePbxBilhete = "UPDATE pbx_bilhetes
SET dst = pc.dst
FROM temp_table_precisa_corrigir pc
WHERE pbx_bilhetes.uniqueid = pc.uniqueid";
if (!$result = pg_query($dbcon, $updatePbxBilhete)) { if (!$result = pg_query($dbcon, $updatePbxBilhete)) {
ibRaiseExcept("Nao possivel corrigir os bilhetes na tabela pbx_bilhetes!"); ibRaiseExcept("Não foi possível corrigir os bilhetes na tabela pbx_bilhetes!");
} }
// Atualizar a tabela ast_bilhetes usando a tabela temporária
$updateAstBilhete = "UPDATE ast_bilhetes
SET dst = pc.dst, amaflags = 99, corrige_num_destino = 1
FROM temp_table_precisa_corrigir pc
WHERE ast_bilhetes.uniqueid = pc.uniqueid";
if (!$result = pg_query($dbcon, $updateAstBilhete)) { if (!$result = pg_query($dbcon, $updateAstBilhete)) {
ibRaiseExcept("Nao possivel corrigir os bilhetes na tabela ast_bilhetes!"); ibRaiseExcept("Não foi possível corrigir os bilhetes na tabela ast_bilhetes!");
} }
}; }
function AtualizaPreVenda($dbcon) { function AtualizaPreVenda($dbcon) {
$pathLog = '/var/log/asterisk/atrualiza_prevenda.log'; $pathLog = '/var/log/asterisk/atrualiza_prevenda.log';

Loading…
Cancel
Save