4 changed files with 650 additions and 11 deletions
-
20src/main/java/in/gov/chennaicorporation/mobileservice/configuration/DataSourceConfig.java
-
82src/main/java/in/gov/chennaicorporation/mobileservice/gccVolunteer/controller/VolunteerController.java
-
544src/main/java/in/gov/chennaicorporation/mobileservice/gccVolunteer/service/VolunteerService.java
-
3src/main/resources/application.properties
@ -0,0 +1,82 @@ |
|||||
|
package in.gov.chennaicorporation.mobileservice.gccVolunteer.controller; |
||||
|
|
||||
|
import java.util.List; |
||||
|
import java.util.Map; |
||||
|
|
||||
|
import org.springframework.beans.factory.annotation.Autowired; |
||||
|
import org.springframework.web.bind.annotation.GetMapping; |
||||
|
import org.springframework.web.bind.annotation.PostMapping; |
||||
|
import org.springframework.web.bind.annotation.RequestBody; |
||||
|
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.multipart.MultipartFile; |
||||
|
|
||||
|
import in.gov.chennaicorporation.mobileservice.gccVolunteer.service.VolunteerService; |
||||
|
|
||||
|
@RequestMapping("/gccofficialapp/api/volunteer") |
||||
|
@RestController("gccofficialappvolunteer") |
||||
|
public class VolunteerController { |
||||
|
|
||||
|
@Autowired |
||||
|
VolunteerService volunteerService; |
||||
|
|
||||
|
@GetMapping("/checkVolunteerLogin") |
||||
|
public Map<String, Object> checkVolunteerLogin(@RequestParam("mobileNo") String mobileNo, |
||||
|
@RequestParam("password") String password, @RequestParam("mid") String mid) { |
||||
|
return volunteerService.checkVolunteerLogin(mobileNo, password, mid); |
||||
|
} |
||||
|
|
||||
|
@GetMapping("/checkVolunteerMobileNo") |
||||
|
public Map<String, Object> checkVolunteerMobileNo(@RequestParam("mobileNo") String mobileNo, |
||||
|
@RequestParam(value = "mid", required = false) String mid) { |
||||
|
return volunteerService.checkVolunteerMobileNo(mobileNo, mid); |
||||
|
} |
||||
|
|
||||
|
@GetMapping("/checkMidStatus") |
||||
|
public String checkMidStatus(@RequestParam("mid") String mid) { |
||||
|
return volunteerService.checkMidStatus(mid); |
||||
|
} |
||||
|
|
||||
|
@PostMapping("/saveVolunteerLoginHistory") |
||||
|
public Map<String, Object> saveVolunteerLoginHistory( |
||||
|
@RequestParam(value = "volunteer_id", required = false) Integer volunteerId, |
||||
|
@RequestParam(value = "mid", required = false) String mid) { |
||||
|
|
||||
|
return volunteerService.saveVolunteerLoginHistory(volunteerId, mid, "LOGIN"); |
||||
|
} |
||||
|
|
||||
|
@PostMapping("/saveVolunteerLocationHistory") |
||||
|
public Map<String, Object> saveVolunteerLocationHistory( |
||||
|
@RequestParam(value = "volunteer_id", required = false) Integer volunteerId, |
||||
|
@RequestParam(value = "mid", required = false) String mid, |
||||
|
@RequestParam(value = "latitude", required = false) String latitude, |
||||
|
@RequestParam(value = "longitude", required = false) String longitude, |
||||
|
@RequestParam(value = "active_status", required = false) String activeStatus) { |
||||
|
|
||||
|
return volunteerService.saveVolunteerLocationHistory(volunteerId, mid, latitude, longitude, activeStatus); |
||||
|
} |
||||
|
|
||||
|
@GetMapping("/getComplaintList") |
||||
|
public Map<String, Object> getComplaintList(@RequestParam("volunteer_id") Integer volunteerId, |
||||
|
@RequestParam(value = "status", required = false) String status) { |
||||
|
return volunteerService.getComplaintList(volunteerId, status); |
||||
|
} |
||||
|
|
||||
|
@PostMapping("/updateComplaintStatus") |
||||
|
public Map<String, Object> updateComplaintStatus( |
||||
|
@RequestParam(value = "volunteer_id", required = false) Integer volunteerId, |
||||
|
@RequestParam(value = "complaint_id", required = false) Integer complaintId, |
||||
|
@RequestParam(value = "status", required = false) String status, |
||||
|
@RequestParam(value = "remarks", required = false) String remarks, |
||||
|
@RequestParam(value = "File", required = false) List<MultipartFile> file) { |
||||
|
|
||||
|
return volunteerService.updateComplaintStatus(volunteerId, complaintId, status, remarks, file); |
||||
|
} |
||||
|
|
||||
|
@GetMapping("/getVolunteerLastLocation") |
||||
|
public Map<String, Object> getVolunteerLastLocation(@RequestParam("volunteer_id") Integer volunteerId) { |
||||
|
return volunteerService.getVolunteerLastLocation(volunteerId); |
||||
|
} |
||||
|
|
||||
|
} |
||||
@ -0,0 +1,544 @@ |
|||||
|
package in.gov.chennaicorporation.mobileservice.gccVolunteer.service; |
||||
|
|
||||
|
import java.awt.image.BufferedImage; |
||||
|
import java.io.ByteArrayInputStream; |
||||
|
import java.io.ByteArrayOutputStream; |
||||
|
import java.io.IOException; |
||||
|
import java.nio.file.Files; |
||||
|
import java.nio.file.Path; |
||||
|
import java.nio.file.Paths; |
||||
|
import java.security.SecureRandom; |
||||
|
import java.time.LocalDate; |
||||
|
import java.time.LocalDateTime; |
||||
|
import java.time.format.DateTimeFormatter; |
||||
|
import java.time.format.DateTimeParseException; |
||||
|
import java.util.ArrayList; |
||||
|
import java.util.Collections; |
||||
|
import java.util.HashMap; |
||||
|
import java.util.List; |
||||
|
import java.util.Map; |
||||
|
import java.util.Random; |
||||
|
|
||||
|
import javax.imageio.ImageIO; |
||||
|
import javax.imageio.ImageWriteParam; |
||||
|
import javax.imageio.ImageWriter; |
||||
|
import javax.imageio.stream.ImageOutputStream; |
||||
|
import javax.sql.DataSource; |
||||
|
|
||||
|
import org.bytedeco.librealsense.rs_extrinsics; |
||||
|
import org.springframework.beans.factory.annotation.Autowired; |
||||
|
import org.springframework.beans.factory.annotation.Qualifier; |
||||
|
import org.springframework.core.env.Environment; |
||||
|
import org.springframework.jdbc.core.JdbcTemplate; |
||||
|
import org.springframework.stereotype.Service; |
||||
|
import org.springframework.web.multipart.MultipartFile; |
||||
|
|
||||
|
import in.gov.chennaicorporation.mobileservice.gccactivity.service.DateTimeUtil; |
||||
|
|
||||
|
@Service |
||||
|
public class VolunteerService { |
||||
|
|
||||
|
@Autowired |
||||
|
private JdbcTemplate jdbcTemplate; |
||||
|
private String fileBaseUrl; |
||||
|
|
||||
|
@Autowired |
||||
|
private Environment environment; |
||||
|
|
||||
|
private static final String CHARACTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; |
||||
|
private static final int STRING_LENGTH = 15; |
||||
|
private static final Random RANDOM = new SecureRandom(); |
||||
|
|
||||
|
@Autowired |
||||
|
public VolunteerService(Environment environment) { |
||||
|
this.environment = environment; |
||||
|
this.fileBaseUrl = environment.getProperty("fileBaseUrl"); |
||||
|
} |
||||
|
|
||||
|
@Autowired |
||||
|
public void setDataSource(@Qualifier("mysqlVolunteerDataSource") DataSource volunteerDataSource) { |
||||
|
this.jdbcTemplate = new JdbcTemplate(volunteerDataSource); |
||||
|
} |
||||
|
|
||||
|
public static String generateRandomString() { |
||||
|
StringBuilder result = new StringBuilder(STRING_LENGTH); |
||||
|
for (int i = 0; i < STRING_LENGTH; i++) { |
||||
|
result.append(CHARACTERS.charAt(RANDOM.nextInt(CHARACTERS.length()))); |
||||
|
} |
||||
|
return result.toString(); |
||||
|
} |
||||
|
|
||||
|
public static String generateRandomFileString(int lenthval) { |
||||
|
StringBuilder result = new StringBuilder(lenthval); |
||||
|
for (int i = 0; i < lenthval; i++) { |
||||
|
result.append(CHARACTERS.charAt(RANDOM.nextInt(CHARACTERS.length()))); |
||||
|
} |
||||
|
return result.toString(); |
||||
|
} |
||||
|
|
||||
|
private byte[] compressImage(BufferedImage image, float quality) throws IOException { |
||||
|
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); |
||||
|
ImageOutputStream imageOutputStream = ImageIO.createImageOutputStream(byteArrayOutputStream); |
||||
|
|
||||
|
ImageWriter writer = ImageIO.getImageWritersByFormatName("jpg").next(); |
||||
|
writer.setOutput(imageOutputStream); |
||||
|
|
||||
|
ImageWriteParam param = writer.getDefaultWriteParam(); |
||||
|
if (param.canWriteCompressed()) { |
||||
|
param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT); |
||||
|
param.setCompressionQuality(quality); |
||||
|
} |
||||
|
|
||||
|
writer.write(null, new javax.imageio.IIOImage(image, null, null), param); |
||||
|
|
||||
|
writer.dispose(); |
||||
|
imageOutputStream.close(); |
||||
|
|
||||
|
return byteArrayOutputStream.toByteArray(); |
||||
|
} |
||||
|
|
||||
|
// Function to convert date string from dd-MM-yyyy to yyyy-MM-dd |
||||
|
public static String convertDateFormat(String inputDate, int add) { |
||||
|
// Check if the inputDate is not null or blank |
||||
|
if (inputDate != null && !inputDate.isBlank()) { |
||||
|
try { |
||||
|
// Define the input and output date formats |
||||
|
DateTimeFormatter inputFormatter = DateTimeFormatter.ofPattern("dd-MM-yyyy"); |
||||
|
DateTimeFormatter outputFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd"); |
||||
|
|
||||
|
// Parse the input date string to LocalDate |
||||
|
LocalDate date = LocalDate.parse(inputDate, inputFormatter); |
||||
|
|
||||
|
// Add the specified number of days to the date if 'add' is greater than 0 |
||||
|
if (add > 0) { |
||||
|
date = date.plusDays(add); |
||||
|
} |
||||
|
|
||||
|
// Format the date to the new format |
||||
|
return date.format(outputFormatter); |
||||
|
|
||||
|
} catch (DateTimeParseException e) { |
||||
|
// Handle the case where the input date is invalid |
||||
|
System.out.println("Invalid date format: " + inputDate); |
||||
|
return ""; |
||||
|
} |
||||
|
} else { |
||||
|
return ""; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
public String fileUpload(String name, String id, MultipartFile file, String filetype) { |
||||
|
|
||||
|
int lastInsertId = 0; |
||||
|
// Set the file path where you want to save it |
||||
|
String uploadDirectory = environment.getProperty("file.upload.directory"); |
||||
|
String serviceFolderName = environment.getProperty("volunteer_foldername"); |
||||
|
var year = DateTimeUtil.getCurrentYear(); |
||||
|
var month = DateTimeUtil.getCurrentMonth(); |
||||
|
var date = DateTimeUtil.getCurrentDay(); |
||||
|
|
||||
|
uploadDirectory = uploadDirectory + serviceFolderName + filetype + "/" + year + "/" + month + "/" + date; |
||||
|
|
||||
|
try { |
||||
|
// Create directory if it doesn't exist |
||||
|
Path directoryPath = Paths.get(uploadDirectory); |
||||
|
if (!Files.exists(directoryPath)) { |
||||
|
Files.createDirectories(directoryPath); |
||||
|
} |
||||
|
|
||||
|
// Datetime string |
||||
|
String datetimetxt = DateTimeUtil.getCurrentDateTime(); |
||||
|
|
||||
|
datetimetxt = datetimetxt + "_" + generateRandomFileString(6); // Attached Random text |
||||
|
|
||||
|
// File name |
||||
|
String fileName = name + "_" + id + "_" + datetimetxt + "_" + file.getOriginalFilename(); |
||||
|
fileName = fileName.replaceAll("\\s+", ""); // Remove space on filename |
||||
|
|
||||
|
String filePath = uploadDirectory + "/" + fileName; |
||||
|
|
||||
|
String filepath_txt = "/" + serviceFolderName + filetype + "/" + year + "/" + month + "/" + date + "/" |
||||
|
+ fileName; |
||||
|
|
||||
|
// Create a new Path object |
||||
|
Path path = Paths.get(filePath); |
||||
|
|
||||
|
// Get the bytes of the file |
||||
|
byte[] bytes = file.getBytes(); |
||||
|
|
||||
|
if (filetype.equalsIgnoreCase("image")) { |
||||
|
// Compress the image |
||||
|
BufferedImage image = ImageIO.read(new ByteArrayInputStream(bytes)); |
||||
|
byte[] compressedBytes = compressImage(image, 0.5f); // Compress with 50% quality |
||||
|
// Write the bytes to the file |
||||
|
Files.write(path, compressedBytes); |
||||
|
} else { |
||||
|
// Write the bytes to the file |
||||
|
Files.write(path, bytes); |
||||
|
} |
||||
|
// Get current date & time |
||||
|
LocalDateTime now = LocalDateTime.now(); |
||||
|
|
||||
|
// Format date-time (optional) |
||||
|
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss"); |
||||
|
|
||||
|
System.out.println("Date: " + now.format(formatter)); |
||||
|
System.out.println("Activity: " + serviceFolderName); |
||||
|
System.out.println("File Type: " + filetype); |
||||
|
System.out.println("File Upload Path: " + filePath); |
||||
|
System.out.println("File Path: " + filepath_txt); |
||||
|
|
||||
|
return filepath_txt; |
||||
|
|
||||
|
} catch (IOException e) { |
||||
|
e.printStackTrace(); |
||||
|
System.out.println("Failed to save file " + file.getOriginalFilename()); |
||||
|
return "error"; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
public Map<String, Object> checkVolunteerLogin(String mobileNo, String password, String mid) { |
||||
|
String sqlQuery = ""; |
||||
|
|
||||
|
Map<String, Object> result = null; |
||||
|
try { |
||||
|
sqlQuery = "SELECT `uid`, `user_name`, `mobile_no` FROM gcc_mobile_menus.login_details WHERE (`mobile_no`=? AND `password`=?) AND (`is_active`=1 AND `is_delete`=0) LIMIT 1"; |
||||
|
result = jdbcTemplate.queryForMap(sqlQuery, mobileNo, password); |
||||
|
} catch (Exception e) { |
||||
|
// Log the error |
||||
|
e.printStackTrace(); |
||||
|
Map<String, Object> response = Map.of( |
||||
|
"status", 200, |
||||
|
"message", "User not found."); |
||||
|
return response; |
||||
|
} |
||||
|
|
||||
|
// If no records found, return a status and message indicating no user found |
||||
|
if (result == null || result.isEmpty()) { |
||||
|
Map<String, Object> response = Map.of( |
||||
|
"status", 200, |
||||
|
"message", "User not found."); |
||||
|
return response; |
||||
|
} |
||||
|
int uid = Integer.parseInt(result.get("uid").toString()); |
||||
|
|
||||
|
// Add a success status and message to the result |
||||
|
Map<String, Object> successResponse = Map.of( |
||||
|
"status", 200, |
||||
|
"message", "login successfuly.", |
||||
|
"data", result); |
||||
|
return successResponse; |
||||
|
} |
||||
|
|
||||
|
public String checkMidStatus(String mid) { |
||||
|
String sqlQuery = ""; |
||||
|
Map<String, Object> result = null; |
||||
|
try { |
||||
|
sqlQuery = "SELECT `mid`, case when is_active = 1 then 'ACTIVE' else 'INACTIVE' END as Status FROM volunteer_login_history WHERE `mid`=? "; |
||||
|
result = jdbcTemplate.queryForMap(sqlQuery, mid); |
||||
|
} catch (Exception e) { |
||||
|
// Log the error |
||||
|
e.printStackTrace(); |
||||
|
return ""; |
||||
|
} |
||||
|
|
||||
|
// If no records found, return a status and message indicating no user found |
||||
|
if (result == null || result.isEmpty()) { |
||||
|
return "Mid not found"; |
||||
|
} |
||||
|
|
||||
|
return result.toString(); |
||||
|
} |
||||
|
|
||||
|
public Map<String, Object> checkVolunteerMobileNo(String mobileNo, String mid) { |
||||
|
String sqlQuery = ""; |
||||
|
Map<String, Object> result = null; |
||||
|
try { |
||||
|
sqlQuery = "SELECT `volunteer_id`, `name`, `mobile_no` FROM volunteer_information WHERE (`mobile_no`=?) LIMIT 1"; |
||||
|
result = jdbcTemplate.queryForMap(sqlQuery, mobileNo); |
||||
|
} catch (Exception e) { |
||||
|
// e.printStackTrace(); |
||||
|
Map<String, Object> response = Map.of( |
||||
|
"status", 200, |
||||
|
"message", "User details not found."); |
||||
|
return response; |
||||
|
} |
||||
|
if (result == null || result.isEmpty()) { |
||||
|
Map<String, Object> response = Map.of( |
||||
|
"status", 200, |
||||
|
"message", "User details not found."); |
||||
|
return response; |
||||
|
} |
||||
|
int uid = Integer.parseInt(result.get("volunteer_id").toString()); |
||||
|
saveVolunteerLoginInfo(uid, mid); |
||||
|
|
||||
|
Map<String, Object> successResponse = Map.of( |
||||
|
"status", 200, |
||||
|
"message", "User details found", |
||||
|
"data", result); |
||||
|
return successResponse; |
||||
|
} |
||||
|
|
||||
|
private void saveVolunteerLoginInfo(int uid, String mid) { |
||||
|
try { |
||||
|
saveVolunteerLoginHistory(uid, mid, "LOGIN"); |
||||
|
} catch (Exception e) { |
||||
|
e.printStackTrace(); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
public Map<String, Object> saveVolunteerLoginHistory(Integer volunteerId, String mid, String logType) { |
||||
|
Map<String, Object> response = new HashMap<>(); |
||||
|
|
||||
|
if (volunteerId == null || volunteerId <= 0) { |
||||
|
response.put("status", 400); |
||||
|
response.put("message", "volunteer_id is required."); |
||||
|
return response; |
||||
|
} |
||||
|
|
||||
|
if (mid == null || mid.trim().isEmpty()) { |
||||
|
response.put("status", 400); |
||||
|
response.put("message", "mid is required."); |
||||
|
return response; |
||||
|
} |
||||
|
|
||||
|
String effectiveMid = mid.trim(); |
||||
|
String effectiveLogType = (logType != null && !logType.trim().isEmpty()) ? logType.trim() : "LOGIN"; |
||||
|
|
||||
|
try { |
||||
|
// Check if active records exist for this volunteer_id |
||||
|
String checkSql = "SELECT id, mid FROM volunteer_login_history WHERE volunteer_id = ? AND is_active = 1 AND is_delete = 0"; |
||||
|
List<Map<String, Object>> activeRecords = jdbcTemplate.queryForList(checkSql, volunteerId); |
||||
|
|
||||
|
boolean sameMidExists = false; |
||||
|
for (Map<String, Object> record : activeRecords) { |
||||
|
String existingMid = record.get("mid") != null ? record.get("mid").toString().trim() : ""; |
||||
|
if (existingMid.equalsIgnoreCase(effectiveMid)) { |
||||
|
sameMidExists = true; |
||||
|
break; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
// If same volunteer_id and mid is available, no need to make new entry |
||||
|
if (sameMidExists) { |
||||
|
response.put("status", 200); |
||||
|
response.put("message", "success"); |
||||
|
response.put("volunteer_id", volunteerId); |
||||
|
response.put("mid", effectiveMid); |
||||
|
return response; |
||||
|
} |
||||
|
|
||||
|
// If mid is different, disable the old entry (is_active = 0 and is_delete = 1) |
||||
|
if (!activeRecords.isEmpty()) { |
||||
|
String disableSql = "UPDATE volunteer_login_history SET is_active = 0, is_delete = 1 WHERE volunteer_id = ? AND is_active = 1 AND is_delete = 0"; |
||||
|
jdbcTemplate.update(disableSql, volunteerId); |
||||
|
} |
||||
|
|
||||
|
// Make the new entry |
||||
|
String insertSql = "INSERT INTO volunteer_login_history (volunteer_id, log_type, mid, is_active, is_delete, cdate) VALUES (?, ?, ?, 1, 0, NOW())"; |
||||
|
jdbcTemplate.update(insertSql, volunteerId, effectiveLogType, effectiveMid); |
||||
|
|
||||
|
response.put("status", 200); |
||||
|
response.put("message", "Volunteer login history saved successfully."); |
||||
|
response.put("volunteer_id", volunteerId); |
||||
|
response.put("mid", effectiveMid); |
||||
|
return response; |
||||
|
|
||||
|
} catch (Exception e) { |
||||
|
e.printStackTrace(); |
||||
|
response.put("status", 500); |
||||
|
response.put("message", "Failed to save volunteer login history: " + e.getMessage()); |
||||
|
return response; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
public Map<String, Object> saveVolunteerLocationHistory(Integer volunteerId, String mid, String latitude, |
||||
|
String longitude, String activeStatus) { |
||||
|
Map<String, Object> response = new HashMap<>(); |
||||
|
|
||||
|
activeStatus = activeStatus.equals("A") ? "1" : "0"; |
||||
|
int aS = Integer.parseInt(activeStatus); |
||||
|
|
||||
|
if (volunteerId == null || volunteerId <= 0) { |
||||
|
response.put("status", 400); |
||||
|
response.put("message", "volunteer_id is required."); |
||||
|
return response; |
||||
|
} |
||||
|
|
||||
|
if (latitude == null || latitude.trim().isEmpty() || longitude == null || longitude.trim().isEmpty()) { |
||||
|
response.put("status", 400); |
||||
|
response.put("message", "latitude and longitude are required."); |
||||
|
return response; |
||||
|
} |
||||
|
|
||||
|
// String midVal = (mid != null && !mid.trim().isEmpty()) ? mid.trim() : null; |
||||
|
|
||||
|
try { |
||||
|
// If one entry is already available for the volunteer_id, make the existing |
||||
|
// entry disabled (is_active = 0 and is_delete = 1) |
||||
|
String disableSql = "UPDATE volunteer_location_history SET is_active = 0, is_delete = 1 WHERE volunteer_id = ? AND is_active = 1 AND is_delete = 0"; |
||||
|
jdbcTemplate.update(disableSql, volunteerId); |
||||
|
|
||||
|
// Insert the new entry |
||||
|
String insertSql = "INSERT INTO volunteer_location_history (volunteer_id, mid, latitude, longitude, is_active, is_delete, cdate) VALUES (?, ?, ?, ?, ?, 0, NOW())"; |
||||
|
jdbcTemplate.update(insertSql, volunteerId, mid, latitude.trim(), longitude.trim(), aS); |
||||
|
|
||||
|
activeStatus = activeStatus.equals("1") ? "Active" : "In-Active"; |
||||
|
response.put("status", 200); |
||||
|
response.put("message", "Volunteer location history saved successfully."); |
||||
|
response.put("volunteer_id", volunteerId); |
||||
|
response.put("active_status", activeStatus); |
||||
|
return response; |
||||
|
|
||||
|
} catch (Exception e) { |
||||
|
e.printStackTrace(); |
||||
|
response.put("status", 500); |
||||
|
response.put("message", "Failed to save volunteer location history: " + e.getMessage()); |
||||
|
return response; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
public Map<String, Object> getComplaintList(Integer volunteerId, String status) { |
||||
|
|
||||
|
Map<String, Object> complaintList = new HashMap<>(); |
||||
|
int st = 0; |
||||
|
if (status != null && !status.trim().isEmpty()) { |
||||
|
status = status.equals("p") ? "2" : "3"; |
||||
|
st = Integer.parseInt(status); |
||||
|
} |
||||
|
|
||||
|
// 1. Check for active VolunteerLoginHistory |
||||
|
String checkLoginSql = "SELECT id FROM volunteer_login_history WHERE volunteer_id = ? AND is_active = 1 AND is_delete = 0 LIMIT 1"; |
||||
|
List<Map<String, Object>> activeLogins = jdbcTemplate.queryForList(checkLoginSql, volunteerId); |
||||
|
|
||||
|
if (activeLogins == null || activeLogins.isEmpty()) { |
||||
|
complaintList.put("response_status", 200); |
||||
|
complaintList.put("message", "Volunteer is not active."); |
||||
|
return complaintList; |
||||
|
} |
||||
|
|
||||
|
// 2. Get complaints for the zone of the active volunteer |
||||
|
// volunteer_information → zone_information → complaints_received |
||||
|
String sqlQuery = """ |
||||
|
select vcl.complaint_id, r.contact_name as complaint_person_name, r.contact_number as complaint_person_mobNo, |
||||
|
r.zone as complainant_zone, r.ward as complainant_ward, r.streetname, r.latitude, r.longitude, r.request_type, r.no_of_count, r.remarks, |
||||
|
r.modeofcomplient, date_format(r.cdate, '%d-%m-%Y %r') as created_date, sm.status as complaint_status |
||||
|
from volunteer_complaintlist vcl |
||||
|
left join gcc_sos.rescue r on vcl.complaint_id = r.id |
||||
|
left join status_master sm on vcl.complaint_status = sm.id |
||||
|
where vcl.volunteer_id = ? and vcl.is_active = 1 and vcl.is_delete = 0 |
||||
|
"""; |
||||
|
|
||||
|
if (status != null && !status.trim().isEmpty()) { |
||||
|
sqlQuery += " and vcl.complaint_status = " + st; |
||||
|
} |
||||
|
|
||||
|
List<Map<String, Object>> complaints = jdbcTemplate.queryForList(sqlQuery, volunteerId); |
||||
|
|
||||
|
String complaintsQuery = """ |
||||
|
select count(id) as totalCount, |
||||
|
sum(case when complaint_status = 2 then 1 else 0 end) as pendingComplaint, |
||||
|
sum(case when complaint_status = 3 then 1 else 0 end) as completedComplaint |
||||
|
from volunteer_complaintlist where volunteer_id = ? and is_active = 1 and is_delete = 0 |
||||
|
group by volunteer_id |
||||
|
"""; |
||||
|
Map<String, Object> complaintCount = jdbcTemplate.queryForMap(complaintsQuery, volunteerId); |
||||
|
|
||||
|
if (complaints == null || complaints.isEmpty()) { |
||||
|
complaintList.put("response_status", 200); |
||||
|
complaintList.put("message", "No complaints found."); |
||||
|
return complaintList; |
||||
|
} |
||||
|
|
||||
|
if (complaintCount != null && !complaintCount.isEmpty()) { |
||||
|
complaintList.put("response_status", 200); |
||||
|
complaintList.put("messsage", "success"); |
||||
|
complaintList.put("complaints", complaints); |
||||
|
complaintList.put("data", complaintCount); |
||||
|
} |
||||
|
|
||||
|
return complaintList; |
||||
|
} |
||||
|
|
||||
|
public Map<String, Object> updateComplaintStatus(Integer volunteerId, Integer complaintId, String status, |
||||
|
String remarks, List<MultipartFile> file) { |
||||
|
Map<String, Object> response = new HashMap<>(); |
||||
|
|
||||
|
if (volunteerId == null || complaintId == null) { |
||||
|
response.put("status", 400); |
||||
|
response.put("message", "volunteer_id and complaint_id are required."); |
||||
|
return response; |
||||
|
} |
||||
|
|
||||
|
try { |
||||
|
// Check if respective volunteerId and complaintId exist in |
||||
|
// volunteer_complaintlist with is_active = 1 and is_delete = 0 |
||||
|
String checkSql = "SELECT id FROM volunteer_complaintlist WHERE volunteer_id = ? AND complaint_id = ? AND is_active = 1 AND is_delete = 0 LIMIT 1"; |
||||
|
List<Map<String, Object>> records = jdbcTemplate.queryForList(checkSql, volunteerId, complaintId); |
||||
|
|
||||
|
if (records == null || records.isEmpty()) { |
||||
|
response.put("status", 400); |
||||
|
response.put("message", "Complaint not found or inactive for the given volunteer."); |
||||
|
return response; |
||||
|
} |
||||
|
|
||||
|
if (file != null && !file.isEmpty()) { |
||||
|
|
||||
|
for (MultipartFile multipartFile : file) { |
||||
|
String fileLocation = fileUpload("resolve_volunteer", complaintId.toString(), multipartFile, |
||||
|
"image"); |
||||
|
|
||||
|
if (fileLocation != null && !fileLocation.isEmpty()) { |
||||
|
|
||||
|
String insertFileSql = "INSERT INTO volunteer_complaint_files (volunteer_id, complaint_id, file_path) VALUES (?, ?, ?)"; |
||||
|
jdbcTemplate.update(insertFileSql, volunteerId, complaintId, fileLocation); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
} |
||||
|
|
||||
|
// Update volunteer.volunteer_complaintlist --> set complaint_status = 3 |
||||
|
String updateComplaintListSql = "UPDATE volunteer_complaintlist SET complaint_status = 3, remarks = ? WHERE volunteer_id = ? AND complaint_id = ? AND is_active = 1 AND is_delete = 0"; |
||||
|
jdbcTemplate.update(updateComplaintListSql, remarks, volunteerId, complaintId); |
||||
|
|
||||
|
// Update gcc_sos.rescue --> set status = 1 |
||||
|
String updateRescueSql = "UPDATE gcc_sos.rescue SET status = 1 WHERE id = ?"; |
||||
|
jdbcTemplate.update(updateRescueSql, complaintId); |
||||
|
|
||||
|
response.put("status", 200); |
||||
|
response.put("message", "Complaint status updated successfully."); |
||||
|
response.put("volunteer_id", volunteerId); |
||||
|
response.put("complaint_id", complaintId); |
||||
|
return response; |
||||
|
|
||||
|
} catch (Exception e) { |
||||
|
e.printStackTrace(); |
||||
|
response.put("status", 500); |
||||
|
response.put("message", "Failed to update complaint status: " + e.getMessage()); |
||||
|
return response; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
public Map<String, Object> getVolunteerLastLocation(Integer volunteerId) { |
||||
|
Map<String, Object> response = new HashMap<>(); |
||||
|
String sqlQuery = "Select id, latitude, longitude, case when is_active = 1 then 'ACTIVE' else 'INACTIVE' END as location_status from volunteer_location_history where volunteer_id = ? order by id desc limit 1"; |
||||
|
try { |
||||
|
Map<String, Object> result = jdbcTemplate.queryForMap(sqlQuery, volunteerId); |
||||
|
if (result.isEmpty()) { |
||||
|
response.put("status", 404); |
||||
|
response.put("message", "No volunteer last location found."); |
||||
|
return response; |
||||
|
} |
||||
|
response.put("status", 200); |
||||
|
response.put("message", "Volunteer last location found successfully."); |
||||
|
response.put("volunteer_id", volunteerId); |
||||
|
response.put("location", result); |
||||
|
} catch (Exception e) { |
||||
|
e.printStackTrace(); |
||||
|
response.put("status", 500); |
||||
|
response.put("message", "Failed to get volunteer last location: " + e.getMessage()); |
||||
|
} |
||||
|
return response; |
||||
|
} |
||||
|
|
||||
|
} |
||||
Write
Preview
Loading…
Cancel
Save
Reference in new issue