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.

442 lines
19 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
​
​
​
​
​
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
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
2 years ago
​
2 years ago
​
2 years ago
  1. package com.gcc.communityHall.service.Impl;
  2. import java.io.FileNotFoundException;
  3. import java.io.FileOutputStream;
  4. import java.io.IOException;
  5. import java.time.LocalDate;
  6. import java.util.ArrayList;
  7. import java.util.Arrays;
  8. import java.util.HashMap;
  9. import java.util.HashSet;
  10. import java.util.List;
  11. import java.util.Map;
  12. import java.util.Optional;
  13. import java.util.stream.Collectors;
  14. import org.springframework.beans.factory.annotation.Autowired;
  15. import org.springframework.beans.factory.annotation.Value;
  16. import org.springframework.stereotype.Service;
  17. import org.springframework.transaction.annotation.Transactional;
  18. import org.springframework.web.multipart.MultipartFile;
  19. import com.gcc.communityHall.entity.FloorDetails;
  20. import com.gcc.communityHall.entity.HallDetails;
  21. import com.gcc.communityHall.entity.HallImages;
  22. import com.gcc.communityHall.repository.FloorDetailsRepository;
  23. import com.gcc.communityHall.repository.HallDetailsRepository;
  24. import com.gcc.communityHall.repository.HallImagesRepository;
  25. import com.gcc.communityHall.service.HallDetailService;
  26. @Service
  27. public class HallDetailServiceImpl implements HallDetailService {
  28. // @Autowired
  29. private HallDetailsRepository hallDetailsRepository;
  30. private HallImagesRepository hallImagesRepository;
  31. private final String hallDirectory;
  32. private final String hallImageUrl;
  33. @Autowired
  34. private FloorDetailsRepository floorDetailsRepository;
  35. @Autowired
  36. HallDetailServiceImpl(HallDetailsRepository hallDetailsRepository, HallImagesRepository hallImagesRepository,
  37. @Value("${file.hall.upload.directory}") String hallDirectory,
  38. @Value("${file.hall.fullpath}") String hallImageUrl) {
  39. this.hallDetailsRepository = hallDetailsRepository;
  40. this.hallDirectory = hallDirectory;
  41. this.hallImageUrl = hallImageUrl;
  42. this.hallImagesRepository = hallImagesRepository;
  43. }
  44. @Override
  45. public HallDetails saveHallDetails(HallDetails hallDetails, List<MultipartFile> hallImages) {
  46. if (hallImages.size() > 0) {
  47. }
  48. return null;
  49. }
  50. @Override
  51. public HallDetails getHallById(int hallId) {
  52. Optional<HallDetails> hallDetails = hallDetailsRepository.findById(hallId);
  53. if (hallDetails.isPresent()) {
  54. return hallDetails.get();
  55. }
  56. return null;
  57. }
  58. @Override
  59. public HallDetails deActivateHallById(int hallId) {
  60. return null;
  61. }
  62. @Override
  63. public HallDetails saveHallByAdmin(
  64. String hallName, float depositAmount, float halfDayAmt,
  65. float fullDayAmt, float gst, float hallArea, String ebNumber, float ebCostPerUnit,
  66. String parkingCapacity, int seatingCapacity,
  67. String doorNo, String street, String area, String zone,
  68. String division, String landMark, String city, int pinCode,
  69. String latitude, String longitude, String hallInchargeName,
  70. long hallInchargeNo, String updatedId, MultipartFile[] hallImages, float halfDepositAmt) {
  71. HallDetails hall = new HallDetails();
  72. hall.setHallName(hallName);
  73. hall.setDoorNo(doorNo);
  74. hall.setHallArea(hallArea);
  75. hall.setStreet(street);
  76. hall.setArea(area);
  77. hall.setCity(city);
  78. hall.setPinCode(pinCode);
  79. hall.setDivision(division);
  80. hall.setZone(zone);
  81. hall.setEbNumber(ebNumber);
  82. hall.setEbCostPerUnit(ebCostPerUnit);
  83. hall.setFullDayRent(fullDayAmt);
  84. hall.setHalfDayRent(halfDayAmt);
  85. hall.setDepositAmount(depositAmount);
  86. hall.setGst(gst);
  87. hall.setHallInchargeName(hallInchargeName);
  88. hall.setHallInchargeMobno(hallInchargeNo);
  89. hall.setSeatingCapacity(seatingCapacity);
  90. hall.setParkingCapacity(String.valueOf(parkingCapacity));
  91. hall.setLatitude(latitude);
  92. hall.setLongitude(longitude);
  93. hall.setLankMark(landMark);
  94. hall.setUpdatedId(updatedId);
  95. hall.setCreateddate(LocalDate.now());
  96. hall.setHalfDepositAmount(halfDepositAmt);
  97. HallDetails hallDetails = hallDetailsRepository.save(hall);
  98. if (hallImages != null && hallImages.length > 0) {
  99. int i = 0;
  100. for (MultipartFile hallImage : hallImages) {
  101. HallImages hallImages1 = new HallImages();
  102. String filePath = hallDirectory + "/" + hallDetails.getHallId() + "_" + i +
  103. "_"
  104. + hallImage.getOriginalFilename();
  105. String imageUrl = hallImageUrl + "hallImages/" + hallDetails.getHallId() +
  106. "_" + i + "_"
  107. + hallImage.getOriginalFilename();
  108. try (FileOutputStream fos = new FileOutputStream(filePath)) {
  109. byte[] bytes = hallImage.getBytes();
  110. fos.write(bytes);
  111. hallImages1.setHallDetails(hallDetails);
  112. hallImages1.setHallImage(imageUrl); // hall/imagename.jpg
  113. hallImagesRepository.save(hallImages1);
  114. } catch (IOException e) {
  115. throw new RuntimeException(e);
  116. }
  117. i++;
  118. }
  119. }
  120. return null;
  121. }
  122. /**
  123. * HQ-specific overload: saves hall details + floor details in one call.
  124. */
  125. @Override
  126. public HallDetails saveHallByHQ(
  127. String hallName, float depositAmount, float halfDayAmt,
  128. float fullDayAmt, float gst, float hallArea, String ebNumber, float ebCostPerUnit,
  129. String parkingCapacity, int seatingCapacity,
  130. String doorNo, String street, String area, String zone,
  131. String division, String landMark, String city, int pinCode,
  132. String latitude, String longitude, String hallInchargeName,
  133. long hallInchargeNo, String updatedId, MultipartFile[] hallImages, float halfDepositAmt,
  134. List<String> floorNames, List<Double> floorCapacities) {
  135. // Reuse the existing saveHall to persist the hall + images
  136. saveHallByAdmin(hallName, depositAmount, halfDayAmt, fullDayAmt, gst, hallArea,
  137. ebNumber, ebCostPerUnit, parkingCapacity, seatingCapacity,
  138. doorNo, street, area, zone, division, landMark, city, pinCode,
  139. latitude, longitude, hallInchargeName, hallInchargeNo,
  140. updatedId, hallImages, halfDepositAmt);
  141. // Fetch the just-saved hall to get its generated ID
  142. // (the hall is the most recently inserted one with this name)
  143. HallDetails savedHall = hallDetailsRepository
  144. .findAll()
  145. .stream()
  146. .filter(h -> hallName.equals(h.getHallName()))
  147. .reduce((first, second) -> second) // last inserted
  148. .orElse(null);
  149. if (savedHall != null && floorNames != null && !floorNames.isEmpty()) {
  150. for (int i = 0; i < floorNames.size(); i++) {
  151. String fname = floorNames.get(i);
  152. if (fname == null || fname.trim().isEmpty())
  153. continue;
  154. double fcap = (floorCapacities != null && i < floorCapacities.size() && floorCapacities.get(i) != null)
  155. ? floorCapacities.get(i)
  156. : 0.0;
  157. FloorDetails fd = new FloorDetails();
  158. fd.setFloor(fname.trim());
  159. fd.setHallarea(fcap);
  160. fd.setHallDetails(savedHall);
  161. fd.setActive(true);
  162. fd.setDelete(false);
  163. floorDetailsRepository.save(fd);
  164. }
  165. }
  166. return savedHall;
  167. }
  168. /**
  169. * Soft-deletes existing active floor rows, then inserts new floor records.
  170. * Existing rows get: is_active = false, is_delete = true.
  171. * New rows get: is_active = true, is_delete = false.
  172. */
  173. @Override
  174. @Transactional
  175. public void updateFloorDetails(int hallId, List<String> floorNames, List<Double> floorCapacities) {
  176. // Soft-delete all currently active floor rows for this hall
  177. floorDetailsRepository.softDeleteByHallId(hallId);
  178. HallDetails hall = hallDetailsRepository.findById(hallId).orElse(null);
  179. if (hall == null)
  180. return;
  181. if (floorNames == null || floorNames.isEmpty())
  182. return;
  183. // Insert new floor records
  184. for (int i = 0; i < floorNames.size(); i++) {
  185. String fname = floorNames.get(i);
  186. if (fname == null || fname.trim().isEmpty())
  187. continue;
  188. double fcap = (floorCapacities != null && i < floorCapacities.size() && floorCapacities.get(i) != null)
  189. ? floorCapacities.get(i)
  190. : 0.0;
  191. FloorDetails fd = new FloorDetails();
  192. fd.setFloor(fname.trim());
  193. fd.setHallarea(fcap);
  194. fd.setHallDetails(hall);
  195. fd.setActive(true);
  196. fd.setDelete(false);
  197. floorDetailsRepository.save(fd);
  198. }
  199. }
  200. public void updatenewHallImages(Integer hallId, MultipartFile[] newHallImages) {
  201. // Fetch the existing hall details from the database
  202. HallDetails hallDetails = hallDetailsRepository.findById(hallId)
  203. .orElseThrow(() -> new RuntimeException("Hall not found with id: " + hallId));
  204. if (newHallImages != null && newHallImages.length > 0) {
  205. int i = hallDetails.getHallImagesList().size(); // Start index after existing images
  206. for (MultipartFile hallImage : newHallImages) {
  207. HallImages hallImageEntity = new HallImages();
  208. String filePath = hallDirectory + "/" + hallDetails.getHallId() + "_" + i + "_"
  209. + hallImage.getOriginalFilename();
  210. String imageUrl = hallImageUrl + "hallImages/" + hallDetails.getHallId() + "_" + i + "_"
  211. + hallImage.getOriginalFilename();
  212. try (FileOutputStream fos = new FileOutputStream(filePath)) {
  213. byte[] bytes = hallImage.getBytes();
  214. fos.write(bytes);
  215. hallImageEntity.setHallDetails(hallDetails);
  216. hallImageEntity.setHallImage(imageUrl);
  217. hallImagesRepository.save(hallImageEntity); // Save the new image in the database
  218. } catch (IOException e) {
  219. throw new RuntimeException("Failed to save hall image", e);
  220. }
  221. i++;
  222. }
  223. }
  224. }
  225. public String updateHallImage(Integer hallImageId, MultipartFile hallImage) throws IOException {
  226. Optional<HallImages> optionalHallImage = hallImagesRepository.findById(hallImageId);
  227. if (optionalHallImage.isPresent()) {
  228. HallImages hallImages = optionalHallImage.get();
  229. // Generate the new file path and URL
  230. String filePath = hallDirectory + "/" + hallImageId + "_" + hallImage.getOriginalFilename();
  231. String imageUrl = hallImageUrl + "hallImages/" + hallImageId + "_" + hallImage.getOriginalFilename();
  232. // Save the new image file
  233. try (FileOutputStream fos = new FileOutputStream(filePath)) {
  234. byte[] bytes = hallImage.getBytes();
  235. fos.write(bytes);
  236. hallImages.setHallImage(imageUrl);
  237. hallImagesRepository.save(hallImages);
  238. }
  239. return imageUrl;
  240. } else {
  241. throw new IllegalArgumentException("Hall image not found with ID: " + hallImageId);
  242. }
  243. }
  244. @Override
  245. public List<HallImages> getHallImages(int hallId) {
  246. // TODO Auto-generated method stub
  247. HallDetails hall = new HallDetails();
  248. hall.setHallId(hallId);
  249. return hallImagesRepository.findByHallDetails(hall);
  250. }
  251. @Override
  252. public String updateHallDetail(HallDetails hallDetails, List<MultipartFile> images) {
  253. HallDetails existingHall = hallDetailsRepository.findById(hallDetails.getHallId()).orElse(null);
  254. if (existingHall != null) {
  255. // Update hall details
  256. existingHall.setZone(hallDetails.getZone());
  257. existingHall.setDivision(hallDetails.getDivision());
  258. existingHall.setHallName(hallDetails.getHallName());
  259. existingHall.setDoorNo(hallDetails.getDoorNo());
  260. existingHall.setStreet(hallDetails.getStreet());
  261. existingHall.setArea(hallDetails.getArea());
  262. existingHall.setCity(hallDetails.getCity());
  263. existingHall.setPinCode(hallDetails.getPinCode());
  264. existingHall.setHallInchargeName(hallDetails.getHallInchargeName());
  265. existingHall.setHallInchargeMobno(hallDetails.getHallInchargeMobno());
  266. existingHall.setLandMark(hallDetails.getLandMark());
  267. existingHall.setLatitude(hallDetails.getLatitude());
  268. existingHall.setLongitude(hallDetails.getLongitude());
  269. existingHall.setDepositAmount(hallDetails.getDepositAmount());
  270. existingHall.setHalfDepositAmount(hallDetails.getHalfDepositAmount());
  271. existingHall.setFullDayRent(hallDetails.getFullDayRent());
  272. existingHall.setHalfDayRent(hallDetails.getHalfDayRent());
  273. existingHall.setParkingCapacity(hallDetails.getParkingCapacity());
  274. existingHall.setSeatingCapacity(hallDetails.getSeatingCapacity());
  275. existingHall.setGst(hallDetails.getGst());
  276. existingHall.setEbCostPerUnit(hallDetails.getEbCostPerUnit());
  277. existingHall.setActive(hallDetails.isActive());
  278. existingHall.setEbNumber(hallDetails.getEbNumber());
  279. // Update images
  280. if (images != null) {
  281. List<HallImages> hallImagesList = existingHall.getHallImagesList();
  282. if (hallImagesList == null) {
  283. hallImagesList = new ArrayList<>();
  284. } else {
  285. hallImagesList.clear(); // Clear existing images
  286. }
  287. int i = 0;
  288. for (MultipartFile image : images) {
  289. HallImages hallImage = new HallImages();
  290. try {
  291. // Save image to storage location
  292. String filePath = hallDirectory + "/" + existingHall.getHallId() + "_" + i + "_"
  293. + image.getOriginalFilename();
  294. String imageUrl = hallImageUrl + "hall/" + existingHall.getHallId() + "_" + i + "_"
  295. + image.getOriginalFilename();
  296. try (FileOutputStream fos = new FileOutputStream(filePath)) {
  297. byte[] bytes = image.getBytes();
  298. fos.write(bytes);
  299. }
  300. hallImage.setHallImage(imageUrl);
  301. hallImage.setHallDetails(existingHall);
  302. hallImagesList.add(hallImage);
  303. } catch (IOException e) {
  304. e.printStackTrace();
  305. }
  306. i++;
  307. }
  308. existingHall.setHallImagesList(hallImagesList);
  309. }
  310. hallDetailsRepository.save(existingHall);
  311. return "success";
  312. }
  313. return "failed";
  314. }
  315. @Override
  316. public List<HallDetails> removeHallById(HashSet<Integer> hallIds) {
  317. if (hallIds.isEmpty()) {
  318. hallIds.add(0);
  319. }
  320. // System.out.println(hallIds);
  321. return hallDetailsRepository.removeHallById(hallIds);
  322. }
  323. public List<HallDetails> getHallNames() {
  324. List<HallDetails> halls = hallDetailsRepository.findAll();
  325. return halls.stream()
  326. .map(hall -> {
  327. HallDetails hallDetails = new HallDetails();
  328. hallDetails.setHallId(hall.getHallId());
  329. hallDetails.setHallName(hall.getHallName());
  330. return hallDetails;
  331. })
  332. .collect(Collectors.toList());
  333. }
  334. @Override
  335. public void saveHallImage(Long hallId, MultipartFile file) throws IOException {
  336. try {
  337. HallDetails hallDetails = hallDetailsRepository.findById(Math.toIntExact(hallId))
  338. .orElseThrow(() -> new IllegalArgumentException("Invalid hall ID"));
  339. HallImages hallImage = new HallImages();
  340. hallImage.setHallImage(Arrays.toString(file.getBytes())); // Check if this line needs adjustment based on
  341. // file handling
  342. hallImage.setHallDetails(hallDetails);
  343. hallImagesRepository.save(hallImage);
  344. } catch (Exception e) {
  345. e.printStackTrace(); // Log the full stack trace for debugging
  346. throw new RuntimeException("Failed to save hall image: " + e.getMessage());
  347. }
  348. }
  349. @Override
  350. public List<HallImages> getHallImagesByHallId(Long hallId) {
  351. return hallImagesRepository.findByHallDetails_HallId(hallId);
  352. }
  353. @Override
  354. public List<HallDetails> getHallNamesbyzone(String zone) {
  355. List<HallDetails> halls = hallDetailsRepository.findByZone(zone);
  356. return halls.stream()
  357. .map(hall -> {
  358. HallDetails hallDetails = new HallDetails();
  359. hallDetails.setHallId(hall.getHallId());
  360. hallDetails.setHallName(hall.getHallName());
  361. return hallDetails;
  362. })
  363. .collect(Collectors.toList());
  364. }
  365. @Override
  366. public Map<String, Object> getHallsDetailsByZone(String zone) {
  367. // Get the total bookings and amount received by zone
  368. Map<String, Object> halldetails = hallDetailsRepository.getTotalBookingsAndAmountReceivedByZone(zone);
  369. // Create a new modifiable map and copy values from the unmodifiable map
  370. Map<String, Object> modifiableHalldetails = new HashMap<>(halldetails);
  371. // Get the details of each hall in the zone
  372. List<Map<String, Object>> zonehalls = hallDetailsRepository.getHallDetailsByZone(zone);
  373. // Add the list of hall details to the new modifiable map
  374. modifiableHalldetails.put("zonehalls", zonehalls);
  375. return modifiableHalldetails;
  376. }
  377. // officer dashboard
  378. @Override
  379. public Map<String, Object> getHallsDetailsForOfficer(String zone) {
  380. // Get the total bookings and amount received by zone
  381. Map<String, Object> halldetails = hallDetailsRepository.getTotalCountsForOfficer(zone);
  382. // Create a new modifiable map and copy values from the unmodifiable map
  383. Map<String, Object> modifiableHalldetails = new HashMap<>(halldetails);
  384. // Get the details of each hall in the zone
  385. List<Map<String, Object>> zonehalls = hallDetailsRepository.getHallCountsForOfficer(zone);
  386. // Add the list of hall details to the new modifiable map
  387. modifiableHalldetails.put("zonehalls", zonehalls);
  388. return modifiableHalldetails;
  389. }
  390. }