Skip to content
Snippets Groups Projects
Commit c484294d authored by Fernando Erd's avatar Fernando Erd :ok_hand:
Browse files

Alternative version employees

parent be0be3d5
No related branches found
No related tags found
No related merge requests found
Pipeline #17676 failed
......@@ -24,7 +24,10 @@ const addMissing = require(`${libs}/middlewares/addMissing`);
const cache = require('apicache').options({ debug: config.debug, statusCodes: {include: [200]} }).middleware;
let rqf = new ReqQueryFields();
let rqfTeacher = new ReqQueryFields();
let rqfSchool = new ReqQueryFields();
employeesApp.use(cache('15 day'));
......@@ -94,7 +97,157 @@ employeesApp.get('/rural_location', (req, res, next) => {
next();
}, response('rural_location'));
rqf.addField({
rqfSchool.addField({
name: 'filter',
field: false,
where: true
}).addField({
name: 'dims',
field: true,
where: false
}).addValue({
name: 'adm_dependency',
table: '@',
tableField: 'dependencia_adm_id',
resultField: 'adm_dependency_id',
where: {
relation: '=',
type: 'integer',
field: 'dependencia_adm_id'
}
}).addValue({
name: 'adm_dependency_detailed',
table: '@',
tableField: 'dependencia_adm_priv',
resultField: 'adm_dependency_detailed_id',
where: {
relation: '=',
type: 'integer',
field: 'dependencia_adm_priv'
}
}).addValue({
name: 'region',
table: 'regiao',
tableField: 'nome',
resultField: 'region_name',
where: {
relation: '=',
type: 'integer',
field: 'id'
},
join: {
primary: 'id',
foreign: 'regiao_id',
foreignTable: '@'
}
}).addValue({
name: 'state',
table: 'estado',
tableField: 'nome',
resultField: 'state_name',
where: {
relation: '=',
type: 'integer',
field: 'id'
},
join: {
primary: 'id',
foreign: 'estado_id',
foreignTable: '@'
}
}).addValueToField({
name: 'city',
table: 'municipio',
tableField: ['nome', 'id'],
resultField: ['city_name', 'city_id'],
where: {
relation: '=',
type: 'integer',
field: 'id'
},
join: {
primary: 'id',
foreign: 'municipio_id',
foreignTable: '@'
}
}, 'dims').addValueToField({
name: 'city',
table: 'municipio',
tableField: 'nome',
resultField: 'city_name',
where: {
relation: '=',
type: 'integer',
field: 'id'
},
join: {
primary: 'id',
foreign: 'municipio_id',
foreignTable: '@'
}
}, 'filter').addValueToField({
name: 'school',
table: 'escola',
tableField: ['nome_escola', 'id'],
resultField: ['school_name', 'school_id'],
where: {
relation: '=',
type: 'integer',
field: 'id'
},
}, 'dims').addValueToField({
name: 'school',
table: 'escola',
tableField: 'nome_escola',
resultField: 'school_name',
where: {
relation: '=',
type: 'integer',
field: 'id'
},
}, 'filter').addValue({
name: 'location',
table: '@',
tableField: 'cod_localizacao',
resultField: 'location_id',
where: {
relation: '=',
type: 'integer',
field: 'cod_localizacao'
}
}).addValue({
name: 'rural_location',
table: '@',
tableField: 'localidade_area_rural',
resultField: 'rural_location_id',
where: {
relation: '=',
type: 'integer',
field: 'localidade_area_rural'
}
}).addValue({
name: 'min_year',
table: '@',
tableField: 'ano_censo',
resultField: 'year',
where: {
relation: '>=',
type: 'integer',
field: 'ano_censo'
}
}).addValue({
name: 'max_year',
table: '@',
tableField: 'ano_censo',
resultField: 'year',
where: {
relation: '<=',
type: 'integer',
field: 'ano_censo'
}
});
rqfTeacher.addField({
name: 'filter',
field: false,
where: true
......@@ -293,7 +446,7 @@ function matchQueries(queryTotal, queryPartial) {
return match;
}
employeesApp.get('/', rqf.parse(), (req, res, next) => {
employeesApp.get('/', rqfSchool.parse(), (req, res, next) => {
req.allEmployees = {}
req.allTeacher = {}
......@@ -310,14 +463,17 @@ employeesApp.get('/', rqf.parse(), (req, res, next) => {
} else {
req.sql.field('SUM(escola.num_funcionarios)', 'total')
.field("'Brasil'", 'name')
.field('escola.id', 'id')
.field('escola.ano_censo', 'year')
.from('escola')
.group('escola.ano_censo')
.group('escola.id')
.order('escola.ano_censo')
.order('escola.id')
.where('(escola.situacao_de_funcionamento = 1) AND (escola.ensino_regular = 1 OR escola.ensino_eja = 1 OR escola.educacao_profissional = 1) AND (dependencia_adm_id = 2 OR dependencia_adm_id = 3 OR dependencia_adm_id = 4)');
next();
}
}, rqf.build(), query, rqf.parse(), id2str.transform(), (req, res, next) => {
}, rqfSchool.build(), query, rqfTeacher.parse(), id2str.transform(), (req, res, next) => {
req.allEmployees = req.result;
req.resetSql();
......@@ -325,18 +481,28 @@ employeesApp.get('/', rqf.parse(), (req, res, next) => {
req.sql.field('COUNT(DISTINCT docente.id)', 'total')
.field("'Brasil'", 'name')
.field('docente.ano_censo', 'year')
.field('docente.escola_id', 'id')
.from('docente')
.join('turma', null, 'docente.turma_id=turma.id AND docente.ano_censo=turma.ano_censo')
.group('docente.ano_censo')
.group('docente.escola_id')
.order('docente.ano_censo')
.order('docente.escola_id')
.where('(docente.tipo_docente <= 6) AND (turma.tipo_turma_id <= 3) AND (docente.dependencia_adm_id > 1)');
next();
}, rqf.build(), query, id2str.transform(), (req, res, next) => {
}, rqfTeacher.build(), query, id2str.transform(), (req, res, next) => {
req.allTeacher = req.result;
let aux_employees = matchQueries(req.allEmployees, req.allTeacher);
req.result = aux_employees;
//req.result = req.allEmployees
req.resultFinal = 0
req.result.forEach((r) => {
//console.log(r.total)
req.resultFinal = req.resultFinal + r.total
});
req.result = req.resultFinal;
next();
}, response('employees'));
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment