Skip to content

Commit

Permalink
Merge pull request #95 from SWM-WeLike2Coding/feat/safetyScore
Browse files Browse the repository at this point in the history
feat: 상품 비교에 스텝다운의 경우 AI safetyScore 값도 받아오도록 로직 추가
  • Loading branch information
kjungw1025 authored Oct 29, 2024
2 parents f107c54 + 5b329f4 commit 587161a
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,10 @@ public class ResponseProductComparisonTargetDto {
@Schema(description = "청약 마감일", example = "2024-06-21")
private final LocalDate subscriptionEndDate;

public ResponseProductComparisonTargetDto(Product product) {
@Schema(description = "AI가 판단한 상품 안전도", example = "0.89")
private final BigDecimal safetyScore;

public ResponseProductComparisonTargetDto(Product product, BigDecimal safetyScore) {
this.id = product.getId();
this.issuer = product.getIssuer();
this.name = product.getName();
Expand All @@ -59,5 +62,6 @@ public ResponseProductComparisonTargetDto(Product product) {
this.maximumLossRate = product.getMaximumLossRate();
this.subscriptionStartDate = product.getSubscriptionStartDate();
this.subscriptionEndDate = product.getSubscriptionEndDate();
this.safetyScore = safetyScore;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,11 @@ public Page<SummarizedOnSaleProductDto> listByOnSale(String type, Pageable pagea

// AI 결과 리스트에서 productId를 key로 하는 Map으로 변환
Map<Long, BigDecimal> productSafetyScoreMap = responseStepDownAIResultDtos.stream()
.collect(Collectors.toMap(ResponseAIResultDto::getProductId, ResponseAIResultDto::getSafetyScore));
.collect(Collectors.toMap(
ResponseAIResultDto::getProductId,
ResponseAIResultDto::getSafetyScore,
(existing, replacement) -> existing // 중복된 경우 기존 값 사용
));

List<SummarizedOnSaleProductDto> summarizedProducts = products.getContent().stream()
.map(product -> {
Expand Down Expand Up @@ -146,17 +150,41 @@ public ResponseSingleProductDto findOne(Long productId, Long userId) {
public Map<String, List<ResponseProductComparisonTargetDto>> findComparisonTargets(Long id) {
Map<String, List<ResponseProductComparisonTargetDto>> result = new HashMap<>();

// 타겟 상품
Product product = productRepository.isItProductOnSale(id).orElseThrow(NotOnSaleProductException::new);
List<TickerSymbol> tickerSymbolEntityList = tickerSymbolRepository.findTickerSymbolList(id);
List<String> tickerSymbolList = tickerSymbolEntityList.stream()
.map(TickerSymbol::getTickerSymbol)
.toList();
List<ResponseProductComparisonTargetDto> target = new ArrayList<>();
target.add(new ResponseProductComparisonTargetDto(product));

// 비교 상품
List<Product> productComparisonResults = productRepository.findComparisonResults(id, product.getEquityCount(), tickerSymbolList);

// 타겟 및 비교 상품들 중 STEP_DOWN 타입 필터링
List<Long> stepDownProductIds = new ArrayList<>(productComparisonResults.stream()
.filter(productComparisonResult -> productComparisonResult.getType() == ProductType.STEP_DOWN)
.map(Product::getId)
.toList());
if (product.getType() == ProductType.STEP_DOWN)
stepDownProductIds.add(product.getId());
List<ResponseAIResultDto> responseStepDownAIResultDtos = listStepDownAIResult(stepDownProductIds);

// AI 결과 리스트에서 productId를 key로 하는 Map으로 변환
Map<Long, BigDecimal> productSafetyScoreMap = responseStepDownAIResultDtos.stream()
.collect(Collectors.toMap(
ResponseAIResultDto::getProductId,
ResponseAIResultDto::getSafetyScore,
(existing, replacement) -> existing // 중복된 경우 기존 값 사용
));

List<ResponseProductComparisonTargetDto> target = new ArrayList<>();
target.add(new ResponseProductComparisonTargetDto(product, productSafetyScoreMap.getOrDefault(product.getId(), null)));

List<ResponseProductComparisonTargetDto> comparisonResults = productComparisonResults.stream()
.map(ResponseProductComparisonTargetDto::new)
.map(productComparisonResult -> new ResponseProductComparisonTargetDto(
productComparisonResult,
productSafetyScoreMap.getOrDefault(productComparisonResult.getId(), null)
))
.toList();

result.put("target", target);
Expand Down

0 comments on commit 587161a

Please sign in to comment.