diff --git a/src/main/java/in/gov/chennaicorporation/gccoffice/shelterinspection/Service/ShelterInspectionService.java b/src/main/java/in/gov/chennaicorporation/gccoffice/shelterinspection/Service/ShelterInspectionService.java index 7bbb601..32b8610 100644 --- a/src/main/java/in/gov/chennaicorporation/gccoffice/shelterinspection/Service/ShelterInspectionService.java +++ b/src/main/java/in/gov/chennaicorporation/gccoffice/shelterinspection/Service/ShelterInspectionService.java @@ -54,55 +54,233 @@ public class ShelterInspectionService { } // Dashboard - public Map getDashboardData(String type, String value) { Map response = new LinkedHashMap<>(); String dateCondition = ""; + String residentQuery = ""; + String residentParameter = ""; + + // ========================================== + // DAY + // ========================================== + /* + * For each shelter: Take the latest qid = 4 record and up to the selected date. + */ + if ("day".equalsIgnoreCase(type)) { + dateCondition = " AND DATE(cdate) = ? "; - } else if ("month".equalsIgnoreCase(type)) { - dateCondition = " AND DATE_FORMAT(cdate,'%Y-%m') = ? "; - } else if ("year".equalsIgnoreCase(type)) { - dateCondition = " AND YEAR(cdate) = ? "; + + residentQuery = """ + COALESCE( + (SELECT SUM(CAST(t.answer AS UNSIGNED)) + FROM ( + SELECT sld.shelter_id, r.answer, ROW_NUMBER() OVER + (PARTITION BY sld.shelter_id ORDER BY r.cdate DESC, r.id DESC) AS rn + FROM night_shelter_inspection_response r + + INNER JOIN surveyor_location_details sld ON sld.survey_id = r.survey_id AND sld.loginId = r.cby + + WHERE r.qid = 4 AND r.isactive = 1 AND r.isdelete = 0 + AND sld.is_active = 1 AND sld.is_delete = 0 + + + AND DATE_FORMAT(r.cdate, '%Y-%m') = DATE_FORMAT( STR_TO_DATE(?, '%Y-%m-%d'), '%Y-%m') + + AND DATE(r.cdate) <= ?) t + + WHERE t.rn = 1), 0) AS totalResidents, + + """; + + residentParameter = value; } + + + // ========================================== + // MONTH + // ========================================== + /* + * Only records from selected month. + + * For each shelter: Take latest qid = 4 record in that month. and Then SUM all shelters. + */ + else if ("month".equalsIgnoreCase(type)) { + + dateCondition = + " AND DATE_FORMAT(cdate, '%Y-%m') = ? "; + + residentQuery = """ + COALESCE( + (SELECT SUM(CAST(t.answer AS UNSIGNED)) + FROM ( + SELECT sld.shelter_id, r.answer, ROW_NUMBER() OVER + (PARTITION BY sld.shelter_id ORDER BY r.cdate DESC, r.id DESC) AS rn + FROM night_shelter_inspection_response r + + INNER JOIN surveyor_location_details sld ON sld.survey_id = r.survey_id AND sld.loginId = r.cby + + WHERE r.qid = 4 AND r.isactive = 1 AND r.isdelete = 0 + AND sld.is_active = 1 AND sld.is_delete = 0 + + AND DATE_FORMAT(r.cdate, '%Y-%m') = ?) t + + WHERE t.rn = 1), 0) AS totalResidents, + + """; + + residentParameter = value; + } + + + + // ========================================== + // YEAR + // ========================================== + /* + * Only records from selected year. + + * For each shelter: Take latest qid = 4 record in that year. and Then SUM all shelters. + */ + else if ("year".equalsIgnoreCase(type)) { + + dateCondition = + " AND YEAR(cdate) = ? "; + + residentQuery = """ + COALESCE( + ( + SELECT SUM(CAST(t.answer AS UNSIGNED)) + + FROM ( + SELECT sld.shelter_id, r.answer, ROW_NUMBER() OVER + (PARTITION BY sld.shelter_id ORDER BY r.cdate DESC, r.id DESC) AS rn + FROM night_shelter_inspection_response r + + INNER JOIN surveyor_location_details sld ON sld.survey_id = r.survey_id AND sld.loginId = r.cby + + WHERE r.qid = 4 AND r.isactive = 1 AND r.isdelete = 0 + AND sld.is_active = 1 AND sld.is_delete = 0 + + AND YEAR(r.cdate) = ? + ) t + + WHERE t.rn = 1), 0) AS totalResidents, + + """; + + residentParameter = value; + } + + + + // ========================================== + // DASHBOARD SQL + // ========================================== String dashboardSql = """ + SELECT - COALESCE(SUM(CASE WHEN qid = 4 THEN CAST(answer AS UNSIGNED) END),0) AS totalResidents, + """ + residentQuery + """ COUNT(DISTINCT survey_id) AS numberOfVisits, - COALESCE(SUM(CASE WHEN qid = 16 THEN CAST(answer AS UNSIGNED) END),0) AS rehabilitation, + COALESCE(SUM(CASE WHEN qid = 16 THEN CAST(answer AS UNSIGNED) END), 0) AS rehabilitation, - COALESCE(SUM(CASE WHEN qid = 17 THEN CAST(answer AS UNSIGNED) END),0) AS reintegration, + COALESCE(SUM(CASE WHEN qid = 17 THEN CAST(answer AS UNSIGNED) END), 0) AS reintegration, - COALESCE(SUM(CASE WHEN qid = 48 THEN CAST(answer AS UNSIGNED) END),0) AS entitlementAccess, + COALESCE(SUM(CASE WHEN qid = 48 THEN CAST(answer AS UNSIGNED) END), 0) AS entitlementAccess, - COALESCE(SUM(CASE WHEN qid = 11 THEN CAST(answer AS UNSIGNED) END),0) AS leftWithInfo, + COALESCE(SUM(CASE WHEN qid = 11 THEN CAST(answer AS UNSIGNED) END), 0) AS leftWithInfo, - COALESCE(SUM(CASE WHEN qid = 13 THEN CAST(answer AS UNSIGNED) END),0) AS leftWithoutInfo, + COALESCE(SUM(CASE WHEN qid = 13 THEN CAST(answer AS UNSIGNED) END), 0) AS leftWithoutInfo, - COALESCE(SUM(CASE WHEN qid = 10 THEN CAST(answer AS UNSIGNED) END),0) AS deathCount + COALESCE(SUM(CASE WHEN qid = 10 THEN CAST(answer AS UNSIGNED) END), 0) AS deathCount FROM night_shelter_inspection_response - WHERE isactive = 1 - AND isdelete = 0 + WHERE isactive = 1 AND isdelete = 0 """ + dateCondition; + + + // ========================================== + // EXECUTE QUERY + // ========================================== + + if (dateCondition.isEmpty()) { + response.putAll(jdbcTemplate.queryForMap(dashboardSql)); + } else if ("day".equalsIgnoreCase(type)) { + + response.putAll(jdbcTemplate.queryForMap(dashboardSql, value, value, value)); } else { - response.putAll(jdbcTemplate.queryForMap(dashboardSql, value)); + + response.putAll(jdbcTemplate.queryForMap(dashboardSql, residentParameter, value)); } return response; } + + + +// public Map getDashboardData(String type, String value) { +// +// Map response = new LinkedHashMap<>(); +// +// String dateCondition = ""; +// +// if ("day".equalsIgnoreCase(type)) { +// dateCondition = " AND DATE(cdate) = ? "; +// } else if ("month".equalsIgnoreCase(type)) { +// dateCondition = " AND DATE_FORMAT(cdate,'%Y-%m') = ? "; +// } else if ("year".equalsIgnoreCase(type)) { +// dateCondition = " AND YEAR(cdate) = ? "; +// } +// +// String dashboardSql = """ +// SELECT +// +// COALESCE((SELECT SUM(CAST(r.answer AS UNSIGNED)) FROM night_shelter_inspection_response r +// WHERE r.qid = 4 AND r.isactive = 1 AND r.isdelete = 0 AND r.id = ( +// SELECT MAX(r2.id) FROM night_shelter_inspection_response r2 +// WHERE r2.cby = r.cby AND r2.qid = 4 AND r2.isactive = 1 AND r2.isdelete = 0 ) ), 0) AS totalResidents, +// +// COUNT(DISTINCT survey_id) AS numberOfVisits, +// +// COALESCE(SUM(CASE WHEN qid = 16 THEN CAST(answer AS UNSIGNED) END),0) AS rehabilitation, +// +// COALESCE(SUM(CASE WHEN qid = 17 THEN CAST(answer AS UNSIGNED) END),0) AS reintegration, +// +// COALESCE(SUM(CASE WHEN qid = 48 THEN CAST(answer AS UNSIGNED) END),0) AS entitlementAccess, +// +// COALESCE(SUM(CASE WHEN qid = 11 THEN CAST(answer AS UNSIGNED) END),0) AS leftWithInfo, +// +// COALESCE(SUM(CASE WHEN qid = 13 THEN CAST(answer AS UNSIGNED) END),0) AS leftWithoutInfo, +// +// COALESCE(SUM(CASE WHEN qid = 10 THEN CAST(answer AS UNSIGNED) END),0) AS deathCount +// +// FROM night_shelter_inspection_response +// +// WHERE isactive = 1 +// AND isdelete = 0 +// +// """ + dateCondition; +// +// if (dateCondition.isEmpty()) { +// response.putAll(jdbcTemplate.queryForMap(dashboardSql)); +// } else { +// response.putAll(jdbcTemplate.queryForMap(dashboardSql, value)); +// } +// +// return response; +// } // Top 5 Shelter List diff --git a/src/main/resources/templates/modules/shelterinspection/visitlist.html b/src/main/resources/templates/modules/shelterinspection/visitlist.html index 8dbec3d..ebdfe01 100644 --- a/src/main/resources/templates/modules/shelterinspection/visitlist.html +++ b/src/main/resources/templates/modules/shelterinspection/visitlist.html @@ -410,6 +410,8 @@
+ +