From beabfdd67569ae142b1f5b05f8279513662b3a24 Mon Sep 17 00:00:00 2001
From: Fernando Erd <fce15@inf.ufpr.br>
Date: Mon, 1 Oct 2018 11:41:47 -0300
Subject: [PATCH] Add new table employees

---
 src/libs/routes/employees.js | 201 ++++-------------------------------
 1 file changed, 19 insertions(+), 182 deletions(-)

diff --git a/src/libs/routes/employees.js b/src/libs/routes/employees.js
index 5e75495b..e1114515 100644
--- a/src/libs/routes/employees.js
+++ b/src/libs/routes/employees.js
@@ -247,165 +247,6 @@ rqfSchool.addField({
     }
 });
 
-rqfTeacher.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: 'escola_regiao_id',
-        foreignTable: '@'
-    }
-}).addValue({
-    name: 'state',
-    table: 'estado',
-    tableField: 'nome',
-    resultField: 'state_name',
-    where: {
-        relation: '=',
-        type: 'integer',
-        field: 'id'
-    },
-    join: {
-        primary: 'id',
-        foreign: 'escola_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: 'escola_municipio_id',
-        foreignTable: '@'
-    }
-}, 'dims').addValueToField({
-    name: 'city',
-    table: 'municipio',
-    tableField: 'nome',
-    resultField: 'city_name',
-    where: {
-        relation: '=',
-        type: 'integer',
-        field: 'id'
-    },
-    join: {
-        primary: 'id',
-        foreign: 'escola_municipio_id',
-        foreignTable: '@'
-    }
-}, 'filter').addValueToField({
-    name: 'school',
-    table: 'escola',
-    tableField: ['nome_escola', 'id'],
-    resultField: ['school_name', 'school_id'],
-    where: {
-        relation: '=',
-        type: 'integer',
-        field: 'id'
-    },
-    join: {
-        primary: ['id', 'ano_censo'],
-        foreign: ['escola_id', 'ano_censo'],
-        foreignTable: 'docente'
-    }
-}, 'dims').addValueToField({
-    name: 'school',
-    table: 'escola',
-    tableField: 'nome_escola',
-    resultField: 'school_name',
-    where: {
-        relation: '=',
-        type: 'integer',
-        field: 'id'
-    },
-    join: {
-        primary: ['id', 'ano_censo'],
-        foreign: ['escola_id', 'ano_censo'],
-        foreignTable: 'docente'
-    }
-}, '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'
-    }
-});
 
 function matchQueries(queryTotal, queryPartial) {
     let match = [];
@@ -463,46 +304,42 @@ employeesApp.get('/', rqfSchool.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();
     }
-}, rqfSchool.build(), query, rqfTeacher.parse(), id2str.transform(), (req, res, next) => {
+}, rqfSchool.build(), query, rqfSchool.parse(), id2str.transform(), (req, res, next) => {
 
     req.allEmployees = req.result;
     req.resetSql();
 
-    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)');
+    if ("school" in req.filter) {
+        req.sql.field('SUM(professor_por_escola.total_professores)', 'total')
+        .field("'Brasil'", 'name')
+        .field('professor_por_escola.ano_censo', 'year')
+        .from('professor_por_escola')
+        .group('professor_por_escola.ano_censo')
+        .order('professor_por_escola.ano_censo')
+        .where('(professor_por_escola.dependencia_adm_id > 1) AND (escola.id=' + req.filter.school + ')');
+    } else {
+        req.sql.field('SUM(professor_por_escola.total_professores)', 'total')
+        .field("'Brasil'", 'name')
+        .field('professor_por_escola.ano_censo', 'year')
+        .from('professor_por_escola')
+        .group('professor_por_escola.ano_censo')
+        .order('professor_por_escola.ano_censo')
+        .where('(professor_por_escola.dependencia_adm_id > 1)');
+    }
     next();
 
-}, rqfTeacher.build(), query,  id2str.transform(),  (req, res, next) => {
+}, rqfSchool.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