-
Notifications
You must be signed in to change notification settings - Fork 4
[Feat] 매칭 점수 계산 및 저장 로직 구현 #173
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
150 changes: 150 additions & 0 deletions
150
src/main/java/com/capstone/pickIt/api/course/service/MatchScoreService.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,150 @@ | ||
| package com.capstone.pickIt.api.course.service; | ||
|
|
||
| import com.capstone.pickIt.domain.course.entity.ImportanceLevel; | ||
| import com.capstone.pickIt.domain.course.entity.RecruitmentStatus; | ||
| import com.capstone.pickIt.domain.course.entity.UserCourseProfile; | ||
| import com.capstone.pickIt.domain.course.entity.UserCourseTrait; | ||
| import com.capstone.pickIt.domain.course.repository.UserCourseProfileRepository; | ||
| import com.capstone.pickIt.domain.matching.entity.MatchScore; | ||
| import com.capstone.pickIt.domain.matching.repository.MatchScoreRepository; | ||
| import com.capstone.pickIt.domain.teamlevel.entity.TeamLevel; | ||
| import com.capstone.pickIt.domain.teamlevel.repository.TeamLevelRepository; | ||
| import com.capstone.pickIt.domain.trait.entity.TraitSide; | ||
| import com.capstone.pickIt.domain.user.entity.User; | ||
| import com.capstone.pickIt.domain.user.entity.UserDefaultTrait; | ||
| import com.capstone.pickIt.domain.user.repository.UserDefaultTraitRepository; | ||
| import com.capstone.pickIt.domain.user.repository.UserRepository; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.stereotype.Service; | ||
| import org.springframework.transaction.annotation.Transactional; | ||
|
|
||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.Optional; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| @Service | ||
| @RequiredArgsConstructor | ||
| public class MatchScoreService { | ||
|
|
||
| private final MatchScoreRepository matchScoreRepository; | ||
| private final UserCourseProfileRepository userCourseProfileRepository; | ||
| private final UserDefaultTraitRepository userDefaultTraitRepository; | ||
| private final TeamLevelRepository teamLevelRepository; | ||
| private final UserRepository userRepository; | ||
|
|
||
| // 카드 생성/수정 시 → 해당 카드에 대해 모든 유저의 점수 계산 | ||
| @Transactional | ||
| public void recalculateForProfile(UserCourseProfile targetProfile) { | ||
| // 기존 점수 삭제 | ||
| matchScoreRepository.deleteByProfileId(targetProfile.getId()); | ||
|
|
||
| // 해당 과목의 다른 유저 목록 조회 | ||
| List<UserCourseProfile> otherProfiles = userCourseProfileRepository | ||
| .findAllByUserIdAndDeletedAtIsNull(targetProfile.getUser().getId()); | ||
|
|
||
| // 같은 과목의 모든 유저 조회 (본인 제외) | ||
| List<User> allUsers = userRepository.findAll().stream() | ||
| .filter(u -> !u.getId().equals(targetProfile.getUser().getId())) | ||
| .toList(); | ||
|
|
||
| // 각 유저와의 점수 계산 | ||
| List<MatchScore> scores = allUsers.stream() | ||
| .map(user -> calculateScore(user, targetProfile)) | ||
| .toList(); | ||
|
|
||
| matchScoreRepository.saveAll(scores); | ||
| } | ||
|
|
||
| // 기본 팀플 성향 수정 시 → 해당 유저가 조회하는 모든 점수 재계산 | ||
| @Transactional | ||
| public void recalculateForUser(Long userId) { | ||
| User user = userRepository.findById(userId).orElseThrow(); | ||
|
|
||
| // 기존 점수 삭제 | ||
| matchScoreRepository.deleteByUserId(userId); | ||
|
|
||
| // 활성화된 모든 카드 조회 (본인 카드 제외) | ||
| List<UserCourseProfile> allProfiles = userCourseProfileRepository.findAll().stream() | ||
| .filter(p -> !p.getUser().getId().equals(userId)) | ||
| .filter(p -> p.getDeletedAt() == null) | ||
| .filter(p -> p.getRecruitmentStatus() != RecruitmentStatus.RECRUITMENT_COMPLETED) | ||
| .toList(); | ||
|
|
||
| List<MatchScore> scores = allProfiles.stream() | ||
| .map(profile -> calculateScore(user, profile)) | ||
| .toList(); | ||
|
|
||
| matchScoreRepository.saveAll(scores); | ||
| } | ||
|
|
||
| private MatchScore calculateScore(User user, UserCourseProfile targetProfile) { | ||
| int traitScore = calculateTraitScore(user.getId(), targetProfile); | ||
| int importanceScore = calculateImportanceScore(user.getId(), targetProfile); | ||
| int levelScore = calculateLevelScore(user.getId(), targetProfile.getUser().getId()); | ||
|
|
||
| return MatchScore.of(user, targetProfile, traitScore, importanceScore, levelScore); | ||
| } | ||
|
|
||
| // 성향 일치도 (최대 5점) | ||
| private int calculateTraitScore(Long userId, UserCourseProfile targetProfile) { | ||
| List<UserDefaultTrait> myTraits = userDefaultTraitRepository.findByUserId(userId); | ||
| List<UserCourseTrait> targetTraits = targetProfile.getTraits(); | ||
|
|
||
| Map<Long, TraitSide> myTraitMap = myTraits.stream() | ||
| .collect(Collectors.toMap( | ||
| t -> t.getTraitItem().getTraitItemsId(), | ||
| UserDefaultTrait::getSelectedSide | ||
| )); | ||
|
|
||
| int score = 0; | ||
| for (UserCourseTrait targetTrait : targetTraits) { | ||
| Long traitItemId = targetTrait.getTraitItem().getTraitItemsId(); | ||
| TraitSide mySide = myTraitMap.get(traitItemId); | ||
| if (mySide != null && mySide == targetTrait.getSelectedSide()) { | ||
| score++; | ||
| } | ||
| } | ||
| return score; | ||
| } | ||
|
|
||
| // 중요도 일치도 (최대 3점) | ||
| private int calculateImportanceScore(Long userId, UserCourseProfile targetProfile) { | ||
| // 내 카드 중 같은 과목 카드 조회 | ||
| UserCourseProfile myProfile = userCourseProfileRepository | ||
| .findByUserIdAndCourseIdAndDeletedAtIsNull(userId, targetProfile.getCourse().getId()) | ||
| .orElse(null); | ||
|
|
||
| if (myProfile == null) return 0; | ||
|
|
||
| int myLevel = importanceToInt(myProfile.getImportanceLevel()); | ||
| int targetLevel = importanceToInt(targetProfile.getImportanceLevel()); | ||
| int diff = Math.abs(myLevel - targetLevel); | ||
|
|
||
| return Math.max(3 - diff, 0); | ||
| } | ||
|
|
||
| // 팀플 레벨 (최대 3점) | ||
| private int calculateLevelScore(Long myUserId, Long targetUserId) { | ||
| Optional<TeamLevel> myTeamLevel = teamLevelRepository.findByUserId(myUserId); | ||
| Optional<TeamLevel> targetTeamLevel = teamLevelRepository.findByUserId(targetUserId); | ||
|
|
||
| // 한쪽이라도 레벨 정보 없으면 0점 | ||
| if (myTeamLevel.isEmpty() || targetTeamLevel.isEmpty()) { | ||
| return 0; | ||
| } | ||
|
|
||
| int myLevel = myTeamLevel.get().getLevel(); | ||
| int targetLevel = targetTeamLevel.get().getLevel(); | ||
| int diff = Math.abs(myLevel - targetLevel); | ||
| return Math.max(3 - diff, 0); | ||
| } | ||
|
|
||
| private int importanceToInt(ImportanceLevel level) { | ||
| return switch (level) { | ||
| case HIGH -> 3; | ||
| case MEDIUM -> 2; | ||
| case LOW -> 1; | ||
| }; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
재계산 경로가 전체 스캔 + N+1 조회 구조라 트래픽 시 급격히 느려질 수 있습니다.
Line 45-52, 66-74에서 전체 로딩 후 스트림 필터링을 하고, 각 항목 계산마다 추가 조회가 발생해(성향/과목 프로필/팀레벨) 재계산 1회당 쿼리 수가 크게 증가합니다. 후보군을 DB에서 먼저 좁히고, 필요한 데이터는 배치 조회로 한 번에 가져오도록 바꾸는 게 안전합니다.
개선 방향 예시
관련 개념: Spring Data JPA에서 projection/배치 조회로 N+1 회피 패턴을 권장합니다.
Also applies to: 66-74, 79-83, 89-90, 112-114, 127-130
🤖 Prompt for AI Agents