From 32d8dcb2b605504349b40e33832c7cfe881c645a Mon Sep 17 00:00:00 2001 From: Dizho A Date: Tue, 11 Aug 2026 10:37:35 +0530 Subject: [PATCH] first commit at shelter inspection project --- .classpath | 8 + .../configuration/DataSourceConfig.java | 12 + .../Controller/MainController.java | 33 + .../ShelterInspectionController.java | 207 ++ .../Service/MasterShelterService.java | 139 + .../Service/ShelterInspectionService.java | 1783 +++++++++++ .../modules/shelterinspection/dashboard.html | 2817 +++++++++++++++++ .../shelterinspection/shelterlist.html | 433 +++ .../modules/shelterinspection/visitlist.html | 1673 ++++++++++ 9 files changed, 7105 insertions(+) create mode 100644 src/main/java/in/gov/chennaicorporation/gccoffice/shelterinspection/Controller/MainController.java create mode 100644 src/main/java/in/gov/chennaicorporation/gccoffice/shelterinspection/Controller/ShelterInspectionController.java create mode 100644 src/main/java/in/gov/chennaicorporation/gccoffice/shelterinspection/Service/MasterShelterService.java create mode 100644 src/main/java/in/gov/chennaicorporation/gccoffice/shelterinspection/Service/ShelterInspectionService.java create mode 100644 src/main/resources/templates/modules/shelterinspection/dashboard.html create mode 100644 src/main/resources/templates/modules/shelterinspection/shelterlist.html create mode 100644 src/main/resources/templates/modules/shelterinspection/visitlist.html diff --git a/.classpath b/.classpath index 83a3b6f..bc93d16 100644 --- a/.classpath +++ b/.classpath @@ -9,6 +9,7 @@ + @@ -28,5 +29,12 @@ + + + + + + + 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 6da1f44..fa02e69 100644 --- a/src/main/java/in/gov/chennaicorporation/gccoffice/configuration/DataSourceConfig.java +++ b/src/main/java/in/gov/chennaicorporation/gccoffice/configuration/DataSourceConfig.java @@ -441,6 +441,18 @@ public class DataSourceConfig { dataSource.setPassword(dbpassword); return dataSource; } + + /////////////// Shelter Inspection ///////////////////// + @Bean(name = "mysqlShelterinspectionDataSource") + public DataSource mysqlShelterinspectionDataSource() { + DriverManagerDataSource dataSource = new DriverManagerDataSource(); + dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver"); + dataSource.setUrl("jdbc:mysql://" + host + "/gcc_night_shelter_inspection"); + dataSource.setUsername("root"); + dataSource.setPassword(dbpassword); + return dataSource; + } + ////////////////////////////// (For GCC USER) //////////////////////// @Bean(name = "mysqlGCCUserDataSource") diff --git a/src/main/java/in/gov/chennaicorporation/gccoffice/shelterinspection/Controller/MainController.java b/src/main/java/in/gov/chennaicorporation/gccoffice/shelterinspection/Controller/MainController.java new file mode 100644 index 0000000..be54f66 --- /dev/null +++ b/src/main/java/in/gov/chennaicorporation/gccoffice/shelterinspection/Controller/MainController.java @@ -0,0 +1,33 @@ +package in.gov.chennaicorporation.gccoffice.shelterinspection.Controller; + +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; + +@Controller("ShelterInspectionController") +@RequestMapping("/gcc/shelterinspection") +public class MainController { + + @GetMapping("/dashboard") + public String Dashboard() { + return "modules/shelterinspection/dashboard"; + } + + @GetMapping("/shelterlist") + public String ShelterList() { + return "modules/shelterinspection/shelterlist"; + } + + @GetMapping("/visitlist") + public String VisitList() { + return "modules/shelterinspection/visitlist"; + } + + @GetMapping("/complaintlist") + public String ComplaintList() { + return "modules/shelterinspection/complaintlist"; + } + + + +} diff --git a/src/main/java/in/gov/chennaicorporation/gccoffice/shelterinspection/Controller/ShelterInspectionController.java b/src/main/java/in/gov/chennaicorporation/gccoffice/shelterinspection/Controller/ShelterInspectionController.java new file mode 100644 index 0000000..ae90cfe --- /dev/null +++ b/src/main/java/in/gov/chennaicorporation/gccoffice/shelterinspection/Controller/ShelterInspectionController.java @@ -0,0 +1,207 @@ +package in.gov.chennaicorporation.gccoffice.shelterinspection.Controller; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.client.RestTemplate; + +import in.gov.chennaicorporation.gccoffice.shelterinspection.Service.MasterShelterService; +import in.gov.chennaicorporation.gccoffice.shelterinspection.Service.ShelterInspectionService; + + +@RestController +@RequestMapping("/gcc/api/shelterinspection") +public class ShelterInspectionController { + + @Autowired + private ShelterInspectionService shelterApiService; + + @Autowired + private MasterShelterService masterShelterService; + + private final RestTemplate restTemplate = new RestTemplate(); + + @GetMapping("/Sheltercount") + public ResponseEntity> getTotalCount() { + + int count = shelterApiService.getTotalShelterCount(); + + Map response = new HashMap<>(); + response.put("totalShelterCount", count); + + return ResponseEntity.ok(response); + } + + @GetMapping("/dashboard") + public Map dashboard( + @RequestParam String type, + @RequestParam String value){ + + return shelterApiService.getDashboardData(type,value); + } + + @GetMapping("/top5Shelters") + public List> top5Shelters( + @RequestParam String type, + @RequestParam String value){ + + return shelterApiService.getTop5Shelters(type,value); + } + + @GetMapping("/regionSummary") + public List> regionSummary( + @RequestParam String type, + @RequestParam String value){ + + return shelterApiService.getRegionSummary(type,value); + } + + @GetMapping("/fundReceivedSummary") + public List> fundSummary( + @RequestParam String type, + @RequestParam String value){ + + return shelterApiService.getFundReceivedSummary(type,value); + } + + @GetMapping("/fundReceivedShelterDetails") + public List> fundReceivedShelterDetails( + @RequestParam String type, + @RequestParam String value) { + + return shelterApiService.getFundReceivedShelterDetails( type, value); + } + + @GetMapping("/monthlyTrends") + public List> monthlyTrends( + @RequestParam String type, + @RequestParam String value){ + + return shelterApiService.getMonthlyTrends(type,value); + } + + @GetMapping("/weeklyVisits") + public List> weeklyVisits( + @RequestParam String type, + @RequestParam String value){ + + List> results = shelterApiService.getWeeklyVisits(type,value); + System.out.println("Weekly Visits Results:" +results); + + return results; + } + + @GetMapping("/latestVisitShelters") + public List> latestVisitShelters( + @RequestParam String type, + @RequestParam String value){ + + return shelterApiService.getLatestVisitShelters(type,value); + } + +// @GetMapping("/dashboard") +// public Map dashboard() { +// return shelterApiService.getDashboardData(); +// } + + +// @GetMapping("/top5Shelters") +// public List> getTop5Shelters() { +// return shelterApiService.getTop5Shelters(); +// } + +// @GetMapping("/regionSummary") +// public List> regionWiseSummary() { +// return shelterApiService.getRegionSummary(); +// } + +// @GetMapping("/fundReceivedSummary") +// public List> fundReceivedSummary() { +// return shelterApiService.getFundReceivedSummary(); +// } +// +// @GetMapping("/monthlyTrends") +// public List> monthlyTrends(){ +// return shelterApiService.getMonthlyTrends(); +// } +// +// @GetMapping("/weeklyVisits") +// public List> weeklyVisits(){ +// return shelterApiService.getWeeklyVisits(); +// } +// +// @GetMapping("/latestVisitShelters") +// public List> latestVisitShelters(){ +// return shelterApiService.getLatestVisitShelters(); +// } + + @GetMapping("/shelterList") + public List> shelterList( + @RequestParam(required = false) String zone, + @RequestParam(required = false) String category) { + return shelterApiService.getShelterList(zone, category); + + } + + @GetMapping("/visitList") + public List> visitList( + @RequestParam(required=false) String fromDate, + @RequestParam(required=false) String toDate, + @RequestParam(required=false) String zone, + @RequestParam(required=false) String cdo){ + + return shelterApiService.getVisitList( fromDate, toDate, zone, cdo); + } + + @GetMapping("/visitDetails") + public Map visitDetails( + @RequestParam String surveyId){ + + return shelterApiService.getVisitDetails(surveyId); + + } + + @GetMapping("/questions") + public ResponseEntity getQuestions() { + + return restTemplate.getForEntity( + "https://gccservices.in/gccofficialapp/api/shelterinspection/questions?cid=1", + Object.class + ); + + } + +// @GetMapping("/shelterList") +// public List> shelterList(){ +// return shelterApiService.getShelterList(); +// } + + @GetMapping("/categories") + public List getCategories() { + return masterShelterService.getCategoryList(); + } + + @GetMapping("/zones") + public List getZones() { + return masterShelterService.getZoneList(); + } + + @GetMapping("/cdos") + public List> getCdos() { + return masterShelterService.getCdos(); + } + + @GetMapping("/years") + public List getYears() { + return masterShelterService.getAvailableYears(); + } + + +} diff --git a/src/main/java/in/gov/chennaicorporation/gccoffice/shelterinspection/Service/MasterShelterService.java b/src/main/java/in/gov/chennaicorporation/gccoffice/shelterinspection/Service/MasterShelterService.java new file mode 100644 index 0000000..b7376f2 --- /dev/null +++ b/src/main/java/in/gov/chennaicorporation/gccoffice/shelterinspection/Service/MasterShelterService.java @@ -0,0 +1,139 @@ +package in.gov.chennaicorporation.gccoffice.shelterinspection.Service; + + +import java.util.Collections; +import java.util.Comparator; + +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.stream.Collectors; + +import javax.sql.DataSource; + + + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.core.ParameterizedTypeReference; +import org.springframework.http.HttpMethod; +import org.springframework.http.ResponseEntity; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.stereotype.Service; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.client.RestTemplate; + +@Service +public class MasterShelterService { + + @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/"; + + // Category Dropdown + + public List getCategoryList() { + + ResponseEntity>> response = + restTemplate.exchange( + API_URL, + HttpMethod.GET, + null, + new ParameterizedTypeReference>>() {}); + + List> apiList = response.getBody(); + + if (apiList == null) { + return Collections.emptyList(); + } + + return apiList.stream() + .map(x -> String.valueOf(x.get("category"))) + .filter(Objects::nonNull) + .distinct() + .sorted() + .collect(Collectors.toList()); + } + + + //Zone Dropdown + + public List getZoneList() { + + ResponseEntity>> response = + restTemplate.exchange( + API_URL, + HttpMethod.GET, + null, + new ParameterizedTypeReference>>() {}); + + List> apiList = response.getBody(); + + if (apiList == null) { + return Collections.emptyList(); + } + + return apiList.stream() + .map(x -> String.valueOf(x.get("zone"))) + .filter(Objects::nonNull) + .distinct() + .sorted(Comparator.comparingInt(z -> + Integer.parseInt(z.replaceAll("[^0-9]", "")))) + .collect(Collectors.toList()); + } + + + + + //CDO Dropdown + + public List> getCdos(){ + + String sql=""" + SELECT DISTINCT + uid, + user_name + FROM login_details + ORDER BY user_name + """; + + return jdbcTemplate.queryForList(sql); + + } + + + + + // Year Dropdown + + public List getAvailableYears() { + + String sql = """ + SELECT DISTINCT YEAR(cdate) AS year + FROM night_shelter_inspection_response + WHERE isactive = 1 + AND isdelete = 0 + ORDER BY YEAR(cdate) DESC + """; + + return jdbcTemplate.queryForList(sql, Integer.class); + } + + + + // SHelter List + + + + + +} 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 new file mode 100644 index 0000000..dccc473 --- /dev/null +++ b/src/main/java/in/gov/chennaicorporation/gccoffice/shelterinspection/Service/ShelterInspectionService.java @@ -0,0 +1,1783 @@ +package in.gov.chennaicorporation.gccoffice.shelterinspection.Service; + +import java.sql.Timestamp; +import java.time.LocalDateTime; +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + +import javax.sql.DataSource; + +import java.time.LocalDate; +import java.time.format.DateTimeFormatter; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.core.ParameterizedTypeReference; +import org.springframework.http.HttpMethod; +import org.springframework.http.ResponseEntity; +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); + } + + + 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>>() {} + ); + + List> shelters = response.getBody(); + + return shelters != null ? shelters.size() : 0; + } + + // Dashboard + // Dashboard + 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(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, + + 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 + AND qid IN (4,5,10,11,13,16,17,48) + """ + dateCondition; + + if (dateCondition.isEmpty()) { + response.putAll(jdbcTemplate.queryForMap(dashboardSql)); + } else { + response.putAll(jdbcTemplate.queryForMap(dashboardSql, value)); + } + + return response; + } + +// public Map getDashboardData() { +// +// Map response = new LinkedHashMap<>(); +// +// String dashboardSql = """ +// SELECT +// +// 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, +// +// 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 +// AND qid IN (4,5,10,11,13,16,17,48) +// """; +// +// response.putAll(jdbcTemplate.queryForMap(dashboardSql)); +// +// return response; +// } + + // Top 5 Shelter List + + public List> getTop5Shelters(String type, String value) { + + ResponseEntity>> response = + restTemplate.exchange( + API_URL, + HttpMethod.GET, + null, + new ParameterizedTypeReference>>() { + }); + + List> apiList = response.getBody(); + + if (apiList == null) { + return Collections.emptyList(); + } + + List> result = new ArrayList<>(); + + // Date filter + StringBuilder dateCondition = new StringBuilder(); + + if ("day".equalsIgnoreCase(type)) { + + dateCondition.append(" AND DATE(r.cdate) = ? "); + + } else if ("month".equalsIgnoreCase(type)) { + + dateCondition.append(" AND DATE_FORMAT(r.cdate,'%Y-%m') = ? "); + + } else if ("year".equalsIgnoreCase(type)) { + + dateCondition.append(" AND YEAR(r.cdate) = ? "); + + } + + for (Map shelter : apiList) { + + String uqid = String.valueOf(shelter.get("uqid")); + + String sql = """ + SELECT + COALESCE(SUM(CAST(r.answer AS UNSIGNED)),0) AS totalResidents + FROM night_shelter_inspection_response r + WHERE r.qid = 4 + AND r.isactive = 1 + AND r.isdelete = 0 + AND EXISTS ( + SELECT 1 + FROM surveyor_location_details s + WHERE s.loginId = r.cby + AND s.shelter_id = ? + ) + """ + dateCondition; + + Integer residents; + + if (dateCondition.length() == 0) { + + residents = jdbcTemplate.queryForObject( + sql, + Integer.class, + uqid); + + } else { + + residents = jdbcTemplate.queryForObject( + sql, + Integer.class, + uqid, + value); + + } + + Map row = new LinkedHashMap<>(); + + row.put("uqid", uqid); + row.put("shelterName", shelter.get("shelter_name")); + row.put("zone", shelter.get("zone")); + row.put("category", shelter.get("category")); + row.put("totalResidents", residents == null ? 0 : residents); + + result.add(row); + } + + result.sort((a, b) -> Integer.compare( + ((Number) b.get("totalResidents")).intValue(), + ((Number) a.get("totalResidents")).intValue())); + + return result.stream() + .limit(5) + .collect(Collectors.toList()); + } + + + +// public List> getTop5Shelters(String type, String value) { +// +// ResponseEntity>> response = +// restTemplate.exchange( +// API_URL, +// HttpMethod.GET, +// null, +// new ParameterizedTypeReference>>() { +// }); +// +// List> apiList = response.getBody(); +// +// if (apiList == null) { +// return Collections.emptyList(); +// } +// +// List> result = new ArrayList<>(); +// +// // Build date condition +// String dateCondition = ""; +// +// if ("day".equalsIgnoreCase(type)) { +// +// dateCondition = " AND DATE(r.cdate) = ? "; +// +// } else if ("month".equalsIgnoreCase(type)) { +// +// dateCondition = " AND DATE_FORMAT(r.cdate,'%Y-%m') = ? "; +// +// } else if ("year".equalsIgnoreCase(type)) { +// +// dateCondition = " AND YEAR(r.cdate) = ? "; +// +// } +// +// for (Map shelter : apiList) { +// +// String uqid = String.valueOf(shelter.get("uqid")); +// +// String sql = """ +// SELECT +// COALESCE(SUM(CAST(r.answer AS UNSIGNED)),0) +// FROM night_shelter_inspection_response r +// LEFT JOIN surveyor_location_details s +// ON r.cby = s.loginId +// WHERE r.qid = 4 +// AND r.isactive = 1 +// AND r.isdelete = 0 +// AND s.shelter_id = ? +// """ + dateCondition; +// +// Integer residents; +// +// if (dateCondition.isEmpty()) { +// +// residents = jdbcTemplate.queryForObject( +// sql, +// Integer.class, +// uqid); +// +// } else { +// +// residents = jdbcTemplate.queryForObject( +// sql, +// Integer.class, +// uqid, +// value); +// } +// +// Map row = new LinkedHashMap<>(); +// +// row.put("uqid", uqid); +// row.put("shelterName", shelter.get("shelter_name")); +// row.put("zone", shelter.get("zone")); +// row.put("category", shelter.get("category")); +// row.put("totalResidents", residents == null ? 0 : residents); +// +// result.add(row); +// } +// +// result.sort((a, b) -> Integer.compare( +// ((Number) b.get("totalResidents")).intValue(), +// ((Number) a.get("totalResidents")).intValue())); +// +// return result.stream() +// .limit(5) +// .collect(Collectors.toList()); +// } + + + +// public List> getTop5Shelters() { +// +// ResponseEntity>> response = +// restTemplate.exchange( +// API_URL, +// HttpMethod.GET, +// null, +// new ParameterizedTypeReference>>() { +// }); +// +// List> apiList = response.getBody(); +// +// if (apiList == null) { +// return Collections.emptyList(); +// } +// +// List> result = new ArrayList<>(); +// +// for (Map shelter : apiList) { +// +// String uqid = String.valueOf(shelter.get("uqid")); +// +// String sql = """ +// SELECT +// COALESCE(SUM(CAST(r.answer AS UNSIGNED)),0) +// FROM night_shelter_inspection_response r +// INNER JOIN surveyor_location_details s +// ON r.cby = s.loginId +// WHERE r.qid = 4 +// AND r.isactive = 1 +// AND r.isdelete = 0 +// AND s.shelter_id = ? +// """; +// +// Integer residents = jdbcTemplate.queryForObject( +// sql, +// Integer.class, +// uqid); +// +// Map row = new LinkedHashMap<>(); +// +// row.put("uqid", uqid); +// row.put("shelterName", shelter.get("shelter_name")); +// row.put("zone", shelter.get("zone")); +// row.put("category", shelter.get("category")); +// row.put("totalResidents", residents == null ? 0 : residents); +// +// result.add(row); +// } +// +// result.sort((a, b) -> Integer.compare( +// ((Number) b.get("totalResidents")).intValue(), +// ((Number) a.get("totalResidents")).intValue())); +// +// return result.stream().limit(5).collect(Collectors.toList()); +// } + + //Region Summary + + public List> getRegionSummary(String type, String value) { + + ResponseEntity>> response = + restTemplate.exchange( + API_URL, + HttpMethod.GET, + null, + new ParameterizedTypeReference>>() { + }); + + List> apiData = response.getBody(); + + if (apiData == null) { + return Collections.emptyList(); + } + + String dateCondition = ""; + + if ("day".equalsIgnoreCase(type)) { + dateCondition = " AND DATE(r.cdate)=? "; + } else if ("month".equalsIgnoreCase(type)) { + 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 + """); + + List params = new ArrayList<>(); + + if ("day".equalsIgnoreCase(type)) { + + sql.append(" WHERE DATE(r.cdate)=? "); + params.add(value); + + } else if ("month".equalsIgnoreCase(type)) { + + sql.append(" WHERE DATE_FORMAT(r.cdate,'%Y-%m')=? "); + params.add(value); + + } else if ("year".equalsIgnoreCase(type)) { + + sql.append(" WHERE YEAR(r.cdate)=? "); + params.add(value); + + } + + sql.append(""" + GROUP BY s.shelter_id + """); + + List> dbData; + + if(params.isEmpty()){ + + dbData = jdbcTemplate.queryForList(sql.toString()); + + }else{ + + dbData = jdbcTemplate.queryForList( + sql.toString(), + params.toArray()); + + } + + Map> dbMap = new HashMap<>(); + + for (Map row : dbData) { + dbMap.put( + String.valueOf(row.get("shelter_id")), + row); + } + + Map> regionMap = new LinkedHashMap<>(); + + for (Map api : apiData) { + + String uqid = String.valueOf(api.get("uqid")); + String region = String.valueOf(api.get("region")); + + Map db = dbMap.get(uqid); + + int residents = 0; + int visits = 0; + + if (db != null) { + residents = ((Number) db.get("totalResidents")).intValue(); + visits = ((Number) db.get("totalVisits")).intValue(); + } + + Map regionRow = + regionMap.computeIfAbsent(region, k -> { + + Map m = new LinkedHashMap<>(); + + m.put("region", region); + m.put("totalShelters", 0); + m.put("totalResidents", 0); + m.put("totalVisits", 0); + + return m; + + }); + + regionRow.put( + "totalShelters", + (Integer) regionRow.get("totalShelters") + 1); + + regionRow.put( + "totalResidents", + (Integer) regionRow.get("totalResidents") + residents); + + regionRow.put( + "totalVisits", + (Integer) regionRow.get("totalVisits") + visits); + + } + + return new ArrayList<>(regionMap.values()); + + } + +// public List> getRegionSummary() { +// +// ResponseEntity>> response = +// restTemplate.exchange( +// API_URL, +// HttpMethod.GET, +// null, +// new ParameterizedTypeReference>>() { +// }); +// +// List> apiData = response.getBody(); +// +// String sql = """ +// 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.cby) AS totalVisits +// +// FROM surveyor_location_details s +// +// LEFT JOIN night_shelter_inspection_response r +// ON s.loginId = r.cby +// AND r.isactive = 1 +// AND r.isdelete = 0 +// +// GROUP BY s.shelter_id +// """; +// +// List> dbData = +// jdbcTemplate.queryForList(sql); +// +// Map> dbMap = new HashMap<>(); +// +// for (Map row : dbData) { +// dbMap.put( +// String.valueOf(row.get("shelter_id")), +// row); +// } +// +// Map> regionMap = new LinkedHashMap<>(); +// +// for (Map api : apiData) { +// +// String uqid = String.valueOf(api.get("uqid")); +// +// String region = String.valueOf(api.get("region")); +// +// Map db = dbMap.get(uqid); +// +// int residents = 0; +// int visits = 0; +// +// if (db != null) { +// residents = ((Number) db.get("totalResidents")).intValue(); +// visits = ((Number) db.get("totalVisits")).intValue(); +// } +// +// Map regionRow = +// regionMap.computeIfAbsent(region, k -> { +// +// Map m = new LinkedHashMap(); +// +// m.put("region", region); +// m.put("totalShelters", 0); +// m.put("totalResidents", 0); +// m.put("totalVisits", 0); +// +// return m; +// }); +// +// regionRow.put( +// "totalShelters", +// (Integer) regionRow.get("totalShelters") + 1); +// +// regionRow.put( +// "totalResidents", +// (Integer) regionRow.get("totalResidents") + residents); +// +// regionRow.put( +// "totalVisits", +// (Integer) regionRow.get("totalVisits") + visits); +// +// } +// +// return new ArrayList<>(regionMap.values()); +// +// } + + + //Fund Received Summary + + public List> getFundReceivedSummary(String type, String value) { + + String dateCondition = ""; + + if ("day".equalsIgnoreCase(type)) { + + dateCondition = " AND DATE(r.cdate)=? "; + + } else if ("month".equalsIgnoreCase(type)) { + + dateCondition = " AND DATE_FORMAT(r.cdate,'%Y-%m')=? "; + + } else if ("year".equalsIgnoreCase(type)) { + + dateCondition = " AND YEAR(r.cdate)=? "; + + } + + String sql = """ + SELECT + + am.answer AS fund_type, + COUNT(r.id) AS total + + 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 + + """ + + WHERE + am.qid=51 + AND am.isactive=1 + AND am.isdelete=0 + + GROUP BY + am.id, + am.answer + + ORDER BY + am.orderby + + """; + + if (dateCondition.isEmpty()) { + + return jdbcTemplate.queryForList(sql); + + } else { + + return jdbcTemplate.queryForList(sql, value); + + } + + } + + + // 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>>() { + }); + + List> apiList = response.getBody(); + + if (apiList == null) { + apiList = Collections.emptyList(); + } + + /* Create API shelter map using uqid */ + Map> shelterApiMap = new HashMap<>(); + + for (Map shelter : apiList) { + String uqid = String.valueOf( shelter.get("uqid")); + shelterApiMap.put(uqid, shelter); + } + + + /* Date filter */ + String dateCondition = ""; + + if ("day".equalsIgnoreCase(type)) { + dateCondition = " AND DATE(r.cdate) = ? "; + } else if ("month".equalsIgnoreCase(type)) { + dateCondition = " AND DATE_FORMAT(r.cdate,'%Y-%m') = ? "; + } else if ("year".equalsIgnoreCase(type)) { + 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 + * + * surveyor_location_details: + * loginId -> r.cby + * survey_id -> r.survey_id + * shelter_id -> shelter + */ + String sql = """ + SELECT + + 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 + + FROM night_shelter_inspection_response r + + 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 + + INNER JOIN surveyor_location_details s + ON s.loginId = r.cby + AND s.survey_id = r.survey_id + + WHERE r.qid = 51 + AND r.isactive = 1 + AND r.isdelete = 0 + """ + + dateCondition + + """ + + ORDER BY am.orderby + """; + + + /* Execute query */ + List> dbList; + + if (dateCondition.isEmpty()) { + dbList = jdbcTemplate.queryForList(sql); + } else { + 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 -> { + + Map map = new LinkedHashMap<>(); + + map.put("fund_id", fundId); + map.put("fund_type", fundType); + map.put("total", 0); + + map.put("shelters", new ArrayList>()); + + return map; + }); + + + /* + * Existing total + 1 + * + * This keeps the same meaning as: + * COUNT(r.id) + */ + int total = ((Number) fundRow.get("total")).intValue(); + fundRow.put("total", total + 1); + + + /*Shelter list*/ + @SuppressWarnings("unchecked") + 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")))); + + + if (!alreadyExists) { + + Map apiShelter = shelterApiMap.get(shelterId); + + Map shelter = new LinkedHashMap<>(); + + shelter.put("shelter_id", shelterId); + + if (apiShelter != null) { + + shelter.put("shelter_name", apiShelter.get("shelter_name")); + shelter.put("zone", apiShelter.get("zone")); + shelter.put("division", apiShelter.get("division")); + shelter.put("shelter_type", apiShelter.get("shelter_type")); + shelter.put("category", apiShelter.get("category")); + + } else { + + shelter.put("shelter_name", "-"); + shelter.put("zone", "-"); + shelter.put("division", "-"); + 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()); + } + + + +// public List> getFundReceivedSummary() { +// +// String sql = """ +// SELECT +// am.answer AS fund_type, +// COUNT(r.id) AS total +// 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 +// WHERE am.qid = 51 +// AND am.isactive = 1 +// AND am.isdelete = 0 +// GROUP BY am.id, am.answer +// ORDER BY am.orderby +// """; +// +// return jdbcTemplate.queryForList(sql); +// } + + //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 + """); + + List params = new ArrayList<>(); + + if ("day".equalsIgnoreCase(type)) { + + sql.append(" AND DATE(cdate)=? "); + params.add(value); + + } else if ("month".equalsIgnoreCase(type)) { + + sql.append(" AND DATE_FORMAT(cdate,'%Y-%m')=? "); + params.add(value); + + } else if ("year".equalsIgnoreCase(type)) { + + sql.append(" AND YEAR(cdate)=? "); + params.add(value); + + } + + sql.append(""" + GROUP BY + MONTH(cdate), + DATE_FORMAT(cdate,'%b') + + ORDER BY + MONTH(cdate) + """); + + List> list; + + if(params.isEmpty()){ + + list = jdbcTemplate.queryForList(sql.toString()); + + }else{ + + list = jdbcTemplate.queryForList( + sql.toString(), + params.toArray()); + + } + + 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(); + + row.put("total_count", totalCount); + + } + + return list; + + } + +// public List> getMonthlyTrends() { +// +// String sql = """ +// 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 +// +// GROUP BY +// MONTH(cdate), +// DATE_FORMAT(cdate,'%b') +// +// ORDER BY +// MONTH(cdate) +// """; +// +// List> list = +// jdbcTemplate.queryForList(sql); +// +// for(Map row:list){ +// +// int total_count = +// ((Number)row.get("residents")).intValue() +// + ((Number)row.get("rehabilitated")).intValue() +// + ((Number)row.get("reintegration")).intValue() +// + ((Number)row.get("death")).intValue(); +// +// row.put("total_count",total_count); +// } +// +// return list; +// } + + + //CDO Week Visit + + public List> getWeeklyVisits(String type, String value) { + + StringBuilder sql = new StringBuilder(); + + sql.append(""" + SELECT + + DAYOFWEEK(sld.cdate) AS day_no, + + DATE_FORMAT(sld.cdate,'%a') AS day_name, + + COUNT(DISTINCT sld.loginId) AS visits + + FROM surveyor_location_details sld + + INNER JOIN night_shelter_inspection_response nr + ON nr.cby = sld.loginId + + WHERE + sld.is_active = 1 + AND sld.is_delete = 0 + """); + + List params = new ArrayList<>(); + + if ("day".equalsIgnoreCase(type)) { + + sql.append(" AND DATE(sld.cdate)=? "); + params.add(value); + + } else if ("month".equalsIgnoreCase(type)) { + + sql.append(" AND DATE_FORMAT(sld.cdate,'%Y-%m')=? "); + params.add(value); + + } else if ("year".equalsIgnoreCase(type)) { + + sql.append(" AND YEAR(sld.cdate)=? "); + params.add(value); + + } else { + + // Default - Current Week + sql.append(" AND YEARWEEK(sld.cdate,1)=YEARWEEK(CURDATE(),1) "); + + } + + sql.append(""" + GROUP BY + DAYOFWEEK(sld.cdate), + DATE_FORMAT(sld.cdate,'%a') + + ORDER BY + DAYOFWEEK(sld.cdate) + """); + + if(params.isEmpty()){ + + return jdbcTemplate.queryForList(sql.toString()); + + } + + return jdbcTemplate.queryForList( + sql.toString(), + params.toArray()); + + } + + +// public List> getWeeklyVisits() { +// +// String sql = +// "SELECT " + +// "DAYOFWEEK(sld.cdate) AS day_no, " + +// "DATE_FORMAT(sld.cdate,'%a') AS day_name, " + +// "COUNT(DISTINCT sld.loginId) AS visits " + +// "FROM surveyor_location_details sld " + +// "INNER JOIN night_shelter_inspection_response nr " + +// "ON nr.cby = sld.loginId " + +// "WHERE YEARWEEK(sld.cdate,1)=YEARWEEK(CURDATE(),1) " + +// "AND sld.is_active=1 " + +// "AND sld.is_delete=0 " + +// "GROUP BY DAYOFWEEK(sld.cdate),DATE_FORMAT(sld.cdate,'%a') " + +// "ORDER BY day_no"; +// +// return jdbcTemplate.queryForList(sql); +// } + + + // Latest Visit Shelter + + public List> getLatestVisitShelters( + String type, + String value) { + + ResponseEntity>> response = + restTemplate.exchange( + API_URL, + HttpMethod.GET, + null, + new ParameterizedTypeReference>>() {}); + + List> apiList = response.getBody(); + + if (apiList == null) { + return Collections.emptyList(); + } + + List> result = new ArrayList<>(); + + for (Map shelter : apiList) { + + String uqid = String.valueOf(shelter.get("uqid")); + + StringBuilder sql = new StringBuilder(); + + sql.append(""" + SELECT + + ld.user_name, + + MAX(r.cdate) AS visit_date + + FROM night_shelter_inspection_response r + + INNER JOIN surveyor_location_details s + ON r.cby = s.loginId + + INNER JOIN login_details ld + ON ld.uid = s.loginId + + WHERE r.isactive = 1 + AND r.isdelete = 0 + AND s.shelter_id = ? + """); + + List params = new ArrayList<>(); + params.add(uqid); + + if ("day".equalsIgnoreCase(type)) { + + sql.append(" AND DATE(r.cdate)=? "); + params.add(value); + + } else if ("month".equalsIgnoreCase(type)) { + + sql.append(" AND DATE_FORMAT(r.cdate,'%Y-%m')=? "); + params.add(value); + + } else if ("year".equalsIgnoreCase(type)) { + + sql.append(" AND YEAR(r.cdate)=? "); + params.add(value); + + } + + sql.append(""" + GROUP BY + ld.user_name + + ORDER BY + visit_date DESC + + LIMIT 1 + """); + + List> list = + jdbcTemplate.queryForList( + sql.toString(), + params.toArray()); + + Map row = new LinkedHashMap<>(); + + row.put("shelterName", shelter.get("shelter_name")); + row.put("zone", shelter.get("zone")); + + if(!list.isEmpty()){ + + LocalDateTime visit = + (LocalDateTime) list.get(0).get("visit_date"); + + row.put("userName", + list.get(0).get("user_name")); + + row.put("visitDate", visit); + + row.put("date", + visit.format( + DateTimeFormatter.ofPattern("dd/MM/yyyy"))); + + row.put("time", + visit.format( + DateTimeFormatter.ofPattern("hh:mm a"))); + + }else{ + + row.put("userName","-"); + row.put("visitDate",null); + row.put("date","-"); + row.put("time","-"); + + } + + result.add(row); + + } + + result.sort((a,b)->{ + + LocalDateTime t1=(LocalDateTime)a.get("visitDate"); + LocalDateTime t2=(LocalDateTime)b.get("visitDate"); + + if(t1==null && t2==null) + return 0; + + if(t1==null) + return 1; + + if(t2==null) + return -1; + + return t2.compareTo(t1); + + }); + + return result.stream() + .limit(5) + .collect(Collectors.toList()); + + } + +// public List> getLatestVisitShelters() { +// +// ResponseEntity>> response = +// restTemplate.exchange( +// API_URL, +// HttpMethod.GET, +// null, +// new ParameterizedTypeReference>>() {}); +// +// List> apiList = response.getBody(); +// +// if (apiList == null) { +// return Collections.emptyList(); +// } +// +// List> result = new ArrayList<>(); +// +// for (Map shelter : apiList) { +// +// String uqid = String.valueOf(shelter.get("uqid")); +// +// String sql = """ +// SELECT +// ld.user_name, +// MAX(r.cdate) AS visit_date +// FROM night_shelter_inspection_response r +// INNER JOIN surveyor_location_details s +// ON r.cby = s.loginId +// INNER JOIN login_details ld +// ON ld.uid = s.loginId +// WHERE r.isactive = 1 +// AND r.isdelete = 0 +// AND s.shelter_id = ? +// GROUP BY ld.user_name +// ORDER BY visit_date DESC +// LIMIT 1 +// """; +// +// List> list = +// jdbcTemplate.queryForList(sql, uqid); +// +// Map row = new LinkedHashMap<>(); +// +// row.put("shelterName", shelter.get("shelter_name")); +// row.put("zone", shelter.get("zone")); +// +// if(!list.isEmpty()){ +// row.put("userName", list.get(0).get("user_name")); +// row.put("visitDate", list.get(0).get("visit_date")); +// }else{ +// row.put("userName", "-"); +// row.put("visitDate", null); +// } +// +// result.add(row); +// } +// +// result.sort((a,b)->{ +// +// LocalDateTime t1 = (LocalDateTime) a.get("visitDate"); +// LocalDateTime t2 = (LocalDateTime) b.get("visitDate"); +// +// if(t1==null && t2==null) return 0; +// if(t1==null) return 1; +// if(t2==null) return -1; +// +// return t2.compareTo(t1); +// }); +// +// return result.stream().limit(5).collect(Collectors.toList()); +// } + + + // SHelter List + + public List> getShelterList(String zone, String category) { + + ResponseEntity>> response = + restTemplate.exchange( + API_URL, + HttpMethod.GET, + null, + new ParameterizedTypeReference>>() {}); + + List> apiList = response.getBody(); + + if (apiList == null) { + return Collections.emptyList(); + } + + // Filter by Zone + if (zone != null && !zone.trim().isEmpty()) { + apiList = apiList.stream() + .filter(s -> zone.equalsIgnoreCase(String.valueOf(s.get("zone")))) + .collect(Collectors.toList()); + } + + // Filter by Category + if (category != null && !category.trim().isEmpty()) { + apiList = apiList.stream() + .filter(s -> category.equalsIgnoreCase(String.valueOf(s.get("category")))) + .collect(Collectors.toList()); + } + + List> result = new ArrayList<>(); + + for (Map shelter : apiList) { + + String uqid = String.valueOf(shelter.get("uqid")); + + // 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 = ? + """; + + + Integer residents = jdbcTemplate.queryForObject( + residentSql, + Integer.class, + uqid); + + // Latest Visit + String latestVisitSql = """ + 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 + + WHERE s.shelter_id = ? + """; + + + LocalDateTime visitDate = jdbcTemplate.queryForObject( + latestVisitSql, + LocalDateTime.class, + uqid); + + String latestVisit = "-"; + + if (visitDate != null) { + + LocalDate today = LocalDate.now(); + + if (visitDate.toLocalDate().isEqual(today)) { + latestVisit = "Today"; + } else if (visitDate.toLocalDate().isEqual(today.minusDays(1))) { + latestVisit = "Yesterday"; + } else { + latestVisit = visitDate.format( + DateTimeFormatter.ofPattern("dd/MM/yyyy")); + } + } + + Map row = new LinkedHashMap<>(); + + row.put("uqid", uqid); + row.put("shelterName", shelter.get("shelter_name")); + row.put("zone", shelter.get("zone")); + row.put("category", shelter.get("category")); + row.put("totalResidents", residents == null ? 0 : residents); + row.put("latestVisit", latestVisit); + + result.add(row); + } + + result.sort((a, b) -> Integer.compare( + ((Number) b.get("totalResidents")).intValue(), + ((Number) a.get("totalResidents")).intValue())); + + 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){ + 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 + + FROM night_shelter_inspection_response r + + INNER JOIN surveyor_location_details s + ON r.cby = s.loginId + + INNER JOIN login_details ld + ON ld.uid = s.loginId + + WHERE r.isactive = 1 + AND r.isdelete = 0 + """); + + List params = new ArrayList<>(); + + if (fromDate != null && !fromDate.isBlank()) { + sql.append(" AND DATE(r.cdate) >= ? "); + params.add(fromDate); + } + + if (toDate != null && !toDate.isBlank()) { + sql.append(" AND DATE(r.cdate) <= ? "); + params.add(toDate); + } + + if (cdo != null && !cdo.isBlank()) { + 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 + + ORDER BY + visitDate DESC + """); + + + List> dbList= + jdbcTemplate.queryForList(sql.toString(),params.toArray()); + + List> result=new ArrayList<>(); + + for(Map db:dbList){ + + String shelterId=String.valueOf(db.get("shelter_id")); + + Map api=apiShelters.stream() + + .filter(x->shelterId.equals(String.valueOf(x.get("uqid")))) + + .findFirst() + + .orElse(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 = "-"; + String time = "-"; + + if (dt != null) { + date = dt.format(DateTimeFormatter.ofPattern("dd/MM/yyyy")); + time = dt.format(DateTimeFormatter.ofPattern("hh:mm a")); + } + + 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("date", date); + row.put("time", time); + + row.put("food",db.get("food")); + row.put("staff",db.get("staff")); + + result.add(row); + + } + + return result; + + } + + + // Visit Details for modal box + + public Map getVisitDetails(String surveyId) { + + ResponseEntity>> response = + restTemplate.exchange( + API_URL, + HttpMethod.GET, + null, + new ParameterizedTypeReference>>() {}); + + List> apiShelters = response.getBody(); + + if (apiShelters == null) { + apiShelters = Collections.emptyList(); + } + + String sql = """ + SELECT + r.qid, + r.answer, + r.cdate, + ld.user_name, + s.shelter_id + + FROM night_shelter_inspection_response r + + INNER JOIN surveyor_location_details s + ON s.loginId = r.cby + + INNER JOIN login_details ld + ON ld.uid = s.loginId + + WHERE r.survey_id = ? + + ORDER BY r.qid + """; + + List> list = + jdbcTemplate.queryForList(sql, surveyId); + + if(list.isEmpty()){ + return Collections.emptyMap(); + } + + Map first = list.get(0); + + String shelterId = String.valueOf(first.get("shelter_id")); + + Map api = apiShelters.stream() + .filter(x -> shelterId.equals(String.valueOf(x.get("uqid")))) + .findFirst() + .orElse(null); + + Map answers = new LinkedHashMap<>(); + + for(Map row : list){ + + answers.put( + String.valueOf(row.get("qid")), + String.valueOf(row.get("answer")) + ); + } + + LocalDateTime dateTime = (LocalDateTime) first.get("cdate"); + + Map result = new LinkedHashMap<>(); + + result.put("surveyId",surveyId); + + result.put("userName",first.get("user_name")); + + result.put("date", + dateTime.format( + DateTimeFormatter.ofPattern("dd/MM/yyyy"))); + + result.put("time", + 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")); + + } + + result.put("answers",answers); + + return result; + + } + + + + + +} diff --git a/src/main/resources/templates/modules/shelterinspection/dashboard.html b/src/main/resources/templates/modules/shelterinspection/dashboard.html new file mode 100644 index 0000000..310cd6b --- /dev/null +++ b/src/main/resources/templates/modules/shelterinspection/dashboard.html @@ -0,0 +1,2817 @@ + + + + + + + + + + + + + + + + + + + + + + + + + +
+
+ +
+
+
+
+ + +
+ +
+
+
+ +
+
Shelters
+ + +
+ +
+ + + + + + + + + + + + + + + + + +
SHELTERZONECATEGORYRESIDENTSSTATUS
+ +
+ +
+ + + +
+ +
+
+
+ + +
+ + +
+
+ +
+ Region-wise Summary +
+ +
+
+ +
+
+ + +
+
+ +
+ Fund Received Summary +
+ +
+
+ +
+
+ + +
+
+ +
+ + + + +
+ + +
+
+ +
+
+ Daily CDO Visits (This Week) +
+ +
+ +
+
+ +
+
+ +
+ + + +
+ + +
+
+ +
+
+ Recent Visits +
+ + + View all + +
+ +
+
+ +
+
+ +
+ + +
+ + +
+ + + + + + + +
+ + +
+ + +
+
+
+
+ + + + + + + +
+ +
+ +
+
+ +
+ Loading shelter details... +
+ +
+ +
+ + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/main/resources/templates/modules/shelterinspection/shelterlist.html b/src/main/resources/templates/modules/shelterinspection/shelterlist.html new file mode 100644 index 0000000..e39ce76 --- /dev/null +++ b/src/main/resources/templates/modules/shelterinspection/shelterlist.html @@ -0,0 +1,433 @@ + + + + + + + + + + + + +
+
+ +
+
+
+
+ + + +
+ + +
+ +
+ +
+ +
+ +
+ + + + +
+ +
+
+ +
+ +
+ +
+ + + + + + + + + + + + + + + + + +
S.NoSHELTERZONECATEGORYRESIDENTSSTATUSLatest Visit
+ +
+ + +
+
+ + + + + +
+
+
+ +
+ + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/main/resources/templates/modules/shelterinspection/visitlist.html b/src/main/resources/templates/modules/shelterinspection/visitlist.html new file mode 100644 index 0000000..a6d508e --- /dev/null +++ b/src/main/resources/templates/modules/shelterinspection/visitlist.html @@ -0,0 +1,1673 @@ + + + + + + + + + + + + +
+
+ +
+
+
+
+ + + +
+ + +
+ +
+ + + + + +
+ +
+ +
+ +
+ +
+ +
+ +
+
+ +
+ +
+ +
+ + + + + + + + + + + + + + + + + + + +
S.NoSHELTERZONECDODATETIMEFOODSTAFFACTION
+ +
+ + +
+
+ + + + + +
+
+
+ + + + + + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + + + \ No newline at end of file