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.

280 lines
12 KiB

2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
  1. package in.gov.chennaicorporation.gccoffice.pensioner.service;
  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.List;
  7. import java.util.Map;
  8. import javax.sql.DataSource;
  9. import org.springframework.beans.factory.annotation.Autowired;
  10. import org.springframework.beans.factory.annotation.Qualifier;
  11. import org.springframework.jdbc.core.JdbcTemplate;
  12. import org.springframework.stereotype.Service;
  13. import org.springframework.transaction.annotation.Transactional;
  14. import in.gov.chennaicorporation.gccoffice.configuration.AppConfig;
  15. @Service
  16. @Transactional
  17. public class PendencyAutoConversionService {
  18. @Autowired
  19. private JdbcTemplate jdbcTemplate;
  20. @Autowired
  21. private AppConfig appConfig;
  22. @Autowired
  23. public void setDataSource(@Qualifier("mysqlPensionerDataSource") DataSource dataSource) {
  24. this.jdbcTemplate = new JdbcTemplate(dataSource);
  25. }
  26. /**
  27. * Main method to process auto-conversion logic.
  28. * - Fetch records where retirement date has crossed.
  29. * - Update pensioner details.
  30. * - Fetch employee details.
  31. * - Save data into pensioner_details_history and file_movement_history.
  32. */
  33. @Transactional
  34. public boolean processAutoConversion() {
  35. try {
  36. // Step 1: Fetch temp IDs that need processing
  37. List<Map<String, Object>> tempList = getConvertingTempIds();
  38. if (tempList.isEmpty()) {
  39. System.out.println("No records found for auto-conversion.");
  40. return false;
  41. }
  42. for (Map<String, Object> tempIdMap : tempList) {
  43. String tempId = tempIdMap.get("temp_id") != null ? String.valueOf(tempIdMap.get("temp_id")) : null;
  44. if (tempId == null || tempId.trim().isEmpty()) {
  45. System.out.println("Skipping null/empty temp_id.");
  46. continue;
  47. }
  48. System.out.println("Processing tempId: " + tempId);
  49. // Step 2: Get file_moved_by value
  50. String fileMovedBy = getFileMovedByTempId(tempId);
  51. if (fileMovedBy == null) {
  52. System.out.println("Skipping tempId " + tempId + " as fileMovedBy is NULL.");
  53. continue;
  54. }
  55. // Step 3: Update pensioner_details table
  56. boolean updated = updateEntryDetails(tempId);
  57. if (!updated) {
  58. System.out.println("Failed to update pensioner_details for tempId: " + tempId);
  59. continue;
  60. }
  61. // Step 4: Fetch employee details
  62. List<Map<String, Object>> empDetails = fetchEmployeeDetails(tempId);
  63. if (empDetails.isEmpty()) {
  64. System.out.println("No employee details found for tempId: " + tempId);
  65. continue;
  66. }
  67. // Step 5: Save history data
  68. boolean historySaved = saveHistory(empDetails, fileMovedBy);
  69. if (!historySaved) {
  70. System.out.println("Failed to save history for tempId: " + tempId);
  71. }
  72. }
  73. System.out.println("Auto-conversion process completed.");
  74. return true;
  75. } catch (Exception e) {
  76. System.err.println("Exception in processAutoConversion: " + e.getMessage());
  77. e.printStackTrace();
  78. return false;
  79. }
  80. }
  81. public List<Map<String, Object>> getConvertingTempIds() {
  82. String sql = "SELECT temp_id FROM pensioner_details WHERE file_category=1 AND file_status <=8 AND retirement_date <= CURDATE() - 1";
  83. return jdbcTemplate.queryForList(sql);
  84. }
  85. private boolean updateEntryDetails(String tempId) {
  86. try {
  87. LocalDate datefield = LocalDate.now();
  88. String formattedDate = datefield.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
  89. String remarks = "Auto_Conversion";
  90. int fileCategory = 3;
  91. String pendingDuration = "yes";
  92. String reasonForPending = "Retirement Date Crossed while file in Processing";
  93. String updateQuery = "UPDATE pensioner_details SET reason_for_pending=?, pending_duration=?, file_category=?, file_moved_date=?, remarks=?, updated_date=CURRENT_TIMESTAMP WHERE temp_id=?";
  94. int rowsUpdated = jdbcTemplate.update(updateQuery, reasonForPending, pendingDuration, fileCategory, formattedDate, remarks, tempId);
  95. System.out.println("Updated pensioner_details for tempId: " + tempId + " - Rows affected: " + rowsUpdated);
  96. return rowsUpdated > 0;
  97. } catch (Exception e) {
  98. System.err.println("Exception in updateEntryDetails: " + e.getMessage());
  99. return false;
  100. }
  101. }
  102. public List<Map<String, Object>> fetchEmployeeDetails(String tempId) {
  103. String sql = "SELECT * FROM pensioner_details WHERE temp_id=?";
  104. return jdbcTemplate.queryForList(sql, tempId);
  105. }
  106. @Transactional
  107. public boolean saveHistory(List<Map<String, Object>> empDetails, String fileMovedBy) {
  108. try {
  109. String empInsertQuery = "INSERT INTO pensioner_details_history (emp_no, emp_name, dept_name, designation, type_of_retirement, " +
  110. "retirement_date, retirement_class, file_moved_date, remarks, file_category, file_status, " +
  111. "file_entry_type, file_moved_by, dept_id, file_category_name, temp_id, pending_duration, reason_for_pending) " +
  112. "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
  113. jdbcTemplate.batchUpdate(empInsertQuery, empDetails, empDetails.size(), (ps, emp) -> {
  114. ps.setString(1, emp.get("emp_no").toString());
  115. ps.setString(2, emp.get("emp_name").toString());
  116. ps.setString(3, emp.get("dept_name").toString());
  117. ps.setString(4, emp.get("designation").toString());
  118. ps.setString(5, emp.get("type_of_retirement").toString());
  119. ps.setString(6, emp.get("retirement_date").toString());
  120. ps.setString(7, emp.get("retirement_class").toString());
  121. ps.setString(8, emp.get("file_moved_date").toString());
  122. ps.setString(9, emp.get("remarks").toString());
  123. ps.setInt(10, Integer.parseInt(emp.get("file_category").toString()));
  124. ps.setInt(11, Integer.parseInt(emp.get("file_status").toString()));
  125. ps.setString(12, emp.get("file_entry_type").toString());
  126. ps.setString(13, fileMovedBy != null ? fileMovedBy : "");
  127. ps.setInt(14, Integer.parseInt(emp.get("dept_id").toString()));
  128. ps.setString(15, emp.get("file_category_name").toString());
  129. ps.setString(16, emp.get("temp_id").toString());
  130. ps.setString(17, emp.get("pending_duration") != null ? emp.get("pending_duration").toString() : null);
  131. ps.setString(18, emp.get("reason_for_pending") != null ? emp.get("reason_for_pending").toString() : null);
  132. });
  133. String historyQuery = "INSERT INTO file_movement_history (emp_no, file_moved_date, file_status, file_moved_by, remarks, temp_id) VALUES (?, ?, ?, ?, ?, ?)";
  134. jdbcTemplate.batchUpdate(historyQuery, empDetails, empDetails.size(), (ps, his) -> {
  135. ps.setString(1, his.get("emp_no").toString());
  136. ps.setString(2, his.get("file_moved_date").toString());
  137. ps.setString(3, his.get("file_status").toString());
  138. ps.setString(4, fileMovedBy != null ? fileMovedBy : "");
  139. ps.setString(5, his.get("remarks").toString());
  140. ps.setString(6, his.get("temp_id").toString());
  141. });
  142. System.out.println("Successfully saved history for " + empDetails.size() + " records.");
  143. return true;
  144. } catch (Exception e) {
  145. System.err.println("Exception in saveHistory: " + e.getMessage());
  146. return false;
  147. }
  148. }
  149. public String getFileMovedByTempId(String tempId) {
  150. String sql = "SELECT file_moved_by FROM pensioner_details_history WHERE temp_id=? ORDER BY created_date DESC LIMIT 1";
  151. return jdbcTemplate.queryForObject(sql, String.class, tempId);
  152. }
  153. public boolean processAutoConversionForTempId(String tempId) {
  154. try {
  155. System.out.println("Running auto-conversion for tempId: " + tempId);
  156. // Step 1: Get fileMovedBy
  157. String fileMovedBy = getFileMovedByTempId(tempId);
  158. if (fileMovedBy == null) {
  159. System.out.println("Skipping tempId " + tempId + " as fileMovedBy is NULL.");
  160. return false;
  161. }
  162. // Step 2: Update pensioner details
  163. boolean updated = updateEntryDetails(tempId);
  164. if (!updated) {
  165. System.out.println("Failed to update pensioner_details for tempId: " + tempId);
  166. return false;
  167. }
  168. // Step 3: Fetch employee details
  169. List<Map<String, Object>> empDetails = fetchEmployeeDetails(tempId);
  170. if (empDetails.isEmpty()) {
  171. System.out.println("No employee details found for tempId: " + tempId);
  172. return false;
  173. }
  174. // Step 4: Save history
  175. boolean historySaved = saveHistory(empDetails, fileMovedBy);
  176. if (!historySaved) {
  177. System.out.println("Failed to save history for tempId: " + tempId);
  178. return false;
  179. }
  180. System.out.println("Successfully processed tempId: " + tempId);
  181. return true;
  182. } catch (Exception e) {
  183. System.err.println("Error processing tempId: " + tempId + " - " + e.getMessage());
  184. e.printStackTrace();
  185. return false;
  186. }
  187. }
  188. public List<Map<String, Object>> getPendencyUpdatingDuration(){
  189. String sqlQuery = "SELECT * FROM pensioner_details WHERE file_category IN (2,3,5) AND (pending_duration = 'yes' OR pending_duration = 'Yes') AND file_status < 11";
  190. List<Map<String, Object>> details = jdbcTemplate.queryForList(sqlQuery);
  191. return details;
  192. }
  193. public List<Map<String, Object>> getProvisionalNoPendingDuration(){
  194. String sqlQuery = "SELECT * FROM pensioner_details WHERE file_category IN (5) AND (pending_duration = 'no' OR pending_duration = 'No') AND file_status < 11 AND retirement_date < curdate()";
  195. List<Map<String, Object>> prdetails = jdbcTemplate.queryForList(sqlQuery);
  196. return prdetails;
  197. }
  198. public String calculatePendingDuration(LocalDate startDate, LocalDate endDate) {
  199. Period period = Period.between(startDate, endDate);
  200. int years = period.getYears();
  201. int months = period.getMonths();
  202. int days = period.getDays();
  203. if (years == 0 && months == 0 && days == 0) {
  204. return "0 days"; // or "Same day"
  205. }
  206. return (years > 0 ? years + " year " : "") +
  207. (months > 0 ? months + " month " : "") +
  208. (days > 0 ? days + " day" : "");
  209. }
  210. public boolean updatePendingDuration(String updateDuration, String tempId) {
  211. String sqlQuery = "UPDATE pensioner_details SET reason_for_pending=? WHERE temp_id=?";
  212. int rowsUpdated = jdbcTemplate.update(sqlQuery, updateDuration, tempId);
  213. System.out.println("Temp ID: " + tempId + " | Rows Updated: " + rowsUpdated);
  214. return rowsUpdated > 0;
  215. }
  216. public boolean updatePendingDurationforPR(String updateDuration1, String tempId1) {
  217. String sqlQuery = "UPDATE pensioner_details SET pending_duration='Yes',reason_for_pending=? WHERE temp_id=?";
  218. int rowsUpdated = jdbcTemplate.update(sqlQuery, updateDuration1, tempId1);
  219. System.out.println("Updating for PR No Temp ID: " + tempId1 + " | Rows Updated: " + rowsUpdated);
  220. return rowsUpdated > 0;
  221. }
  222. // New method to get the count of pending pensioners
  223. public int getPendingPensionersCount() {
  224. String sqlQuery = "SELECT COUNT(*) FROM pensioner_details WHERE file_category=3 AND pending_duration='yes' AND file_status < 11";
  225. Integer count = jdbcTemplate.queryForObject(sqlQuery, Integer.class);
  226. return count != null ? count : 0;
  227. }
  228. }