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.

332 lines
14 KiB

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.web.multipart.MultipartFile;
  18. import com.gcc.communityHall.entity.HallDetails;
  19. import com.gcc.communityHall.entity.HallImages;
  20. import com.gcc.communityHall.repository.HallDetailsRepository;
  21. import com.gcc.communityHall.repository.HallImagesRepository;
  22. import com.gcc.communityHall.service.HallDetailService;
  23. @Service
  24. public class HallDetailServiceImpl implements HallDetailService {
  25. // @Autowired
  26. private HallDetailsRepository hallDetailsRepository;
  27. private HallImagesRepository hallImagesRepository;
  28. private final String hallDirectory;
  29. private final String hallImageUrl;
  30. @Autowired
  31. HallDetailServiceImpl(HallDetailsRepository hallDetailsRepository, HallImagesRepository hallImagesRepository, @Value("${file.hall.upload.directory}") String hallDirectory, @Value("${file.hall.fullpath}") String hallImageUrl){
  32. this.hallDetailsRepository=hallDetailsRepository;
  33. this.hallDirectory=hallDirectory;
  34. this.hallImageUrl=hallImageUrl;
  35. this.hallImagesRepository = hallImagesRepository;
  36. }
  37. @Override
  38. public HallDetails saveHallDetails(HallDetails hallDetails, List<MultipartFile> hallImages) {
  39. if(hallImages.size()>0){
  40. }
  41. return null;
  42. }
  43. @Override
  44. public HallDetails getHallById(int hallId) {
  45. Optional<HallDetails> hallDetails = hallDetailsRepository.findById(hallId);
  46. if(hallDetails.isPresent()){
  47. return hallDetails.get();
  48. }
  49. return null;
  50. }
  51. @Override
  52. public HallDetails deActivateHallById(int hallId) {
  53. return null;
  54. }
  55. @Override
  56. public HallDetails saveHall(
  57. String hallName,float depositAmount, float halfDayAmt,
  58. float fullDayAmt, float gst,float hallArea, String ebNumber, float ebCostPerUnit,
  59. int parkingCapacity, int seatingCapacity,
  60. String doorNo, String street, String area, String zone,
  61. String division, String landMark, String city, int pinCode,
  62. String latitude, String longitude, String hallInchargeName,
  63. long hallInchargeNo,String updatedId, MultipartFile[] hallImages) {
  64. HallDetails hall = new HallDetails();
  65. hall.setHallName(hallName);
  66. hall.setDoorNo(doorNo);
  67. hall.setHallArea(hallArea);
  68. hall.setStreet(street);
  69. hall.setArea(area);
  70. hall.setCity(city);
  71. hall.setPinCode(pinCode);
  72. hall.setDivision(division);
  73. hall.setZone(zone);
  74. hall.setEbNumber(ebNumber);
  75. hall.setEbCostPerUnit(ebCostPerUnit);
  76. hall.setFullDayRent(fullDayAmt);
  77. hall.setHalfDayRent(halfDayAmt);
  78. hall.setDepositAmount(depositAmount);
  79. hall.setGst(gst);
  80. hall.setHallInchargeName(hallInchargeName);
  81. hall.setHallInchargeMobno(hallInchargeNo);
  82. hall.setSeatingCapacity(seatingCapacity);
  83. hall.setParkingCapacity(String.valueOf(parkingCapacity));
  84. hall.setLatitude(latitude);
  85. hall.setLongitude(longitude);
  86. hall.setLankMark(landMark);
  87. hall.setUpdatedId(updatedId);
  88. hall.setCreateddate(LocalDate.now());
  89. HallDetails hallDetails = hallDetailsRepository.save(hall);
  90. if(hallImages!=null && hallImages.length>0){
  91. int i=0;
  92. for (MultipartFile hallImage : hallImages) {
  93. HallImages hallImages1 = new HallImages();
  94. String filePath = hallDirectory+"/"+hallDetails.getHallId()+ "_"+i+"_" + hallImage.getOriginalFilename();
  95. String imageUrl = hallImageUrl+"hallImages/"+hallDetails.getHallId()+ "_"+i+"_" + hallImage.getOriginalFilename();
  96. try(FileOutputStream fos = new FileOutputStream(filePath)){
  97. byte [] bytes = hallImage.getBytes();
  98. fos.write(bytes);
  99. hallImages1.setHallDetails(hallDetails);
  100. hallImages1.setHallImage(imageUrl); // hall/imagename.jpg
  101. hallImagesRepository.save(hallImages1);
  102. } catch (IOException e) {
  103. throw new RuntimeException(e);
  104. }
  105. i++;
  106. }
  107. }
  108. return null;
  109. }
  110. public void updatenewHallImages(Integer hallId, MultipartFile[] newHallImages) {
  111. // Fetch the existing hall details from the database
  112. HallDetails hallDetails = hallDetailsRepository.findById(hallId)
  113. .orElseThrow(() -> new RuntimeException("Hall not found with id: " + hallId));
  114. if (newHallImages != null && newHallImages.length > 0) {
  115. int i = hallDetails.getHallImagesList().size(); // Start index after existing images
  116. for (MultipartFile hallImage : newHallImages) {
  117. HallImages hallImageEntity = new HallImages();
  118. String filePath = hallDirectory + "/" + hallDetails.getHallId() + "_" + i + "_" + hallImage.getOriginalFilename();
  119. String imageUrl = hallImageUrl + "hallImages/" + hallDetails.getHallId() + "_" + i + "_" + hallImage.getOriginalFilename();
  120. try (FileOutputStream fos = new FileOutputStream(filePath)) {
  121. byte[] bytes = hallImage.getBytes();
  122. fos.write(bytes);
  123. hallImageEntity.setHallDetails(hallDetails);
  124. hallImageEntity.setHallImage(imageUrl);
  125. hallImagesRepository.save(hallImageEntity); // Save the new image in the database
  126. } catch (IOException e) {
  127. throw new RuntimeException("Failed to save hall image", e);
  128. }
  129. i++;
  130. }
  131. }
  132. }
  133. public String updateHallImage(Integer hallImageId, MultipartFile hallImage) throws IOException {
  134. Optional<HallImages> optionalHallImage = hallImagesRepository.findById(hallImageId);
  135. if (optionalHallImage.isPresent()) {
  136. HallImages hallImages = optionalHallImage.get();
  137. // Generate the new file path and URL
  138. String filePath = hallDirectory + "/" + hallImageId + "_" + hallImage.getOriginalFilename();
  139. String imageUrl = hallImageUrl + "hallImages/" + hallImageId + "_" + hallImage.getOriginalFilename();
  140. // Save the new image file
  141. try (FileOutputStream fos = new FileOutputStream(filePath)) {
  142. byte[] bytes = hallImage.getBytes();
  143. fos.write(bytes);
  144. hallImages.setHallImage(imageUrl);
  145. hallImagesRepository.save(hallImages);
  146. }
  147. return imageUrl;
  148. } else {
  149. throw new IllegalArgumentException("Hall image not found with ID: " + hallImageId);
  150. }
  151. }
  152. @Override
  153. public List<HallImages> getHallImages(int hallId) {
  154. // TODO Auto-generated method stub
  155. HallDetails hall = new HallDetails();
  156. hall.setHallId(hallId);
  157. return hallImagesRepository.findByHallDetails(hall);
  158. }
  159. @Override
  160. public String updateHallDetail(HallDetails hallDetails, List<MultipartFile> images) {
  161. HallDetails existingHall = hallDetailsRepository.findById(hallDetails.getHallId()).orElse(null);
  162. if (existingHall != null) {
  163. // Update hall details
  164. existingHall.setZone(hallDetails.getZone());
  165. existingHall.setDivision(hallDetails.getDivision());
  166. existingHall.setHallName(hallDetails.getHallName());
  167. existingHall.setDoorNo(hallDetails.getDoorNo());
  168. existingHall.setStreet(hallDetails.getStreet());
  169. existingHall.setArea(hallDetails.getArea());
  170. existingHall.setCity(hallDetails.getCity());
  171. existingHall.setPinCode(hallDetails.getPinCode());
  172. existingHall.setHallInchargeName(hallDetails.getHallInchargeName());
  173. existingHall.setHallInchargeMobno(hallDetails.getHallInchargeMobno());
  174. existingHall.setLandMark(hallDetails.getLandMark());
  175. existingHall.setLatitude(hallDetails.getLatitude());
  176. existingHall.setLongitude(hallDetails.getLongitude());
  177. existingHall.setDepositAmount(hallDetails.getDepositAmount());
  178. existingHall.setFullDayRent(hallDetails.getFullDayRent());
  179. existingHall.setHalfDayRent(hallDetails.getHalfDayRent());
  180. existingHall.setParkingCapacity(hallDetails.getParkingCapacity());
  181. existingHall.setSeatingCapacity(hallDetails.getSeatingCapacity());
  182. existingHall.setGst(hallDetails.getGst());
  183. existingHall.setEbCostPerUnit(hallDetails.getEbCostPerUnit());
  184. existingHall.setActive(hallDetails.isActive());
  185. existingHall.setEbNumber(hallDetails.getEbNumber());
  186. // Update images
  187. if (images != null) {
  188. List<HallImages> hallImagesList = existingHall.getHallImagesList();
  189. if (hallImagesList == null) {
  190. hallImagesList = new ArrayList<>();
  191. } else {
  192. hallImagesList.clear(); // Clear existing images
  193. }
  194. int i=0;
  195. for (MultipartFile image : images) {
  196. HallImages hallImage = new HallImages();
  197. try {
  198. // Save image to storage location
  199. String filePath = hallDirectory + "/" + existingHall.getHallId() + "_"+i+"_" + image.getOriginalFilename();
  200. String imageUrl = hallImageUrl + "hall/" + existingHall.getHallId() + "_" +i+"_"+ image.getOriginalFilename();
  201. try (FileOutputStream fos = new FileOutputStream(filePath)) {
  202. byte[] bytes = image.getBytes();
  203. fos.write(bytes);
  204. }
  205. hallImage.setHallImage(imageUrl);
  206. hallImage.setHallDetails(existingHall);
  207. hallImagesList.add(hallImage);
  208. } catch (IOException e) {
  209. e.printStackTrace();
  210. }
  211. i++;
  212. }
  213. existingHall.setHallImagesList(hallImagesList);
  214. }
  215. hallDetailsRepository.save(existingHall);
  216. return "success";
  217. }
  218. return "failed";
  219. }
  220. @Override
  221. public List<HallDetails> removeHallById(HashSet<Integer> hallIds) {
  222. if(hallIds.isEmpty()){
  223. hallIds.add(0);
  224. }
  225. //System.out.println(hallIds);
  226. return hallDetailsRepository.removeHallById(hallIds);
  227. }
  228. public List<HallDetails> getHallNames() {
  229. List<HallDetails> halls = hallDetailsRepository.findAll();
  230. return halls.stream()
  231. .map(hall -> {
  232. HallDetails hallDetails = new HallDetails();
  233. hallDetails.setHallId(hall.getHallId());
  234. hallDetails.setHallName(hall.getHallName());
  235. return hallDetails;
  236. })
  237. .collect(Collectors.toList());
  238. }
  239. @Override
  240. public void saveHallImage(Long hallId, MultipartFile file) throws IOException {
  241. try {
  242. HallDetails hallDetails = hallDetailsRepository.findById(Math.toIntExact(hallId))
  243. .orElseThrow(() -> new IllegalArgumentException("Invalid hall ID"));
  244. HallImages hallImage = new HallImages();
  245. hallImage.setHallImage(Arrays.toString(file.getBytes())); // Check if this line needs adjustment based on file handling
  246. hallImage.setHallDetails(hallDetails);
  247. hallImagesRepository.save(hallImage);
  248. } catch (Exception e) {
  249. e.printStackTrace(); // Log the full stack trace for debugging
  250. throw new RuntimeException("Failed to save hall image: " + e.getMessage());
  251. }
  252. }
  253. @Override
  254. public List<HallImages> getHallImagesByHallId(Long hallId) {
  255. return hallImagesRepository.findByHallDetails_HallId(hallId);
  256. }
  257. @Override
  258. public List<HallDetails> getHallNamesbyzone(String zone) {
  259. List<HallDetails> halls = hallDetailsRepository.findByZone(zone);
  260. return halls.stream()
  261. .map(hall -> {
  262. HallDetails hallDetails = new HallDetails();
  263. hallDetails.setHallId(hall.getHallId());
  264. hallDetails.setHallName(hall.getHallName());
  265. return hallDetails;
  266. })
  267. .collect(Collectors.toList());
  268. }
  269. @Override
  270. public Map<String, Object> getHallsDetailsByZone(String zone) {
  271. // Get the total bookings and amount received by zone
  272. Map<String, Object> halldetails = hallDetailsRepository.getTotalBookingsAndAmountReceivedByZone(zone);
  273. // Create a new modifiable map and copy values from the unmodifiable map
  274. Map<String, Object> modifiableHalldetails = new HashMap<>(halldetails);
  275. // Get the details of each hall in the zone
  276. List<Map<String, Object>> zonehalls = hallDetailsRepository.getHallDetailsByZone(zone);
  277. // Add the list of hall details to the new modifiable map
  278. modifiableHalldetails.put("zonehalls", zonehalls);
  279. return modifiableHalldetails;
  280. }
  281. //officer dashboard
  282. @Override
  283. public Map<String, Object> getHallsDetailsForOfficer(String zone) {
  284. // Get the total bookings and amount received by zone
  285. Map<String, Object> halldetails = hallDetailsRepository.getTotalCountsForOfficer(zone);
  286. // Create a new modifiable map and copy values from the unmodifiable map
  287. Map<String, Object> modifiableHalldetails = new HashMap<>(halldetails);
  288. // Get the details of each hall in the zone
  289. List<Map<String, Object>> zonehalls = hallDetailsRepository.getHallCountsForOfficer(zone);
  290. // Add the list of hall details to the new modifiable map
  291. modifiableHalldetails.put("zonehalls", zonehalls);
  292. return modifiableHalldetails;
  293. }
  294. }