Newer
Older
/*
Copyright (C) 2016 Centro de Computacao Cientifica e Software Livre
Departamento de Informatica - Universidade Federal do Parana - C3SL/UFPR
This file is part of simcaq-node.
simcaq-node is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
simcaq-node is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with simcaq-node. If not, see <https://www.gnu.org/licenses/>.
*/
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
const express = require('express');
const infrastructureApp = express.Router();
const libs = `${process.cwd()}/libs`;
const log = require(`${libs}/log`)(module);
const squel = require('squel');
const query = require(`${libs}/middlewares/query`).query;
const multiQuery = require(`${libs}/middlewares/multiQuery`);
const response = require(`${libs}/middlewares/response`);
const id2str = require(`${libs}/middlewares/id2str`);
const ReqQueryFields = require(`${libs}/middlewares/reqQueryFields`);
const config = require(`${libs}/config`);
const cache = require('apicache').options({ debug: config.debug, statusCodes: {include: [200]} }).middleware;
let rqf = new ReqQueryFields();
infrastructureApp.use(cache('15 day'));
infrastructureApp.get('/year_range', (req, res, next) => {
req.sql.from('escola')
.field('MIN(escola.ano_censo)', 'start_year')
.field('MAX(escola.ano_censo)', 'end_year');
next();
}, query, response('range'));
infrastructureApp.get('/years', (req, res, next) => {
req.sql.from('escola')
next();
}, query, response('years'));
infrastructureApp.get('/source', (req, res, next) => {
req.sql.from('fonte')
.field('fonte', 'source')
.where('tabela = \'escola\'');
next();
}, query, response('source'));
infrastructureApp.get('/location', (req, res, next) => {
req.result = [
{id: 1, name: 'Urbana'},
{id: 2, name: 'Rural'}
];
next();
}, response('location'));
infrastructureApp.get('/rural_location', (req, res, next) => {
req.result = [
{id: 1, name: "Urbana"},
{id: 2, name: "Rural"},
{id: 3, name: "Rural - Área de assentamento"},
{id: 4, name: "Rural - Terra indígena"},
{id: 5, name: "Rural - Área remanescente de quilombos"},
{id: 6, name: "Rural - Unidade de uso sustentável"}
];
next();
}, response('rural_location'));
infrastructureApp.get('/adm_dependency', (req, res, next) => {
req.result = [];
for(let i = 1; i <= 4; ++i) {
req.result.push({
id: i,
name: id2str.admDependency(i)
});
};
infrastructureApp.get('/adm_dependency_detailed', (req, res, next) => {
req.result = [];
for(let i = 1; i <= 6; ++i) {
req.result.push({
id: i,
name: id2str.admDependencyPriv(i)
});
};
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
rqf.addField({
name: 'filter',
field: false,
where: true
}).addField({
name: 'dims',
field: true,
where: false
}).addValueToField({
name: 'city',
table: 'municipio',
tableField: ['nome', 'id'],
resultField: ['city_name', 'city_id'],
where: {
relation: '=',
type: 'integer',
field: 'municipio_id',
table: 'escola'
},
join: {
primary: 'id',
foreign: 'municipio_id',
foreignTable: 'escola'
}
}, 'dims').addValueToField({
name: 'city',
table: 'municipio',
tableField: 'id',
resultField: 'city_id',
where: {
relation: '=',
type: 'integer',
field: 'municipio_id',
table: 'escola'
},
join: {
primary: 'id',
foreign: 'municipio_id',
foreignTable: 'escola'
}
}, 'filter').addValueToField({
name: 'state',
table: 'estado',
tableField: ['nome', 'id'],
resultField: ['state_name', 'state_id'],
where: {
relation: '=',
type: 'integer',
field: 'estado_id',
table: 'escola'
},
join: {
primary: 'id',
foreign: 'estado_id',
foreignTable: 'escola'
}
}, 'dims').addValueToField({
name: 'state',
table: 'estado',
tableField: 'id',
resultField: 'state_id',
where: {
relation: '=',
type: 'integer',
field: 'estado_id',
table: 'escola'
},
join: {
primary: 'id',
foreign: 'estado_id',
foreignTable: 'escola'
}
}, 'filter').addValue({
name: 'region',
table: 'regiao',
tableField: 'nome',
resultField: 'region_name',
where: {
relation: '=',
type: 'integer',
field: 'id'
},
join: {
primary: 'id',
foreign: 'regiao_id',
foreignTable: 'escola'
}
}).addValue({
name: 'location',
table: 'escola',
resultField: 'location_id',
where: {
relation: '=',
type: 'integer',
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
}
}).addValue({
name: 'rural_location',
table: 'escola',
tableField: 'localidade_area_rural',
resultField: 'rural_location_id',
where: {
relation: '=',
type: 'integer',
field: 'localidade_area_rural'
}
}).addValue({
name: 'adm_dependency',
table: 'escola',
tableField: 'dependencia_adm_id',
resultField: 'adm_dependency_id',
where: {
relation: '=',
type: 'integer',
field: 'dependencia_adm_id'
}
}).addValue({
name: 'adm_dependency_detailed',
table: 'escola',
tableField: 'dependencia_adm_priv',
resultField: 'adm_dependency_detailed_id',
where: {
relation: '=',
type: 'integer',
field: 'dependencia_adm_priv'
}
}).addValue({
name: 'min_year',
table: 'escola',
tableField: 'ano_censo',
resultField: 'year',
where: {
relation: '>=',
type: 'integer',
field: 'ano_censo'
}
}).addValue({
name: 'max_year',
table: 'escola',
tableField: 'ano_censo',
resultField: 'year',
where: {
relation: '<=',
type: 'integer',
field: 'ano_censo'
}
});
function matchQueries(queryTotal, queryPartial, queryNeeded, zeroPercentage=false) {
let match = [];
queryTotal.forEach((result) => {
let newObj = {};
let keys = Object.keys(result);
keys.forEach((key) => {
newObj[key] = result[key];
});
let index = keys.indexOf('total');
if(index > -1) keys.splice(index, 1);
let partialMatch = null;
let needMatch = null;
for(let i = 0; i < queryPartial.length; ++i) {
let partial = queryPartial[i];
let foundMatch = true;
for(let j = 0; j < keys.length; ++j) {
let key = keys[j];
if(partial[key] !== result[key]) {
foundMatch = false;
break;
}
}
if(foundMatch) {
if(queryPartial.length == 0) {
partialMatch = JSON.parse(JSON.stringify(result));
partialMatch.total = 0;
}
for(let i = 0; i < queryNeeded.length; ++i) {
let needed = queryNeeded[i];
let foundMatch = true;
for(let j = 0; j < keys.length; ++j) {
let key = keys[j];
if(needed[key] !== result[key]) {
foundMatch = false;
break;
}
}
if(foundMatch) {
needMatch = needed;
break;
}
}
if(queryNeeded.length == 0) {
needMatch = JSON.parse(JSON.stringify(result));
needMatch.total = 0;
}
newObj.percentage = ( ( partialMatch ? partialMatch.total : 0) / result.total) * 100;
if(zeroPercentage) newObj.percentage = 0;
newObj.partial = ( partialMatch ? partialMatch.total : 0);
newObj.total = result.total;
newObj.need_adaptation = needMatch ? needMatch.total : 0;
match.push(newObj);
});
return match;
}
infrastructureApp.get('/', rqf.parse(), rqf.build(), (req, res, next) => {
req.querySet = [];
req.queryIndex = {};
// Local de funcionamento
let allSchools = req.sql.clone();
allSchools.from('escola').field('COUNT(escola.id)', 'total')
.field("'Brasil'", 'name')
.field('escola.ano_censo', 'year')
.group('escola.ano_censo')
.where('escola.situacao_de_funcionamento = 1')
.where('escola.ensino_regular = 1 OR escola.ensino_eja = 1 OR escola.educacao_profissional = 1')
.where('escola.local_func_predio_escolar = 1')
.where('escola.dependencia_adm_id <= 3')
req.queryIndex.allSchools = req.querySet.push(allSchools) - 1;
let allUrbanSchools = allSchools.clone();
allUrbanSchools.where('escola.localizacao_id = 1');
req.queryIndex.allUrbanSchools = req.querySet.push(allUrbanSchools) - 1;
let allCountrySchools = allSchools.clone();
allCountrySchools.where('escola.localizacao_id = 2');
req.queryIndex.allCountrySchools = req.querySet.push(allCountrySchools) - 1;
let allSchoolsNotSchoolBuilding = req.sql.clone();
allSchoolsNotSchoolBuilding.from('escola').field('COUNT(escola.id)', 'total')
.field("'Brasil'", 'name')
.field('escola.ano_censo', 'year')
.group('escola.ano_censo')
.where('escola.situacao_de_funcionamento = 1')
.where('escola.ensino_regular = 1 OR escola.ensino_eja = 1 OR escola.educacao_profissional = 1')
.where('escola.local_func_predio_escolar = 0')
.where('escola.dependencia_adm_id <= 3')
.order('escola.ano_censo');
req.queryIndex.allSchoolsNotSchoolBuilding = req.querySet.push(allSchoolsNotSchoolBuilding) - 1;
// Se (situacao_de_funcionamento=1) & (dependencia_adm_id<=3) & (CEBES027P1=1) &
// (ensino_regular=1 | ensino_eja=1 | educacao_profissional=1) &
// (reg_fund_ai_t1=1 | reg_fund_af_t1=1 | reg_medio_medio_t1=1 | ensino_eja_fund= 1 |
// ensino_eja_medio= 1 | ensino_eja_prof= 1 | esp_eja_fund=1 | esp_eja_medio=1 | ensino_esp_exclusiva_eja_prof=1) & (localizacao_id=1) então conta id
let allLibraries = allUrbanSchools.clone();
allLibraries.where('reg_fund_ai_t1=1 OR reg_fund_af_t1=1 OR reg_medio_medio_t1=1 OR ensino_eja_fund= 1 OR ensino_eja_medio= 1 OR ensino_eja_prof= 1 OR esp_eja_fund=1 OR esp_eja_medio=1 OR ensino_esp_exclusiva_eja_prof=1');
req.queryIndex.allLibraries = req.querySet.push(allLibraries) - 1;
let haveLibraries = allLibraries.clone();
haveLibraries.where('escola.biblioteca = 1');
req.queryIndex.haveLibraries = req.querySet.push(haveLibraries) - 1;
needLibraries.where('escola.biblioteca = 0');
req.queryIndex.needLibraries = req.querySet.push(needLibraries) - 1;
// Se (situacao_de_funcionamento=1) & (dependencia_adm_id<=3) & (CEBES027P1=1) & (ensino_regular=1 | ensino_eja=1 | educacao_profissional=1) &
// (reg_fund_ai_t1=1 | reg_fund_af_t1=1 | reg_medio_medio_t1=1 | ensino_eja_fund= 1 | ensino_eja_medio= 1 |
// ensino_eja_prof= 1 | esp_eja_fund=1 | esp_eja_medio=1 | ensino_esp_exclusiva_eja_prof=1) & (localizacao_id=2) então conta id
let allLibrariesReadingRoom = allCountrySchools.clone();
allLibrariesReadingRoom.where('reg_fund_ai_t1=1 OR reg_fund_af_t1=1 OR reg_medio_medio_t1=1 OR ensino_eja_fund=1 OR ensino_eja_medio=1 OR ensino_eja_prof=1 OR esp_eja_fund=1 OR esp_eja_medio=1 OR ensino_esp_exclusiva_eja_prof=1');
req.queryIndex.allLibrariesReadingRoom = req.querySet.push(allLibrariesReadingRoom) - 1;
let haveLibrariesReadingRoom = allLibrariesReadingRoom.clone();
haveLibrariesReadingRoom.where('escola.biblioteca_sala_leitura = true');
req.queryIndex.haveLibrariesReadingRoom = req.querySet.push(haveLibrariesReadingRoom) - 1;
let needLibrariesReadingRoom = allLibrariesReadingRoom.clone();
needLibrariesReadingRoom.where('escola.biblioteca_sala_leitura = false');
req.queryIndex.needLibrariesReadingRoom = req.querySet.push(needLibrariesReadingRoom) - 1;
// Se (situacao_de_funcionamento=1) & (dependencia_adm_id<=3) & (CEBES027P1=1) &
// (ensino_regular=1 | ensino_eja=1 | educacao_profissional=1) &
// ( reg_fund_ai_t1=1 | reg_fund_af_t1=1 | reg_medio_medio_t1=1 | ensino_eja_fund= 1 | ensino_eja_medio= 1 | ensino_eja_prof= 1 | esp_eja_fund=1 |
// esp_eja_medio=1 | ensino_esp_exclusiva_eja_prof=1) então conta id
allInfLab.where('reg_fund_ai_t1=1 OR reg_fund_af_t1=1 OR reg_medio_medio_t1=1 OR ensino_eja_fund=1 OR ensino_eja_medio=1 OR ensino_eja_prof=1 OR esp_eja_fund=1 OR esp_eja_medio=1 OR ensino_esp_exclusiva_eja_prof=1');
req.queryIndex.allInfLab = req.querySet.push(allInfLab) - 1;
let haveInfLab = allInfLab.clone();
haveInfLab.where('escola.lab_informatica = 1');
req.queryIndex.haveInfLab = req.querySet.push(haveInfLab) - 1;
let needInfLab = allInfLab.clone();
needInfLab.where('escola.lab_informatica = 0');
req.queryIndex.needInfLab = req.querySet.push(needInfLab) - 1;
allScienceLab.where('reg_fund_af_t1=1 OR reg_medio_medio_t1=1 OR ensino_eja_fund=1 OR ensino_eja_medio=1 OR ensino_eja_prof=1 OR esp_eja_fund=1 OR esp_eja_medio=1 OR ensino_esp_exclusiva_eja_prof=1');
req.queryIndex.allScienceLab = req.querySet.push(allScienceLab) - 1;
let haveScienceLab = allScienceLab.clone();
haveScienceLab.where('escola.lab_ciencias = true');
req.queryIndex.haveScienceLab = req.querySet.push(haveScienceLab) - 1;
needScienceLab.where('escola.lab_ciencias = false');
req.queryIndex.needScienceLab = req.querySet.push(needScienceLab) - 1;
// Se (situacao_de_funcionamento=1) and (ensino_regular=1 OR ensino_eja=1 OR educacao_profissional=1) and
// (local_func_predio_escolar=1) and (dependencia_adm_id<=3) and (reg_infantil_creche_t1=1 or reg_infantil_preescola_t1=1 or reg_fund_ai_t1=1) então conta id
allKidsPark.where('reg_infantil_creche_t1=1 OR reg_infantil_preescola_t1=1 OR reg_fund_ai_t1=1');
req.queryIndex.allKidsPark = req.querySet.push(allKidsPark) - 1;
let haveKidsPark = allKidsPark.clone();
haveKidsPark.where('escola.parque_infantil = 1');
req.queryIndex.haveKidsPark = req.querySet.push(haveKidsPark) - 1;
let needKidsPark = allKidsPark.clone();
needKidsPark.where('escola.parque_infantil = 0');
req.queryIndex.needKidsPark = req.querySet.push(needKidsPark) - 1;
// Berçário
let allCribs = allSchools.clone();
req.queryIndex.allCribs = req.querySet.push(allCribs) - 1;
let haveCribs = allCribs.clone();
haveCribs.where('escola.bercario = 1');
req.queryIndex.haveCribs = req.querySet.push(haveCribs) - 1;
let needCribs = allCribs.clone();
needCribs.where('escola.bercario = 0');
req.queryIndex.needCribs = req.querySet.push(needCribs) - 1;
// Quadra Coberta
let allSportsCourt = allSchools.clone();
allSportsCourt.where('reg_fund_ai_t1=1 or reg_fund_af_t1=1 or reg_medio_medio_t1=1 or ensino_eja_fund= 1 or ensino_eja_medio= 1 or ensino_eja_prof= 1 or esp_eja_fund=1 or esp_eja_medio=1 or ensino_esp_exclusiva_eja_prof=1');
req.queryIndex.allSportsCourt = req.querySet.push(allSportsCourt) - 1;
let haveSportsCourt = allSportsCourt.clone();
haveSportsCourt.where('escola.quadra_esportes = 1');
req.queryIndex.haveSportsCourt = req.querySet.push(haveSportsCourt) - 1;
let needSportsCourt = allSportsCourt.clone();
needSportsCourt.where('escola.quadra_esportes = 0');
req.queryIndex.needSportsCourt = req.querySet.push(needSportsCourt) - 1;
// Cobertura de quadra esportiva
// Se (situacao_de_funcionamento=1) and (ensino_regular=1 OR ensino_eja=1 OR educacao_profissional=1) and (local_func_predio_escolar=1) and
// (dependencia_adm_id<=3) and (reg_fund_ai_t1=1 or reg_fund_af_t1=1 or reg_medio_medio_t1=1 or ensino_eja_fund= 1 or ensino_eja_medio= 1 or
// ensino_eja_prof= 1 or esp_eja_fund=1 or esp_eja_medio=1 or ensino_esp_exclusiva_eja_prof=1) and (quadra_esportes_descoberta=1) então conta id
let allSportsCourtCoverage = allSportsCourt.clone();
allSportsCourtCoverage.where('escola.quadra_esportes_descoberta = 1');
req.queryIndex.allSportsCourtCoverage = req.querySet.push(allSportsCourtCoverage) -1;
req.queryIndex.haveSportsCourtCoverage = req.queryIndex.allSportsCourtCoverage; // It must be []
req.queryIndex.needSportsCourtCoverage = req.queryIndex.allSportsCourtCoverage;
// Pátio
req.queryIndex.allCourtyard = req.queryIndex.allSchools;
haveCourtyard.where('escola.patio = 2 OR escola.patio = 1');
req.queryIndex.haveCourtyard = req.querySet.push(haveCourtyard) - 1;
req.queryIndex.needCourtyard = req.querySet.push(needCourtyard) - 1;
// Cobertura do Pátio
let allCourtyardCoverage = allSchools.clone();
allCourtyardCoverage.where('escola.patio = 1');
req.queryIndex.allCourtyardCoverage = req.querySet.push(allCourtyardCoverage) - 1;
req.queryIndex.haveCourtyardCoverage = req.queryIndex.allCourtyardCoverage; // It must be []
req.queryIndex.needCourtyardCoverage = req.queryIndex.allCourtyardCoverage;
req.queryIndex.allDirectorRoom = req.queryIndex.allUrbanSchools;
let haveDirectorRoom = allUrbanSchools.clone();
haveDirectorRoom.where('escola.sala_diretoria = 1');
req.queryIndex.haveDirectorRoom = req.querySet.push(haveDirectorRoom) - 1;
let needDirectorRoom = allUrbanSchools.clone();
needDirectorRoom.where('escola.sala_diretoria = 0');
req.queryIndex.needDirectorRoom = req.querySet.push(needDirectorRoom) - 1;
req.queryIndex.allSecretary = req.queryIndex.allSchools;
haveSecretary.where('escola.secretaria = 1');
req.queryIndex.haveSecretary = req.querySet.push(haveSecretary) - 1;
let needSecretary = allSchools.clone();
needSecretary.where('escola.secretaria = 0');
req.queryIndex.needSecretary = req.querySet.push(needSecretary) - 1;
req.queryIndex.allTeacherRoom = req.queryIndex.allSchools;
haveTeacherRoom.where('escola.sala_professor = 1');
req.queryIndex.haveTeacherRoom = req.querySet.push(haveTeacherRoom) - 1;
let needTeacherRoom = allSchools.clone();
needTeacherRoom.where('escola.sala_professor = 0');
req.queryIndex.needTeacherRoom = req.querySet.push(needTeacherRoom) - 1;
req.queryIndex.allKitchen = req.queryIndex.allSchools;
haveKitchen.where('escola.cozinha = 1');
req.queryIndex.haveKitchen = req.querySet.push(haveKitchen) - 1;
let needKitchen = allSchools.clone();
needKitchen.where('escola.cozinha = 0');
req.queryIndex.needKitchen = req.querySet.push(needKitchen) - 1;
req.queryIndex.allStoreroom = req.queryIndex.allSchools;
haveStoreroom.where('escola.despensa = 1');
req.queryIndex.haveStoreroom = req.querySet.push(haveStoreroom) - 1;
let needStoreroom = allSchools.clone();
needStoreroom.where('escola.despensa = 0');
req.queryIndex.needStoreroom = req.querySet.push(needStoreroom) - 1;
req.queryIndex.allWarehouse = req.queryIndex.allSchools;
haveWarehouse.where('escola.almoxarifado = 1');
req.queryIndex.haveWarehouse = req.querySet.push(haveWarehouse) - 1;
req.queryIndex.needWarehouse = req.querySet.push(needWarehouse) - 1;
req.queryIndex.allInternet = req.queryIndex.allCountrySchools;
let haveInternet = allCountrySchools.clone();
haveInternet.where('escola.internet = 1');
req.queryIndex.haveInternet = req.querySet.push(haveInternet) - 1;
let needInternet = allCountrySchools.clone();
needInternet.where('escola.internet = 0');
req.queryIndex.needInternet = req.querySet.push(needInternet) - 1;
// Se (situacao_de_funcionamento=1) and (ensino_regular=1 OR ensino_eja=1 OR educacao_profissional=1) and (local_func_predio_escolar=1) and
// (dependencia_adm_id<=3) and (localizacao_id=2) então conta id
req.queryIndex.allBroadbandInternet = req.queryIndex.allUrbanSchools;
let haveBroadbandInternet = allUrbanSchools.clone();
haveBroadbandInternet.where('escola.internet_banda_larga = 1');
req.queryIndex.haveBroadbandInternet = req.querySet.push(haveBroadbandInternet) - 1;
let needBroadbandInternet = allUrbanSchools.clone();
needBroadbandInternet.where('escola.internet_banda_larga = 0');
req.queryIndex.needBroadbandInternet = req.querySet.push(needBroadbandInternet) - 1;
// Sanitário dentro do prédio
req.queryIndex.allInsideBathroom = req.queryIndex.allSchools;
let haveInsideBathroom = allSchools.clone();
haveInsideBathroom.where('escola.sanitario_dentro_predio = 1');
req.queryIndex.haveInsideBathroom = req.querySet.push(haveInsideBathroom) - 1;
let needInsideBathroom = allSchools.clone();
needInsideBathroom.where('escola.sanitario_dentro_predio = 0');
req.queryIndex.needInsideBathroom = req.querySet.push(needInsideBathroom) - 1;
// Banheiro adequado para educação infantil dentro do prédio
// Se (situacao_de_funcionamento=1) and (ensino_regular=1 OR ensino_eja=1 OR educacao_profissional=1) and (local_func_predio_escolar=1) and
// (dependencia_adm_id<=3) and (reg_infantil_creche_t1=1 or reg_infantil_preescola_t1=1 or reg_fund_ai_t1=1) então conta id
let allInsideKidsBathroom = allSchools.clone();
allInsideKidsBathroom.where('reg_infantil_creche_t1=1 OR reg_infantil_preescola_t1=1 OR reg_fund_ai_t1=1');
req.queryIndex.allInsideKidsBathroom = req.querySet.push(allInsideKidsBathroom) - 1;
let haveInsideKidsBathroom = allInsideKidsBathroom.clone();
haveInsideKidsBathroom.where('escola.sanitario_ei = 1');
req.queryIndex.haveInsideKidsBathroom = req.querySet.push(haveInsideKidsBathroom) - 1;
let needInsideKidsBathroom = allInsideKidsBathroom.clone();
needInsideKidsBathroom.where('escola.sanitario_ei = 0');
req.queryIndex.needInsideKidsBathroom = req.querySet.push(needInsideKidsBathroom) - 1;
// Fornecimento de energia
req.queryIndex.allEletricPower = req.queryIndex.allSchools;
let haveEletricPower = allSchools.clone();
haveEletricPower.where('escola.fornecimento_energia = 1');
req.queryIndex.haveEletricPower = req.querySet.push(haveEletricPower) - 1;
let needEletricPower = allSchools.clone();
needEletricPower.where('escola.fornecimento_energia = 0');
req.queryIndex.needEletricPower = req.querySet.push(needEletricPower) - 1;
req.queryIndex.allWaterSupply = req.queryIndex.allSchools;
let haveWaterSupply = allSchools.clone();
haveWaterSupply.where('escola.fornecimento_agua = 1');
req.queryIndex.haveWaterSupply = req.querySet.push(haveWaterSupply) - 1;
let needWaterSupply = allSchools.clone();
needWaterSupply.where('escola.fornecimento_agua = 0');
req.queryIndex.needWaterSupply = req.querySet.push(needWaterSupply) - 1;
req.queryIndex.allFilteredWater = req.queryIndex.allSchools;
let haveFilteredWater = allSchools.clone();
haveFilteredWater.where('escola.agua_filtrada = 1');
req.queryIndex.haveFilteredWater = req.querySet.push(haveFilteredWater) - 1;
let needFilteredWater = allSchools.clone();
needFilteredWater.where('escola.agua_filtrada = 0');
req.queryIndex.needFilteredWater = req.querySet.push(needFilteredWater) - 1;
req.queryIndex.allSewage = req.queryIndex.allSchools;
haveSewage.where('escola.esgoto_sanitario = 1');
req.queryIndex.haveSewage = req.querySet.push(haveSewage) - 1;
let needSewage = allSchools.clone();
needSewage.where('escola.esgoto_sanitario = 0');
req.queryIndex.needSewage = req.querySet.push(needSewage) - 1;
// Dependências adaptada para pessoas com deficiências
req.queryIndex.allAdaptedBuilding = req.queryIndex.allSchools;
let haveAdaptedBuilding = allSchools.clone();
haveAdaptedBuilding.where('escola.dependencias_pne = 1');
req.queryIndex.haveAdaptedBuilding = req.querySet.push(haveAdaptedBuilding) - 1;
let needAdaptedBuilding = allSchools.clone();
needAdaptedBuilding.where('escola.dependencias_pne = 0');
req.queryIndex.needAdaptedBuilding = req.querySet.push(needAdaptedBuilding) - 1;
// Banheiros adaptados para pessoas com deficiências
req.queryIndex.allSpecialBathroom = req.queryIndex.allSchools;
let haveSpecialBathroom = allSchools.clone();
haveSpecialBathroom.where('escola.sanitario_pne = 1');
req.queryIndex.haveSpecialBathroom = req.querySet.push(haveSpecialBathroom) - 1;
let needSpecialBathroom = allSchools.clone();
req.queryIndex.needSpecialBathroom = req.querySet.push(needSpecialBathroom) - 1;
next();
}, multiQuery, (req, res, next) => {
let schools_in_school_buildings = req.result[req.queryIndex.allSchools];
let urban_schools_in_school_buildings = req.result[req.queryIndex.allUrbanSchools];
let country_schools_in_school_buildings = req.result[req.queryIndex.allCountrySchools];
let schools_not_in_school_buildings = req.result[req.queryIndex.allSchoolsNotSchoolBuilding];
let libraries = matchQueries(req.result[req.queryIndex.allLibraries], req.result[req.queryIndex.haveLibraries], req.result[req.queryIndex.needLibraries]);
let libraries_reading_room = matchQueries(req.result[req.queryIndex.allLibrariesReadingRoom], req.result[req.queryIndex.haveLibrariesReadingRoom], req.result[req.queryIndex.needLibrariesReadingRoom]);
let computer_lab = matchQueries(req.result[req.queryIndex.allInfLab], req.result[req.queryIndex.haveInfLab], req.result[req.queryIndex.needInfLab]);
let science_lab = matchQueries(req.result[req.queryIndex.allScienceLab], req.result[req.queryIndex.haveScienceLab], req.result[req.queryIndex.needScienceLab]);
let kids_park = matchQueries(req.result[req.queryIndex.allKidsPark], req.result[req.queryIndex.haveKidsPark], req.result[req.queryIndex.needKidsPark]);
let nursery = matchQueries(req.result[req.queryIndex.allCribs], req.result[req.queryIndex.haveCribs], req.result[req.queryIndex.needCribs]);
let sports_court = matchQueries(req.result[req.queryIndex.allSportsCourt], req.result[req.queryIndex.haveSportsCourt], req.result[req.queryIndex.needSportsCourt]);
let sports_court_coverage = matchQueries(req.result[req.queryIndex.allSportsCourtCoverage], [], req.result[req.queryIndex.needSportsCourtCoverage], true); // have = []
let courtyard = matchQueries(req.result[req.queryIndex.allCourtyard], req.result[req.queryIndex.haveCourtyard], req.result[req.queryIndex.needCourtyard]);
let courtyard_coverage = matchQueries(req.result[req.queryIndex.allCourtyardCoverage], [], req.result[req.queryIndex.needCourtyardCoverage], true); // have = []
let director_room = matchQueries(req.result[req.queryIndex.allDirectorRoom], req.result[req.queryIndex.haveDirectorRoom], req.result[req.queryIndex.needDirectorRoom]);
let secretary = matchQueries(req.result[req.queryIndex.allSecretary], req.result[req.queryIndex.haveSecretary], req.result[req.queryIndex.needSecretary]);
let teacher_room = matchQueries(req.result[req.queryIndex.allTeacherRoom], req.result[req.queryIndex.haveTeacherRoom], req.result[req.queryIndex.needTeacherRoom]);
let kitchen = matchQueries(req.result[req.queryIndex.allKitchen], req.result[req.queryIndex.haveKitchen], req.result[req.queryIndex.needKitchen]);
let storeroom = matchQueries(req.result[req.queryIndex.allStoreroom], req.result[req.queryIndex.haveStoreroom], req.result[req.queryIndex.needStoreroom]);
let warehouse = matchQueries(req.result[req.queryIndex.allWarehouse], req.result[req.queryIndex.haveWarehouse], req.result[req.queryIndex.needWarehouse]);
let internet = matchQueries(req.result[req.queryIndex.allInternet], req.result[req.queryIndex.haveInternet], req.result[req.queryIndex.needInternet]);
let broadband_internet = matchQueries(req.result[req.queryIndex.allBroadbandInternet], req.result[req.queryIndex.haveBroadbandInternet], req.result[req.queryIndex.needBroadbandInternet]);
let inside_bathroom = matchQueries(req.result[req.queryIndex.allInsideBathroom], req.result[req.queryIndex.haveInsideBathroom], req.result[req.queryIndex.needInsideBathroom]);
let inside_kids_bathroom = matchQueries(req.result[req.queryIndex.allInsideKidsBathroom], req.result[req.queryIndex.haveInsideKidsBathroom], req.result[req.queryIndex.needInsideKidsBathroom]);
let eletrical_power = matchQueries(req.result[req.queryIndex.allEletricPower], req.result[req.queryIndex.haveEletricPower], req.result[req.queryIndex.needEletricPower]);
let water_supply = matchQueries(req.result[req.queryIndex.allWaterSupply], req.result[req.queryIndex.haveWaterSupply], req.result[req.queryIndex.needWaterSupply]);
let filtered_water = matchQueries(req.result[req.queryIndex.allFilteredWater], req.result[req.queryIndex.haveFilteredWater], req.result[req.queryIndex.needFilteredWater]);
let sewage = matchQueries(req.result[req.queryIndex.allSewage], req.result[req.queryIndex.haveSewage], req.result[req.queryIndex.needSewage]);
let adapted_building = matchQueries(req.result[req.queryIndex.allAdaptedBuilding], req.result[req.queryIndex.haveAdaptedBuilding], req.result[req.queryIndex.needAdaptedBuilding]);
let adapted_bathroom = matchQueries(req.result[req.queryIndex.allSpecialBathroom], req.result[req.queryIndex.haveSpecialBathroom], req.result[req.queryIndex.needSpecialBathroom]);
schools_in_school_buildings,
urban_schools_in_school_buildings,
country_schools_in_school_buildings,
schools_not_in_school_buildings,
libraries,
libraries_reading_room,
computer_lab,
science_lab,
kids_park,
nursery,
sports_court,
sports_court_coverage,
courtyard,
courtyard_coverage,
director_room,
secretary,
teacher_room,
kitchen,
storeroom,
warehouse,
internet,
broadband_internet,
eletric_energy: eletrical_power,
sewage_treatment: sewage,
special_bathroom: adapted_bathroom
}];
next();
}, id2str.multitransform(false), response('infrastructure'));
module.exports = infrastructureApp;