|
| 1 | +package com.knu.ddip.location.application.service; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.databind.JsonNode; |
| 4 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 5 | +import com.google.common.geometry.S2CellId; |
| 6 | +import com.knu.ddip.location.application.dto.UpdateMyLocationRequest; |
| 7 | +import com.knu.ddip.location.application.util.S2Converter; |
| 8 | +import com.knu.ddip.location.application.util.UuidBase64Utils; |
| 9 | +import lombok.RequiredArgsConstructor; |
| 10 | +import lombok.extern.slf4j.Slf4j; |
| 11 | +import org.springframework.core.io.ClassPathResource; |
| 12 | +import org.springframework.stereotype.Service; |
| 13 | +import org.springframework.transaction.annotation.Transactional; |
| 14 | + |
| 15 | +import java.io.IOException; |
| 16 | +import java.io.InputStream; |
| 17 | +import java.nio.charset.StandardCharsets; |
| 18 | +import java.util.ArrayList; |
| 19 | +import java.util.List; |
| 20 | +import java.util.UUID; |
| 21 | +import java.util.stream.Collectors; |
| 22 | +import java.util.stream.StreamSupport; |
| 23 | + |
| 24 | +@Slf4j |
| 25 | +@Service |
| 26 | +@Transactional(readOnly = true) |
| 27 | +@RequiredArgsConstructor |
| 28 | +public class LocationService { |
| 29 | + |
| 30 | + private final LocationReader locationReader; |
| 31 | + private final LocationWriter locationWriter; |
| 32 | + |
| 33 | + private final ObjectMapper objectMapper; |
| 34 | + |
| 35 | + public static final int LEVEL = 17; |
| 36 | + public static final String KNU_GEOJSON_FEATURE_FILENAME = "geojson/cells.geojson"; |
| 37 | + |
| 38 | + // KNU GeoJSON 파일을 읽어서 각 Feature를 DB에 저장 |
| 39 | + @Transactional |
| 40 | + public void loadAndSaveGeoJsonFeatures() { |
| 41 | + // GeoJson 파일 읽기 |
| 42 | + String geoJsonContent = getGeoJsonContent(); |
| 43 | + |
| 44 | + try { |
| 45 | + // 기존 데이터 삭제 |
| 46 | + locationWriter.deleteAll(); |
| 47 | + |
| 48 | + // JSON 파싱 |
| 49 | + JsonNode rootNode = objectMapper.readTree(geoJsonContent); |
| 50 | + JsonNode featuresNode = rootNode.get("features"); |
| 51 | + List<String> cellIds = StreamSupport.stream(featuresNode.spliterator(), false) |
| 52 | + .map(featureNode -> featureNode.get("properties").get("id").asText()) |
| 53 | + .collect(Collectors.toList()); |
| 54 | + |
| 55 | + locationWriter.saveAll(cellIds); |
| 56 | + |
| 57 | + log.info("총 {}개의 S2Cell Feature가 저장되었습니다.", cellIds.size()); |
| 58 | + } catch (IOException e) { |
| 59 | + e.printStackTrace(); |
| 60 | + log.error(e.getMessage()); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + public void saveUserLocationAtomic(UUID userId, UpdateMyLocationRequest request) { |
| 65 | + S2CellId cellIdObj = S2Converter.toCellId(request.lat(), request.lng(), LEVEL); |
| 66 | + String cellId = cellIdObj.toToken(); |
| 67 | + |
| 68 | + // 경북대 내부에 위치하는지 확인 |
| 69 | + boolean cellIdNotInTargetArea = locationReader.isCellIdNotInTargetArea(cellId); |
| 70 | + |
| 71 | + String encodedUserId = UuidBase64Utils.uuidToBase64String(userId); |
| 72 | + |
| 73 | + locationWriter.saveUserIdByCellIdAtomic(cellId, cellIdNotInTargetArea, encodedUserId); |
| 74 | + } |
| 75 | + |
| 76 | + // 요청 전송 시 이웃 userIds 조회 |
| 77 | + public List<UUID> getNeighborRecipientUserIds(UUID myUserId, double lat, double lng) { |
| 78 | + S2CellId cellIdObj = S2Converter.toCellId(lat, lng, LEVEL); |
| 79 | + String cellId = cellIdObj.toToken(); |
| 80 | + |
| 81 | + // 경북대 내부에 위치하는지 확인 |
| 82 | + locationReader.validateLocationByCellId(cellId); |
| 83 | + |
| 84 | + // 이웃 cellIds 가져오기 |
| 85 | + List<S2CellId> neighbors = new ArrayList<>(); |
| 86 | + cellIdObj.getAllNeighbors(LEVEL, neighbors); |
| 87 | + List<String> neighborCellIds = neighbors.stream() |
| 88 | + .map(S2CellId::toToken) |
| 89 | + .collect(Collectors.toList()); |
| 90 | + |
| 91 | + // 경북대 내부에 위치하는 이웃 cellIds만 가져오기 |
| 92 | + List<String> targetCellIds = locationReader.findAllLocationsByCellIdIn(neighborCellIds); |
| 93 | + targetCellIds.add(cellId); |
| 94 | + |
| 95 | + // targetCellId의 userIds만 가져오기 |
| 96 | + |
| 97 | + List<UUID> userIds = locationReader.findUserIdsByCellIds(targetCellIds).stream() |
| 98 | + .map(UuidBase64Utils::base64StringToUuid) |
| 99 | + .filter(userId -> !userId.equals(myUserId)) |
| 100 | + .collect(Collectors.toList()); |
| 101 | + |
| 102 | + return userIds; |
| 103 | + } |
| 104 | + |
| 105 | + // 초기 화면에서 인접한 요청 가져오기 (현재는 인접한 cellId 가져오는 것만 구현) |
| 106 | + public List<String> getNeighborCellIds(double lat, double lng) { |
| 107 | + S2CellId cellIdObj = S2Converter.toCellId(lat, lng, LEVEL); |
| 108 | + String cellId = cellIdObj.toToken(); |
| 109 | + |
| 110 | + // 경북대 내부에 위치하는지 확인 |
| 111 | + locationReader.validateLocationByCellId(cellId); |
| 112 | + |
| 113 | + // 이웃 cellIds 가져오기 |
| 114 | + List<S2CellId> neighbors = new ArrayList<>(); |
| 115 | + cellIdObj.getAllNeighbors(LEVEL, neighbors); |
| 116 | + List<String> neighborCellIds = neighbors.stream() |
| 117 | + .map(S2CellId::toToken) |
| 118 | + .collect(Collectors.toList()); |
| 119 | + |
| 120 | + // 경북대 내부에 위치하는 이웃 cellIds만 가져오기 |
| 121 | + List<String> targetCellIds = locationReader.findAllLocationsByCellIdIn(neighborCellIds); |
| 122 | + targetCellIds.add(cellId); |
| 123 | + |
| 124 | + return targetCellIds; |
| 125 | + } |
| 126 | + |
| 127 | + private String getGeoJsonContent() { |
| 128 | + ClassPathResource resource = new ClassPathResource(KNU_GEOJSON_FEATURE_FILENAME); |
| 129 | + try (InputStream is = resource.getInputStream()) { |
| 130 | + return new String(is.readAllBytes(), StandardCharsets.UTF_8); |
| 131 | + } catch (IOException e) { |
| 132 | + throw new RuntimeException(e); |
| 133 | + } |
| 134 | + } |
| 135 | +} |
0 commit comments