From 01a97c6a5d2fdc12d6679ea6ec9de324f9f12f49 Mon Sep 17 00:00:00 2001 From: "douglas.strappasson" Date: Wed, 18 Oct 2023 16:01:40 -0400 Subject: [PATCH] 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 --- .../scripts/manutencaoDB/importaBilhetes.php | 111 ++++++++++-------- 1 file changed, 63 insertions(+), 48 deletions(-) diff --git a/asterisk/var_lib_asterisk/scripts/manutencaoDB/importaBilhetes.php b/asterisk/var_lib_asterisk/scripts/manutencaoDB/importaBilhetes.php index 01c63b1..4a62dd9 100755 --- a/asterisk/var_lib_asterisk/scripts/manutencaoDB/importaBilhetes.php +++ b/asterisk/var_lib_asterisk/scripts/manutencaoDB/importaBilhetes.php @@ -285,60 +285,75 @@ function CorrigeNumeroDestino($dbcon) { function CorrigeNumeroDestinoImportados($dbcon) { - $precisaCorrecao = "WITH importado_sem_corrigir AS ( - SELECT uniqueid FROM ast_bilhetes ab - WHERE corrige_num_destino = 0 - AND EXISTS (SELECT '' FROM pbx_bilhetes WHERE id_bilhetes = ab.id_bilhetes) - ), - precisa_corrigir AS ( - SELECT DISTINCT a.uniqueid, a.id_bilhetes, MIN( - CASE - WHEN POSITION('/' IN b.agente) > 0 THEN SUBSTRING(b.agente, POSITION('/' IN b.agente) + 1) - ELSE soNumero(b.agente) - END - ) AS dst - FROM ast_bilhetes a - INNER JOIN ast_eventos_dacs b ON b.uid2 = a.uniqueid - INNER JOIN importado_sem_corrigir c ON c.uniqueid = a.uniqueid - WHERE b.evento IN ('COMPLETEAGENT', 'COMPLETECALLER', 'TRANSFER') - AND POSITION('Agent/' IN b.agente) = 0 - AND a.corrige_num_destino = 0 - GROUP BY a.id_bilhetes, a.uniqueid - UNION - SELECT DISTINCT a.uniqueid, a.id_bilhetes, MIN( - CASE - WHEN POSITION('Local' IN a.dstchannel) > 0 THEN SUBSTRING(a.dstchannel, 7, POSITION('@' IN a.dstchannel) - 7) - WHEN POSITION('SIP' IN a.dstchannel) > 0 THEN SUBSTRING(a.dstchannel, 5, POSITION('-' IN a.dstchannel) - 5) - ELSE soNumero(a.dstchannel) - END - ) AS dst - FROM ast_bilhetes a - INNER JOIN ast_eventos_dacs b ON b.uid2 = a.uniqueid - INNER JOIN importado_sem_corrigir c ON c.uniqueid = a.uniqueid - WHERE b.evento IN ('COMPLETEAGENT', 'COMPLETECALLER', 'TRANSFER') - AND POSITION('Agent/' IN b.agente) > 0 - AND a.corrige_num_destino = 0 - GROUP BY a.id_bilhetes, a.uniqueid - )"; - - $updatePbxBilhete = $precisaCorrecao . "UPDATE pbx_bilhetes - SET dst = pc.dst - FROM precisa_corrigir pc - WHERE pbx_bilhetes.uniqueid = pc.uniqueid"; - - $updateAstBilhete = $precisaCorrecao . "UPDATE ast_bilhetes - SET dst = pc.dst, amaflags = 99, corrige_num_destino = 1 - FROM precisa_corrigir pc - WHERE ast_bilhetes.uniqueid = pc.uniqueid"; + // Apagar a tabela temporária + $dropTempTable = "DROP TABLE IF EXISTS temp_table_precisa_corrigir;"; + + if (!$result = pg_query($dbcon, $dropTempTable)) { + ibRaiseExcept("Não foi possível apagar a tabela temporária precisa_corrigir!"); + } + + // Criar a tabela temporária precisa_corrigir + $createTempTable = "CREATE TEMP TABLE temp_table_precisa_corrigir AS + WITH importado_sem_corrigir AS ( + SELECT uniqueid FROM ast_bilhetes ab + WHERE corrige_num_destino = 0 + AND EXISTS (SELECT '' FROM pbx_bilhetes WHERE id_bilhetes = ab.id_bilhetes) + ), + precisa_corrigir AS ( + SELECT DISTINCT a.uniqueid, a.id_bilhetes, MIN( + CASE + WHEN POSITION('/' IN b.agente) > 0 THEN SUBSTRING(b.agente, POSITION('/' IN b.agente) + 1) + ELSE soNumero(b.agente) + END + ) AS dst + FROM ast_bilhetes a + INNER JOIN ast_eventos_dacs b ON b.uid2 = a.uniqueid + INNER JOIN importado_sem_corrigir c ON c.uniqueid = a.uniqueid + WHERE b.evento IN ('COMPLETEAGENT', 'COMPLETECALLER', 'TRANSFER') + AND POSITION('Agent/' IN b.agente) = 0 + AND a.corrige_num_destino = 0 + GROUP BY a.id_bilhetes, a.uniqueid + UNION + SELECT DISTINCT a.uniqueid, a.id_bilhetes, MIN( + CASE + WHEN POSITION('Local' IN a.dstchannel) > 0 THEN SUBSTRING(a.dstchannel, 7, POSITION('@' IN a.dstchannel) - 7) + WHEN POSITION('SIP' IN a.dstchannel) > 0 THEN SUBSTRING(a.dstchannel, 5, POSITION('-' IN a.dstchannel) - 5) + ELSE soNumero(a.dstchannel) + END + ) AS dst + FROM ast_bilhetes a + INNER JOIN ast_eventos_dacs b ON b.uid2 = a.uniqueid + INNER JOIN importado_sem_corrigir c ON c.uniqueid = a.uniqueid + WHERE b.evento IN ('COMPLETEAGENT', 'COMPLETECALLER', 'TRANSFER') + AND POSITION('Agent/' IN b.agente) > 0 + AND a.corrige_num_destino = 0 + GROUP BY a.id_bilhetes, a.uniqueid + )SELECT * FROM precisa_corrigir;"; + + 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)) { - 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)) { - 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) { $pathLog = '/var/log/asterisk/atrualiza_prevenda.log';