diff --git a/src/libs/routes/classroomCount.js b/src/libs/routes/classroomCount.js index f0df94158d7b171475d7f2f70649f6be1e419e83..1d33ab2ebedde3abd33134e949fae02d41455ef6 100644 --- a/src/libs/routes/classroomCount.js +++ b/src/libs/routes/classroomCount.js @@ -330,8 +330,7 @@ classroomCountApp.post('/', rqf.parse(), (req, res, next) => { } }; - // location.education_level.push(educationLevel); - // Para manter a orde da etapa de ensino + // Para manter a ordem da etapa de ensino if (location.education_level.length == 0) { location.education_level.push(educationLevel); } else { @@ -390,7 +389,7 @@ classroomCountApp.post('/', rqf.parse(), (req, res, next) => { ++i; } - // TODO: agregar por estado e brasil + // Agregar por estado e brasil let reduction = null; if(req.dims.state || !req.dims.city) { // Se um dos dois acontecer, sabemos que devemos agregar let i = 0; @@ -422,8 +421,64 @@ classroomCountApp.post('/', rqf.parse(), (req, res, next) => { currentObj = reduction[reduction.length - 1]; } - // TODO: Fazer "merge" do array locations da cidade com o da agregação - // TODO: Somar total de turmas, matrÃculas e salas a serem construÃdas + // Fazer "merge" do array locations da cidade com o da agregação + if(currentObj.locations.length == 0) { + currentObj.locations = [...city.locations]; + } else { + let j = 0; + let k = 0; + let cityLocation = null; + let currentLocation = null; + while((typeof cityLocation !== 'undefined') && (typeof currentLocation !== 'undefined')) { + cityLocation = city.locations[j]; + currentLocation = currentObj.locations[k]; + if(cityLocation.location_id < currentLocation.location_id) { + ++j; + cityLocation = city.locations[j]; + continue; + } else if(cityLocation.location_id > currentLocation.location_id) { + ++k; + currentLocation = currentObj.locations[k]; + continue; + } + + // Fazer merge do array education_level + if(currentLocation.education_level.length == 0) { + currentLocation.education_level = [...cityLocation.education_level]; + } else { + let l = 0; + while(l < cityLocation.education_level.length) { + let cityEducation = cityLocation.education_level[l]; + let m = 0; + let currentEducation = currentLocation.education_level[m]; + while(m < currentLocation.education_level.length && cityEducation.education_level_short_id > currentEducation.education_level_short_id) { + ++m; + currentEducation = currentLocation.education_level[m]; + } + if(m >= currentLocation.education_level.length) --m; + currentEducation = currentLocation.education_level[m]; + + if(currentEducation.education_level_short_id == cityEducation.education_level_short_id) { + currentEducation.enrollment.total_enrollment_day += cityEducation.enrollment.total_enrollment_day; + currentEducation.enrollment.total_enrollment_night += cityEducation.enrollment.total_enrollment_night; + currentEducation.enrollment.full_period_classes += cityEducation.enrollment.full_period_classes; + currentEducation.enrollment.day_classes += cityEducation.enrollment.day_classes; + currentEducation.enrollment.night_classes += cityEducation.enrollment.night_classes; + currentEducation.enrollment.total_classrooms_needed += cityEducation.enrollment.total_classrooms_needed; + } else { + currentLocation.education_level.splice(m, 0, cityEducation); + } + ++l; + } + } + + currentLocation.total_classroom += cityLocation.total_classroom; + currentLocation.total_classroom_be_built += cityLocation.total_classroom_be_built; + + ++j; + cityLocation = city.locations[j]; + } + } ++i; } } @@ -434,7 +489,7 @@ classroomCountApp.post('/', rqf.parse(), (req, res, next) => { console.log('BRASIL!!!'); } - req.result = result; + req.result = reduction || result; next(); }, response('classroom_count'));