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.

161 lines
6.5 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. package in.gov.chennaicorporation.gccoffice.cdwaste.service;
  2. import java.net.URLEncoder;
  3. import java.nio.charset.StandardCharsets;
  4. import java.time.LocalDate;
  5. import java.time.LocalDateTime;
  6. import java.time.LocalTime;
  7. import java.util.ArrayList;
  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.stereotype.Service;
  15. import org.springframework.transaction.annotation.Transactional;
  16. import org.springframework.web.client.RestTemplate;
  17. @Service
  18. public class OfficerService {
  19. @Autowired
  20. private JdbcTemplate jdbcTemplate;
  21. @Autowired
  22. private RestTemplate restTemplate;
  23. @Autowired
  24. private SMSService smsService;
  25. @Autowired
  26. public void setDataSourcemysql(@Qualifier("mysqlCDManagerSystemDataSource") DataSource dataSource) {
  27. this.jdbcTemplate = new JdbcTemplate(dataSource);
  28. }
  29. @Transactional
  30. public void saveRequestDetails(Map<String, Object> payload) {
  31. double actualWeight = Double.parseDouble(payload.get("actualWeight").toString());
  32. String paymentType = payload.get("paymentType").toString();
  33. /*
  34. * double processingCharge =
  35. * Double.parseDouble(payload.get("processingCharge").toString()); double
  36. * wasteCharge = Double.parseDouble(payload.get("wasteCharge").toString());
  37. */
  38. double processingCharge = payload.get("processingCharge") != null
  39. ? Double.parseDouble(payload.get("processingCharge").toString())
  40. : 0.0;
  41. double wasteCharge = payload.get("wasteCharge") != null
  42. ? Double.parseDouble(payload.get("wasteCharge").toString())
  43. : 0.0;
  44. double totalCharge = Double.parseDouble(payload.get("totalCharge").toString());
  45. String paymentMobNo = payload.get("paymentMobileNo") != null ? payload.get("paymentMobileNo").toString() : null;
  46. String refId = payload.get("refId") != null ? payload.get("refId").toString() : null;
  47. // ✅ Check if record exists
  48. String checkSql = "SELECT COUNT(*) FROM waste_collectors_request WHERE refid = ?";
  49. Integer count = jdbcTemplate.queryForObject(checkSql, new Object[]{refId}, Integer.class);
  50. if (count != null && count > 0) {
  51. // ✅ Update the existing record
  52. String updateSql = "UPDATE waste_collectors_request SET actual_weight = ?, payment_type = ?, processing_charge = ?, waste_collection_charge = ?, total_charge = ?, payment_mobile_no = ? WHERE refid = ?";
  53. jdbcTemplate.update(updateSql, actualWeight, paymentType, processingCharge, wasteCharge, totalCharge, paymentMobNo, refId);
  54. // ✅ Send WhatsApp message if payment type is 'generateLink' and mobile number is provided
  55. if ("generateLink".equalsIgnoreCase(paymentType) && paymentMobNo != null && !paymentMobNo.isBlank()) {
  56. sendWhatsappMessage(refId, actualWeight, totalCharge, paymentMobNo);
  57. }
  58. } else {
  59. throw new RuntimeException("No request found for Ref ID: " + refId);
  60. }
  61. }
  62. public void sendWhatsappMessage(String refId, double actualWeight, double totalCharge, String mobileNumber) {
  63. try {
  64. String message = String.format(
  65. "Your request (Request ID: *%s*) has been processed. Please proceed with the payment.\n\nActual Weight: *%.0f kg*\nAmount Payable: *%.0f*",
  66. refId, actualWeight, totalCharge
  67. );
  68. String encodedMessage = URLEncoder.encode(message, StandardCharsets.UTF_8);
  69. String encodedHeader = URLEncoder.encode("C&D Waste Collection – Payment Request", StandardCharsets.UTF_8);
  70. String encodedFooter = URLEncoder.encode("Greater Chennai Corporation", StandardCharsets.UTF_8);
  71. String redirectUrlParam = URLEncoder.encode("dumpregistration/paymentform?requestId=" + refId, StandardCharsets.UTF_8);
  72. String apiUrl = "https://media.smsgupshup.com/GatewayAPI/rest?userid=2000233507" +
  73. "&password=h2YjFNcJ" +
  74. "&send_to=" + mobileNumber +
  75. "&v=1.1&format=json&msg_type=TEXT&method=SENDMESSAGE" +
  76. "&msg=" + encodedMessage +
  77. "&isTemplate=true" +
  78. "&header=" + encodedHeader +
  79. "&footer=" + encodedFooter +
  80. "&buttonUrlParam=" + redirectUrlParam;
  81. // Optional: log URL before hitting for debugging
  82. System.out.println("WhatsApp API URL: " + apiUrl);
  83. // Make API call
  84. // restTemplate.getForObject(apiUrl, String.class);
  85. smsService.sendMsg(apiUrl);
  86. } catch (Exception e) {
  87. e.printStackTrace();
  88. // Optionally log or alert failure
  89. }
  90. }
  91. public List<Map<String, Object>> getAllRequestDetails(LocalDate fromDate, LocalDate toDate, String wastequantity, String zone) {
  92. StringBuilder query = new StringBuilder(
  93. "SELECT wcr.id, wcr.name, wcr.mobile_no, wcr.area, wcr.zone, qm.quantity, " +
  94. "wtm.waste_type, dl.location, wcr.actual_weight, wcr.payment_status,wcr.waste_collection_charge, " +
  95. "wcr.processing_charge, wcr.total_charge, DATE_FORMAT(wcr.cdate, '%d-%m-%Y') AS request_date, wcr.refid " +
  96. "FROM waste_collectors_request wcr " +
  97. "JOIN dumping_location dl ON wcr.dump_location_id = dl.id " +
  98. "JOIN waste_type_master wtm ON wcr.waste_type_id = wtm.id " +
  99. "JOIN quantity_master qm ON wcr.waste_quantity_id = qm.id " +
  100. "WHERE wcr.cdate BETWEEN ? AND ?");
  101. LocalDateTime fromDateTime = fromDate.atStartOfDay();
  102. LocalDateTime toDateTime = toDate.atTime(LocalTime.MAX);
  103. List<Object> params = new ArrayList<>();
  104. params.add(fromDateTime);
  105. params.add(toDateTime);
  106. if (wastequantity != null && !wastequantity.isEmpty()) {
  107. query.append(" AND qm.quantity = ?");
  108. params.add(wastequantity);
  109. }
  110. if (zone != null && !zone.isEmpty()) {
  111. query.append(" AND wcr.zone = ?");
  112. params.add(zone);
  113. }
  114. return jdbcTemplate.queryForList(query.toString(), params.toArray());
  115. }
  116. public List<Map<String, Object>> getAllwastequantity() {
  117. String sql = " SELECT id,quantity FROM quantity_master where is_active = 1 ";
  118. return jdbcTemplate.queryForList(sql);
  119. }
  120. public List<Map<String, Object>> getZonesList() {
  121. String sql = " SELECT id,zone_name from zones where is_active = 1 ";
  122. return jdbcTemplate.queryForList(sql);
  123. }
  124. }