diff --git a/src/main/java/com/oven/server/api/user/repository/RatingWorkRepository.java b/src/main/java/com/oven/server/api/user/repository/RatingWorkRepository.java index 5703fea..b3427d7 100644 --- a/src/main/java/com/oven/server/api/user/repository/RatingWorkRepository.java +++ b/src/main/java/com/oven/server/api/user/repository/RatingWorkRepository.java @@ -13,6 +13,8 @@ public interface RatingWorkRepository extends JpaRepository { List findByUserOrderByCreatedAtDesc(User user); + List findByUser(User user); + RatingWork findByUserAndWork(User user, Work work); } diff --git a/src/main/java/com/oven/server/api/user/repository/UserRepository.java b/src/main/java/com/oven/server/api/user/repository/UserRepository.java index 0b5d748..27a02b2 100644 --- a/src/main/java/com/oven/server/api/user/repository/UserRepository.java +++ b/src/main/java/com/oven/server/api/user/repository/UserRepository.java @@ -4,6 +4,7 @@ import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; +import java.util.List; import java.util.Optional; @@ -14,4 +15,6 @@ public interface UserRepository extends JpaRepository { Optional findByUsername(String username); + List findUsersByIdIsNot(Long userId); + } diff --git a/src/main/java/com/oven/server/api/work/controller/HomeController.java b/src/main/java/com/oven/server/api/work/controller/HomeController.java index 008b1dd..99defcb 100644 --- a/src/main/java/com/oven/server/api/work/controller/HomeController.java +++ b/src/main/java/com/oven/server/api/work/controller/HomeController.java @@ -51,17 +51,23 @@ public Response> getPopularWorkList() { @Operation(summary = "맞춤 작품 추천 API") @GetMapping("/recommendation/works") public Response> getRecommendWorkList(@AuthenticationPrincipal User user) { - - String userId = String.valueOf(user.getId()); - String response = flaskFeignClient.getDataFromFlask(userId); - try { - List recommendWorkDtoList = getRecommendWorkListService.getRecommendWorkList(response); - return Response.success(ResponseCode.SUCCESS_OK, recommendWorkDtoList); - } catch (JsonProcessingException e) { - throw new RuntimeException(e); - } + return Response.success(ResponseCode.SUCCESS_OK, getRecommendWorkListService.getRecommendWorkList(user)); } +// @Operation(summary = "맞춤 작품 추천 API") +// @GetMapping("/recommendation/works") +// public Response> getRecommendWorkList(@AuthenticationPrincipal User user) { +// +// String userId = String.valueOf(user.getId()); +// String response = flaskFeignClient.getDataFromFlask(userId); +// try { +// List recommendWorkDtoList = getRecommendWorkListService.getRecommendWorkList(response); +// return Response.success(ResponseCode.SUCCESS_OK, recommendWorkDtoList); +// } catch (JsonProcessingException e) { +// throw new RuntimeException(e); +// } +// } + @Autowired public HomeController(GetRecommendProviderService getRecommendProviderService, GetPopularWorkListService getPopularWorkListService, GetRecommendWorksService getRecommendWorkListService, FlaskFeignClient flaskFeignClient) { this.getRecommendProviderService = getRecommendProviderService; diff --git a/src/main/java/com/oven/server/api/work/repository/WorkRepository.java b/src/main/java/com/oven/server/api/work/repository/WorkRepository.java index 5d1bea2..19c40ab 100644 --- a/src/main/java/com/oven/server/api/work/repository/WorkRepository.java +++ b/src/main/java/com/oven/server/api/work/repository/WorkRepository.java @@ -16,4 +16,7 @@ public interface WorkRepository extends JpaRepository, WorkRepositor @Query(value = "SELECT * FROM oven.work order by RAND() limit 4",nativeQuery = true) List findRandoms(); + @Query(value = "select w from Work w where w.id in :ids") + List findWorksByIdIn(List ids); + } diff --git a/src/main/java/com/oven/server/api/work/service/GetRecommendWorksService.java b/src/main/java/com/oven/server/api/work/service/GetRecommendWorksService.java index d8484e2..1c2aa00 100644 --- a/src/main/java/com/oven/server/api/work/service/GetRecommendWorksService.java +++ b/src/main/java/com/oven/server/api/work/service/GetRecommendWorksService.java @@ -3,58 +3,132 @@ import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; +import com.oven.server.api.user.domain.RatingWork; +import com.oven.server.api.user.domain.User; +import com.oven.server.api.user.repository.RatingWorkRepository; +import com.oven.server.api.user.repository.UserRepository; import com.oven.server.common.exception.BaseException; import com.oven.server.api.work.domain.Work; import com.oven.server.api.work.dto.response.WorkListDto; import com.oven.server.api.work.repository.WorkRepository; +import com.oven.server.common.response.ResponseCode; import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Map; +import java.util.concurrent.ThreadLocalRandom; import java.util.stream.Collectors; @Service @RequiredArgsConstructor +@Slf4j public class GetRecommendWorksService { private final WorkRepository workRepository; + private final UserRepository userRepository; + private final RatingWorkRepository ratingWorkRepository; - public List getRecommendWorkList(String dataFromFlask) throws BaseException, JsonProcessingException { + public List getRecommendWorkList(User user) { + user = userRepository.findByUsername(user.getUsername()).orElseThrow( + () -> new BaseException(ResponseCode.USER_NOT_FOUND) + ); - ObjectMapper objectMapper = new ObjectMapper(); - System.out.println(dataFromFlask); - System.out.println(dataFromFlask.getClass().getName()); + List ratingWorks = ratingWorkRepository.findByUser(user); - Map dataMap = objectMapper.readValue(dataFromFlask, new TypeReference>() {}); - String resultString = dataMap.get("result"); + log.info("----ratingWorks: {}", ratingWorks.size()); - List longList = Arrays.stream(resultString.split(",")) - .map(s -> s.replaceAll("[\\[\\]\\\\\"]", "")).map(String::trim) - .map(Long::parseLong) - .collect(Collectors.toList()); + int max = 0; + List result = new ArrayList<>(); + + for(User findUser : userRepository.findUsersByIdIsNot(user.getId())) { + + log.info("-----findUser: {}", findUser.getUsername()); + + List findRatingWorks = ratingWorkRepository.findByUser(findUser); + List tmp = findRatingWorks; + List sameWorks = new ArrayList<>(); + + log.info("-----findRatingWorks: {}", findRatingWorks.size()); + + int count = 0; - List recommendations = new ArrayList(); + for(int i = 0; i < ratingWorks.size(); i++) { + for(int j = 0; j < findRatingWorks.size(); j++) { + if(ratingWorks.get(i).getWork().equals(findRatingWorks.get(j).getWork())) { + count++; + tmp.remove(findRatingWorks.get(j)); + log.info("-----ratingWork: {}, findRatingWork: {}", ratingWorks.get(i).getWork().getId(),findRatingWorks.get(j).getWork().getId()); + } + } + } - for (int i = 0; i < longList.size(); i++) { - recommendations.add(workRepository.findById(longList.get(i)).get()); + log.info("-----count: {}", count); + log.info("----tmp size: {}", tmp.size()); + + if(count > max) { + max = count; + result = tmp; + } + } + + log.info("-----max: {}", max); + + List recommend = new ArrayList<>(); + + for(RatingWork ratingWork : result) { + recommend.add(ratingWork.getWork().getId()); } - List recommendWorkDtoList = recommendations - .stream() + return workRepository.findWorksByIdIn(recommend).stream() .map( - recommendWork -> WorkListDto - .builder() - .workId(recommendWork.getId()) - .title(recommendWork.getTitleKr()) - .poster(recommendWork.getPoster()) + work -> WorkListDto.builder() + .workId(work.getId()) + .title(work.getTitleKr()) + .poster(work.getPoster()) .build() ) .collect(Collectors.toList()); - return recommendWorkDtoList; } + +// public List getRecommendWorkList(String dataFromFlask) throws BaseException, JsonProcessingException { +// +// +// ObjectMapper objectMapper = new ObjectMapper(); +// System.out.println(dataFromFlask); +// System.out.println(dataFromFlask.getClass().getName()); +// +// Map dataMap = objectMapper.readValue(dataFromFlask, new TypeReference>() {}); +// String resultString = dataMap.get("result"); +// +// List longList = Arrays.stream(resultString.split(",")) +// .map(s -> s.replaceAll("[\\[\\]\\\\\"]", "")).map(String::trim) +// .map(Long::parseLong) +// .collect(Collectors.toList()); +// +// List recommendations = new ArrayList(); +// +// for (int i = 0; i < longList.size(); i++) { +// recommendations.add(workRepository.findById(longList.get(i)).get()); +// } +// +// List recommendWorkDtoList = recommendations +// .stream() +// .map( +// recommendWork -> WorkListDto +// .builder() +// .workId(recommendWork.getId()) +// .title(recommendWork.getTitleKr()) +// .poster(recommendWork.getPoster()) +// .build() +// ) +// .collect(Collectors.toList()); +// +// return recommendWorkDtoList; +// } }