Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ricardogranafirst/667d7d39520358025684f482e1270819 to your computer and use it in GitHub Desktop.
Save ricardogranafirst/667d7d39520358025684f482e1270819 to your computer and use it in GitHub Desktop.
Importações
## Importar partidos
INSERT INTO electoralparty
(`id`,
`name`,
`resumeName`,
`isActive`,
`identifier`)
SELECT (SELECT UUID()), nm_partido, sg_partido, 1, nr_partido
from (select distinct nm_partido, sg_partido, 1, nr_partido from tse2018am) as partidos
left join electoralparty ep on ep.identifier = partidos.NR_PARTIDO
where nm_partido <> '#NULO#' and ep.identifier is null;
## Caso necessário, importar o cargo com o respectivo codigo no arquivo
insert into candidateposition (id, name, identifier)
values (uuid(), 'Vereador', '13');
## Inserir candidatos
## ATENÇÃO: A consulta SELECT busca e insere os candidatos que não estão no banco.
## PODE HAVER candidatos já existentes no banco, porém com nome diferente no arquivo csv.
## Para evitar inserir em duplicidade, executar o SELECT desta instrução e verificar se o candidato já não existe
## com outro nome. Se for o caso, fazer UPDATE no nome para bater com o cadastrado no banco e NÃO inseri-lo.
INSERT INTO `lhince`.`candidate`
(`isActive`,
`id`,
`name`,
`email`,
`telphone`,
`cpf`,
`genderId`,
`zoneId`,
`cityId`,
`candidatePositionId`,
`electoralPartyId`)
SELECT 1, (select uuid()), nm_votavel, 'candidato@gmail.com', '9299999999',
floor(rand() * 100000000000), 'ae35febe-20c2-11ee-8773-0242c0a86002',
'af2980b6-20c2-11ee-8773-0242c0a86002', 'afc1c8ec-20c2-11ee-8773-0242c0a86002',
(select id from candidateposition pos where pos.identifier = candidatos.cd_cargo_pergunta),
(select id from electoralparty ep where ep.identifier = candidatos.nr_partido)
from (
select distinct nr_partido, nm_votavel, cd_cargo_pergunta
from tse2018am
where nr_partido > 0
and nm_municipio = 'MANAUS'
) as candidatos
LEFT JOIN candidate c on UPPER(c.name) = nm_votavel
WHERE c.id IS NULL;
## inserir uma pesquisa pelo sistema ou banco
## importar os candidatos para a pesquisa
insert into researchcandidate (id, researchId, candidateId, identifier)
select (select uuid()), 'ID DA PESQUISA',
(select c.id from candidate c
where c.name = candidatos.nm_votavel),
nr_votavel
from (
select distinct nr_partido, nm_votavel, nr_votavel
from tse2018am
where nr_partido > 0
and nm_municipio = 'MANAUS'
and cd_cargo_pergunta = CODIGO_DO_CARGO
) as candidatos;
## importar BRANCOS
insert into pooling_resume (id, researchId, placeId, section, candidateId, alternativeIntentionId, qtt_intention
SELECT (select uuid()), 'CODIGO_PESQUISA',
p.id,
nr_secao,
null,
'911a786a-1127-11ee-8d1c-0144af130089', -- BRANCOS
qt_votos
from tse2018am a
INNER JOIN city c on c.id = 'afc1c8ec-20c2-11ee-8773-0242c0a86002'
LEFT JOIN electoral_zone z on z.cityId = c.id and z.identifier = a.nr_zona
LEFT JOIN pooling_place p on p.zoneId = z.id and p.identifier = a.nr_local_votacao
WHERE a.cd_municipio = 2550
and a.cd_cargo_pergunta = CODIGO_CARGO
and a.cd_tipo_votavel = 2
## importar NULOS
insert into pooling_resume (id, researchId, placeId, section, candidateId, alternativeIntentionId, qtt_intention
SELECT (select uuid()), 'CODIGO_PESQUISA',
p.id,
nr_secao,
null,
'911a786a-1127-11ee-8d1c-0242ac130212', -- NULOS
qt_votos
from tse2018am a
INNER JOIN city c on c.id = 'afc1c8ec-20c2-11ee-8773-0242c0a86002'
LEFT JOIN electoral_zone z on z.cityId = c.id and z.identifier = a.nr_zona
LEFT JOIN pooling_place p on p.zoneId = z.id and p.identifier = a.nr_local_votacao
WHERE a.cd_municipio = 2550
and a.cd_cargo_pergunta = 11
and a.cd_tipo_votavel = 3
## Importar candidatos
## ATENÇÃO: A consulta é por NOME. Pode acontecer de o nome diferenciar do que está no BANCO. Conferir quem ficou NULL
## e fazer update o nome para o que está no banco para importar corretamente.
insert into pooling_resume (id, researchId, placeId, section, candidateId, alternativeIntentionId, qtt_intention
SELECT (select uuid()), 'CODIGO_PESQUISA',
p.id,
nr_secao,
cand.id,
null,
qt_votos
from tse2018am a
INNER JOIN city c on c.id = 'afc1c8ec-20c2-11ee-8773-0242c0a86002'
LEFT JOIN candidate cand on UPPER(cand.name) = UPPER(a.nm_votavel)
LEFT JOIN electoral_zone z on z.cityId = c.id and z.identifier = a.nr_zona
LEFT JOIN pooling_place p on p.zoneId = z.id and p.identifier = a.nr_local_votacao
WHERE a.cd_municipio = 2550
and a.cd_cargo_pergunta = 11
and a.cd_tipo_votavel = 1
## * Os 3 inserts acima não foram testados previamente e podem conter erros. Verificar se alguma coluna ficou vazia após a importação
## * Pode ser que o PLACE fique NULL. Nesse caso, por ser outro pleito, o local de votação pode não ter sido cadastrado ainda. Precisa verificar cada caso e inserir na tabela pooling_place
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment