diff --git a/src/libs/routes/classroomCount.js b/src/libs/routes/classroomCount.js index a2e94b13f761307dfc09368d4bc08a90eb4e8ef8..4e297d53968e48f3ba7b5217ec9f295107dffbcf 100644 --- a/src/libs/routes/classroomCount.js +++ b/src/libs/routes/classroomCount.js @@ -39,12 +39,12 @@ rqf.addField({ relation: '=', type: 'integer', field: 'municipio_id', - table: 'matricula' + table: '@' }, join: { primary: 'id', foreign: 'municipio_id', - foreignTable: 'matricula' + foreignTable: '@' } }, 'filter').addValueToField({ name: 'city', @@ -59,9 +59,9 @@ rqf.addField({ join: { primary: 'id', foreign: 'municipio_id', - foreignTable: 'matricula' + foreignTable: '@' } -}, 'dims').addValue({ +}, 'dims').addValueToField({ name: 'state', table: 'estado', tableField: 'nome', @@ -70,14 +70,29 @@ rqf.addField({ relation: '=', type: 'integer', field: 'estado_id', - table: 'matricula' + table: '@' }, join: { primary: 'id', foreign: 'estado_id', - foreignTable: 'matricula' + foreignTable: '@' } -}).addValue({ +}, 'filter').addValueToField({ + name: 'state', + table: 'estado', + tableField: ['nome', 'id'], + resultField: ['state_name', 'state_id'], + where: { + relation: '=', + type: 'integer', + field: 'id', + }, + join: { + primary: 'id', + foreign: 'estado_id', + foreignTable: '@' + } +}, 'dims').addValue({ name: 'region', table: 'regiao', tableField: 'nome', @@ -90,11 +105,11 @@ rqf.addField({ join: { primary: 'id', foreign: 'regiao_id', - foreignTable: 'matricula' + foreignTable: '@' } }).addValue({ name: 'min_year', - table: 'matricula', + table: '@', tableField: 'ano_censo', resultField: 'year', where: { @@ -104,7 +119,7 @@ rqf.addField({ } }).addValue({ name: 'max_year', - table: 'matricula', + table: '@', tableField: 'ano_censo', resultField: 'year', where: { @@ -114,7 +129,7 @@ rqf.addField({ } }).addValue({ name: 'period', - table: 'matricula', + table: '@', tableField: 'turma_turno_id', resultField: 'period_id', where: { @@ -124,7 +139,7 @@ rqf.addField({ } }).addValue({ name: 'school_year', - table: 'matricula', + table: '@', tableField: 'serie_ano_id', resultField: 'school_year_id', where: { @@ -134,7 +149,7 @@ rqf.addField({ } }).addValue({ name: 'location', - table: 'matricula', + table: '@', tableField: 'localizacao_id', resultField: 'location_id', where: { @@ -144,7 +159,7 @@ rqf.addField({ } }).addValue({ name: 'education_level_short', - table: 'matricula', + table: '@', tableField: 'etapa_resumida', resultField: 'education_level_short_id', where: { @@ -155,14 +170,6 @@ rqf.addField({ }); classroomCountApp.post('/', rqf.parse(), (req, res, next) => { - console.log('body'); - console.log(req.body); - - let classSize = JSON.parse(req.body.class_size) || null; - let integralTime = JSON.parse(req.body.integral_time) || null; - - console.log(classSize, integralTime); - req.dims.state = true; req.dims.city = true; req.dims.period = true; @@ -179,11 +186,84 @@ classroomCountApp.post('/', rqf.parse(), (req, res, next) => { next(); }, rqf.build(), query, id2str.transform(), (req, res, next) => { - let result = req.result; - // result.forEach((r) => { + req.enrollment = req.result; + req.resetSql(); + next(); +}, rqf.parse(), (req, res, next) => { + + req.dims.state = true; + req.dims.city = true; + req.dims.location = true; + + req.sql.field('SUM(escola.num_salas)', 'total') + .field("'Brasil'", 'name') + .field('escola.ano_censo', 'year') + .from('escola') + .group('escola.ano_censo') + .order('escola.ano_censo') + .where('escola.situacao_de_funcionamento = 1 AND escola.local_func_predio_escolar = 1'); + next(); +}, rqf.build(), query, id2str.transform(), (req, res, next) => { + let classSize = JSON.parse(req.body.class_size) || null; + let integralTime = JSON.parse(req.body.integral_time) || null; - // }); + console.log(classSize, integralTime); + req.classroom = req.result; + let classroom = []; + + req.classroom.forEach((c) => { + let cityObjects = req.classroom.filter((value) => { + return (c.city_id == value.city_id && c.year == value.year); + }); + + let obj = { + state_id: c.state_id, + state_name: c.state_name, + city_id: c.city_id, + city_name: c.city_name, + year: c.year, + locations: [] + }; + + cityObjects.forEach((co) => { + obj.locations.push({ + location_id: co.location_id, + location_name: co.location_name, + total_classroom: co.total + }); + }); + + if(classroom.findIndex((el) => {return el.city_id === obj.city_id}) === -1) { + classroom.push(obj); + } + }); + + let result = []; + classroom.forEach((c) => { + function filterUrbanEnrollment(e) { + return (e.city_id == c.city_id && e.year == c.year && e.location_id == 1); + } + + function filterRuralEnrollment(e) { + return (e.city_id == c.city_id && e.year == c.year && e.location_id == 2); + } + + let urbanEnrollments = req.enrollment.filter(filterUrbanEnrollment); + let ruralEnrollments = req.enrollment.filter(filterRuralEnrollment); + + let r = c; + // r.locations[0].enrollments = urbanEnrollments; + // r.locations[1].enrollments = ruralEnrollments; + + result.push(r); + }); + console.log('FEITO'); + // console.log('SALAS'); + // console.log(classroom[0]); + // console.log('FILTRO'); + // console.log(classEnrollments); + req.result = result; next(); }, response('classroom_count'));