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.
174 lines
6.8 KiB
174 lines
6.8 KiB
package com.gcc.erppostdata.swimmingpool;
|
|
|
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
import com.gcc.erppostdata.config.AppConfig;
|
|
import com.gcc.erppostdata.repo.SwimLogRepo;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Qualifier;
|
|
import org.springframework.jdbc.core.JdbcTemplate;
|
|
import org.springframework.stereotype.Component;
|
|
|
|
import javax.sql.DataSource;
|
|
import java.io.BufferedReader;
|
|
import java.io.InputStreamReader;
|
|
import java.net.HttpURLConnection;
|
|
import java.net.URL;
|
|
import java.net.URLEncoder;
|
|
import java.time.LocalDate;
|
|
import java.util.*;
|
|
|
|
@Component
|
|
public class SWPostData {
|
|
|
|
@Autowired
|
|
private AppConfig appConfig;
|
|
|
|
@Autowired
|
|
private SwimLogRepo swimLogRepo;
|
|
|
|
@Autowired
|
|
public void setDataSource(@Qualifier("mysqlSwimmingPoolDataSource") DataSource dataSource) {
|
|
this.jdbcTemplate = new JdbcTemplate(dataSource);
|
|
}
|
|
|
|
private JdbcTemplate jdbcTemplate;
|
|
|
|
public void fetchSwimData(String sql, String id) {
|
|
System.out.println("SQL: " + sql);
|
|
|
|
List<Map<String, Object>> results = jdbcTemplate.queryForList(sql);
|
|
List<Map<String, Object>> swimDataList = new ArrayList<>();
|
|
|
|
System.out.println("Records fetched: " + results.size());
|
|
|
|
for (Map<String, Object> row : results) {
|
|
try {
|
|
Map<String, Object> data = new TreeMap<>();
|
|
|
|
// Safe value extraction
|
|
String bookingId = row.get("bookingid") != null ? row.get("bookingid").toString() : "";
|
|
String paymentId = row.get("payment_id") != null ? row.get("payment_id").toString() : "";
|
|
String paymentDate = row.get("paymentdate") != null ? row.get("paymentdate").toString() : "";
|
|
String name = row.get("name") != null ? row.get("name").toString() : "";
|
|
String mobile = row.get("mobile_no") != null ? row.get("mobile_no").toString() : "";
|
|
String amountStr = row.get("amount") != null ? row.get("amount").toString() : "0";
|
|
String orderid = row.get("orderid") != null ? row.get("orderid").toString() : "";
|
|
|
|
double amount = 0;
|
|
try {
|
|
amount = Double.parseDouble(amountStr);
|
|
} catch (NumberFormatException e) {
|
|
System.err.println("Invalid amount for bookingId: " + bookingId);
|
|
}
|
|
|
|
String assessmentNo = LocalDate.now().getYear() + "/SWIM/" + bookingId;
|
|
|
|
data.put("serviceId", "getOtherCollection");
|
|
data.put("COLLECTION_TYPE", "Swimming Pool");
|
|
data.put("TOTALTRANSACTIONCODE", "450310000");
|
|
data.put("ASSESSMENTNO", assessmentNo);
|
|
data.put("UNIQUERECEIPTID", bookingId);
|
|
data.put("TRANSACTIONNO", orderid);
|
|
data.put("TRANSACTIONREFNO", paymentId);
|
|
data.put("TRANSACTIONDATE", paymentDate);
|
|
data.put("TOTALTRANSACTIONAMOUNT", amount);
|
|
data.put("MSTRCODEONE", "020");
|
|
data.put("SPLITAMOUNTONE", amountStr);
|
|
data.put("PARTYNAME", name);
|
|
data.put("PARTYMOBILE", mobile);
|
|
data.put("PARTYADDRESS", "NA");
|
|
data.put("bookingId", bookingId);
|
|
|
|
postDataToERP(data, id);
|
|
|
|
} catch (Exception e) {
|
|
System.err.println("Error processing row: " + row);
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
private void postDataToERP(Map<String, Object> data, String id) {
|
|
try {
|
|
StringBuilder encodedUrl = new StringBuilder(appConfig.getErpUrl());
|
|
boolean firstParam = true;
|
|
|
|
for (Map.Entry<String, Object> entry : data.entrySet()) {
|
|
if (!firstParam) encodedUrl.append("&");
|
|
firstParam = false;
|
|
String key = URLEncoder.encode(entry.getKey(), "UTF-8");
|
|
String value = URLEncoder.encode(String.valueOf(entry.getValue()), "UTF-8");
|
|
encodedUrl.append(key).append("=").append(value);
|
|
}
|
|
|
|
String finalUrl = encodedUrl.toString();
|
|
System.out.println("Final Encoded URL: " + finalUrl);
|
|
|
|
HttpURLConnection connection = (HttpURLConnection) new URL(finalUrl).openConnection();
|
|
connection.setRequestMethod("GET");
|
|
int responseCode = connection.getResponseCode();
|
|
System.out.println("Response Code: " + responseCode);
|
|
|
|
StringBuilder response = new StringBuilder();
|
|
BufferedReader in = new BufferedReader(new InputStreamReader(
|
|
responseCode == 200 ? connection.getInputStream() : connection.getErrorStream()
|
|
));
|
|
String inputLine;
|
|
while ((inputLine = in.readLine()) != null) response.append(inputLine);
|
|
in.close();
|
|
|
|
System.out.println("ERP Response: " + response);
|
|
|
|
if (id != null) {
|
|
updateLog(data, response, id);
|
|
} else {
|
|
saveLog(data, response);
|
|
}
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
|
|
private void saveLog(Map<String, Object> map, StringBuilder response) throws JsonProcessingException {
|
|
logToRepo(map, response);
|
|
}
|
|
|
|
private void updateLog(Map<String, Object> map, StringBuilder response, String id) throws JsonProcessingException {
|
|
logToRepo(map, response, id);
|
|
}
|
|
|
|
private void logToRepo(Map<String, Object> map, StringBuilder response) throws JsonProcessingException {
|
|
logToRepo(map, response, null);
|
|
}
|
|
|
|
private void logToRepo(Map<String, Object> map, StringBuilder response, String id) throws JsonProcessingException {
|
|
String statusCode = "0";
|
|
if (response != null) {
|
|
ObjectMapper objectMapper = new ObjectMapper();
|
|
Map<String, Object> res = objectMapper.readValue(response.toString(), Map.class);
|
|
String msg = String.valueOf(res.get("Message"));
|
|
String status = String.valueOf(res.getOrDefault("ResultStatus", ""));
|
|
|
|
if ("Connection Failure".equals(msg)) {
|
|
statusCode = "2";
|
|
} else if ("Already Updated this collection".equals(msg) && "false".equals(status)) {
|
|
statusCode = "1";
|
|
} else if ("Failure".equals(msg) && "false".equals(status)) {
|
|
statusCode = "2";
|
|
} else if ("true".equals(status)) {
|
|
statusCode = "1";
|
|
}
|
|
}
|
|
|
|
map.put("status", statusCode);
|
|
map.put("response", response.toString());
|
|
|
|
if (id != null) {
|
|
swimLogRepo.updateLog(statusCode, response.toString(), id);
|
|
} else {
|
|
swimLogRepo.insert(map);
|
|
}
|
|
}
|
|
}
|