Skip to content

Commit

Permalink
FAACC 추천 전략 로직 수정 #138
Browse files Browse the repository at this point in the history
  • Loading branch information
bongsh0112 committed Feb 2, 2024
1 parent cf6d652 commit 29c1bae
Showing 1 changed file with 51 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,15 @@
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Stream;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import tify.server.core.exception.BaseException;
import tify.server.domain.domains.product.adaptor.ProductAdaptor;
import tify.server.domain.domains.product.domain.Product;
import tify.server.domain.domains.question.adaptor.FavorAnswerAdaptor;
import tify.server.domain.domains.question.domain.FavorAnswer;
import tify.server.domain.domains.question.dto.condition.FavorRecommendationDTO;
import tify.server.domain.domains.question.exception.QuestionException;

@Component
@RequiredArgsConstructor
Expand All @@ -34,12 +33,19 @@ public List<Product> recommendation(Long userId, String categoryName) {

List<FavorRecommendationDTO> recommendationDTO = getRecommendationDTO(userId);

validateAnswerSize(recommendationDTO);
// validateAnswerSize(recommendationDTO);

List<Product> firstFindProducts =
firstStep(categoryName, recommendationDTO.get(0).getAnswer());
List<Product> firstStep = firstStep(categoryName, recommendationDTO.get(0).getAnswer());

return secondStep(firstFindProducts, recommendationDTO.get(1).getAnswer());
if (recommendationDTO.size() == 2) { // 액서서리의 종류를 한가지만 고르면 size가 2
return getConditionalProducts(firstStep, recommendationDTO.get(1).getAnswer());
} else { // 아니라면 3
List<String> answers =
List.of(
recommendationDTO.get(1).getAnswer(),
recommendationDTO.get(2).getAnswer());
return secondStep(firstStep, answers);
}
}

@Override
Expand Down Expand Up @@ -72,18 +78,49 @@ private List<FavorRecommendationDTO> getRecommendationDTO(Long userId) {
}

private List<Product> firstStep(String categoryName, String answer) {
return productAdaptor.queryAllByCategoryNameAndCharacter(categoryName, answer);
String[] splitAnswer = answer.split(", ");
if (splitAnswer.length == 1) {
return productAdaptor.queryAllByCategoryNameAndCharacter(categoryName, splitAnswer[0]);
} else {
return Stream.concat(
productAdaptor
.queryAllByCategoryNameAndCharacter(
categoryName, splitAnswer[0])
.stream(),
productAdaptor
.queryAllByCategoryNameAndCharacter(
categoryName, splitAnswer[1])
.stream())
.toList();
}
}

private List<Product> secondStep(List<Product> products, String answer) {
return products.stream()
.filter(product -> product.getCharacteristic().contains(answer))
.toList();
private List<Product> secondStep(List<Product> productList, List<String> answers) {
List<Product> result = new ArrayList<>();
answers.forEach(answer -> result.addAll(getConditionalProducts(productList, answer)));
return result;
}

private void validateAnswerSize(List<FavorRecommendationDTO> dto) {
if (dto.size() != ANSWER_CNT) {
throw new BaseException(QuestionException.FAVOR_ANSWER_SIZE_NOT_MATCHED_CATEGORY_ERROR);
private List<Product> getConditionalProducts(List<Product> productList, String answer) {
String[] splitAnswer = answer.split(", ");
if (splitAnswer.length == 1) {
return productList.stream()
.filter(product -> product.getCharacteristic().contains(answer))
.toList();
} else {
return productList.stream()
.filter(
product ->
product.getCharacteristic().contains(splitAnswer[0])
|| product.getCharacteristic().contains(splitAnswer[1]))
.toList();
}
}

// private void validateAnswerSize(List<FavorRecommendationDTO> dto) {
// if (dto.size() != ANSWER_CNT) {
// throw new
// BaseException(QuestionException.FAVOR_ANSWER_SIZE_NOT_MATCHED_CATEGORY_ERROR);
// }
// }
}

0 comments on commit 29c1bae

Please sign in to comment.