import 'package:rkm/service/db/db_sqlite_load.dart';
import 'package:sqflite/sqflite.dart';

class DBQuerys{
  duplicidadeCNS(String CNS)async{
    // 707806656421614 JÁ EXISTE
    // String teste = '707806656421614';
    Database db = await DbSqliteLoad().intialDB();
    // print(await db.rawQuery('SELECT * FROM CIDADAOS WHERE CNS=$teste'));
    List list = await db.rawQuery('SELECT * FROM CIDADAOS WHERE CNS = $CNS');
    return list;
  }
  duplicidadeCPF(numCPF)async{
    // 11992420475 já existe
    String teste = '11992420475';
    Database db = await DbSqliteLoad().intialDB();
    // print(await db.rawQuery('SELECT CPF FROM CIDADAOS'));
    List list = await db.rawQuery("SELECT * FROM CIDADAOS WHERE CPF = $numCPF");
    // List list = await db.rawQuery("SELECT * FROM CIDADAOS WHERE CPF = $teste");
    return list;
  }
  buscaBairro(nomeBairro)async{
    Database db = await DbSqliteLoad().intialDB();
    List list = await db.rawQuery("SELECT * FROM BAIRROS WHERE NO_BAIRRO = '$nomeBairro'");
    // print(await db.rawQuery('SELECT NO_BAIRRO FROM BAIRROS'));
    // print(list[0]['ID_BAIRRO']);
    return list[0]['ID_BAIRRO'].toString();
  }
  buscaTipoLogradouro(tipo)async{
    Database db = await DbSqliteLoad().intialDB();
    List list = await db.rawQuery("SELECT * FROM LOGRADOUROS_TIPOS WHERE NO_LOGR_TIPO = '$tipo'");
    // print(await db.rawQuery('SELECT NO_BAIRRO FROM BAIRROS'));
    print(list[0]['ID_LOGR_TIPO']);
    return list[0]['ID_LOGR_TIPO'];
  }
  buscaprocedimento()async{
    Database db = await DbSqliteLoad().intialDB();
    var query = "SELECT * FROM PROCEDIMENTOS_SIGTAP";
    List list = await db.rawQuery(query);
    // print(list);
    return list;
  }
  buscaCBOS()async{
    Database db = await DbSqliteLoad().intialDB();
    var query = "SELECT * FROM CBOS ORDER BY DESCRICAO_CBO";
    List list = await db.rawQuery(query);
    // print(list);
    return list;
  }
  buscaProcIniciais()async{
    Database db = await DbSqliteLoad().intialDB();
    var query = "SELECT DISTINCT ATENDIMENTO, COD_PROC_SIGTAP, CODIGO_CBO FROM PROCEDIMENTOS_INICIAIS ORDER BY ATENDIMENTO, COD_PROC_SIGTAP, CODIGO_CBO";
    List list = await db.rawQuery(query);
    // print(list);
    return list;
  }
  buscaProceduresCBO(String COD_PROC_SIGTAP,String CODIGO_CBO)async{
    Database db = await DbSqliteLoad().intialDB();
    var query = 'SELECT * FROM PROCEDIMENTOS_SIGTAP_CBO WHERE COD_PROC_SIGTAP = "$COD_PROC_SIGTAP" AND CODIGO_CBO = "$CODIGO_CBO"';
    List list = await db.rawQuery(query);
    return list;
  }
  buscaMunicipioporNome(String NO_MUNICIPIO)async{
    Database db = await DbSqliteLoad().intialDB();
    var query = 'SELECT * FROM MUNICIPIOS WHERE NO_MUNICIPIO = "$NO_MUNICIPIO"';
    List list = await db.rawQuery(query);
    // print(list);
    return list;
  }
  buscaMunicipioporId(int ID_MUNICIPIO)async{
    Database db = await DbSqliteLoad().intialDB();
    var query = 'SELECT * FROM MUNICIPIOS WHERE ID_MUNICIPIO = $ID_MUNICIPIO';
    List list = await db.rawQuery(query);
    // print(list);
    return list;
  }
  buscaTipoLogradouroPorId(int ID_LOGR_TIPO)async{
    Database db = await DbSqliteLoad().intialDB();
    var query = 'SELECT * FROM LOGRADOUROS_TIPOS WHERE ID_LOGR_TIPO = $ID_LOGR_TIPO';
    List list = await db.rawQuery(query);
    // print(list);
    return list;
  }
  buscaLoteamentoPorDesc(String DESC_LOGR_LOTEAMENTO)async{
    Database db = await DbSqliteLoad().intialDB();
    var query = 'SELECT * FROM LOGRADOUROS_LOTEAMENTOS WHERE DESC_LOGR_LOTEAMENTO = "$DESC_LOGR_LOTEAMENTO"';
    List list = await db.rawQuery(query);
    // print(list);
    int result = list[0]['ID_LOGR_LOTEAMENTO'];
    return result;
  }
  buscaLoteamentoPorID(int ID_LOGR_LOTEAMENTO)async{
    Database db = await DbSqliteLoad().intialDB();
    var query = 'SELECT * FROM LOGRADOUROS_LOTEAMENTOS WHERE ID_LOGR_LOTEAMENTO = $ID_LOGR_LOTEAMENTO';
    List list = await db.rawQuery(query);
    print(list);
    String result = list[0]['DESC_LOGR_LOTEAMENTO'];
    return result;
  }
  buscaTodosMunicipios()async{
    Database db = await DbSqliteLoad().intialDB();
    var query = "SELECT * FROM MUNICIPIOS ORDER BY NO_MUNICIPIO";
    List list = await db.rawQuery(query);
    // print(list);
    return list;
  }
  Future buscaCidadao(NOME_CIDADAO)async{
    Database db = await DbSqliteLoad().intialDB();
    var query = 'SELECT * FROM CIDADAOS WHERE NOME_CIDADAO = "$NOME_CIDADAO"';
    List list = await db.rawQuery(query);
    // print(list);
    return list;
  }
  Future deleteCidadao()async{
    Database db = await DbSqliteLoad().intialDB();
    var query = "DELETE FROM CIDADAOS WHERE CD_USUARIO_SUS = '872488F88D1B2DB54D55BC8BBA2FAD1B'";
    await db.rawQuery(query);
    // print(list);
    return 'ok';
  }
  ///////INICIO CIDADAO/////
  Future cadCidadaos()async{
    Database db = await DbSqliteLoad().intialDB();
    var query = "SELECT * FROM CIDADAOS WHERE ORIGEM = 'A' AND SINCRONIZADO = 'N'";
    List<Map<String, dynamic>> maps = await db.rawQuery(query);
    // print(list);
    return List.generate(maps.length, (i) {
      return maps[i];
    });
  }
  Future cadDomiciliares()async{
    Database db = await DbSqliteLoad().intialDB();
    var query = "SELECT * FROM CIDADAO_DOMICILIO WHERE ORIGEM = 'A' AND SINCRONIZADO = 'N'";
    List<Map<String, dynamic>> maps =  await db.rawQuery(query);
    // print(list);
    return List.generate(maps.length, (i) {
      return maps[i];
    });
  }
  Future domCidadaos()async{
    Database db = await DbSqliteLoad().intialDB();
    // Busca a relação de Domicilio e Cidadao
    var query = "SELECT CC.* FROM DOMICILIOS_CIDADAOS CC INNER JOIN CIDADAO_DOMICILIO CD ON CD.ID_DOMICILIO = CC.ID_DOMICILIO WHERE CD.ORIGEM = 'A' AND CD.SINCRONIZADO = 'N'";
    List<Map<String, dynamic>> maps = await db.rawQuery(query);
    // print(list);
    return List.generate(maps.length, (i) {
      return maps[i];
    });
  }
  Future logrDomicilios()async{
    Database db = await DbSqliteLoad().intialDB();
    // Busca os Endereços de cada Domicilio
    var query = "SELECT DISTINCT CD.ID_DOMICILIO, C.NO_BAIRRO_LOGR, C.ID_LOGR_TIPO, C.NOME_LOGR, C.NUMERO_LOGR, C.COMPL_LOGR, C.LOTEAMENTO_LOGR FROM CIDADAO_DOMICILIO CD INNER JOIN CIDADAOS C ON CD.ID_DOMICILIO = C.ID_DOMICILIO WHERE C.ORIGEM = 'A' AND CD.SINCRONIZADO = 'N'";
    List<Map<String, dynamic>> maps =  await db.rawQuery(query);
    // print(list);
    return List.generate(maps.length, (i) {
      return maps[i];
    });
  }
  Future cadIndividuais()async{
    Database db = await DbSqliteLoad().intialDB();
    var query = "SELECT * FROM CADASTROS_INDIVIDUAIS WHERE ORIGEM = 'A' AND SINCRONIZADO = 'N'";
    List<Map<String, dynamic>> maps = await db.rawQuery(query);
    // print(list);
    return List.generate(maps.length, (i) {
      return maps[i];
    });
  }
///////FIM CIDADAO/////

///////ATENDIMENTOS - TODOS REGISTROS/////
  Future atendsIndivCompl()async{
    Database db = await DbSqliteLoad().intialDB();
    var query = "SELECT AIC.* FROM ATENDS_INDIV_COMPL AIC INNER JOIN ATENDS_INDIVIDUAIS AI ON AI.ID_ATEND_INDIVIDUAL = AIC.ID_ATEND_INDIVIDUAL WHERE AI.SINCRONIZADO = 'N'";
    List<Map<String, dynamic>> maps = await db.rawQuery(query);
    // print(list);
    return List.generate(maps.length, (i) {
      return maps[i];
    });
  }
  Future atendsIndivExames()async{
    Database db = await DbSqliteLoad().intialDB();
    var query = "SELECT AIE.* FROM ATENDS_INDIV_EXAMES AIE INNER JOIN ATENDS_INDIVIDUAIS AI ON AI.ID_ATEND_INDIVIDUAL = AIE.ID_ATEND_INDIVIDUAL WHERE AI.SINCRONIZADO = 'N'";
    List<Map<String, dynamic>> maps = await db.rawQuery(query);
    // print(list);
    return List.generate(maps.length, (i) {
      return maps[i];
    });
  }
  Future atendsIndivProc()async{
    Database db = await DbSqliteLoad().intialDB();
    var query = "SELECT AIP.* FROM ATENDS_INDIV_PROC AIP INNER JOIN ATENDS_INDIVIDUAIS AI ON AI.ID_ATEND_INDIVIDUAL = AIP.ID_ATEND_INDIVIDUAL WHERE AI.SINCRONIZADO = 'N'";
    List<Map<String, dynamic>> maps = await db.rawQuery(query);
    // print(list);
    return List.generate(maps.length, (i) {
      return maps[i];
    });
  }
  Future atendsIndivProf()async{
    Database db = await DbSqliteLoad().intialDB();
    var query = "SELECT AIPR.* FROM ATENDS_INDIV_PROF AIPR INNER JOIN ATENDS_INDIVIDUAIS AI ON AI.ID_ATEND_INDIVIDUAL = AIPR.ID_ATEND_INDIVIDUAL WHERE AI.SINCRONIZADO = 'N'";
    List<Map<String, dynamic>> maps = await db.rawQuery(query);
    // print(list);
    return List.generate(maps.length, (i) {
      return maps[i];
    });
  }
  Future atendsIndividuais()async{
    Database db = await DbSqliteLoad().intialDB();
    var query = "SELECT AI.*, CI.ID_AREA_CIDADAO, CI.ID_MICROAREA_CIDADAO FROM ATENDS_INDIVIDUAIS AI LEFT JOIN CADASTROS_INDIVIDUAIS CI ON AI.ID_CIDADAO = CI.ID_CIDADAO WHERE AI.SINCRONIZADO = 'N'";
    List<Map<String, dynamic>> maps = await db.rawQuery(query);
    // print(list);
    return List.generate(maps.length, (i) {
      return maps[i];
    });
  }
  Future procedimentosFichas()async{
    Database db = await DbSqliteLoad().intialDB();
    var query = "SELECT PF.* FROM PROCEDIMENTOS_FICHAS PF WHERE PF.SINCRONIZADO = 'N'";
    List<Map<String, dynamic>> maps = await db.rawQuery(query);
    // print(list);
    return List.generate(maps.length, (i) {
      return maps[i];
    });
  }
  Future procedimentosIndiv()async{
    Database db = await DbSqliteLoad().intialDB();
    var query = "SELECT PI.* FROM PROCEDIMENTOS_INDIV PI WHERE PI.SINCRONIZADO = 'N'";
    List<Map<String, dynamic>> maps = await db.rawQuery(query);
    // print(list);
    return List.generate(maps.length, (i) {
      return maps[i];
    });
  }
  Future procedimentosIndivFicha()async{
    Database db = await DbSqliteLoad().intialDB();
    var query = "SELECT PIF.* FROM PROCEDIMENTOS_INDIV_FICHA PIF INNER JOIN PROCEDIMENTOS_INDIV PI ON PI.ID_PROCEDIMENTOS_INDIV = PIF.ID_PROCEDIMENTOS_INDIV WHERE PI.SINCRONIZADO = 'N'";
    List<Map<String, dynamic>> maps = await db.rawQuery(query);
    // print(list);
    return List.generate(maps.length, (i) {
      return maps[i];
    });
  }
  Future procedimentosIndivSigtap()async{
    Database db = await DbSqliteLoad().intialDB();
    var query = "SELECT PIS.* FROM PROCEDIMENTOS_INDIV_SIGTAP PIS INNER JOIN PROCEDIMENTOS_INDIV PI ON PI.ID_PROCEDIMENTOS_INDIV = PIS.ID_PROCEDIMENTOS_INDIV WHERE PI.SINCRONIZADO = 'N'";
    List<Map<String, dynamic>> maps = await db.rawQuery(query);
    // print(list);
    return List.generate(maps.length, (i) {
      return maps[i];
    });
  }
  Future visitasDomiciliares()async{
    Database db = await DbSqliteLoad().intialDB();
    var query = "SELECT VD.* FROM VISITAS_DOMICILIARES VD WHERE VD.SINCRONIZADO = 'N'";
    List<Map<String, dynamic>> maps = await db.rawQuery(query);
    // print(list);
    return List.generate(maps.length, (i) {
      return maps[i];
    });
  }
  Future visitasDomiciliaresCompl()async{
    Database db = await DbSqliteLoad().intialDB();
    var query = "SELECT VDC.* FROM VISITAS_DOMICILIARES_COMPL VDC INNER JOIN VISITAS_DOMICILIARES VD ON VD.ID_VISITA_DOMICILIAR = VDC.ID_VISITA_DOMICILIAR WHERE VD.SINCRONIZADO = 'N'";
    List<Map<String, dynamic>> maps = await db.rawQuery(query);
    // print(list);
    return List.generate(maps.length, (i) {
      return maps[i];
    });
  }
  Future visitasDomiciliaresProc()async{
    Database db = await DbSqliteLoad().intialDB();
    var query = "SELECT VDP.* FROM VISITAS_DOMICILIARES_PROC VDP INNER JOIN VISITAS_DOMICILIARES VD ON VD.ID_VISITA_DOMICILIAR = VDP.ID_VISITA_DOMICILIAR WHERE VD.SINCRONIZADO = 'N'";
    List<Map<String, dynamic>> maps = await db.rawQuery(query);
    // print(list);
    return List.generate(maps.length, (i) {
      return maps[i];
    });
  }
  Future fichasComplementares()async{
    Database db = await DbSqliteLoad().intialDB();
    var query = "SELECT FC.* FROM FICHAS_COMPLEMENTARES FC WHERE FC.SINCRONIZADO = 'N'";
    List<Map<String, dynamic>> maps = await db.rawQuery(query);
    // print(list);
    return List.generate(maps.length, (i) {
      return maps[i];
    });
  }
  Future atendimentosDomiciliares()async{
    Database db = await DbSqliteLoad().intialDB();
    var query = "SELECT AD.* FROM ATENDIMENTOS_DOMICILIARES AD WHERE AD.SINCRONIZADO = 'N'";
    List<Map<String, dynamic>> maps = await db.rawQuery(query);
    // print(list);
    return List.generate(maps.length, (i) {
      return maps[i];
    });
  }
  Future atendDomCompl()async{
    Database db = await DbSqliteLoad().intialDB();
    var query = "SELECT ADC.* FROM ATENDS_DOM_COMPL ADC INNER JOIN ATENDIMENTOS_DOMICILIARES AD ON AD.ID_ATENDIMENTO_DOMICILIAR = ADC.ID_ATENDIMENTO_DOMICILIAR WHERE AD.SINCRONIZADO = 'N'";
    List<Map<String, dynamic>> maps = await db.rawQuery(query);
    // print(list);
    return List.generate(maps.length, (i) {
      return maps[i];
    });
  }
  Future atendDomProcsFicha()async{
    Database db = await DbSqliteLoad().intialDB();
    var query = "SELECT ADP.* FROM ATENDS_DOM_PROC ADP INNER JOIN ATENDIMENTOS_DOMICILIARES AD ON AD.ID_ATENDIMENTO_DOMICILIAR = ADP.ID_ATENDIMENTO_DOMICILIAR WHERE AD.SINCRONIZADO = 'N'";
    List<Map<String, dynamic>> maps = await db.rawQuery(query);
    // print(list);
    return List.generate(maps.length, (i) {
      return maps[i];
    });
  }
  Future atendDomProcsSigtap()async{
    Database db = await DbSqliteLoad().intialDB();
    var query = "SELECT ADS.* FROM ATENDS_DOM_SIGTAP ADS INNER JOIN ATENDIMENTOS_DOMICILIARES AD ON AD.ID_ATENDIMENTO_DOMICILIAR = ADS.ID_ATENDIMENTO_DOMICILIAR WHERE AD.SINCRONIZADO = 'N'";
    List<Map<String, dynamic>> maps = await db.rawQuery(query);
    // print(list);
    return List.generate(maps.length, (i) {
      return maps[i];
    });
  }
  Future atendsOdontologicos()async{
    Database db = await DbSqliteLoad().intialDB();
    var query = "SELECT AO.* FROM ATENDS_ODONTOLOGICOS AO WHERE AO.SINCRONIZADO = 'N'";
    List<Map<String, dynamic>> maps = await db.rawQuery(query);
    // print(list);
    return List.generate(maps.length, (i) {
      return maps[i];
    });
  }
  Future atendsOdontoProf()async{
    Database db = await DbSqliteLoad().intialDB();
    var query = "SELECT AOC.* FROM ATENDS_ODONTO_PROF AOC INNER JOIN ATENDS_ODONTOLOGICOS AO ON AO.ID_ATENDIMENTO_ODONTOLOGICO = AOC.ID_ATENDIMENTO_ODONTOLOGICO WHERE AO.SINCRONIZADO = 'N'";
    List<Map<String, dynamic>> maps = await db.rawQuery(query);
    // print(list);
    return List.generate(maps.length, (i) {
      return maps[i];
    });
  }
  Future atendsOdontoProc()async{
    Database db = await DbSqliteLoad().intialDB();
    var query = "SELECT AOC.* FROM ATENDS_ODONTO_PROC AOC INNER JOIN ATENDS_ODONTOLOGICOS AO ON AO.ID_ATENDIMENTO_ODONTOLOGICO = AOC.ID_ATENDIMENTO_ODONTOLOGICO WHERE AO.SINCRONIZADO = 'N'";
    List<Map<String, dynamic>> maps = await db.rawQuery(query);
    // print(list);
    return List.generate(maps.length, (i) {
      return maps[i];
    });
  }
  Future atendsOdontoSigtap()async{
    Database db = await DbSqliteLoad().intialDB();
    var query = "SELECT AOC.* FROM ATENDS_ODONTO_SIGTAP AOC INNER JOIN ATENDS_ODONTOLOGICOS AO ON AO.ID_ATENDIMENTO_ODONTOLOGICO = AOC.ID_ATENDIMENTO_ODONTOLOGICO WHERE AO.SINCRONIZADO = 'N'";
    List<Map<String, dynamic>> maps = await db.rawQuery(query);
    // print(list);
    return List.generate(maps.length, (i) {
      return maps[i];
    });
  }
  Future atendsOdontoCompl()async{
    Database db = await DbSqliteLoad().intialDB();
    var query = "SELECT AOC.* FROM ATENDS_ODONTO_COMPL AOC INNER JOIN ATENDS_ODONTOLOGICOS AO ON AO.ID_ATENDIMENTO_ODONTOLOGICO = AOC.ID_ATENDIMENTO_ODONTOLOGICO WHERE AO.SINCRONIZADO = 'N'";
    List<Map<String, dynamic>> maps = await db.rawQuery(query);
    // print(list);
    return List.generate(maps.length, (i) {
      return maps[i];
    });
  }
  Future marcadoresConQuest()async{
    Database db = await DbSqliteLoad().intialDB();
    var query = "SELECT MCQ.* FROM MARCADORES_CON_QUEST MCQ INNER JOIN MARCADORES_CONSUMO MC ON MC.ID_MARCADOR_CONSUMO = MCQ.ID_MARCADOR_CONSUMO WHERE MC.SINCRONIZADO = 'N'";
    List<Map<String, dynamic>> maps = await db.rawQuery(query);
    // print(list);
    return List.generate(maps.length, (i) {
      return maps[i];
    });
  }
  Future marcadoresConsumo()async{
    Database db = await DbSqliteLoad().intialDB();
    var query = "SELECT MC.* FROM MARCADORES_CONSUMO MC WHERE MC.SINCRONIZADO = 'N'";
    List<Map<String, dynamic>> maps = await db.rawQuery(query);
    // print(list);
    return List.generate(maps.length, (i) {
      return maps[i];
    });
  }
  Future avalElegCondAval()async{
    Database db = await DbSqliteLoad().intialDB();
    var query = "SELECT AECA.* FROM AVAL_ELEG_COND_AVAL AECA INNER JOIN AVALIACOES_ELEGIBILIDADE AA ON AA.ID_AVALIACAO_ELEGIBILIDADE = AECA.ID_AVALIACAO_ELEGIBILIDADE WHERE AA.SINCRONIZADO = 'N'";
    List<Map<String, dynamic>> maps = await db.rawQuery(query);
    // print(list);
    return List.generate(maps.length, (i) {
      return maps[i];
    });
  }
  Future avalElegConclIne()async{
    Database db = await DbSqliteLoad().intialDB();
    var query = "SELECT AECI.* FROM AVAL_ELEG_CONCL_INE AECI INNER JOIN AVALIACOES_ELEGIBILIDADE AA ON AA.ID_AVALIACAO_ELEGIBILIDADE = AECI.ID_AVALIACAO_ELEGIBILIDADE WHERE AA.SINCRONIZADO = 'N'";
    List<Map<String, dynamic>> maps = await db.rawQuery(query);
    // print(list);
    return List.generate(maps.length, (i) {
      return maps[i];
    });
  }
  Future avaliacoesElegibilidade()async{
    Database db = await DbSqliteLoad().intialDB();
    var query = "SELECT AA.* FROM AVALIACOES_ELEGIBILIDADE AA WHERE AA.SINCRONIZADO = 'N'";
    List<Map<String, dynamic>> maps = await db.rawQuery(query);
    // print(list);
    return List.generate(maps.length, (i) {
      return maps[i];
    });
  }
  ///FIM SINCRONIZAÇÃO
  ///UPDATE APOS SINCRONIZACAO
  Future updateCidadao()async{
    Map<String, dynamic> row = {
      'SINCRONIZADO'        : 'S',
    };
    Database db = await DbSqliteLoad().intialDB();
    await db.update('CIDADAOS', row,where: "SINCRONIZADO = 'N'");
  }
}