You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

188 lines
8.1 KiB

2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. package in.gov.chennaicorporation.gccoffice.pensioner.scheduler;
  2. import java.time.LocalDate;
  3. import java.time.Period;
  4. import java.time.format.DateTimeFormatter;
  5. import java.util.ArrayList;
  6. import java.util.HashMap;
  7. import java.util.List;
  8. import java.util.Map;
  9. import org.springframework.beans.factory.annotation.Autowired;
  10. import org.springframework.jdbc.core.JdbcTemplate;
  11. import org.springframework.scheduling.annotation.Scheduled;
  12. import org.springframework.stereotype.Component;
  13. import in.gov.chennaicorporation.gccoffice.pensioner.service.PendencyAutoConversionService;
  14. @Component
  15. public class PendencyAutoConversionScheduler {
  16. @Autowired
  17. private PendencyAutoConversionService pendencyAutoConversionService;
  18. /**
  19. * Scheduled method that runs every 60 seconds to process auto-conversion tasks.
  20. */
  21. @Scheduled(cron = "*/20 * * * * ?") // Runs every 60 seconds
  22. public void executeAutoConversion() {
  23. try {
  24. System.out.println(" Running scheduled auto-conversion task...");
  25. // Step 1: Fetch temp IDs that need processing
  26. List<Map<String, Object>> tempIds = pendencyAutoConversionService.getConvertingTempIds();
  27. if (tempIds == null || tempIds.isEmpty()) {
  28. System.out.println(" No records found for auto-conversion.");
  29. return;
  30. }
  31. System.out.println(" Found " + tempIds.size() + " records for processing.");
  32. // Step 2: Process each tempId
  33. for (Map<String, Object> tempIdMap : tempIds) {
  34. String tempId = tempIdMap.get("temp_id") != null ? tempIdMap.get("temp_id").toString() : null;
  35. if (tempId == null || tempId.trim().isEmpty()) {
  36. System.out.println(" Skipping empty temp_id.");
  37. continue;
  38. }
  39. System.out.println(" Processing tempId: " + tempId);
  40. // Step 3: Call service method to process auto-conversion
  41. boolean success = pendencyAutoConversionService.processAutoConversionForTempId(tempId);
  42. if (success) {
  43. System.out.println(" Auto-conversion completed for tempId: " + tempId);
  44. } else {
  45. System.out.println(" Auto-conversion failed for tempId: " + tempId);
  46. }
  47. }
  48. } catch (Exception e) {
  49. System.err.println(" Error during auto-conversion task: " + e.getMessage());
  50. e.printStackTrace();
  51. }
  52. }
  53. @Scheduled(cron = "*/40 * * * * ?") // Runs every 40 seconds
  54. public void scheduledPendencyUpdate() {
  55. System.out.println("Scheduled Task Started: Updating Pensioner Pending Duration 30");
  56. Map<String, Object> result = GetRecordsUpdatePendencyDuration();
  57. System.out.println("Scheduled Task Completed: " + result);
  58. // Fetch the pending pensioners count after the update
  59. int pendingCount = pendencyAutoConversionService.getPendingPensionersCount();
  60. System.out.println("Total pending pensioners after update: " + pendingCount);
  61. }
  62. public Map<String, Object> GetRecordsUpdatePendencyDuration() {
  63. Map<String, Object> response = new HashMap<>();
  64. // Lists to store tempIds based on update success or failure
  65. List<String> successfulUpdates = new ArrayList<>();
  66. List<String> failedUpdates = new ArrayList<>();
  67. List<Map<String, Object>> details = pendencyAutoConversionService.getPendencyUpdatingDuration();
  68. if(!details.isEmpty()) {
  69. for (Map<String, Object> tempIdMap : details) {
  70. String tempId = tempIdMap.get("temp_id") != null ? String.valueOf(tempIdMap.get("temp_id")) : null;
  71. String retirementDateStr = tempIdMap.get("retirement_date") != null ? String.valueOf(tempIdMap.get("retirement_date")) : null;
  72. String serviceDeathDate = tempIdMap.get("service_death") != null ? String.valueOf(tempIdMap.get("service_death")) : null;
  73. // Corrected parsing for file_category
  74. int fileCat = tempIdMap.get("file_category") != null ? Integer.parseInt(tempIdMap.get("file_category").toString()) : null;
  75. if(fileCat==3) {
  76. if (retirementDateStr != null && !retirementDateStr.trim().isEmpty()) {
  77. try {
  78. // Convert retirement_date to LocalDate
  79. DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
  80. LocalDate startDate = LocalDate.parse(retirementDateStr, formatter);
  81. LocalDate endDate = LocalDate.now(); // Current date
  82. System.out.printf("Processing Temp ID: %s | Start Date: %s | End Date: %s%n", tempId, startDate, endDate);
  83. // Calculate duration
  84. String updateDuration = pendencyAutoConversionService.calculatePendingDuration(startDate, endDate);
  85. System.out.println("Updated Duration: " + updateDuration);
  86. // Update the record in the database
  87. boolean isUpdated = pendencyAutoConversionService.updatePendingDuration(updateDuration, tempId);
  88. // Add tempId to the respective list
  89. if (isUpdated) {
  90. successfulUpdates.add(tempId);
  91. } else {
  92. failedUpdates.add(tempId);
  93. }
  94. } catch (Exception e) {
  95. System.err.println("Error parsing retirement_date for tempId " + tempId + ": " + e.getMessage());
  96. }
  97. } else {
  98. System.out.println("Skipping: Retirement date is null or empty for tempId: " + tempId);
  99. }
  100. }
  101. if(fileCat==2) {
  102. if (serviceDeathDate != null && !serviceDeathDate.trim().isEmpty()) {
  103. try {
  104. // Convert retirement_date to LocalDate
  105. DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
  106. LocalDate startDate = LocalDate.parse(serviceDeathDate, formatter);
  107. LocalDate endDate = LocalDate.now(); // Current date
  108. System.out.printf("Processing Temp ID: %s | Start Date: %s | End Date: %s%n", tempId, startDate, endDate);
  109. // Calculate duration
  110. String updateDuration = pendencyAutoConversionService.calculatePendingDuration(startDate, endDate);
  111. System.out.println("Updated Duration: " + updateDuration);
  112. // Update the record in the database
  113. boolean isUpdated = pendencyAutoConversionService.updatePendingDuration(updateDuration, tempId);
  114. // Add tempId to the respective list
  115. if (isUpdated) {
  116. successfulUpdates.add(tempId);
  117. } else {
  118. failedUpdates.add(tempId);
  119. }
  120. } catch (Exception e) {
  121. System.err.println("Error parsing service death date for tempId " + tempId + ": " + e.getMessage());
  122. }
  123. } else {
  124. System.out.println("Skipping: service death date is null or empty for tempId: " + tempId);
  125. }
  126. }
  127. }
  128. response.put("status", "Success");
  129. response.put("message", "Pendency duration updated");
  130. response.put("total_records_processed", details.size());
  131. response.put("successful_updates_count", successfulUpdates.size());
  132. response.put("failed_updates_count", failedUpdates.size());
  133. response.put("successful_updates", successfulUpdates);
  134. response.put("failed_updates", failedUpdates);
  135. }
  136. response.put("status", "No Data");
  137. response.put("message", "No Updation in Pending Duration");
  138. return response;
  139. }
  140. }