|
|
@ -54,56 +54,234 @@ public class ShelterInspectionService { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// Dashboard |
|
|
// Dashboard |
|
|
|
|
|
|
|
|
public Map<String, Object> getDashboardData(String type, String value) { |
|
|
public Map<String, Object> getDashboardData(String type, String value) { |
|
|
|
|
|
|
|
|
Map<String, Object> response = new LinkedHashMap<>(); |
|
|
Map<String, Object> response = new LinkedHashMap<>(); |
|
|
|
|
|
|
|
|
String dateCondition = ""; |
|
|
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)) { |
|
|
if ("day".equalsIgnoreCase(type)) { |
|
|
|
|
|
|
|
|
dateCondition = " AND DATE(cdate) = ? "; |
|
|
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 = """ |
|
|
String dashboardSql = """ |
|
|
|
|
|
|
|
|
SELECT |
|
|
SELECT |
|
|
|
|
|
|
|
|
COALESCE(SUM(CASE WHEN qid = 4 THEN CAST(answer AS UNSIGNED) END),0) AS totalResidents, |
|
|
|
|
|
|
|
|
""" + residentQuery + """ |
|
|
|
|
|
|
|
|
COUNT(DISTINCT survey_id) AS numberOfVisits, |
|
|
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 |
|
|
FROM night_shelter_inspection_response |
|
|
|
|
|
|
|
|
WHERE isactive = 1 |
|
|
|
|
|
AND isdelete = 0 |
|
|
|
|
|
|
|
|
WHERE isactive = 1 AND isdelete = 0 |
|
|
|
|
|
|
|
|
""" + dateCondition; |
|
|
""" + dateCondition; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// ========================================== |
|
|
|
|
|
// EXECUTE QUERY |
|
|
|
|
|
// ========================================== |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (dateCondition.isEmpty()) { |
|
|
if (dateCondition.isEmpty()) { |
|
|
|
|
|
|
|
|
response.putAll(jdbcTemplate.queryForMap(dashboardSql)); |
|
|
response.putAll(jdbcTemplate.queryForMap(dashboardSql)); |
|
|
|
|
|
} else if ("day".equalsIgnoreCase(type)) { |
|
|
|
|
|
|
|
|
|
|
|
response.putAll(jdbcTemplate.queryForMap(dashboardSql, value, value, value)); |
|
|
} else { |
|
|
} else { |
|
|
response.putAll(jdbcTemplate.queryForMap(dashboardSql, value)); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
response.putAll(jdbcTemplate.queryForMap(dashboardSql, residentParameter, value)); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
return response; |
|
|
return response; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// public Map<String, Object> getDashboardData(String type, String value) { |
|
|
|
|
|
// |
|
|
|
|
|
// Map<String, Object> 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 |
|
|
// Top 5 Shelter List |
|
|
|
|
|
|
|
|
public List<Map<String, Object>> getTop5Shelters(String type, String value) { |
|
|
public List<Map<String, Object>> getTop5Shelters(String type, String value) { |
|
|
|