Skip to content

Commit

Permalink
Merge pull request #65 from HanaPiece/feat/user-goal
Browse files Browse the repository at this point in the history
feat/목표 리스트 상세 조회 enrolledId 추가 및 목표에 대한 계좌 정보 조회 계좌 1개 오류 해결
  • Loading branch information
jungjaegun authored Jun 9, 2024
2 parents 1c0bfd4 + e4a527c commit 1da9bbe
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
import com.project.hana_piece.account.domain.Account;
import com.project.hana_piece.account.projection.AccountAutoDebitSummary;
import com.project.hana_piece.account.projection.UserGoalAccountSummary;
import java.util.List;
import java.util.Optional;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;
import java.util.Optional;

public interface AccountRepository extends JpaRepository<Account, Long> {

@Query(value = "SELECT * FROM accounts " +
Expand All @@ -25,42 +26,42 @@ public interface AccountRepository extends JpaRepository<Account, Long> {


@Query(value = "WITH EnrolledProductData AS ("
+ " SELECT "
+ " ep.enrolled_product_id, "
+ " ep.user_goal_id, "
+ " ep.product_id, "
+ " ep.created_at, "
+ " ep.maturity_date, "
+ " p.product_nm, "
+ " ac.account_id, "
+ " ac.account_number "
+ " FROM enrolled_products ep "
+ " JOIN products p ON ep.product_id = p.product_id "
+ " JOIN accounts ac ON ep.enrolled_product_id = ac.enrolled_product_id "
+ " WHERE ep.user_goal_id = :userGoalId "
+ "), "
+ "TransactionData AS ("
+ " SELECT "
+ " ac.account_id, "
+ " SUM(CASE WHEN act.account_transaction_type_cd != 'INTEREST' THEN act.amount ELSE 0 END) as principal, "
+ " SUM(CASE WHEN act.account_transaction_type_cd = 'INTEREST' THEN act.amount ELSE 0 END) as interestAmount "
+ " FROM account_transactions act "
+ " JOIN accounts ac ON act.account_id = ac.account_id "
+ " JOIN EnrolledProductData ed ON ac.account_id = ed.account_id "
+ " WHERE act.created_at BETWEEN DATE_FORMAT(ed.created_at, '%Y-%m-%d') AND DATE_FORMAT(ed.maturity_date, '%Y-%m-%d') "
+ " GROUP BY ac.account_id "
+ ") "
+ "SELECT "
+ " ed.account_id as accountId, "
+ " ed.product_nm as productNm, "
+ " ed.account_number as accountNumber, "
+ " ed.created_at as openingDate, "
+ " td.principal as principal, "
+ " ug.amount as targetAmount, "
+ " td.interestAmount as interestAmount "
+ " FROM EnrolledProductData ed "
+ " JOIN TransactionData td ON ed.account_id = td.account_id "
+ " JOIN user_goals ug ON ed.user_goal_id = ug.user_goal_id", nativeQuery = true)
+ " SELECT "
+ " ep.enrolled_product_id, "
+ " ep.user_goal_id, "
+ " ep.product_id, "
+ " ep.created_at, "
+ " ep.maturity_date, "
+ " p.product_nm, "
+ " ac.account_id, "
+ " ac.account_number "
+ " FROM enrolled_products ep "
+ " JOIN products p ON ep.product_id = p.product_id "
+ " JOIN accounts ac ON ep.enrolled_product_id = ac.enrolled_product_id "
+ " WHERE ep.user_goal_id = :userGoalId "
+ "), "
+ "TransactionData AS ("
+ " SELECT "
+ " ac.account_id, "
+ " SUM(CASE WHEN act.account_transaction_type_cd != 'INTEREST' THEN act.amount ELSE 0 END) as principal, "
+ " SUM(CASE WHEN act.account_transaction_type_cd = 'INTEREST' THEN act.amount ELSE 0 END) as interestAmount "
+ " FROM account_transactions act "
+ " JOIN accounts ac ON act.account_id = ac.account_id "
+ " JOIN EnrolledProductData ed ON ac.account_id = ed.account_id "
+ " WHERE act.created_at BETWEEN DATE_FORMAT(ed.created_at, '%Y-%m-%d') AND DATE_FORMAT(ed.maturity_date, '%Y-%m-%d') "
+ " GROUP BY ac.account_id "
+ ") "
+ "SELECT "
+ " ed.account_id as accountId, "
+ " ed.product_nm as productNm, "
+ " ed.account_number as accountNumber, "
+ " ed.created_at as openingDate, "
+ " td.principal as principal, "
+ " ug.amount as targetAmount, "
+ " td.interestAmount as interestAmount "
+ " FROM EnrolledProductData ed "
+ " LEFT JOIN TransactionData td ON ed.account_id = td.account_id "
+ " JOIN user_goals ug ON ed.user_goal_id = ug.user_goal_id", nativeQuery = true)
List<UserGoalAccountSummary> findUserGoalAccountList(@Param("userGoalId") Long userGoalId);

@Query(value = "SELECT * FROM accounts " +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

import com.project.hana_piece.goal.projection.UserGoalSummary;

import java.util.Collections;
import java.util.List;
import java.util.stream.Stream;

public record UserGoalListGetResponse(
Long userGoalId,
Expand All @@ -14,16 +12,10 @@ public record UserGoalListGetResponse(
String goalBeginDate,
Integer duration,
Long amount,
List<String> productNames,
List<EnrolledProductResponse> enrolledProducts,
Long savingMoney
) {
public static UserGoalListGetResponse fromProjection(UserGoalSummary projection) {
List<String> productNamesList = projection.getProductNames() != null ?
Stream.of(projection.getProductNames().split(","))
.map(String::trim) // 공백 제거
.filter(s -> !s.isEmpty()) // 빈 문자열 제거
.toList() : Collections.emptyList();

public static UserGoalListGetResponse fromProjection(UserGoalSummary projection, List<EnrolledProductResponse> enrolledProducts) {
return new UserGoalListGetResponse(
projection.getUserGoalId(),
projection.getGoalAlias(),
Expand All @@ -32,9 +24,14 @@ public static UserGoalListGetResponse fromProjection(UserGoalSummary projection)
projection.getGoalBeginDate(),
projection.getDuration(),
projection.getAmount(),
productNamesList,
enrolledProducts,
projection.getSavingMoney()
);
}
}

public static record EnrolledProductResponse(
Long enrolledProductId,
String enrolledProductName
) {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ public interface UserGoalSummary {
Long getGoalSpecificId();
Integer getDuration();
Long getAmount();
String getProductNames(); // List<String> 형태로 변환될 문자열
String getProductIds();
String getProductNames();
Long getSavingMoney();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public interface UserGoalRepository extends JpaRepository<UserGoal, Long> {
+ " ug.duration, "
+ " ug.amount, "
+ " GROUP_CONCAT(DISTINCT p.product_nm) as productNames, "
+ " GROUP_CONCAT(DISTINCT ep.enrolled_product_id) as productIds, "
+ " COALESCE(SUM(CASE WHEN at.account_transaction_type_cd != 'INTEREST' THEN at.amount ELSE 0 END), 0) as savingMoney "
+ " FROM user_goals ug "
+ " LEFT JOIN enrolled_products ep ON ug.user_goal_id = ep.user_goal_id "
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
import com.project.hana_piece.goal.repository.CarRepository;
import com.project.hana_piece.goal.repository.UserGoalRepository;
import com.project.hana_piece.goal.repository.WishRepository;
import com.project.hana_piece.product.domain.Product;
import com.project.hana_piece.product.exception.ProductNotFoundException;
import com.project.hana_piece.product.repository.ProductRepository;
import com.project.hana_piece.user.domain.User;
import com.project.hana_piece.user.exception.UserNotFoundException;
import com.project.hana_piece.user.repository.UserRepository;
Expand All @@ -18,12 +21,15 @@
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Stream;

@Service
@RequiredArgsConstructor
public class UserGoalService {


private final UserRepository userRepository;
private final ProductRepository productRepository;
private final UserGoalRepository userGoalRepository;
private final ApartmentRepository apartmentRepository;
private final CarRepository carRepository;
Expand Down Expand Up @@ -112,6 +118,20 @@ private UserGoal updateUserGoal(UserGoal existingUserGoal, UserGoalUpsertRequest
@Transactional(readOnly = true)
public List<UserGoalListGetResponse> findUserGoalList(Long userId) {
List<UserGoalSummary> userGoalSummaryList = userGoalRepository.findUserGoalList(userId);
return userGoalSummaryList.stream().map(UserGoalListGetResponse::fromProjection).toList();

return userGoalSummaryList.stream().map(summary -> {
List<UserGoalListGetResponse.EnrolledProductResponse> enrolledProducts = Stream.of(summary.getProductIds().split(","))
.map(String::trim) // 공백 제거
.filter(s -> !s.isEmpty()) // 빈 문자열 제거
.map(Long::parseLong) // 문자열을 Long으로 변환
.map(id -> new UserGoalListGetResponse.EnrolledProductResponse(id, getProductNameById(id)))
.toList();
return UserGoalListGetResponse.fromProjection(summary, enrolledProducts);
}).toList();
}

private String getProductNameById(Long id) {
Product product = productRepository.findById(id).orElseThrow(() -> new ProductNotFoundException(id));
return product.getProductNm();
}
}

0 comments on commit 1da9bbe

Please sign in to comment.