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.

498 lines
22 KiB

2 years ago
​
​
2 years ago
2 years ago
​
​
​
​
​
2 years ago
​
​
​
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
2 years ago
​
​
​
​
​
2 years ago
​
2 years ago
​
​
2 years ago
​
​
​
​
2 years ago
​
​
2 years ago
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
​
​
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
  1. package com.gcc.communityHall.controller;
  2. import java.net.URLEncoder;
  3. import java.nio.charset.StandardCharsets;
  4. import java.time.LocalDate;
  5. import java.util.ArrayList;
  6. import java.util.Collections;
  7. import java.util.HashMap;
  8. import java.util.HashSet;
  9. import java.util.List;
  10. import java.util.Map;
  11. import org.springframework.beans.factory.annotation.Autowired;
  12. import org.springframework.http.HttpStatus;
  13. import org.springframework.http.ResponseEntity;
  14. import org.springframework.stereotype.Controller;
  15. import org.springframework.ui.Model;
  16. import org.springframework.web.bind.annotation.GetMapping;
  17. import org.springframework.web.bind.annotation.ModelAttribute;
  18. import org.springframework.web.bind.annotation.PostMapping;
  19. import org.springframework.web.bind.annotation.RequestMapping;
  20. import org.springframework.web.bind.annotation.RequestParam;
  21. import org.springframework.web.bind.annotation.ResponseBody;
  22. import com.gcc.communityHall.entity.BookingCalendar;
  23. import com.gcc.communityHall.entity.BookingDetails;
  24. import com.gcc.communityHall.entity.HallDetails;
  25. import com.gcc.communityHall.entity.Notification;
  26. import com.gcc.communityHall.entity.Users;
  27. import com.gcc.communityHall.entity.Users.userType;
  28. import com.gcc.communityHall.others.UserSession;
  29. import com.gcc.communityHall.repository.HallDetailsRepository;
  30. import com.gcc.communityHall.repository.NotificationRepository;
  31. import com.gcc.communityHall.service.BookingService;
  32. import com.gcc.communityHall.service.CalendarService;
  33. import com.gcc.communityHall.service.HallDetailService;
  34. import com.gcc.communityHall.service.UserService;
  35. import com.gcc.communityHall.service.Impl.WhatsappMsgServiceImpl;
  36. @Controller
  37. @RequestMapping(value = { "/communityhall/user", "/communityhall", "", "/" })
  38. public class UserController {
  39. @Autowired
  40. UserSession userSession;
  41. @Autowired
  42. private BookingService bookingService;
  43. @Autowired
  44. private HallDetailsRepository hallDetailsRepository;
  45. @Autowired
  46. private HallDetailService hallDetailsService;
  47. @Autowired
  48. private UserService userService;
  49. @Autowired
  50. CalendarService calendarService;
  51. @Autowired
  52. private WhatsappMsgServiceImpl whatsappMsgServiceImpl;
  53. @Autowired
  54. private HallDetailService hallDetailService;
  55. @Autowired
  56. private NotificationRepository notificationRepository;
  57. @GetMapping(value = { "", "/", "/dashboard", "/index" })
  58. public String viewDashboard(Model model) {
  59. System.out.println("User in dashboard / index Page");
  60. List<HallDetails> halls = hallDetailsRepository.findActiveHalls();
  61. List<HallDetails> hallnames = hallDetailsService.getHallNames();
  62. for (HallDetails hall : halls) {
  63. if (hall.getHallImagesList() != null && !hall.getHallImagesList().isEmpty()) {
  64. model.addAttribute("hasImages", true);
  65. break;
  66. }
  67. }
  68. List<Notification> newnotification = notificationRepository.findActivenotifications();
  69. // Create a string to concatenate all notifications descriptions
  70. StringBuilder notificationText = new StringBuilder();
  71. for (Notification notification : newnotification) {
  72. notificationText.append(notification.getNotification_description());
  73. }
  74. // System.out.println("user controller \nCounter Id:
  75. // "+userSession.getCounter()+"\nCounter Name: "+userSession.getCounterName());
  76. // Pass the concatenated string to the model
  77. model.addAttribute("newnotification", notificationText.toString());
  78. model.addAttribute("userName", userSession.getDispayName());
  79. model.addAttribute("halls", halls);
  80. model.addAttribute("hallnames", hallnames);
  81. model.addAttribute("serverDate", LocalDate.now().toString()); // for server date
  82. model.addAttribute("counter", userSession.getCounter());
  83. model.addAttribute("counterName", userSession.getCounterName());
  84. return "user/dashboard";
  85. }
  86. // Checkavilability Api
  87. @GetMapping("/checkAvail")
  88. public String checkAvail(@RequestParam("fromDate") String fromDate,
  89. @RequestParam("toDate") String toDate,
  90. @RequestParam("starting") String starting,
  91. @RequestParam("ending") String ending,
  92. @RequestParam(name = "hallId", required = false) String hallId,
  93. @RequestParam(name = "zone", required = false) String zone,
  94. Model model) {
  95. System.out.println("User Checking availability for hall Id:" + hallId);
  96. List<Notification> newnotification = notificationRepository.findActivenotifications();
  97. // Create a string to concatenate all notifications descriptions
  98. StringBuilder notificationText = new StringBuilder();
  99. for (Notification notification : newnotification) {
  100. notificationText.append(notification.getNotification_description());
  101. }
  102. // Pass the concatenated string to the model
  103. model.addAttribute("newnotification", notificationText.toString());
  104. List<HallDetails> hallnames = hallDetailService.getHallNames();
  105. List<HallDetails> hallsByZone;
  106. int hallIdd = 0;
  107. // Parse hallId if provided
  108. if (hallId != null && !hallId.isEmpty()) {
  109. try {
  110. hallIdd = Integer.parseInt(hallId);
  111. } catch (NumberFormatException e) {
  112. System.err.println("Invalid hallId: " + hallId);
  113. }
  114. }
  115. // Fetch halls by zone if zone is provided
  116. if (zone != null && !zone.isEmpty()) {
  117. hallsByZone = hallDetailsRepository.findByZone(zone);
  118. } else {
  119. hallsByZone = hallDetailsRepository.findActiveHalls();
  120. }
  121. // Fetch calendar data based on provided parameters
  122. List<BookingCalendar> data = calendarService.getCalendarByDateAndHallIdAndSlot(fromDate, toDate, starting,
  123. ending, hallIdd);
  124. HashSet<Integer> removeHallId = new HashSet<>();
  125. // Collect hall IDs to be removed
  126. for (BookingCalendar calendar : data) {
  127. boolean isSameDay = fromDate.equals(toDate) && fromDate.equals(calendar.getBdate().toString());
  128. boolean isMorningStart = starting.equals("1");
  129. boolean isMorningEnd = ending.equals("1");
  130. boolean isEveningStart = starting.equals("2");
  131. boolean isEveningEnd = ending.equals("2");
  132. if (isSameDay) {
  133. // Same day booking cases
  134. if (isMorningStart && isMorningEnd
  135. && (calendar.getBdate().toString().equals(fromDate) && calendar.isMorningSlot())) {
  136. // System.out.println("Same day morning-morning match found: hallId=" +
  137. // calendar.getHallId());
  138. removeHallId.add(calendar.getHallId());
  139. } else if (isEveningStart && isEveningEnd
  140. && (calendar.getBdate().toString().equals(fromDate) && calendar.isEveningSlot())) {
  141. // System.out.println("Same day evening-evening match found: hallId=" +
  142. // calendar.getHallId());
  143. removeHallId.add(calendar.getHallId());
  144. } else if (isMorningStart && isEveningEnd &&
  145. ((calendar.getBdate().toString().equals(fromDate) && calendar.isMorningSlot()) ||
  146. (calendar.getBdate().toString().equals(toDate) && calendar.isEveningSlot()))) {
  147. // System.out.println("Same day morning-evening match found: hallId=" +
  148. // calendar.getHallId());
  149. removeHallId.add(calendar.getHallId());
  150. }
  151. } else {
  152. // Multi-day booking cases
  153. if ((isMorningStart && isMorningEnd) &&
  154. ((calendar.getBdate().toString().equals(fromDate) && calendar.isMorningSlot()) ||
  155. (calendar.getBdate().toString().equals(toDate) && calendar.isMorningSlot()))) {
  156. // System.out.println("Multi-day morning-morning match found: hallId=" +
  157. // calendar.getHallId());
  158. removeHallId.add(calendar.getHallId());
  159. } else if ((isEveningStart && isEveningEnd) &&
  160. ((calendar.getBdate().toString().equals(fromDate) && calendar.isEveningSlot()) ||
  161. (calendar.getBdate().toString().equals(toDate) && calendar.isEveningSlot()))) {
  162. // System.out.println("Multi-day evening-evening match found: hallId=" +
  163. // calendar.getHallId());
  164. removeHallId.add(calendar.getHallId());
  165. } else if (isMorningStart && isEveningEnd &&
  166. ((calendar.getBdate().toString().equals(fromDate) && calendar.isMorningSlot()) ||
  167. (calendar.getBdate().toString().equals(toDate) && calendar.isEveningSlot()))) {
  168. // System.out.println("Multi-day morning-evening match found: hallId=" +
  169. // calendar.getHallId());
  170. removeHallId.add(calendar.getHallId());
  171. } else if (isEveningStart && isMorningEnd &&
  172. ((calendar.getBdate().toString().equals(fromDate) && calendar.isEveningSlot()) ||
  173. (calendar.getBdate().toString().equals(toDate) && calendar.isMorningSlot()))) {
  174. // System.out.println("Multi-day evening-morning match found: hallId=" +
  175. // calendar.getHallId());
  176. removeHallId.add(calendar.getHallId());
  177. }
  178. }
  179. }
  180. // Remove unavailable halls from the list
  181. hallsByZone.removeIf(hall -> removeHallId.contains(hall.getHallId()));
  182. List<HallDetails> removeHallById = new ArrayList<>();
  183. if (hallIdd != 0) {
  184. HallDetails hallDetails = hallDetailService.getHallById(hallIdd);
  185. // Ensure the hall is available before adding it to the list
  186. if (!removeHallId.contains(hallIdd)) {
  187. removeHallById.add(hallDetails);
  188. }
  189. } else {
  190. removeHallById = hallDetailService.removeHallById(removeHallId);
  191. }
  192. // Populate the model attributes
  193. model.addAttribute("userName", userSession.getDispayName());
  194. model.addAttribute("hallnames", hallnames);
  195. model.addAttribute("fromDate", fromDate);
  196. model.addAttribute("toDate", toDate);
  197. model.addAttribute("startingSlot", starting.equals("1") ? "Morning" : "Evening");
  198. model.addAttribute("endingSlot", ending.equals("1") ? "Morning" : "Evening");
  199. model.addAttribute("halls", (hallId != null && !hallId.isEmpty()) ? removeHallById : hallsByZone);
  200. model.addAttribute("counter", userSession.getCounter());
  201. model.addAttribute("counterName", userSession.getCounterName());
  202. return "user/dashboard";
  203. }
  204. @PostMapping("/newregistrationform")
  205. public String showRegistrationForm(@ModelAttribute("newform") Users newform, Model model) {
  206. System.out.println("New User Registration Page");
  207. Users user = userService.user(newform);
  208. userSession.setMobileno(user.getMobileNo());
  209. userSession.setDispayName(user.getName());
  210. userSession.setUserId(user.getUserId());
  211. userSession.setUserType(user.getUsertype().toString());
  212. System.out.println("usetrtype==" + user.getUsertype().toString());
  213. // System.out.println("employee id"+user.getGovernmentId());
  214. // System.out.println("employee id"+user.getDepartment());
  215. String apikey = "5c995535-6244-11f0-98fc-02c8a5e042bd";
  216. String from = "919445061913";
  217. String placeholders = "";
  218. String mobileNo = String.valueOf(user.getMobileNo());
  219. System.out.println("mobileNo>>>>>>>>>>>" + mobileNo);
  220. if (user.getUsertype() == userType.PUBLIC) {
  221. String registersms = "Welcome to GCC, Your User Registration for booking Community Centre is successful.";
  222. String registerurlString = "https://tmegov.onex-aura.com/api/sms?key=pfTEYN6H&to=" + user.getMobileNo()
  223. + "&from=GCCCRP&body=" + registersms
  224. + "&entityid=1401572690000011081&templateid=1407172346621223019";
  225. placeholders = URLEncoder.encode(mobileNo, StandardCharsets.UTF_8);
  226. String registerwhatsupmsg = "https://sendapiv1.pinbot.ai/pinwa/sendMessage?apikey=" + apikey + "&from="
  227. + from + "&to=91" + user.getMobileNo() + ""
  228. + "&type=template&templateid=3315165 ";
  229. // String registerwhatsupmsg =
  230. // "https://media.smsgupshup.com/GatewayAPI/rest?userid=2000233507&password=h2YjFNcJ&send_to="
  231. // + user.getMobileNo()
  232. // +
  233. // "&v=1.1&format=json&msg_type=TEXT&method=SENDMESSAGE&msg=Welcome+to+GCC%2C+%0A%0AYour+User+Registration+for+booking+Community+Centre+is+successful.";
  234. whatsappMsgServiceImpl.sendMsg(registerwhatsupmsg);
  235. whatsappMsgServiceImpl.sendSMS(registerurlString);
  236. } else {
  237. String registerpendingsms = "Welcome to GCC, Your User Registration request for booking Community Centre is submitted successfully.";
  238. String registerpendingurlString = "https://tmegov.onex-aura.com/api/sms?key=pfTEYN6H&to="
  239. + user.getMobileNo() + "&from=GCCCRP&body=" + registerpendingsms
  240. + "&entityid=1401572690000011081&templateid=1407172346629919381";
  241. placeholders = URLEncoder.encode(mobileNo, StandardCharsets.UTF_8);
  242. String registerpendingwhatsupmsg = "https://sendapiv1.pinbot.ai/pinwa/sendMessage?apikey=" + apikey
  243. + "&from="
  244. + from + "&to=91" + user.getMobileNo() + ""
  245. + "&type=template&templateid=3315168 ";
  246. // String registerpendingwhatsupmsg =
  247. // "https://media.smsgupshup.com/GatewayAPI/rest?userid=2000233507&password=h2YjFNcJ&send_to="
  248. // + user.getMobileNo()
  249. // +
  250. // "&v=1.1&format=json&msg_type=TEXT&method=SENDMESSAGE&msg=Welcome+to+GCC%2C+%0A%0AYour+User+Registration+request+for+booking+Community+Centre+submitted+successfully.";
  251. whatsappMsgServiceImpl.sendMsg(registerpendingwhatsupmsg);
  252. whatsappMsgServiceImpl.sendSMS(registerpendingurlString);
  253. }
  254. return "redirect:/communityhall/dashboard";
  255. }
  256. @GetMapping("/hallbookingform")
  257. public String viewform(Model model) {
  258. String username = userSession.getDispayName();
  259. System.out.println("User in Hall Booking Form Page");
  260. model.addAttribute("serverDate", LocalDate.now().toString()); // for server date
  261. model.addAttribute("userName", username);
  262. model.addAttribute("counter", userSession.getCounter());
  263. model.addAttribute("counterName", userSession.getCounterName());
  264. return "user/hallbookingform";
  265. }
  266. // ak
  267. @GetMapping("/acknowledgementwithoutrefid")
  268. public String viewakowledgement(Model model, int bookingId) {
  269. System.out.println("User in Acknowledgement without Ref ID Page");
  270. BookingDetails bookingsDetails = bookingService.getBookingsById(bookingId);
  271. model.addAttribute("bookingsDetails", bookingsDetails); // Add single BookingDetails object
  272. model.addAttribute("userName", userSession.getDispayName());
  273. return "user/acknowledgement";
  274. }
  275. // ak
  276. @GetMapping("/acknowledgement")
  277. public String viewAcknowledgement(@RequestParam("ref_id") String refId, Model model) {
  278. System.out.println("User in Acknowledgement with Ref ID Page");
  279. // Fetch booking using booking_ref_id
  280. BookingDetails bookingsDetails = bookingService.getBookingByRefId(refId);
  281. if (bookingsDetails == null) {
  282. model.addAttribute("errorMsg", "Invalid booking reference number.");
  283. return "user/error";
  284. }
  285. model.addAttribute("bookingsDetails", bookingsDetails);
  286. model.addAttribute("userName", userSession.getDispayName());
  287. return "user/acknowledgement";
  288. }
  289. // ak
  290. @PostMapping("/migrate-old-bookings")
  291. @ResponseBody
  292. public ResponseEntity<String> migrateOldBookings() {
  293. try {
  294. bookingService.migrateOldBookingsToRefId();
  295. return ResponseEntity.ok("Migration completed successfully. All old bookings now have ref_ids.");
  296. } catch (Exception e) {
  297. e.printStackTrace();
  298. return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
  299. .body("Migration failed: " + e.getMessage());
  300. }
  301. }
  302. @GetMapping("/logout")
  303. public String logout(Model model) {
  304. // Clear all login session (Check API Controller /gcc-login)
  305. userSession.setUserId(null);
  306. userSession.setMobileno(null);
  307. userSession.setUserType(null);
  308. userSession.setDispayName(null);
  309. return "redirect:/communityhall/user/dashboard";
  310. }
  311. /*
  312. * @GetMapping("/viewhistory")
  313. * public String viewBookingDetails(Model model, @RequestParam(name =
  314. * "bookingStatus", required = false) String bookingStatus) {
  315. * // Retrieve userId from session
  316. * Integer userId = userSession.getUserId();
  317. * model.addAttribute("userName", userSession.getDispayName());
  318. *
  319. * //List<String> test = new ArrayList<>();
  320. * model.addAttribute("userName", userSession.getDispayName());
  321. * model.addAttribute("counter", userSession.getCounter());
  322. * model.addAttribute("counterName", userSession.getCounterName());
  323. *
  324. * List<Map<String, Object>> bookingPast =
  325. * bookingService.getAllBookingEventsByUserId(userId);
  326. *
  327. * model.addAttribute("userBookingspast", bookingPast);
  328. * model.addAttribute("serverDate", LocalDate.now().toString()); //for server
  329. * date
  330. *
  331. * return "user/viewhistory";
  332. * }
  333. */
  334. // ak
  335. @GetMapping("/viewhistory")
  336. public String viewBookingDetails(Model model,
  337. @RequestParam(name = "bookingStatus", required = false) String bookingStatus) {
  338. System.out.println("User in View History Page");
  339. Integer userId = userSession.getUserId();
  340. // Extract booking_ref_id per booking
  341. Map<Integer, String> refIdMap = new HashMap<>();
  342. model.addAttribute("userName", userSession.getDispayName());
  343. model.addAttribute("counter", userSession.getCounter());
  344. model.addAttribute("counterName", userSession.getCounterName());
  345. // Fetch all past bookings
  346. // List<Map<String, Object>> bookingPast =
  347. // bookingService.getAllBookingEventsByUserId(userId);
  348. // if (!bookingPast.isEmpty() || bookingPast.size() == 0) {
  349. // for (Map<String, Object> row : bookingPast) {
  350. // Integer bookingId = (Integer) row.get("booking_id");
  351. // String refId = (String) row.get("booking_ref_id"); // <-- coming from SQL
  352. // if (bookingId != null && refId != null) {
  353. // refIdMap.put(bookingId, refId);
  354. // }
  355. // }
  356. // System.out.println("Row keys: " + bookingPast.get(0).keySet());
  357. // System.out.println("RefId Map123: " + refIdMap);
  358. // }
  359. // model.addAttribute("userBookingspast", bookingPast);
  360. // model.addAttribute("bookingRefIds", refIdMap); // <-- SENT TO FRONTEND
  361. // model.addAttribute("serverDate", LocalDate.now().toString());
  362. List<Map<String, Object>> bookingPast = bookingService.getAllBookingEventsByUserId(userId);
  363. if (bookingPast != null && !bookingPast.isEmpty()) {
  364. for (Map<String, Object> row : bookingPast) {
  365. Integer bookingId = row.get("booking_id") != null
  366. ? ((Number) row.get("booking_id")).intValue()
  367. : null;
  368. String refId = row.get("booking_ref_id") != null
  369. ? row.get("booking_ref_id").toString()
  370. : null;
  371. if (bookingId != null && refId != null) {
  372. refIdMap.put(bookingId, refId);
  373. }
  374. }
  375. } else {
  376. System.out.println("No past bookings found for userId: " + userId);
  377. }
  378. model.addAttribute("userBookingspast", bookingPast != null ? bookingPast : Collections.emptyList());
  379. model.addAttribute("bookingRefIds", refIdMap);
  380. model.addAttribute("serverDate", LocalDate.now().toString());
  381. return "user/viewhistory";
  382. }
  383. @GetMapping("/CommunityHallList")
  384. public String viewhalllist(Model model) {
  385. System.out.println("User in View Hall List Page");
  386. model.addAttribute("userName", userSession.getDispayName());
  387. model.addAttribute("counter", userSession.getCounter());
  388. model.addAttribute("counterName", userSession.getCounterName());
  389. return "user/communityHallList";
  390. }
  391. @GetMapping("/hallAmountCalculation")
  392. public String viewCalculate(Model model) {
  393. System.out.println("User in Calculate Hall Amount Page");
  394. model.addAttribute("userName", userSession.getDispayName());
  395. model.addAttribute("counter", userSession.getCounter());
  396. model.addAttribute("counterName", userSession.getCounterName());
  397. return "user/calculatehallamount";
  398. }
  399. @GetMapping("/collectionpending")
  400. public String viewCollectionpending(Model model, int bookingId) {
  401. System.out.println("User in Collection Pending Page");
  402. BookingDetails bookingsDetails = bookingService.getBookingsById(bookingId);
  403. model.addAttribute("bookingsDetails", bookingsDetails); // Add single BookingDetails object
  404. model.addAttribute("userName", userSession.getDispayName());
  405. return "user/collectionpending";
  406. }
  407. @GetMapping("/collectionpendingacknowledgement")
  408. public String viewCollectionpendingAck(Model model, int bookingId) {
  409. System.out.println("User in Collection Pending Acknowledgement Page");
  410. BookingDetails bookingsDetails = bookingService.getBookingsById(bookingId);
  411. model.addAttribute("bookingsDetails", bookingsDetails); // Add single BookingDetails object
  412. model.addAttribute("userName", userSession.getDispayName());
  413. return "user/extraebdamageamountack";
  414. }
  415. @GetMapping("/viewhallform")
  416. public String viewhallform(Model model) {
  417. System.out.println("User in View Hall Form Page");
  418. String username = userSession.getDispayName();
  419. model.addAttribute("userName", username);
  420. model.addAttribute("serverDate", LocalDate.now().toString()); // for server date
  421. return "user/viewhallform";
  422. }
  423. }