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.

201 lines
8.6 KiB

2 years ago
​
​
2 years ago
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. package in.gov.chennaicorporation.gccoffice.pensioner.controller;
  2. import java.time.LocalDate;
  3. import java.time.Period;
  4. import java.time.format.DateTimeFormatter;
  5. import java.time.temporal.ChronoUnit;
  6. import java.util.ArrayList;
  7. import java.util.HashMap;
  8. import java.util.List;
  9. import java.util.Map;
  10. import javax.sql.DataSource;
  11. import org.springframework.beans.factory.annotation.Autowired;
  12. import org.springframework.beans.factory.annotation.Qualifier;
  13. import org.springframework.jdbc.core.JdbcTemplate;
  14. import org.springframework.scheduling.annotation.Scheduled;
  15. import org.springframework.web.bind.annotation.GetMapping;
  16. import org.springframework.web.bind.annotation.RequestMapping;
  17. import org.springframework.web.bind.annotation.RestController;
  18. import in.gov.chennaicorporation.gccoffice.pensioner.service.DeptAuditService;
  19. import in.gov.chennaicorporation.gccoffice.pensioner.service.NewEntryService;
  20. import in.gov.chennaicorporation.gccoffice.pensioner.service.PendencyAutoConversionService;
  21. @RequestMapping("gcc/api/pensioner/autoconversion")
  22. @RestController
  23. public class PendencyAutoConversionApiController {
  24. @Autowired
  25. private JdbcTemplate jdbcTemplate;
  26. @Autowired
  27. public void setDataSource(@Qualifier("mysqlPensionerDataSource") DataSource dataSource) {
  28. this.jdbcTemplate = new JdbcTemplate(dataSource);
  29. }
  30. // @Autowired
  31. // private DeptAuditService deptAuditService;
  32. //
  33. // @Autowired
  34. // public PendencyAutoConversionService pendencyAutoConversionService;
  35. //
  36. // @GetMapping("/updateintopendency")
  37. // public boolean updateIfRetirementDateCrossed() {
  38. // try {
  39. // List<Map<String, Object>> covertList = pendencyAutoConversionService.getConvertingTempIds();
  40. // System.out.println("covertList===" + covertList);
  41. //
  42. // if (!covertList.isEmpty()) {
  43. // for (Map<String, Object> tempIdMap : covertList) {
  44. // try {
  45. // // Extract temp_id and convert it to a String
  46. // Object tempIdRaw = tempIdMap.get("temp_id");
  47. // String tempId = tempIdRaw != null ? String.valueOf(tempIdRaw) : null;
  48. //
  49. // System.out.println("Converted tempId: " + tempId);
  50. //
  51. // // Ensure tempId is not null before processing
  52. // if (tempId != null && !tempId.trim().isEmpty()) {
  53. //
  54. // String fileMovedBy=pendencyAutoConversionService.getFileMovedByTempId(tempId);
  55. //
  56. // if(fileMovedBy!=null) {
  57. // boolean isProcessed = pendencyAutoConversionService.updateEntryDetails(tempId);
  58. //
  59. // if (isProcessed) {
  60. // List<Map<String, Object>> emp_details = deptAuditService.fetchEmployeeById(tempId);
  61. // System.out.println("emp_details: " + emp_details);
  62. //
  63. // boolean updated = pendencyAutoConversionService.saveHistory(emp_details,fileMovedBy);
  64. //
  65. // if (!updated) {
  66. // System.out.println("Failed to save history for tempId: " + tempId);
  67. // continue;
  68. // }
  69. // }
  70. // }
  71. // else {
  72. // System.out.println("fileMovedBy is NUll for temp_id="+tempId);
  73. // }
  74. //
  75. // }
  76. // } catch (Exception innerEx) {
  77. // System.err.println("Error processing tempId: " + tempIdMap.get("temp_id"));
  78. // innerEx.printStackTrace();
  79. // continue; // Continue processing the next record
  80. // }
  81. // }
  82. // return true;
  83. // } else {
  84. // System.out.println("No records found for processing.");
  85. // return false;
  86. // }
  87. // } catch (Exception e) {
  88. // System.err.println("Exception in updateIfRetirementDateCrossed: " + e.getMessage());
  89. // e.printStackTrace();
  90. // return false;
  91. // }
  92. // }
  93. @Scheduled(cron = "*/40 * * * * ?") // Runs every 40 seconds
  94. public void scheduledPendencyUpdate() {
  95. System.out.println("Scheduled Task Started: Updating Pensioner Pending Duration 40");
  96. Map<String, Object> result = GetRecordsUpdatePendencyDuration();
  97. System.out.println("Scheduled Task Completed: " + result);
  98. // Fetch the pending pensioners count after the update
  99. int pendingCount = getPendingPensionersCount();
  100. System.out.println("Total pending pensioners after update: " + pendingCount);
  101. }
  102. public Map<String, Object> GetRecordsUpdatePendencyDuration() {
  103. Map<String, Object> response = new HashMap<>();
  104. // Lists to store tempIds based on update success or failure
  105. List<String> successfulUpdates = new ArrayList<>();
  106. List<String> failedUpdates = new ArrayList<>();
  107. String sqlQuery = "SELECT * FROM pensioner_details WHERE file_category=3 AND pending_duration='yes' AND file_status < 11";
  108. List<Map<String, Object>> details = jdbcTemplate.queryForList(sqlQuery);
  109. for (Map<String, Object> tempIdMap : details) {
  110. String tempId = tempIdMap.get("temp_id") != null ? String.valueOf(tempIdMap.get("temp_id")) : null;
  111. String retirementDateStr = tempIdMap.get("retirement_date") != null ? String.valueOf(tempIdMap.get("retirement_date")) : null;
  112. if (retirementDateStr != null && !retirementDateStr.trim().isEmpty()) {
  113. try {
  114. // Convert retirement_date to LocalDate
  115. DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
  116. LocalDate startDate = LocalDate.parse(retirementDateStr, formatter);
  117. LocalDate endDate = LocalDate.now(); // Current date
  118. System.out.printf("Processing Temp ID: %s | Start Date: %s | End Date: %s%n", tempId, startDate, endDate);
  119. // Calculate duration
  120. String updateDuration = calculatePendingDuration(startDate, endDate);
  121. System.out.println("Updated Duration: " + updateDuration);
  122. // Update the record in the database
  123. boolean isUpdated = updatePendingDuration(updateDuration, tempId);
  124. // Add tempId to the respective list
  125. if (isUpdated) {
  126. successfulUpdates.add(tempId);
  127. } else {
  128. failedUpdates.add(tempId);
  129. }
  130. } catch (Exception e) {
  131. System.err.println("Error parsing retirement_date for tempId " + tempId + ": " + e.getMessage());
  132. }
  133. } else {
  134. System.out.println("Skipping: Retirement date is null or empty for tempId: " + tempId);
  135. }
  136. }
  137. response.put("status", "Success");
  138. response.put("message", "Pendency duration updated");
  139. response.put("total_records_processed", details.size());
  140. response.put("successful_updates_count", successfulUpdates.size());
  141. response.put("failed_updates_count", failedUpdates.size());
  142. response.put("successful_updates", successfulUpdates);
  143. response.put("failed_updates", failedUpdates);
  144. return response;
  145. }
  146. public String calculatePendingDuration(LocalDate startDate, LocalDate endDate) {
  147. Period period = Period.between(startDate, endDate);
  148. int years = period.getYears();
  149. int months = period.getMonths();
  150. int days = period.getDays();
  151. return (years > 0 ? years + " year " : "") +
  152. (months > 0 ? months + " month " : "") +
  153. (days > 0 ? days + " day" : "");
  154. }
  155. public boolean updatePendingDuration(String updateDuration, String tempId) {
  156. String sqlQuery = "UPDATE pensioner_details SET reason_for_pending=? WHERE temp_id=?";
  157. int rowsUpdated = jdbcTemplate.update(sqlQuery, updateDuration, tempId);
  158. System.out.println("Temp ID: " + tempId + " | Rows Updated: " + rowsUpdated);
  159. return rowsUpdated > 0;
  160. }
  161. // New method to get the count of pending pensioners
  162. public int getPendingPensionersCount() {
  163. String sqlQuery = "SELECT COUNT(*) FROM pensioner_details WHERE file_category=3 AND pending_duration='yes' AND file_status < 11";
  164. Integer count = jdbcTemplate.queryForObject(sqlQuery, Integer.class);
  165. return count != null ? count : 0;
  166. }
  167. }