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.

229 lines
8.5 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. package com.gcc.erppostdata.mudhalvarpadaipagam;
  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.CoworkLogRepo;
  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.text.SimpleDateFormat;
  17. import java.time.LocalDate;
  18. import java.util.*;
  19. @Component
  20. public class CWPostData {
  21. @Autowired
  22. private JdbcTemplate jdbcTemplate;
  23. @Autowired
  24. private AppConfig appConfig;
  25. @Autowired
  26. private CoworkLogRepo coworkLogRepo;
  27. private List<Map<String, Object>> coworkDataList;
  28. // @PersistenceContext(unitName = "coworkspace")
  29. // private EntityManager entityManager;
  30. // @Autowired
  31. // @Qualifier("mysqlmudhalvarpadaipagamDataSource")
  32. // private EntityManager entityManager;
  33. @Autowired
  34. public void setDataSource(@Qualifier("mysqlmudhalvarpadaipagamDataSource") DataSource dataSource) {
  35. this.jdbcTemplate = new JdbcTemplate(dataSource);
  36. }
  37. public void fetchCoworkData(String sql, String id) {
  38. System.out.println("SQL: " + sql);
  39. coworkDataList = new ArrayList<>();
  40. List<Map<String, Object>> results = jdbcTemplate.queryForList(sql);
  41. for (Map<String, Object> row : results) {
  42. Map<String, Object> data = new TreeMap<>();
  43. String assessmentNo = LocalDate.now().getYear() + "/MUTHAPADAI/" + row.get("booking_id").toString();
  44. // Static mandatory input
  45. data.put("serviceId", "getOtherCollection");
  46. data.put("COLLECTION_TYPE", "Muthalvar Padaippagam");
  47. data.put("TOTALTRANSACTIONCODE", "450310000");
  48. // Unique data
  49. data.put("ASSESSMENTNO", assessmentNo);
  50. data.put("UNIQUERECEIPTID", row.get("booking_id").toString());
  51. data.put("TRANSACTIONNO", row.get("order_id").toString());
  52. data.put("TRANSACTIONREFNO", row.get("payment_id").toString());
  53. data.put("TRANSACTIONDATE", row.get("paymentdate").toString());
  54. data.put("TOTALTRANSACTIONAMOUNT", Double.parseDouble(row.get("amount").toString()));
  55. // Amount breakdown
  56. data.put("MSTRCODEONE", "019");
  57. data.put("SPLITAMOUNTONE", row.get("amount").toString());
  58. // Party details
  59. data.put("PARTYNAME", row.get("user_name").toString());
  60. data.put("PARTYMOBILE", row.get("mobile").toString());
  61. data.put("PARTYADDRESS", "NA");// For internal tracking
  62. coworkDataList.add(data);
  63. }
  64. postDataToERP(coworkDataList, id);
  65. }
  66. public void postDataToERP(List<Map<String, Object>> coworkList, String id) {
  67. for (Map<String, Object> data : coworkList) {
  68. StringBuilder response = new StringBuilder();
  69. System.out.println("Sending Data: " + data);
  70. StringBuilder encodedUrl = new StringBuilder(appConfig.getErpUrl());
  71. try {
  72. boolean firstParam = true;
  73. // SimpleDateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
  74. //
  75. // SimpleDateFormat outputFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
  76. //
  77. // outputFormat.setTimeZone(TimeZone.getTimeZone("Asia/Kolkata"));
  78. for (Map.Entry<String, Object> entry : data.entrySet()) {
  79. if (!firstParam) {
  80. encodedUrl.append("&");
  81. }
  82. firstParam = false;
  83. String key = URLEncoder.encode(entry.getKey(), "UTF-8");
  84. String value = "";
  85. if (entry.getValue() == null) {
  86. value = "";
  87. } else {
  88. value = entry.getValue().toString();
  89. // if (entry.getKey().equals("TRANSACTIONDATE")) {
  90. // try {
  91. // Date date = inputFormat.parse(value);
  92. // value = outputFormat.format(date);
  93. // } catch (Exception e) {
  94. // System.out.println("Error parsing TRANSACTIONDATE: " + value);
  95. // }
  96. // }
  97. }
  98. encodedUrl.append(key).append("=").append(URLEncoder.encode(value, "UTF-8"));
  99. }
  100. String finalUrl = encodedUrl.toString();
  101. System.out.println("Final Encoded URL: " + finalUrl);
  102. HttpURLConnection connection = (HttpURLConnection) new URL(finalUrl).openConnection();
  103. connection.setRequestMethod("GET");
  104. int responseCode = connection.getResponseCode();
  105. System.out.println("Response Code: " + responseCode);
  106. if (responseCode == HttpURLConnection.HTTP_OK) {
  107. BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
  108. String inputLine;
  109. while ((inputLine = in.readLine()) != null) {
  110. response.append(inputLine);
  111. }
  112. in.close();
  113. } else {
  114. System.out.println("GET request failed.");
  115. }
  116. if (id != null) {
  117. updateTheLog(data, response, id);
  118. } else {
  119. saveTheLog(data, response);
  120. }
  121. } catch (Exception e) {
  122. try {
  123. if (id != null) {
  124. updateTheLog(data, response, id);
  125. } else {
  126. saveTheLog(data, response);
  127. }
  128. } catch (Exception ex) {
  129. ex.printStackTrace();
  130. }
  131. e.printStackTrace();
  132. }
  133. }
  134. }
  135. public void saveTheLog(Map<String, Object> map, StringBuilder response) throws JsonProcessingException {
  136. String status = "", msgStatus = "";
  137. if (response != null) {
  138. ObjectMapper objectMapper = new ObjectMapper();
  139. Map<String, Object> res = objectMapper.readValue(response.toString(), Map.class);
  140. msgStatus = String.valueOf(res.get("Message"));
  141. if (res.get("ResultStatus") != null) {
  142. status = String.valueOf(res.get("ResultStatus"));
  143. }
  144. }
  145. System.out.println("ResultStatus: " + status + " msgStatus: " + msgStatus);
  146. String statusCode = "0";
  147. if ("Connection Failure".equals(msgStatus)) {
  148. statusCode = "2";
  149. } else if ("Already Updated this collection".equals(msgStatus) && "false".equals(status)) {
  150. statusCode = "1";
  151. } else if ("Failure".equals(msgStatus) && "false".equals(status)) {
  152. statusCode = "2";
  153. } else if ("true".equals(status)) {
  154. statusCode = "1";
  155. }
  156. map.put("status", statusCode);
  157. map.put("response", response.toString());
  158. coworkLogRepo.insert(map);
  159. }
  160. public void updateTheLog(Map<String, Object> map, StringBuilder response, String id) throws JsonProcessingException {
  161. String status = "", msgStatus = "";
  162. if (response != null) {
  163. ObjectMapper objectMapper = new ObjectMapper();
  164. Map<String, Object> res = objectMapper.readValue(response.toString(), Map.class);
  165. msgStatus = String.valueOf(res.get("Message"));
  166. if (res.get("ResultStatus") != null) {
  167. status = String.valueOf(res.get("ResultStatus"));
  168. }
  169. }
  170. System.out.println("ResultStatus: " + status + " msgStatus: " + msgStatus);
  171. String statusCode = "0";
  172. if ("Connection Failure".equals(msgStatus)) {
  173. statusCode = "2";
  174. } else if ("Already Updated this collection".equals(msgStatus) && "false".equals(status)) {
  175. statusCode = "1";
  176. } else if ("Failure".equals(msgStatus) && "false".equals(status)) {
  177. statusCode = "2";
  178. } else if ("true".equals(status)) {
  179. statusCode = "1";
  180. }
  181. coworkLogRepo.updateLog(statusCode, response.toString(), id);
  182. }
  183. }