From d8f9b150c94aae7f11c6ac9edd55c54877c2e4c3 Mon Sep 17 00:00:00 2001 From: AJITHKUMAR12 <48044092+AJITHKUMAR12@users.noreply.github.com> Date: Fri, 14 Aug 2026 16:53:02 +0530 Subject: [PATCH] added gcc_mobile_menus schema --- .../Service/ChildrenSurveyService.java | 2 +- .../configuration/DataSourceConfig.java | 12 +- .../Service/HomelessSurveyService.java | 2 +- .../Service/ShelterInspectionService.java | 1152 ++++++++--------- src/main/resources/application.properties | 4 +- 5 files changed, 550 insertions(+), 622 deletions(-) diff --git a/src/main/java/in/gov/chennaicorporation/gccoffice/childrensurvey/Service/ChildrenSurveyService.java b/src/main/java/in/gov/chennaicorporation/gccoffice/childrensurvey/Service/ChildrenSurveyService.java index 10b32f7..a70e925 100644 --- a/src/main/java/in/gov/chennaicorporation/gccoffice/childrensurvey/Service/ChildrenSurveyService.java +++ b/src/main/java/in/gov/chennaicorporation/gccoffice/childrensurvey/Service/ChildrenSurveyService.java @@ -979,7 +979,7 @@ public class ChildrenSurveyService { DATE(sr.cdate) AS survey_date - FROM login_details d + FROM gcc_mobile_menus.login_details d INNER JOIN surveyor_location_details ld ON d.uid = ld.loginId diff --git a/src/main/java/in/gov/chennaicorporation/gccoffice/configuration/DataSourceConfig.java b/src/main/java/in/gov/chennaicorporation/gccoffice/configuration/DataSourceConfig.java index fa02e69..acabf76 100644 --- a/src/main/java/in/gov/chennaicorporation/gccoffice/configuration/DataSourceConfig.java +++ b/src/main/java/in/gov/chennaicorporation/gccoffice/configuration/DataSourceConfig.java @@ -24,14 +24,15 @@ import java.util.Properties; @EnableTransactionManagement public class DataSourceConfig { -// private static String host = "localhost:3306"; -// private static String dbpassword = "root"; + private static String host = "localhost:3306"; + private static String dbpassword = "root"; // private static String dbpassword = "gccroot"; // AWS - private static String host = "gcc-facial-db-instance-1.cf48eqcciziq.ap-south-1.rds.amazonaws.com:3306"; - private static String dbpassword = "gcc-facial-password"; + // private static String host = + // "gcc-facial-db-instance-1.cf48eqcciziq.ap-south-1.rds.amazonaws.com:3306"; + // private static String dbpassword = "gcc-facial-password"; ////////////////////////////// (For GCC APP) //////////////////////// @Configuration @@ -441,7 +442,7 @@ public class DataSourceConfig { dataSource.setPassword(dbpassword); return dataSource; } - + /////////////// Shelter Inspection ///////////////////// @Bean(name = "mysqlShelterinspectionDataSource") public DataSource mysqlShelterinspectionDataSource() { @@ -452,7 +453,6 @@ public class DataSourceConfig { dataSource.setPassword(dbpassword); return dataSource; } - ////////////////////////////// (For GCC USER) //////////////////////// @Bean(name = "mysqlGCCUserDataSource") diff --git a/src/main/java/in/gov/chennaicorporation/gccoffice/homelesssurvey/Service/HomelessSurveyService.java b/src/main/java/in/gov/chennaicorporation/gccoffice/homelesssurvey/Service/HomelessSurveyService.java index b3fffed..ad8fc17 100644 --- a/src/main/java/in/gov/chennaicorporation/gccoffice/homelesssurvey/Service/HomelessSurveyService.java +++ b/src/main/java/in/gov/chennaicorporation/gccoffice/homelesssurvey/Service/HomelessSurveyService.java @@ -549,7 +549,7 @@ public class HomelessSurveyService { INNER JOIN surveyor_location_details sl ON sl.survey_id = hs.survey_id - INNER JOIN login_details d + INNER JOIN gcc_mobile_menus.login_details d ON d.uid = sl.loginId WHERE 1 = 1 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 915f78d..7bbb601 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 @@ -24,40 +24,36 @@ import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.stereotype.Service; import org.springframework.web.client.RestTemplate; - - @Service public class ShelterInspectionService { - - @Autowired - private JdbcTemplate jdbcTemplate; - - @Autowired - public void setDataSource(@Qualifier("mysqlShelterinspectionDataSource") DataSource dataSource) { - this.jdbcTemplate = new JdbcTemplate(dataSource); - } - - + + @Autowired + private JdbcTemplate jdbcTemplate; + + @Autowired + public void setDataSource(@Qualifier("mysqlShelterinspectionDataSource") DataSource dataSource) { + this.jdbcTemplate = new JdbcTemplate(dataSource); + } + private final RestTemplate restTemplate = new RestTemplate(); private static final String API_URL = "https://nightshelter.gccservices.in/api/shelters/"; public int getTotalShelterCount() { - ResponseEntity>> response = - restTemplate.exchange( - API_URL, - HttpMethod.GET, - null, - new ParameterizedTypeReference>>() {} - ); + ResponseEntity>> response = restTemplate.exchange( + API_URL, + HttpMethod.GET, + null, + new ParameterizedTypeReference>>() { + }); List> shelters = response.getBody(); return shelters != null ? shelters.size() : 0; } - - // Dashboard + + // Dashboard public Map getDashboardData(String type, String value) { @@ -74,30 +70,30 @@ public class ShelterInspectionService { } String dashboardSql = """ - SELECT + SELECT - COALESCE(SUM(CASE WHEN qid = 4 THEN CAST(answer AS UNSIGNED) END),0) AS totalResidents, + COALESCE(SUM(CASE WHEN qid = 4 THEN CAST(answer AS UNSIGNED) END),0) AS totalResidents, - COALESCE(SUM(CASE WHEN qid = 5 THEN CAST(answer AS UNSIGNED) END),0) 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 - AND qid IN (4,5,10,11,13,16,17,48) - """ + dateCondition; + WHERE isactive = 1 + AND isdelete = 0 + + """ + dateCondition; if (dateCondition.isEmpty()) { response.putAll(jdbcTemplate.queryForMap(dashboardSql)); @@ -107,19 +103,17 @@ public class ShelterInspectionService { return response; } - - + // Top 5 Shelter List - + public List> getTop5Shelters(String type, String value) { - ResponseEntity>> response = - restTemplate.exchange( - API_URL, - HttpMethod.GET, - null, - new ParameterizedTypeReference>>() { - }); + ResponseEntity>> response = restTemplate.exchange( + API_URL, + HttpMethod.GET, + null, + new ParameterizedTypeReference>>() { + }); List> apiList = response.getBody(); @@ -193,19 +187,17 @@ public class ShelterInspectionService { .limit(5) .collect(Collectors.toList()); } - - - //Region Summary - + + // Region Summary + public List> getRegionSummary(String type, String value) { - ResponseEntity>> response = - restTemplate.exchange( - API_URL, - HttpMethod.GET, - null, - new ParameterizedTypeReference>>() { - }); + ResponseEntity>> response = restTemplate.exchange( + API_URL, + HttpMethod.GET, + null, + new ParameterizedTypeReference>>() { + }); List> apiData = response.getBody(); @@ -221,35 +213,35 @@ public class ShelterInspectionService { dateCondition = " AND DATE_FORMAT(r.cdate,'%Y-%m')=? "; } else if ("year".equalsIgnoreCase(type)) { dateCondition = " AND YEAR(r.cdate)=? "; - } - + } + StringBuilder sql = new StringBuilder(); sql.append(""" - SELECT - s.shelter_id, - - COALESCE( - SUM( - CASE - WHEN r.qid = 4 - THEN CAST(r.answer AS UNSIGNED) - ELSE 0 - END - ), - 0 - ) AS totalResidents, - - COUNT(DISTINCT r.survey_id) AS totalVisits - - FROM surveyor_location_details s - - LEFT JOIN night_shelter_inspection_response r - ON r.survey_id = s.survey_id - AND r.cby = s.loginId - AND r.isactive = 1 - AND r.isdelete = 0 - """); + SELECT + s.shelter_id, + + COALESCE( + SUM( + CASE + WHEN r.qid = 4 + THEN CAST(r.answer AS UNSIGNED) + ELSE 0 + END + ), + 0 + ) AS totalResidents, + + COUNT(DISTINCT r.survey_id) AS totalVisits + + FROM surveyor_location_details s + + LEFT JOIN night_shelter_inspection_response r + ON r.survey_id = s.survey_id + AND r.cby = s.loginId + AND r.isactive = 1 + AND r.isdelete = 0 + """); List params = new ArrayList<>(); @@ -271,16 +263,16 @@ public class ShelterInspectionService { } sql.append(""" - GROUP BY s.shelter_id - """); + GROUP BY s.shelter_id + """); - List> dbData; + List> dbData; - if(params.isEmpty()){ + if (params.isEmpty()) { dbData = jdbcTemplate.queryForList(sql.toString()); - }else{ + } else { dbData = jdbcTemplate.queryForList( sql.toString(), @@ -313,19 +305,18 @@ public class ShelterInspectionService { visits = ((Number) db.get("totalVisits")).intValue(); } - Map regionRow = - regionMap.computeIfAbsent(region, k -> { + Map regionRow = regionMap.computeIfAbsent(region, k -> { - Map m = new LinkedHashMap<>(); + Map m = new LinkedHashMap<>(); - m.put("region", region); - m.put("totalShelters", 0); - m.put("totalResidents", 0); - m.put("totalVisits", 0); + m.put("region", region); + m.put("totalShelters", 0); + m.put("totalResidents", 0); + m.put("totalVisits", 0); - return m; + return m; - }); + }); regionRow.put( "totalShelters", @@ -344,11 +335,9 @@ public class ShelterInspectionService { return new ArrayList<>(regionMap.values()); } - - - - //Fund Received Summary - + + // Fund Received Summary + public List> getFundReceivedSummary(String type, String value) { String dateCondition = ""; @@ -362,35 +351,35 @@ public class ShelterInspectionService { } String sql = """ - SELECT + SELECT - am.answer AS fund_type, - COUNT(r.id) AS total + am.answer AS fund_type, + COUNT(r.id) AS total - FROM night_shelter_inspection_answer_master am - LEFT JOIN night_shelter_inspection_response r + FROM night_shelter_inspection_answer_master am + LEFT JOIN night_shelter_inspection_response r - ON CAST(r.answer AS UNSIGNED)=am.id - AND r.qid=51 - AND r.isactive=1 - AND r.isdelete=0 - """ - + dateCondition + - """ + ON CAST(r.answer AS UNSIGNED)=am.id + AND r.qid=51 + AND r.isactive=1 + AND r.isdelete=0 + """ + + dateCondition + + """ - WHERE - am.qid=51 - AND am.isactive=1 - AND am.isdelete=0 + WHERE + am.qid=51 + AND am.isactive=1 + AND am.isdelete=0 - GROUP BY - am.id, - am.answer + GROUP BY + am.id, + am.answer - ORDER BY - am.orderby + ORDER BY + am.orderby - """; + """; if (dateCondition.isEmpty()) { return jdbcTemplate.queryForList(sql); @@ -399,22 +388,20 @@ public class ShelterInspectionService { } } - - + // Fund Recieved Total Details - + public List> getFundReceivedShelterDetails( String type, String value) { - /* Get shelter details from external API */ - ResponseEntity>> response = - restTemplate.exchange( - API_URL, - HttpMethod.GET, - null, - new ParameterizedTypeReference>>() { - }); + /* Get shelter details from external API */ + ResponseEntity>> response = restTemplate.exchange( + API_URL, + HttpMethod.GET, + null, + new ParameterizedTypeReference>>() { + }); List> apiList = response.getBody(); @@ -426,11 +413,10 @@ public class ShelterInspectionService { Map> shelterApiMap = new HashMap<>(); for (Map shelter : apiList) { - String uqid = String.valueOf( shelter.get("uqid")); + String uqid = String.valueOf(shelter.get("uqid")); shelterApiMap.put(uqid, shelter); } - /* Date filter */ String dateCondition = ""; @@ -442,59 +428,57 @@ public class ShelterInspectionService { dateCondition = " AND YEAR(r.cdate) = ? "; } - /* * Get fund response + shelter mapping * - * r.answer -> fund master id - * r.qid -> 51 - * r.cby -> loginId - * r.survey_id -> survey_id + * r.answer -> fund master id + * r.qid -> 51 + * r.cby -> loginId + * r.survey_id -> survey_id * * surveyor_location_details: - * loginId -> r.cby - * survey_id -> r.survey_id - * shelter_id -> shelter + * loginId -> r.cby + * survey_id -> r.survey_id + * shelter_id -> shelter */ String sql = """ - SELECT + SELECT - am.id AS fund_id, - am.answer AS fund_type, + am.id AS fund_id, + am.answer AS fund_type, - r.id AS response_id, - r.qid, - r.cby, - r.survey_id, - - s.loginId, - s.survey_id AS location_survey_id, - s.shelter_id + r.id AS response_id, + r.qid, + r.cby, + r.survey_id, - FROM night_shelter_inspection_response r + s.loginId, + s.survey_id AS location_survey_id, + s.shelter_id - INNER JOIN night_shelter_inspection_answer_master am - ON CAST(r.answer AS UNSIGNED) = am.id - AND am.qid = 51 - AND am.isactive = 1 - AND am.isdelete = 0 + FROM night_shelter_inspection_response r - INNER JOIN surveyor_location_details s - ON s.loginId = r.cby - AND s.survey_id = r.survey_id + INNER JOIN night_shelter_inspection_answer_master am + ON CAST(r.answer AS UNSIGNED) = am.id + AND am.qid = 51 + AND am.isactive = 1 + AND am.isdelete = 0 - WHERE r.qid = 51 - AND r.isactive = 1 - AND r.isdelete = 0 - """ - + dateCondition + - """ + INNER JOIN surveyor_location_details s + ON s.loginId = r.cby + AND s.survey_id = r.survey_id - ORDER BY am.orderby - """; + WHERE r.qid = 51 + AND r.isactive = 1 + AND r.isdelete = 0 + """ + + dateCondition + + """ + ORDER BY am.orderby + """; - /* Execute query */ + /* Execute query */ List> dbList; if (dateCondition.isEmpty()) { @@ -503,33 +487,30 @@ public class ShelterInspectionService { dbList = jdbcTemplate.queryForList(sql, value); } - /* Group by Fund Type */ Map> fundMap = new LinkedHashMap<>(); - for (Map row : dbList) { String fundId = String.valueOf(row.get("fund_id")); String fundType = String.valueOf(row.get("fund_type")); String shelterId = String.valueOf(row.get("shelter_id")); - //Create fund group - - Map fundRow =fundMap.computeIfAbsent( - fundId, k -> { + // Create fund group - Map map = new LinkedHashMap<>(); + Map fundRow = fundMap.computeIfAbsent( + fundId, k -> { - map.put("fund_id", fundId); - map.put("fund_type", fundType); - map.put("total", 0); + Map map = new LinkedHashMap<>(); - map.put("shelters", new ArrayList>()); + map.put("fund_id", fundId); + map.put("fund_type", fundType); + map.put("total", 0); - return map; - }); + map.put("shelters", new ArrayList>()); + return map; + }); /* * Existing total + 1 @@ -540,28 +521,22 @@ public class ShelterInspectionService { int total = ((Number) fundRow.get("total")).intValue(); fundRow.put("total", total + 1); - - /*Shelter list*/ + /* Shelter list */ @SuppressWarnings("unchecked") - List> shelters = - (List>) - fundRow.get("shelters"); - + List> shelters = (List>) fundRow.get("shelters"); // Avoid duplicate shelter in same fund type - - boolean alreadyExists = - shelters.stream() - .anyMatch( - x -> shelterId.equals( - String.valueOf( - x.get("shelter_id")))); + boolean alreadyExists = shelters.stream() + .anyMatch( + x -> shelterId.equals( + String.valueOf( + x.get("shelter_id")))); if (!alreadyExists) { Map apiShelter = shelterApiMap.get(shelterId); - + Map shelter = new LinkedHashMap<>(); shelter.put("shelter_id", shelterId); @@ -579,76 +554,71 @@ public class ShelterInspectionService { shelter.put("shelter_name", "-"); shelter.put("zone", "-"); shelter.put("division", "-"); - shelter.put( "shelter_type", "-"); - shelter.put("category", "-"); + shelter.put("shelter_type", "-"); + shelter.put("category", "-"); } - // Add DB details also - + shelter.put("qid", row.get("qid")); shelter.put("cby", row.get("cby")); shelter.put("survey_id", row.get("survey_id")); shelter.put("loginId", row.get("loginId")); shelter.put("location_survey_id", row.get("location_survey_id")); - shelters.add(shelter); } } - return new ArrayList<>(fundMap.values()); } - - - //Monthly Trends - + // Monthly Trends + public List> getMonthlyTrends(String type, String value) { StringBuilder sql = new StringBuilder(); sql.append(""" - SELECT - - MONTH(cdate) AS month_no, - - DATE_FORMAT(cdate,'%b') AS month, - - COALESCE(SUM( - CASE - WHEN qid = 4 - THEN CAST(answer AS UNSIGNED) - END - ),0) AS residents, - - COALESCE(SUM( - CASE - WHEN qid = 16 - THEN CAST(answer AS UNSIGNED) - END - ),0) AS rehabilitated, - - COALESCE(SUM( - CASE - WHEN qid = 17 - THEN CAST(answer AS UNSIGNED) - END - ),0) AS reintegration, - - COALESCE(SUM( - CASE - WHEN qid = 10 - THEN CAST(answer AS UNSIGNED) - END - ),0) AS death - - FROM night_shelter_inspection_response - - WHERE isactive = 1 - AND isdelete = 0 - """); + SELECT + + MONTH(cdate) AS month_no, + + DATE_FORMAT(cdate,'%b') AS month, + + COALESCE(SUM( + CASE + WHEN qid = 4 + THEN CAST(answer AS UNSIGNED) + END + ),0) AS residents, + + COALESCE(SUM( + CASE + WHEN qid = 16 + THEN CAST(answer AS UNSIGNED) + END + ),0) AS rehabilitated, + + COALESCE(SUM( + CASE + WHEN qid = 17 + THEN CAST(answer AS UNSIGNED) + END + ),0) AS reintegration, + + COALESCE(SUM( + CASE + WHEN qid = 10 + THEN CAST(answer AS UNSIGNED) + END + ),0) AS death + + FROM night_shelter_inspection_response + + WHERE isactive = 1 + AND isdelete = 0 + """); List params = new ArrayList<>(); @@ -670,21 +640,21 @@ public class ShelterInspectionService { } sql.append(""" - GROUP BY - MONTH(cdate), - DATE_FORMAT(cdate,'%b') + GROUP BY + MONTH(cdate), + DATE_FORMAT(cdate,'%b') - ORDER BY - MONTH(cdate) - """); + ORDER BY + MONTH(cdate) + """); - List> list; + List> list; - if(params.isEmpty()){ + if (params.isEmpty()) { list = jdbcTemplate.queryForList(sql.toString()); - }else{ + } else { list = jdbcTemplate.queryForList( sql.toString(), @@ -692,13 +662,12 @@ public class ShelterInspectionService { } - for(Map row : list){ + for (Map row : list) { - int totalCount = - ((Number)row.get("residents")).intValue() - + ((Number)row.get("rehabilitated")).intValue() - + ((Number)row.get("reintegration")).intValue() - + ((Number)row.get("death")).intValue(); + int totalCount = ((Number) row.get("residents")).intValue() + + ((Number) row.get("rehabilitated")).intValue() + + ((Number) row.get("reintegration")).intValue() + + ((Number) row.get("death")).intValue(); row.put("total_count", totalCount); @@ -707,32 +676,31 @@ public class ShelterInspectionService { return list; } - - - //CDO Week Visit - + + // CDO Week Visit + public List> getWeeklyVisits(String type, String value) { StringBuilder sql = new StringBuilder(); sql.append(""" - SELECT + SELECT - DAYOFWEEK(sld.cdate) AS day_no, + DAYOFWEEK(sld.cdate) AS day_no, - DATE_FORMAT(sld.cdate,'%a') AS day_name, + DATE_FORMAT(sld.cdate,'%a') AS day_name, - COUNT(DISTINCT sld.loginId) AS visits + COUNT(DISTINCT sld.loginId) AS visits - FROM surveyor_location_details sld + FROM surveyor_location_details sld - INNER JOIN night_shelter_inspection_response nr - ON nr.cby = sld.loginId + INNER JOIN night_shelter_inspection_response nr + ON nr.cby = sld.loginId - WHERE - sld.is_active = 1 - AND sld.is_delete = 0 - """); + WHERE + sld.is_active = 1 + AND sld.is_delete = 0 + """); List params = new ArrayList<>(); @@ -759,15 +727,15 @@ public class ShelterInspectionService { } sql.append(""" - GROUP BY - DAYOFWEEK(sld.cdate), - DATE_FORMAT(sld.cdate,'%a') + GROUP BY + DAYOFWEEK(sld.cdate), + DATE_FORMAT(sld.cdate,'%a') - ORDER BY - DAYOFWEEK(sld.cdate) - """); + ORDER BY + DAYOFWEEK(sld.cdate) + """); - if(params.isEmpty()){ + if (params.isEmpty()) { return jdbcTemplate.queryForList(sql.toString()); @@ -778,21 +746,19 @@ public class ShelterInspectionService { params.toArray()); } - - - + // Latest Visit Shelter - + public List> getLatestVisitShelters( String type, String value) { - ResponseEntity>> response = - restTemplate.exchange( - API_URL, - HttpMethod.GET, - null, - new ParameterizedTypeReference>>() {}); + ResponseEntity>> response = restTemplate.exchange( + API_URL, + HttpMethod.GET, + null, + new ParameterizedTypeReference>>() { + }); List> apiList = response.getBody(); @@ -809,24 +775,24 @@ public class ShelterInspectionService { StringBuilder sql = new StringBuilder(); sql.append(""" - SELECT + SELECT - ld.user_name, + ld.user_name, - MAX(r.cdate) AS visit_date + MAX(r.cdate) AS visit_date - FROM night_shelter_inspection_response r + FROM night_shelter_inspection_response r - INNER JOIN surveyor_location_details s - ON r.cby = s.loginId AND r.survey_id = s.survey_id + INNER JOIN surveyor_location_details s + ON r.cby = s.loginId AND r.survey_id = s.survey_id - INNER JOIN gcc_children_survey.login_details ld - ON ld.uid = s.loginId + INNER JOIN gcc_mobile_menus.login_details ld + ON ld.uid = s.loginId - WHERE r.isactive = 1 - AND r.isdelete = 0 - AND s.shelter_id = ? - """); + WHERE r.isactive = 1 + AND r.isdelete = 0 + AND s.shelter_id = ? + """); List params = new ArrayList<>(); params.add(uqid); @@ -849,29 +815,27 @@ public class ShelterInspectionService { } sql.append(""" - GROUP BY - ld.user_name + GROUP BY + ld.user_name - ORDER BY - visit_date DESC + ORDER BY + visit_date DESC - LIMIT 1 - """); + LIMIT 1 + """); - List> list = - jdbcTemplate.queryForList( - sql.toString(), - params.toArray()); + List> list = jdbcTemplate.queryForList( + sql.toString(), + params.toArray()); - Map row = new LinkedHashMap<>(); + Map row = new LinkedHashMap<>(); row.put("shelterName", shelter.get("shelter_name")); row.put("zone", shelter.get("zone")); - if(!list.isEmpty()){ + if (!list.isEmpty()) { - LocalDateTime visit = - (LocalDateTime) list.get(0).get("visit_date"); + LocalDateTime visit = (LocalDateTime) list.get(0).get("visit_date"); row.put("userName", list.get(0).get("user_name")); @@ -886,12 +850,12 @@ public class ShelterInspectionService { visit.format( DateTimeFormatter.ofPattern("hh:mm a"))); - }else{ + } else { - row.put("userName","-"); - row.put("visitDate",null); - row.put("date","-"); - row.put("time","-"); + row.put("userName", "-"); + row.put("visitDate", null); + row.put("date", "-"); + row.put("time", "-"); } @@ -899,18 +863,18 @@ public class ShelterInspectionService { } - result.sort((a,b)->{ + result.sort((a, b) -> { - LocalDateTime t1=(LocalDateTime)a.get("visitDate"); - LocalDateTime t2=(LocalDateTime)b.get("visitDate"); + LocalDateTime t1 = (LocalDateTime) a.get("visitDate"); + LocalDateTime t2 = (LocalDateTime) b.get("visitDate"); - if(t1==null && t2==null) + if (t1 == null && t2 == null) return 0; - if(t1==null) + if (t1 == null) return 1; - if(t2==null) + if (t2 == null) return -1; return t2.compareTo(t1); @@ -922,18 +886,17 @@ public class ShelterInspectionService { .collect(Collectors.toList()); } - - + // SHelter List - + public List> getShelterList(String zone, String category) { - ResponseEntity>> response = - restTemplate.exchange( - API_URL, - HttpMethod.GET, - null, - new ParameterizedTypeReference>>() {}); + ResponseEntity>> response = restTemplate.exchange( + API_URL, + HttpMethod.GET, + null, + new ParameterizedTypeReference>>() { + }); List> apiList = response.getBody(); @@ -963,25 +926,24 @@ public class ShelterInspectionService { // Total Residents String residentSql = """ - SELECT - COALESCE(SUM( - CASE - WHEN r.qid = 4 - THEN CAST(r.answer AS UNSIGNED) - ELSE 0 - END - ),0) - FROM surveyor_location_details s - - LEFT JOIN night_shelter_inspection_response r - ON r.survey_id = s.survey_id - AND r.cby = s.loginId - AND r.isactive = 1 - AND r.isdelete = 0 - - WHERE s.shelter_id = ? - """; - + SELECT + COALESCE(SUM( + CASE + WHEN r.qid = 4 + THEN CAST(r.answer AS UNSIGNED) + ELSE 0 + END + ),0) + FROM surveyor_location_details s + + LEFT JOIN night_shelter_inspection_response r + ON r.survey_id = s.survey_id + AND r.cby = s.loginId + AND r.isactive = 1 + AND r.isdelete = 0 + + WHERE s.shelter_id = ? + """; Integer residents = jdbcTemplate.queryForObject( residentSql, @@ -990,18 +952,17 @@ public class ShelterInspectionService { // Latest Visit String latestVisitSql = """ - SELECT MAX(r.cdate) - FROM surveyor_location_details s + SELECT MAX(r.cdate) + FROM surveyor_location_details s - LEFT JOIN night_shelter_inspection_response r - ON r.survey_id = s.survey_id - AND r.cby = s.loginId - AND r.isactive = 1 - AND r.isdelete = 0 + LEFT JOIN night_shelter_inspection_response r + ON r.survey_id = s.survey_id + AND r.cby = s.loginId + AND r.isactive = 1 + AND r.isdelete = 0 - WHERE s.shelter_id = ? - """; - + WHERE s.shelter_id = ? + """; LocalDateTime visitDate = jdbcTemplate.queryForObject( latestVisitSql, @@ -1023,47 +984,45 @@ public class ShelterInspectionService { DateTimeFormatter.ofPattern("dd/MM/yyyy")); } } - - - // Maintenance Status - String maintenanceSql = """ - SELECT - SUM(CASE WHEN CAST(r.answer AS UNSIGNED) = 1 THEN 1 ELSE 0 END) AS answer1_count, - SUM(CASE WHEN CAST(r.answer AS UNSIGNED) = 2 THEN 1 ELSE 0 END) AS answer2_count, - SUM(CASE WHEN CAST(r.answer AS UNSIGNED) = 3 THEN 1 ELSE 0 END) AS answer3_count, - SUM(CASE WHEN CAST(r.answer AS UNSIGNED) = 4 THEN 1 ELSE 0 END) AS answer4_count, - - MAX(CASE WHEN m.id = 1 THEN m.english_name END) AS well_maintained, - MAX(CASE WHEN m.id = 2 THEN m.english_name END) AS averagely_maintained, - MAX(CASE WHEN m.id = 3 THEN m.english_name END) AS poorly_maintained, - MAX(CASE WHEN m.id = 4 THEN m.english_name END) AS extremely_bad - - FROM surveyor_location_details s - - LEFT JOIN night_shelter_inspection_response r - ON r.survey_id = s.survey_id - AND r.cby = s.loginId - AND r.isactive = 1 - AND r.isdelete = 0 - AND r.qid BETWEEN 53 AND 58 - - LEFT JOIN night_shelter_inspection_questions_master q - ON q.qid = r.qid - AND q.master_table_name = 'shelter_maintenance_master' - AND q.isactive = 1 - AND q.isdelete = 0 - LEFT JOIN shelter_maintenance_master m - ON m.id = CAST(r.answer AS UNSIGNED) - AND m.isactive = 1 - AND m.isdelete = 0 - - WHERE s.shelter_id = ? - """; + // Maintenance Status + String maintenanceSql = """ + SELECT + SUM(CASE WHEN CAST(r.answer AS UNSIGNED) = 1 THEN 1 ELSE 0 END) AS answer1_count, + SUM(CASE WHEN CAST(r.answer AS UNSIGNED) = 2 THEN 1 ELSE 0 END) AS answer2_count, + SUM(CASE WHEN CAST(r.answer AS UNSIGNED) = 3 THEN 1 ELSE 0 END) AS answer3_count, + SUM(CASE WHEN CAST(r.answer AS UNSIGNED) = 4 THEN 1 ELSE 0 END) AS answer4_count, + + MAX(CASE WHEN m.id = 1 THEN m.english_name END) AS well_maintained, + MAX(CASE WHEN m.id = 2 THEN m.english_name END) AS averagely_maintained, + MAX(CASE WHEN m.id = 3 THEN m.english_name END) AS poorly_maintained, + MAX(CASE WHEN m.id = 4 THEN m.english_name END) AS extremely_bad + + FROM surveyor_location_details s + + LEFT JOIN night_shelter_inspection_response r + ON r.survey_id = s.survey_id + AND r.cby = s.loginId + AND r.isactive = 1 + AND r.isdelete = 0 + AND r.qid BETWEEN 53 AND 58 + + LEFT JOIN night_shelter_inspection_questions_master q + ON q.qid = r.qid + AND q.master_table_name = 'shelter_maintenance_master' + AND q.isactive = 1 + AND q.isdelete = 0 + + LEFT JOIN shelter_maintenance_master m + ON m.id = CAST(r.answer AS UNSIGNED) + AND m.isactive = 1 + AND m.isdelete = 0 + + WHERE s.shelter_id = ? + """; Map maintenanceData = jdbcTemplate.queryForMap(maintenanceSql, uqid); - int answer1Count = maintenanceData.get("answer1_count") == null ? 0 : ((Number) maintenanceData.get("answer1_count")).intValue(); @@ -1084,14 +1043,11 @@ public class ShelterInspectionService { if (answer1Count >= 4) { maintenanceStatus = "Well maintained"; - } - else if (answer2Count >= 3) { + } else if (answer2Count >= 3) { maintenanceStatus = "Averagely maintained"; - } - else if (answer3Count >= 2) { + } else if (answer3Count >= 2) { maintenanceStatus = "Poorly maintained"; - } - else if (answer4Count >= 2) { + } else if (answer4Count >= 2) { maintenanceStatus = "Extremely bad condition"; } @@ -1103,7 +1059,7 @@ public class ShelterInspectionService { row.put("category", shelter.get("category")); row.put("totalResidents", residents == null ? 0 : residents); row.put("latestVisit", latestVisit); - + // NEW row.put("maintenanceStatus", maintenanceStatus); @@ -1116,106 +1072,103 @@ public class ShelterInspectionService { return result; } - - - - //Visit List - public List> getVisitList(String fromDate, String toDate, String zone, String cdo) { - - ResponseEntity>> response = - restTemplate.exchange( - API_URL, - HttpMethod.GET, - null, - new ParameterizedTypeReference>>() { - }); - - List> apiShelters = response.getBody(); - - if(apiShelters==null){ + + // Visit List + public List> getVisitList(String fromDate, String toDate, String zone, String cdo) { + + ResponseEntity>> response = restTemplate.exchange( + API_URL, + HttpMethod.GET, + null, + new ParameterizedTypeReference>>() { + }); + + List> apiShelters = response.getBody(); + + if (apiShelters == null) { return Collections.emptyList(); } - + StringBuilder sql = new StringBuilder(); - + sql.append(""" - SELECT - r.survey_id, - s.shelter_id, - - ld.uid, - ld.user_name, - MAX(r.cdate) AS visitDate, - - MAX( - CASE - WHEN r.qid = 19 AND r.answer = '19' THEN 'OK' - WHEN r.qid = 19 AND r.answer = '20' THEN 'Issue' - END - ) AS food, - - CONCAT( - MAX( - CASE - WHEN r.qid = 3 - THEN LENGTH(r.answer) - LENGTH(REPLACE(r.answer, ',', '')) + 1 - END - ), - '/5' - ) AS staff, - - SUM( - CASE - WHEN r.qid BETWEEN 53 AND 58 - AND CAST(r.answer AS UNSIGNED) = 1 - THEN 1 - ELSE 0 - END - ) AS maintenance_1, - - - SUM( - CASE - WHEN r.qid BETWEEN 53 AND 58 - AND CAST(r.answer AS UNSIGNED) = 2 - THEN 1 - ELSE 0 - END - ) AS maintenance_2, - - - SUM( - CASE - WHEN r.qid BETWEEN 53 AND 58 - AND CAST(r.answer AS UNSIGNED) = 3 - THEN 1 - ELSE 0 - END - ) AS maintenance_3, - - - SUM( - CASE - WHEN r.qid BETWEEN 53 AND 58 - AND CAST(r.answer AS UNSIGNED) = 4 - THEN 1 - ELSE 0 - END - ) AS maintenance_4 - - - FROM night_shelter_inspection_response r - - INNER JOIN surveyor_location_details s - ON r.cby = s.loginId - AND r.survey_id = s.survey_id - - INNER JOIN gcc_children_survey.login_details ld - ON ld.uid = s.loginId - - WHERE r.isactive = 1 - AND r.isdelete = 0 - """); + SELECT + r.survey_id, + s.shelter_id, + + ld.uid, + ld.user_name, + MAX(r.cdate) AS visitDate, + + MAX( + CASE + WHEN r.qid = 19 AND r.answer = '19' THEN 'OK' + WHEN r.qid = 19 AND r.answer = '20' THEN 'Issue' + END + ) AS food, + + CONCAT( + MAX( + CASE + WHEN r.qid = 3 + THEN LENGTH(r.answer) - LENGTH(REPLACE(r.answer, ',', '')) + 1 + END + ), + '/5' + ) AS staff, + + SUM( + CASE + WHEN r.qid BETWEEN 53 AND 58 + AND CAST(r.answer AS UNSIGNED) = 1 + THEN 1 + ELSE 0 + END + ) AS maintenance_1, + + + SUM( + CASE + WHEN r.qid BETWEEN 53 AND 58 + AND CAST(r.answer AS UNSIGNED) = 2 + THEN 1 + ELSE 0 + END + ) AS maintenance_2, + + + SUM( + CASE + WHEN r.qid BETWEEN 53 AND 58 + AND CAST(r.answer AS UNSIGNED) = 3 + THEN 1 + ELSE 0 + END + ) AS maintenance_3, + + + SUM( + CASE + WHEN r.qid BETWEEN 53 AND 58 + AND CAST(r.answer AS UNSIGNED) = 4 + THEN 1 + ELSE 0 + END + ) AS maintenance_4 + + + FROM night_shelter_inspection_response r + + INNER JOIN surveyor_location_details s + ON r.cby = s.loginId + AND r.survey_id = s.survey_id + + INNER JOIN gcc_mobile_menus.login_details ld + ON ld.uid = s.loginId + + WHERE r.isactive = 1 + AND r.isdelete = 0 + """); List params = new ArrayList<>(); @@ -1233,47 +1186,44 @@ public class ShelterInspectionService { sql.append(" AND ld.uid = ? "); params.add(Integer.parseInt(cdo)); } - + sql.append(""" - GROUP BY - r.survey_id, - s.shelter_id, - ld.uid, - ld.user_name + GROUP BY + r.survey_id, + s.shelter_id, + ld.uid, + ld.user_name - ORDER BY - visitDate DESC - """); - + ORDER BY + visitDate DESC + """); - List> dbList= - jdbcTemplate.queryForList(sql.toString(),params.toArray()); + List> dbList = jdbcTemplate.queryForList(sql.toString(), params.toArray()); - List> result=new ArrayList<>(); + List> result = new ArrayList<>(); - for(Map db:dbList){ + for (Map db : dbList) { - String shelterId=String.valueOf(db.get("shelter_id")); + String shelterId = String.valueOf(db.get("shelter_id")); - Map api=apiShelters.stream() + Map api = apiShelters.stream() - .filter(x->shelterId.equals(String.valueOf(x.get("uqid")))) + .filter(x -> shelterId.equals(String.valueOf(x.get("uqid")))) .findFirst() .orElse(null); - if(api==null) + if (api == null) continue; - String apiZone = String.valueOf(api.get("zone")); if (zone != null && !zone.isBlank() && !zone.equalsIgnoreCase(apiZone)) { continue; } - + LocalDateTime dt = (LocalDateTime) db.get("visitDate"); String date = "-"; @@ -1283,47 +1233,35 @@ public class ShelterInspectionService { date = dt.format(DateTimeFormatter.ofPattern("dd/MM/yyyy")); time = dt.format(DateTimeFormatter.ofPattern("hh:mm a")); } - + // ========================= // MAINTENANCE COUNTS // ========================= - int maintenance1 = - db.get("maintenance_1") == null - ? 0 - : ((Number) - db.get("maintenance_1")) - .intValue(); - - - int maintenance2 = - db.get("maintenance_2") == null - ? 0 - : ((Number) - db.get("maintenance_2")) - .intValue(); - - - int maintenance3 = - db.get("maintenance_3") == null - ? 0 - : ((Number) - db.get("maintenance_3")) - .intValue(); + int maintenance1 = db.get("maintenance_1") == null + ? 0 + : ((Number) db.get("maintenance_1")) + .intValue(); + int maintenance2 = db.get("maintenance_2") == null + ? 0 + : ((Number) db.get("maintenance_2")) + .intValue(); - int maintenance4 = - db.get("maintenance_4") == null - ? 0 - : ((Number) - db.get("maintenance_4")) - .intValue(); + int maintenance3 = db.get("maintenance_3") == null + ? 0 + : ((Number) db.get("maintenance_3")) + .intValue(); + int maintenance4 = db.get("maintenance_4") == null + ? 0 + : ((Number) db.get("maintenance_4")) + .intValue(); // ========================= // MAINTENANCE STATUS // ========================= - + String maintenanceStatus = "Not Available"; if (maintenance1 >= 4) { @@ -1336,21 +1274,20 @@ public class ShelterInspectionService { maintenanceStatus = "Extremely bad condition"; } + Map row = new LinkedHashMap<>(); - Map row=new LinkedHashMap<>(); - row.put("surveyId", db.get("survey_id")); - row.put("shelterName",api.get("shelter_name")); - row.put("zone",api.get("zone")); - row.put("userName",db.get("user_name")); - + row.put("shelterName", api.get("shelter_name")); + row.put("zone", api.get("zone")); + row.put("userName", db.get("user_name")); + row.put("date", date); row.put("time", time); - row.put("food",db.get("food")); - row.put("staff",db.get("staff")); - + row.put("food", db.get("food")); + row.put("staff", db.get("staff")); + row.put("maintenanceStatus", maintenanceStatus); result.add(row); @@ -1360,18 +1297,17 @@ public class ShelterInspectionService { return result; } - - + // Visit Details for modal box - + public Map getVisitDetails(String surveyId) { - ResponseEntity>> response = - restTemplate.exchange( - API_URL, - HttpMethod.GET, - null, - new ParameterizedTypeReference>>() {}); + ResponseEntity>> response = restTemplate.exchange( + API_URL, + HttpMethod.GET, + null, + new ParameterizedTypeReference>>() { + }); List> apiShelters = response.getBody(); @@ -1392,7 +1328,7 @@ public class ShelterInspectionService { INNER JOIN surveyor_location_details s ON s.loginId = r.cby - INNER JOIN gcc_children_survey.login_details ld + INNER JOIN gcc_mobile_menus.login_details ld ON ld.uid = s.loginId WHERE r.survey_id = ? @@ -1400,39 +1336,37 @@ public class ShelterInspectionService { ORDER BY r.qid """; - List> list = - jdbcTemplate.queryForList(sql, surveyId); + List> list = jdbcTemplate.queryForList(sql, surveyId); - if(list.isEmpty()){ + if (list.isEmpty()) { return Collections.emptyMap(); } - Map first = list.get(0); + Map first = list.get(0); String shelterId = String.valueOf(first.get("shelter_id")); - Map api = apiShelters.stream() + Map api = apiShelters.stream() .filter(x -> shelterId.equals(String.valueOf(x.get("uqid")))) .findFirst() .orElse(null); - Map answers = new LinkedHashMap<>(); + Map answers = new LinkedHashMap<>(); - for(Map row : list){ + for (Map row : list) { answers.put( String.valueOf(row.get("qid")), - String.valueOf(row.get("answer")) - ); + String.valueOf(row.get("answer"))); } LocalDateTime dateTime = (LocalDateTime) first.get("cdate"); - Map result = new LinkedHashMap<>(); + Map result = new LinkedHashMap<>(); - result.put("surveyId",surveyId); + result.put("surveyId", surveyId); - result.put("userName",first.get("user_name")); + result.put("userName", first.get("user_name")); result.put("date", dateTime.format( @@ -1442,32 +1376,26 @@ public class ShelterInspectionService { dateTime.format( DateTimeFormatter.ofPattern("hh:mm a"))); - if(api!=null){ - - result.put("region",api.get("region")); - result.put("zone",api.get("zone")); - result.put("division",api.get("division")); - result.put("uqid",api.get("uqid")); - result.put("shelterName",api.get("shelter_name")); - result.put("category",api.get("category")); - result.put("shelterType",api.get("shelter_type")); - result.put("latitude",api.get("latitude")); - result.put("longitude",api.get("longitude")); - result.put("address",api.get("address")); - result.put("image",api.get("image")); - - + if (api != null) { + + result.put("region", api.get("region")); + result.put("zone", api.get("zone")); + result.put("division", api.get("division")); + result.put("uqid", api.get("uqid")); + result.put("shelterName", api.get("shelter_name")); + result.put("category", api.get("category")); + result.put("shelterType", api.get("shelter_type")); + result.put("latitude", api.get("latitude")); + result.put("longitude", api.get("longitude")); + result.put("address", api.get("address")); + result.put("image", api.get("image")); } - result.put("answers",answers); + result.put("answers", answers); return result; } - - - - } diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 2f562e6..5c2b240 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -1,5 +1,5 @@ -#spring.profiles.active=local -spring.profiles.active=production +spring.profiles.active=local +#spring.profiles.active=production #For Server production #assets.base-url=/gcc/assets/ #For local test