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.

173 lines
6.8 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. package com.gcc.erppostdata.swimmingpool;
  2. import com.fasterxml.jackson.core.JsonProcessingException;
  3. import com.fasterxml.jackson.databind.ObjectMapper;
  4. import com.gcc.erppostdata.config.AppConfig;
  5. import com.gcc.erppostdata.repo.SwimLogRepo;
  6. import org.springframework.beans.factory.annotation.Autowired;
  7. import org.springframework.beans.factory.annotation.Qualifier;
  8. import org.springframework.jdbc.core.JdbcTemplate;
  9. import org.springframework.stereotype.Component;
  10. import javax.sql.DataSource;
  11. import java.io.BufferedReader;
  12. import java.io.InputStreamReader;
  13. import java.net.HttpURLConnection;
  14. import java.net.URL;
  15. import java.net.URLEncoder;
  16. import java.time.LocalDate;
  17. import java.util.*;
  18. @Component
  19. public class SWPostData {
  20. @Autowired
  21. private AppConfig appConfig;
  22. @Autowired
  23. private SwimLogRepo swimLogRepo;
  24. @Autowired
  25. public void setDataSource(@Qualifier("mysqlSwimmingPoolDataSource") DataSource dataSource) {
  26. this.jdbcTemplate = new JdbcTemplate(dataSource);
  27. }
  28. private JdbcTemplate jdbcTemplate;
  29. public void fetchSwimData(String sql, String id) {
  30. System.out.println("SQL: " + sql);
  31. List<Map<String, Object>> results = jdbcTemplate.queryForList(sql);
  32. List<Map<String, Object>> swimDataList = new ArrayList<>();
  33. System.out.println("Records fetched: " + results.size());
  34. for (Map<String, Object> row : results) {
  35. try {
  36. Map<String, Object> data = new TreeMap<>();
  37. // Safe value extraction
  38. String bookingId = row.get("bookingid") != null ? row.get("bookingid").toString() : "";
  39. String paymentId = row.get("payment_id") != null ? row.get("payment_id").toString() : "";
  40. String paymentDate = row.get("paymentdate") != null ? row.get("paymentdate").toString() : "";
  41. String name = row.get("name") != null ? row.get("name").toString() : "";
  42. String mobile = row.get("mobile_no") != null ? row.get("mobile_no").toString() : "";
  43. String amountStr = row.get("amount") != null ? row.get("amount").toString() : "0";
  44. String orderid = row.get("orderid") != null ? row.get("orderid").toString() : "";
  45. double amount = 0;
  46. try {
  47. amount = Double.parseDouble(amountStr);
  48. } catch (NumberFormatException e) {
  49. System.err.println("Invalid amount for bookingId: " + bookingId);
  50. }
  51. String assessmentNo = LocalDate.now().getYear() + "/SWIM/" + bookingId;
  52. data.put("serviceId", "getOtherCollection");
  53. data.put("COLLECTION_TYPE", "Swimming Pool");
  54. data.put("TOTALTRANSACTIONCODE", "450310000");
  55. data.put("ASSESSMENTNO", assessmentNo);
  56. data.put("UNIQUERECEIPTID", bookingId);
  57. data.put("TRANSACTIONNO", orderid);
  58. data.put("TRANSACTIONREFNO", paymentId);
  59. data.put("TRANSACTIONDATE", paymentDate);
  60. data.put("TOTALTRANSACTIONAMOUNT", amount);
  61. data.put("MSTRCODEONE", "020");
  62. data.put("SPLITAMOUNTONE", amountStr);
  63. data.put("PARTYNAME", name);
  64. data.put("PARTYMOBILE", mobile);
  65. data.put("PARTYADDRESS", "NA");
  66. data.put("bookingId", bookingId);
  67. postDataToERP(data, id);
  68. } catch (Exception e) {
  69. System.err.println("Error processing row: " + row);
  70. e.printStackTrace();
  71. }
  72. }
  73. }
  74. private void postDataToERP(Map<String, Object> data, String id) {
  75. try {
  76. StringBuilder encodedUrl = new StringBuilder(appConfig.getErpUrl());
  77. boolean firstParam = true;
  78. for (Map.Entry<String, Object> entry : data.entrySet()) {
  79. if (!firstParam) encodedUrl.append("&");
  80. firstParam = false;
  81. String key = URLEncoder.encode(entry.getKey(), "UTF-8");
  82. String value = URLEncoder.encode(String.valueOf(entry.getValue()), "UTF-8");
  83. encodedUrl.append(key).append("=").append(value);
  84. }
  85. String finalUrl = encodedUrl.toString();
  86. System.out.println("Final Encoded URL: " + finalUrl);
  87. HttpURLConnection connection = (HttpURLConnection) new URL(finalUrl).openConnection();
  88. connection.setRequestMethod("GET");
  89. int responseCode = connection.getResponseCode();
  90. System.out.println("Response Code: " + responseCode);
  91. StringBuilder response = new StringBuilder();
  92. BufferedReader in = new BufferedReader(new InputStreamReader(
  93. responseCode == 200 ? connection.getInputStream() : connection.getErrorStream()
  94. ));
  95. String inputLine;
  96. while ((inputLine = in.readLine()) != null) response.append(inputLine);
  97. in.close();
  98. System.out.println("ERP Response: " + response);
  99. if (id != null) {
  100. updateLog(data, response, id);
  101. } else {
  102. saveLog(data, response);
  103. }
  104. } catch (Exception e) {
  105. e.printStackTrace();
  106. }
  107. }
  108. private void saveLog(Map<String, Object> map, StringBuilder response) throws JsonProcessingException {
  109. logToRepo(map, response);
  110. }
  111. private void updateLog(Map<String, Object> map, StringBuilder response, String id) throws JsonProcessingException {
  112. logToRepo(map, response, id);
  113. }
  114. private void logToRepo(Map<String, Object> map, StringBuilder response) throws JsonProcessingException {
  115. logToRepo(map, response, null);
  116. }
  117. private void logToRepo(Map<String, Object> map, StringBuilder response, String id) throws JsonProcessingException {
  118. String statusCode = "0";
  119. if (response != null) {
  120. ObjectMapper objectMapper = new ObjectMapper();
  121. Map<String, Object> res = objectMapper.readValue(response.toString(), Map.class);
  122. String msg = String.valueOf(res.get("Message"));
  123. String status = String.valueOf(res.getOrDefault("ResultStatus", ""));
  124. if ("Connection Failure".equals(msg)) {
  125. statusCode = "2";
  126. } else if ("Already Updated this collection".equals(msg) && "false".equals(status)) {
  127. statusCode = "1";
  128. } else if ("Failure".equals(msg) && "false".equals(status)) {
  129. statusCode = "2";
  130. } else if ("true".equals(status)) {
  131. statusCode = "1";
  132. }
  133. }
  134. map.put("status", statusCode);
  135. map.put("response", response.toString());
  136. if (id != null) {
  137. swimLogRepo.updateLog(statusCode, response.toString(), id);
  138. } else {
  139. swimLogRepo.insert(map);
  140. }
  141. }
  142. }