From c484294d3e658c5c9eac144855ac1eef8debe026 Mon Sep 17 00:00:00 2001
From: Fernando Erd <fce15@inf.ufpr.br>
Date: Fri, 14 Sep 2018 10:37:54 -0300
Subject: [PATCH] Alternative version employees

---
 src/libs/routes/employees.js | 176 ++++++++++++++++++++++++++++++++++-
 1 file changed, 171 insertions(+), 5 deletions(-)

diff --git a/src/libs/routes/employees.js b/src/libs/routes/employees.js
index cf7dc1ee..5e75495b 100644
--- a/src/libs/routes/employees.js
+++ b/src/libs/routes/employees.js
@@ -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'));
 
-- 
GitLab