|
1 | 1 | package com.jangburich.domain.store.service;
|
2 | 2 |
|
| 3 | +import java.time.LocalDate; |
| 4 | +import java.util.Optional; |
| 5 | + |
| 6 | +import org.springframework.stereotype.Service; |
| 7 | +import org.springframework.transaction.annotation.Transactional; |
| 8 | + |
3 | 9 | import com.jangburich.domain.point.domain.PointTransaction;
|
4 | 10 | import com.jangburich.domain.point.domain.TransactionType;
|
5 | 11 | import com.jangburich.domain.point.domain.repository.PointTransactionRepository;
|
6 | 12 | import com.jangburich.domain.store.domain.Store;
|
7 | 13 | import com.jangburich.domain.store.domain.StoreTeam;
|
8 | 14 | import com.jangburich.domain.store.dto.request.PrepayRequest;
|
| 15 | +import com.jangburich.domain.store.dto.response.PrepaymentInfoResponse; |
9 | 16 | import com.jangburich.domain.store.repository.StoreRepository;
|
10 | 17 | import com.jangburich.domain.store.repository.StoreTeamRepository;
|
11 | 18 | import com.jangburich.domain.team.domain.Team;
|
|
15 | 22 | import com.jangburich.global.error.DefaultNullPointerException;
|
16 | 23 | import com.jangburich.global.payload.ErrorCode;
|
17 | 24 | import com.jangburich.global.payload.Message;
|
18 |
| -import java.time.LocalDate; |
19 |
| -import java.util.Optional; |
| 25 | + |
20 | 26 | import lombok.RequiredArgsConstructor;
|
21 |
| -import org.springframework.stereotype.Service; |
22 |
| -import org.springframework.transaction.annotation.Transactional; |
23 | 27 |
|
24 | 28 | @Service
|
25 | 29 | @Transactional(readOnly = true)
|
26 | 30 | @RequiredArgsConstructor
|
27 | 31 | public class PrepayService {
|
28 | 32 |
|
29 |
| - private final UserRepository userRepository; |
30 |
| - private final StoreRepository storeRepository; |
31 |
| - private final TeamRepository teamRepository; |
32 |
| - private final StoreTeamRepository storeTeamRepository; |
33 |
| - private final PointTransactionRepository pointTransactionRepository; |
34 |
| - |
35 |
| - @Transactional |
36 |
| - public Message prepay(String userId, PrepayRequest prepayRequest) { |
37 |
| - User user = userRepository.findByProviderId(userId) |
38 |
| - .orElseThrow(() -> new DefaultNullPointerException(ErrorCode.INVALID_AUTHENTICATION)); |
39 |
| - |
40 |
| - Team team = teamRepository.findById(prepayRequest.teamId()) |
41 |
| - .orElseThrow(() -> new IllegalArgumentException("유효하지 않은 팀 id 입니다.")); |
42 |
| - |
43 |
| - Store store = storeRepository.findById(prepayRequest.storeId()) |
44 |
| - .orElseThrow(() -> new IllegalArgumentException("유효하지 않은 가게 id 입니다.")); |
45 |
| - |
46 |
| - team.validateIsTeamLeader(team.getTeamLeader().getUser_id(), user.getUserId()); |
47 |
| - |
48 |
| - user.validateHasPointWithPrepayAmount(prepayRequest.prepayAmount(), user.getPoint()); |
49 |
| - |
50 |
| - user.usePoint(prepayRequest.prepayAmount()); |
51 |
| - PointTransaction pointTransaction = PointTransaction |
52 |
| - .builder() |
53 |
| - .transactionType(TransactionType.PREPAY) |
54 |
| - .transactionedPoint(prepayRequest.prepayAmount()) |
55 |
| - .user(user) |
56 |
| - .store(store) |
57 |
| - .build(); |
58 |
| - |
59 |
| - pointTransactionRepository.save(pointTransaction); |
60 |
| - |
61 |
| - LocalDate expirationDate = LocalDate.now().plusDays(store.getPrepaymentDuration()); |
62 |
| - |
63 |
| - StoreTeam buildedStoreTeam = StoreTeam |
64 |
| - .builder() |
65 |
| - .team(team) |
66 |
| - .store(store) |
67 |
| - .point(prepayRequest.prepayAmount()) |
68 |
| - .personalAllocatedPoint(prepayRequest.personalAllocatedAmount()) |
69 |
| - .remainPoint(prepayRequest.prepayAmount()) |
70 |
| - .prepaidExpirationDate(expirationDate) |
71 |
| - .build(); |
72 |
| - |
73 |
| - Optional<StoreTeam> storeAndTeam = storeTeamRepository.findByStoreAndTeam(store, team); |
74 |
| - |
75 |
| - if (storeAndTeam.isEmpty()) { |
76 |
| - storeAndTeam = Optional.of(storeTeamRepository.save(buildedStoreTeam)); |
77 |
| - } |
78 |
| - |
79 |
| - StoreTeam storeTeam = storeAndTeam.get(); |
80 |
| - storeTeam.recharge(prepayRequest.prepayAmount()); |
81 |
| - |
82 |
| - return Message.builder() |
83 |
| - .message("매장 선결제가 완료되었습니다.") |
84 |
| - .build(); |
85 |
| - } |
| 33 | + private final UserRepository userRepository; |
| 34 | + private final StoreRepository storeRepository; |
| 35 | + private final TeamRepository teamRepository; |
| 36 | + private final StoreTeamRepository storeTeamRepository; |
| 37 | + private final PointTransactionRepository pointTransactionRepository; |
| 38 | + |
| 39 | + @Transactional |
| 40 | + public Message prepay(String userId, PrepayRequest prepayRequest) { |
| 41 | + User user = userRepository.findByProviderId(userId) |
| 42 | + .orElseThrow(() -> new DefaultNullPointerException(ErrorCode.INVALID_AUTHENTICATION)); |
| 43 | + |
| 44 | + Team team = teamRepository.findById(prepayRequest.teamId()) |
| 45 | + .orElseThrow(() -> new IllegalArgumentException("유효하지 않은 팀 id 입니다.")); |
| 46 | + |
| 47 | + Store store = storeRepository.findById(prepayRequest.storeId()) |
| 48 | + .orElseThrow(() -> new IllegalArgumentException("유효하지 않은 가게 id 입니다.")); |
| 49 | + |
| 50 | + team.validateIsTeamLeader(team.getTeamLeader().getUser_id(), user.getUserId()); |
| 51 | + |
| 52 | + user.validateHasPointWithPrepayAmount(prepayRequest.prepayAmount(), user.getPoint()); |
| 53 | + if (!team.getTeamLeader().getUser_id().equals(user.getUserId())) { |
| 54 | + return Message.builder() |
| 55 | + .message("팀의 리더가 아닌 사람은 선결제를 할 수 없습니다.") |
| 56 | + .build(); |
| 57 | + } |
| 58 | + |
| 59 | + if (prepayRequest.prepayAmount() > user.getPoint()) { |
| 60 | + return Message.builder() |
| 61 | + .message("보유하고 있는 금액이 선결제 하려는 금액보다 적습니다.") |
| 62 | + .build(); |
| 63 | + } |
| 64 | + |
| 65 | + user.usePoint(prepayRequest.prepayAmount()); |
| 66 | + PointTransaction pointTransaction = PointTransaction |
| 67 | + .builder() |
| 68 | + .transactionType(TransactionType.PREPAY) |
| 69 | + .transactionedPoint(prepayRequest.prepayAmount()) |
| 70 | + .user(user) |
| 71 | + .store(store) |
| 72 | + .build(); |
| 73 | + |
| 74 | + pointTransactionRepository.save(pointTransaction); |
| 75 | + |
| 76 | + LocalDate expirationDate = LocalDate.now().plusDays(store.getPrepaymentDuration()); |
| 77 | + |
| 78 | + StoreTeam buildedStoreTeam = StoreTeam |
| 79 | + .builder() |
| 80 | + .team(team) |
| 81 | + .store(store) |
| 82 | + .point(prepayRequest.prepayAmount()) |
| 83 | + .personalAllocatedPoint(prepayRequest.personalAllocatedAmount()) |
| 84 | + .remainPoint(prepayRequest.prepayAmount()) |
| 85 | + .prepaidExpirationDate(expirationDate) |
| 86 | + .build(); |
| 87 | + |
| 88 | + Optional<StoreTeam> storeAndTeam = storeTeamRepository.findByStoreAndTeam(store, team); |
| 89 | + |
| 90 | + if (storeAndTeam.isEmpty()) { |
| 91 | + storeAndTeam = Optional.of(storeTeamRepository.save(buildedStoreTeam)); |
| 92 | + } |
| 93 | + |
| 94 | + StoreTeam storeTeam = storeAndTeam.get(); |
| 95 | + storeTeam.recharge(prepayRequest.prepayAmount()); |
| 96 | + |
| 97 | + return Message.builder() |
| 98 | + .message("매장 선결제가 완료되었습니다.") |
| 99 | + .build(); |
| 100 | + } |
| 101 | + |
| 102 | + @Transactional |
| 103 | + public PrepaymentInfoResponse getPrepayInfo(String userId, Long storeId, Long teamId) { |
| 104 | + User user = userRepository.findByProviderId(userId) |
| 105 | + .orElseThrow(() -> new DefaultNullPointerException(ErrorCode.INVALID_AUTHENTICATION)); |
| 106 | + |
| 107 | + StoreTeam storeTeam = storeTeamRepository.findByStoreIdAndTeamId(storeId, teamId) |
| 108 | + .orElse(null); |
| 109 | + |
| 110 | + Store store = storeRepository.findById(storeId) |
| 111 | + .orElseThrow(() -> new IllegalArgumentException("유효하지 않은 가게 id 입니다.")); |
| 112 | + |
| 113 | + PrepaymentInfoResponse prepaymentInfoResponse = new PrepaymentInfoResponse(); |
| 114 | + if (storeTeam == null) { |
| 115 | + prepaymentInfoResponse.setRemainPrepay(0); |
| 116 | + } else { |
| 117 | + prepaymentInfoResponse.setRemainPrepay(storeTeam.getRemainPoint()); |
| 118 | + } |
| 119 | + prepaymentInfoResponse.setMinPrepayAmount(store.getMinPrepayment()); |
| 120 | + prepaymentInfoResponse.setWallet(user.getPoint()); |
| 121 | + return prepaymentInfoResponse; |
| 122 | + } |
86 | 123 | }
|
0 commit comments