|
| 1 | +package com.backend.domain.cash.service; |
| 2 | + |
| 3 | +import com.backend.domain.bid.repository.BidRepository; |
| 4 | +import com.backend.domain.cash.constant.RelatedType; |
| 5 | +import com.backend.domain.cash.dto.CashResponse; |
| 6 | +import com.backend.domain.cash.dto.CashTransactionItemResponse; |
| 7 | +import com.backend.domain.cash.dto.CashTransactionResponse; |
| 8 | +import com.backend.domain.cash.dto.CashTransactionsResponse; |
| 9 | +import com.backend.domain.cash.entity.Cash; |
| 10 | +import com.backend.domain.cash.entity.CashTransaction; |
| 11 | +import com.backend.domain.cash.repository.CashRepository; |
| 12 | +import com.backend.domain.cash.repository.CashTransactionRepository; |
| 13 | +import com.backend.domain.member.entity.Member; |
| 14 | +import lombok.RequiredArgsConstructor; |
| 15 | +import org.springframework.data.domain.Page; |
| 16 | +import org.springframework.data.domain.PageRequest; |
| 17 | +import org.springframework.http.HttpStatus; |
| 18 | +import org.springframework.stereotype.Service; |
| 19 | +import org.springframework.transaction.annotation.Transactional; |
| 20 | +import org.springframework.web.server.ResponseStatusException; |
| 21 | + |
| 22 | +import java.time.Instant; |
| 23 | +import java.time.format.DateTimeFormatter; |
| 24 | +import java.util.List; |
| 25 | + |
| 26 | +@Service |
| 27 | +@RequiredArgsConstructor |
| 28 | +public class CashService { |
| 29 | + |
| 30 | + private final CashRepository cashRepository; |
| 31 | + private final CashTransactionRepository cashTransactionRepository; |
| 32 | + private final BidRepository bidRepository; |
| 33 | + |
| 34 | + @Transactional(readOnly = true) |
| 35 | + public CashResponse getMyCashResponse(Member member) { |
| 36 | + Cash cash = cashRepository.findByMember(member) |
| 37 | + .orElseThrow(() -> |
| 38 | + new ResponseStatusException(HttpStatus.NOT_FOUND, "지갑이 아직 생성되지 않았습니다.") |
| 39 | + ); |
| 40 | + |
| 41 | + return CashResponse.builder() |
| 42 | + .cashId(cash.getId()) |
| 43 | + .memberId(member.getId()) |
| 44 | + .balance(cash.getBalance()) |
| 45 | + .createDate(cash.getCreateDate() != null ? cash.getCreateDate().toString() : null) |
| 46 | + .modifyDate(cash.getModifyDate() != null ? cash.getModifyDate().toString() : null) |
| 47 | + .build(); |
| 48 | + } |
| 49 | + |
| 50 | + // 원장 목록 조회.. |
| 51 | + @Transactional(readOnly = true) |
| 52 | + public CashTransactionsResponse getMyTransactions(Member member, int page1Base, int size) { |
| 53 | + Cash cash = cashRepository.findByMember(member) |
| 54 | + .orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "지갑이 아직 생성되지 않았습니다.")); |
| 55 | + |
| 56 | + int page0 = Math.max(0, page1Base - 1); |
| 57 | + PageRequest pageable = PageRequest.of(page0, Math.max(1, size)); |
| 58 | + |
| 59 | + Page<CashTransaction> page = cashTransactionRepository.findAllByCashOrderByIdDesc(cash, pageable); |
| 60 | + |
| 61 | + List<CashTransactionItemResponse> items = page.getContent().stream() |
| 62 | + .map(this::toItem) |
| 63 | + .toList(); |
| 64 | + |
| 65 | + return CashTransactionsResponse.builder() |
| 66 | + .page(page1Base) |
| 67 | + .size(size) |
| 68 | + .total(page.getTotalElements()) |
| 69 | + .items(items) |
| 70 | + .build(); |
| 71 | + } |
| 72 | + |
| 73 | + private CashTransactionItemResponse toItem(CashTransaction tx) { |
| 74 | + String createdAt = tx.getCreateDate() != null |
| 75 | + ? tx.getCreateDate().format(DateTimeFormatter.ISO_LOCAL_DATE_TIME) |
| 76 | + : null; |
| 77 | + |
| 78 | + // 관련 정보(ERD의 related_type / related_id 기반).. |
| 79 | + CashTransactionItemResponse.Related related = null; |
| 80 | + if (tx.getRelatedType() != null && tx.getRelatedId() != null) { |
| 81 | + related = CashTransactionItemResponse.Related.builder() |
| 82 | + .type(tx.getRelatedType().name()) |
| 83 | + .id(tx.getRelatedId()) |
| 84 | + .build(); |
| 85 | + } |
| 86 | + |
| 87 | + return CashTransactionItemResponse.builder() |
| 88 | + .transactionId(tx.getId()) |
| 89 | + .cashId(tx.getCash().getId()) |
| 90 | + .type(tx.getType().name()) |
| 91 | + .amount(tx.getAmount()) |
| 92 | + .balanceAfter(tx.getBalanceAfter()) |
| 93 | + .createdAt(createdAt) |
| 94 | + .related(related) |
| 95 | + .build(); |
| 96 | + } |
| 97 | + |
| 98 | + // 원장 단건 상세.. |
| 99 | + @Transactional(readOnly = true) |
| 100 | + public CashTransactionResponse getMyTransactionDetail(Member member, Long transactionId) { |
| 101 | + |
| 102 | + // 내 지갑.. |
| 103 | + Cash cash = cashRepository.findByMember(member) |
| 104 | + .orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "지갑이 아직 생성되지 않았습니다.")); |
| 105 | + |
| 106 | + // 내 지갑에 속한 트랜잭션만 조회 (타인의 데이터 차단).. |
| 107 | + CashTransaction tx = cashTransactionRepository.findByIdAndCash(transactionId, cash) |
| 108 | + .orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "원장 내역을 찾을 수 없습니다.")); |
| 109 | + |
| 110 | + String createdAt = tx.getCreateDate() != null |
| 111 | + ? tx.getCreateDate().format(DateTimeFormatter.ISO_LOCAL_DATE_TIME) |
| 112 | + : null; |
| 113 | + |
| 114 | + // related + 링크(선택).. |
| 115 | + CashTransactionResponse.Related related = null; |
| 116 | + if (tx.getRelatedType() != null && tx.getRelatedId() != null) { |
| 117 | + CashTransactionResponse.Links links = buildLinks(tx.getRelatedType(), tx.getRelatedId()); |
| 118 | + |
| 119 | + related = CashTransactionResponse.Related.builder() |
| 120 | + .type(tx.getRelatedType().name()) |
| 121 | + .id(tx.getRelatedId()) |
| 122 | + .links(links) |
| 123 | + .build(); |
| 124 | + } |
| 125 | + |
| 126 | + return CashTransactionResponse.builder() |
| 127 | + .transactionId(tx.getId()) |
| 128 | + .cashId(tx.getCash().getId()) |
| 129 | + .type(tx.getType().name()) |
| 130 | + .amount(tx.getAmount()) |
| 131 | + .balanceAfter(tx.getBalanceAfter()) |
| 132 | + .createdAt(createdAt) |
| 133 | + .related(related) |
| 134 | + .build(); |
| 135 | + } |
| 136 | + |
| 137 | + private CashTransactionResponse.Links buildLinks(RelatedType type, Long id) { |
| 138 | + if (id == null) return null; |
| 139 | + return switch (type) { |
| 140 | + case PAYMENT -> CashTransactionResponse.Links.builder() |
| 141 | + .paymentDetail("/api/v1/payments/me/" + id) |
| 142 | + .build(); |
| 143 | + case BID -> { |
| 144 | + Long productId = bidRepository.findById(id) |
| 145 | + .map(b -> b.getProduct().getId()) |
| 146 | + .orElse(null); |
| 147 | + yield productId == null ? null : |
| 148 | + CashTransactionResponse.Links.builder() |
| 149 | + .bidDetail("/api/v1/bids/products/" + productId) |
| 150 | + .build(); |
| 151 | + } |
| 152 | + default -> null; // REFUND/ADJUSTMENT/PROMOTION 등은 링크 없을 수 있음.. |
| 153 | + }; |
| 154 | + } |
| 155 | +} |
0 commit comments