Skip to content

Commit

Permalink
feat: 좌표 근처 시설 가져오는 메서드 추가 및 스케줄러 순서 오류 변경
Browse files Browse the repository at this point in the history
- 세부정보 갱신을 제일 마지막으로 변경
- 수행시간을 로그로 남김(차후 필요없게 되면 제거 예정)
  • Loading branch information
JHwan96 committed Sep 15, 2024
1 parent 4fda662 commit 92833bd
Show file tree
Hide file tree
Showing 20 changed files with 309 additions and 107 deletions.
8 changes: 8 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ repositories {
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'

compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'

implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.hibernate:hibernate-spatial:6.6.0.Final'

// Use JUnit Jupiter for testing.
testImplementation libs.junit.jupiter

Expand All @@ -29,6 +35,8 @@ dependencies {

// This dependency is used by the application.
implementation libs.guava

implementation(project(':db'))
}

// Apply a specific Java toolchain to ease working on different environments.
Expand Down
11 changes: 11 additions & 0 deletions app/src/main/java/org/healthmap/app/config/JpaConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.healthmap.app.config;

import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;

@Configuration
@EntityScan(basePackages = "org.healthmap.db")
@EnableJpaRepositories(basePackages = "org.healthmap.db")
public class JpaConfig {
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package org.healthmap.app.controller;

import lombok.RequiredArgsConstructor;
import org.healthmap.app.dto.HealthMapRequestDto;
import org.healthmap.app.dto.HealthMapResponseDto;
import org.healthmap.app.service.HealthMapService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequiredArgsConstructor
@RequestMapping("/healthmap")
public class HealthMapController {
private final HealthMapService healthMapService;

@GetMapping
public HealthMapRequestDto findAllDrugstore() {
return null;
}

@GetMapping("/around")
public ResponseEntity<List<HealthMapResponseDto>> findDrugstoreAround(
@ModelAttribute HealthMapRequestDto requestDto
) {
List<HealthMapResponseDto> nearByMedicalFacility = healthMapService.getNearByMedicalFacility(requestDto);

return ResponseEntity.ok(nearByMedicalFacility);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package org.healthmap.app.converter;

import org.healthmap.app.dto.HealthMapResponseDto;
import org.healthmap.db.medicalfacility.MedicalFacilityEntity;
import org.locationtech.jts.geom.Point;

public class HealthMapConverter {
public static HealthMapResponseDto toHealthMapResponseDto(MedicalFacilityEntity entity, double distance) {
Point coordinate = entity.getCoordinate();
double longitude = coordinate.getX();
double latitude = coordinate.getY();
return HealthMapResponseDto.of(
entity.getId(), entity.getName(), entity.getAddress(), entity.getPhoneNumber(), entity.getUrl(), entity.getPostNumber(),
entity.getType(), entity.getState(), entity.getCity(), entity.getTown(), latitude, longitude, entity.getParking(),
entity.getParkingEtc(), entity.getTreatmentMon(), entity.getTreatmentTue(), entity.getTreatmentWed(), entity.getTreatmentThu(),
entity.getTreatmentFri(), entity.getTreatmentSat(), entity.getTreatmentSun(), entity.getReceiveWeek(), entity.getReceiveSat(),
entity.getLunchWeek(), entity.getLunchSat(), entity.getNoTreatmentSun(), entity.getNoTreatmentHoliday(), entity.getEmergencyDay(),
entity.getEmergencyNight(), distance
);
}
}
41 changes: 0 additions & 41 deletions app/src/main/java/org/healthmap/app/dto/DrugstoreDto.java

This file was deleted.

17 changes: 17 additions & 0 deletions app/src/main/java/org/healthmap/app/dto/HealthMapRequestDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.healthmap.app.dto;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;

@Setter
@Getter
@AllArgsConstructor
public class HealthMapRequestDto {
private double latitude;
private double longitude;

public static HealthMapRequestDto of(double latitude, double longitude) {
return new HealthMapRequestDto(latitude, longitude);
}
}
58 changes: 58 additions & 0 deletions app/src/main/java/org/healthmap/app/dto/HealthMapResponseDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package org.healthmap.app.dto;

import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;


@AllArgsConstructor(access = AccessLevel.PROTECTED)
@NoArgsConstructor
@Getter
public class HealthMapResponseDto {
private String code; //암호화 요양 기호
private String name; //요양기관명(병원명)
private String address; //주소
private String phoneNumber; //전화번호
private String pageUrl; //병원 홈페이지
private String postNumber; //우편번호
private String type; //종별코드명
private String state; //시도코드명
private String city; //시군구코드
private String town; //읍면동
private double latitude;
private double longitude;
private String parking;
private String parkingEtc;
private String treatmentMon; // 진료시간_월
private String treatmentTue;
private String treatmentWed;
private String treatmentThu;
private String treatmentFri;
private String treatmentSat;
private String treatmentSun;
private String receiveWeek; // 접수시간_평일
private String receiveSat; // 접수시간_토요일
private String lunchWeek; // 점심시간_평일
private String lunchSat; // 점심시간_토
private String noTreatmentSun; // 일요일 휴진
private String noTreatmentHoliday; // 공휴일 휴진
private String emergencyDay;
private String emergencyNight;
private double distance;

public static HealthMapResponseDto of(
String code, String name, String address, String phoneNumber, String pageUrl, String postNumber, String type,
String state, String city, String town, double latitude, double longitude, String parking, String parkingEtc, String treatmentMon,
String treatmentTue, String treatmentWed, String treatmentThu, String treatmentFri, String treatmentSat, String treatmentSun,
String receiveWeek, String receiveSat, String lunchWeek, String lunchSat, String noTreatmentSun, String noTreatmentHoliday,
String emergencyDay, String emergencyNight, double distance) {
return new HealthMapResponseDto(
code, name, address, phoneNumber, pageUrl, postNumber, type, state, city,
town, latitude, longitude, parking, parkingEtc, treatmentMon, treatmentTue, treatmentWed,
treatmentThu, treatmentFri, treatmentSat, treatmentSun, receiveWeek, receiveSat,
lunchWeek, lunchSat, noTreatmentSun, noTreatmentHoliday, emergencyDay, emergencyNight, distance
);
}

}
39 changes: 39 additions & 0 deletions app/src/main/java/org/healthmap/app/service/HealthMapService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package org.healthmap.app.service;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.healthmap.app.converter.HealthMapConverter;
import org.healthmap.app.dto.HealthMapRequestDto;
import org.healthmap.app.dto.HealthMapResponseDto;
import org.healthmap.db.medicalfacility.MedicalFacilityEntity;
import org.healthmap.db.medicalfacility.MedicalFacilityRepository;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

@RequiredArgsConstructor
@Service
@Slf4j
public class HealthMapService {
private final MedicalFacilityRepository medicalFacilityRepository;

// 2km 이내의 시설 찾기
public List<HealthMapResponseDto> getNearByMedicalFacility(HealthMapRequestDto requestDto) {
int range = 2;
List<Object[]> nearMedicalFacility = medicalFacilityRepository.findNearMedicalFacility(requestDto.getLatitude(), requestDto.getLongitude(), range);
List<HealthMapResponseDto> healthMapResponseDtoList = Optional.ofNullable(nearMedicalFacility)
.map(objList -> objList.stream()
.map(x -> {
MedicalFacilityEntity entity = (MedicalFacilityEntity) x[0];
double distance = (double) x[1];
return HealthMapConverter.toHealthMapResponseDto(entity, distance);
})
.collect(Collectors.toList())
)
.orElseThrow(() -> new NullPointerException("값이 없습니다."));
log.info("healthMapResponseDtoList size : {}", healthMapResponseDtoList.size());
return healthMapResponseDtoList;
}
}
8 changes: 8 additions & 0 deletions app/src/main/resources/application.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
spring:
profiles:
include:
- secret
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:13306/healthmap?useSSL=false&allowPublicKeyRetrieval=true&characterEncoding=UTF-8&serverTimezone=Asia/Seoul
username: root
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package org.healthmap.app.service;

import org.assertj.core.api.Assertions;
import org.healthmap.app.dto.HealthMapRequestDto;
import org.healthmap.app.dto.HealthMapResponseDto;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.List;

@SpringBootTest
class HealthMapServiceTest {
@Autowired
private HealthMapService healthMapService;

@Test
@DisplayName("좌표 근처의 병원, 약국을 가져온다")
void test() {
HealthMapRequestDto healthMapRequestDto = HealthMapRequestDto.of(37.4828517, 126.9963104);
List<HealthMapResponseDto> nearByMedicalFacility = healthMapService.getNearByMedicalFacility(healthMapRequestDto);

Assertions.assertThat(nearByMedicalFacility).isNotEmpty();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@

public interface CustomMedicalFacilityRepository {
void customSaveAll(List<MedicalFacilityEntity> medicalFacilityEntities);

List<Object[]> findNearMedicalFacility(double latitude, double longitude, int range);
}
Original file line number Diff line number Diff line change
@@ -1,23 +1,40 @@
package org.healthmap.db.medicalfacility;

import jakarta.persistence.EntityManager;
import jakarta.persistence.Query;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Repository;

import java.util.List;

@RequiredArgsConstructor
@Repository
public class CustomMedicalFacilityRepositoryImpl implements CustomMedicalFacilityRepository{
public class CustomMedicalFacilityRepositoryImpl implements CustomMedicalFacilityRepository {
private final EntityManager em;


@Override
public void customSaveAll(List<MedicalFacilityEntity> entityList) {
for(MedicalFacilityEntity entity : entityList) {
for (MedicalFacilityEntity entity : entityList) {
em.persist(entity);
}
em.flush();
em.clear();
}

@Override
public List<Object[]> findNearMedicalFacility(double latitude, double longitude, int range) {
String nativeQuery = "SELECT m.*, " +
"ST_Distance_Sphere(ST_GeomFromText(CONCAT('POINT(', ?1, ' ', ?2, ')'), 4326), coordinate) AS distance " +
"FROM medical_facility m " +
"WHERE ST_Distance_Sphere(ST_GeomFromText(CONCAT('POINT(', ?1, ' ', ?2, ')'), 4326), coordinate) <= ?3 " +
"ORDER BY distance limit 30";
Query query = em.createNativeQuery(nativeQuery, "MedicalFacilityMapping");
query.setParameter(1, latitude);
query.setParameter(2, longitude);
query.setParameter(3, range * 1000);

List<Object[]> result = query.getResultList();

return result;
}
}
Loading

0 comments on commit 92833bd

Please sign in to comment.