-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #61 from Donut-DONationUTile/feature/wallet
Feature/wallet
- Loading branch information
Showing
9 changed files
with
231 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
src/main/java/zero/eight/donut/controller/WalletController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package zero.eight.donut.controller; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.PathVariable; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
import zero.eight.donut.common.response.ApiResponse; | ||
import zero.eight.donut.service.WalletService; | ||
|
||
@RequiredArgsConstructor | ||
@RequestMapping("/api/wallet") | ||
@RestController | ||
public class WalletController { | ||
|
||
private final WalletService walletService; | ||
|
||
@GetMapping("/main") | ||
public ApiResponse<?> walletMain() { | ||
return walletService.walletMain(); | ||
} | ||
|
||
@GetMapping("/{giftId}") | ||
public ApiResponse<?> walletDetail(@PathVariable Long giftId) { | ||
return walletService.walletDetail(giftId); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
src/main/java/zero/eight/donut/dto/wallet/WalletGiftInfoDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package zero.eight.donut.dto.wallet; | ||
|
||
import lombok.Builder; | ||
import lombok.Getter; | ||
|
||
import java.time.LocalDateTime; | ||
|
||
@Getter | ||
@Builder | ||
public class WalletGiftInfoDto { | ||
private long giftId; // 기프티콘 고유 ID | ||
private int days; // 디데이 남은 일수 | ||
private String store; // 사용처 | ||
private LocalDateTime dueDate; // 사용 기한 | ||
private String product; // 상품명 | ||
private int price; // 상품 금액 | ||
} |
18 changes: 18 additions & 0 deletions
18
src/main/java/zero/eight/donut/dto/wallet/WalletResponseDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package zero.eight.donut.dto.wallet; | ||
|
||
import lombok.Builder; | ||
import lombok.Getter; | ||
|
||
import java.util.List; | ||
|
||
@Getter | ||
@Builder | ||
public class WalletResponseDto { | ||
private int receiver; // 수혜자 수 | ||
private double amount; // 기프티콘 총액 | ||
private int cu; // 보유 기프티콘 개수(CU) | ||
private int gs25; // 보유 기프티콘 개수(GS25) | ||
private int seveneleven; // 보유 기프티콘 개수(7eleven) | ||
private List<WalletGiftInfoDto> impendingList; // 임박 기프티콘 리스트 | ||
private List<WalletGiftInfoDto> giftList; // 일반 기프티콘 리스트 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
130 changes: 130 additions & 0 deletions
130
src/main/java/zero/eight/donut/service/WalletService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
package zero.eight.donut.service; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.cglib.core.Local; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
import zero.eight.donut.common.response.ApiResponse; | ||
import zero.eight.donut.config.jwt.AuthUtils; | ||
import zero.eight.donut.domain.Gift; | ||
import zero.eight.donut.domain.Giver; | ||
import zero.eight.donut.domain.enums.Store; | ||
import zero.eight.donut.dto.auth.Role; | ||
import zero.eight.donut.dto.wallet.WalletGiftInfoDto; | ||
import zero.eight.donut.dto.wallet.WalletResponseDto; | ||
import zero.eight.donut.exception.Error; | ||
import zero.eight.donut.exception.Success; | ||
import zero.eight.donut.repository.GiftRepository; | ||
import zero.eight.donut.repository.ReceiverRepository; | ||
|
||
import java.time.Duration; | ||
import java.time.LocalDateTime; | ||
import java.util.*; | ||
import java.util.stream.Collectors; | ||
|
||
@RequiredArgsConstructor | ||
@Service | ||
public class WalletService { | ||
|
||
private final AuthUtils authUtils; | ||
private final ReceiverRepository receiverRepository; | ||
private final GiftRepository giftRepository; | ||
private final HomeReceiverService homeReceiverService; | ||
|
||
@Transactional | ||
// 월렛 화면 조회 | ||
public ApiResponse<?> walletMain() { | ||
// 변수 선언 | ||
LocalDateTime now = LocalDateTime.now(); // 조회 시각 | ||
Giver giver = authUtils.getGiver(); // 기부자 정보 | ||
int receiver = receiverRepository.countBy(); // 수혜자 수 | ||
double amount = 0d; // 기프티콘 총액 | ||
int cu = 0; // 보유 기프티콘 개수(CU) | ||
int gs25 = 0; // 보유 기프티콘 개수(GS25) | ||
int seveneleven = 0; // 보유 기프티콘 개수(7eleven) | ||
List<WalletGiftInfoDto> impendingList = new ArrayList<>(); // 임박 기프티콘 리스트 | ||
List<WalletGiftInfoDto> giftList = new ArrayList<>(); // 일반 기프티콘 리스트 | ||
|
||
// 월렛 기프티콘 리스트 조회 | ||
List<Gift> targetList = giftRepository.findAllByGiverAndStatusAndDueDateAfterOrToday(giver.getId(), now); | ||
|
||
if (targetList != null || !targetList.isEmpty()) { // 보유 기프티콘이 있음 | ||
// 사용처별 기프티콘 개수 계산 | ||
Map<Store, Long> giftCountMap = countGiftsByStore(targetList); | ||
cu = Math.toIntExact(giftCountMap.get(Store.CU)); | ||
gs25 = Math.toIntExact(giftCountMap.get(Store.GS25)); | ||
seveneleven = Math.toIntExact(giftCountMap.get(Store.SEVENELEVEN)); | ||
|
||
for (Gift g : targetList) { | ||
// 기프티콘 총액 계산 | ||
amount += g.getPrice(); | ||
// 기프티콘 분류 | ||
if (g.getDueDate().isBefore(LocalDateTime.now().plusDays(30))) { // 유효 기간 30일 이내 | ||
// 임박 리스트에 할당 | ||
impendingList.add( | ||
WalletGiftInfoDto.builder() | ||
.giftId(g.getId()) | ||
.days(Math.toIntExact(Duration.between(now, g.getDueDate()).toDaysPart())) | ||
.store(g.getStore().getStore()) | ||
.dueDate(g.getDueDate()) | ||
.product(g.getProduct()) | ||
.price(g.getPrice()) | ||
.build() | ||
); | ||
} | ||
else { // 유효 기간 30일 이후 | ||
// 일반 리스트에 할당 | ||
giftList.add( | ||
WalletGiftInfoDto.builder() | ||
.giftId(g.getId()) | ||
.days(Math.toIntExact(Duration.between(now, g.getDueDate()).toDaysPart())) | ||
.store(g.getStore().getStore()) | ||
.dueDate(g.getDueDate()) | ||
.product(g.getProduct()) | ||
.price(g.getPrice()) | ||
.build() | ||
); | ||
} | ||
} | ||
} | ||
|
||
// response data 생성 | ||
WalletResponseDto responseDto = WalletResponseDto.builder() | ||
.receiver(receiver) | ||
.amount(amount) | ||
.cu(cu) | ||
.gs25(gs25) | ||
.seveneleven(seveneleven) | ||
.impendingList(impendingList) | ||
.giftList(giftList) | ||
.build(); | ||
|
||
return ApiResponse.success(Success.GET_WALLET_SUCCESS, responseDto); | ||
} | ||
|
||
// 사용처(store) 필드의 값마다 gift 개수 세기 | ||
public Map<Store, Long> countGiftsByStore(List<Gift> giftList) { | ||
// Gift 객체 리스트를 사용처(store) 필드의 값마다 그룹화하여 각 그룹의 개수를 계산하여 Map으로 반환 | ||
return giftList.stream() | ||
.collect(Collectors.groupingBy(Gift::getStore, Collectors.counting())); | ||
} | ||
|
||
@Transactional | ||
public ApiResponse<?> walletDetail(Long giftId) { | ||
|
||
// Security Config로 책임 전가 | ||
// // 기부자 여부 검증 | ||
// if (!authUtils.getCurrentUserRole().equals(Role.ROLE_GIVER)) { | ||
// return ApiResponse.failure(Error.NOT_AUTHENTICATED_EXCEPTION); | ||
// } | ||
|
||
//Gift 있는지 확인 | ||
Optional<Gift> giftOptional = giftRepository.findById(giftId); | ||
if(giftOptional.isEmpty()) | ||
return ApiResponse.failure(Error.GIFT_NOT_FOUND_EXCEPTION); | ||
|
||
Gift gift = giftOptional.get(); | ||
|
||
return ApiResponse.success(Success.HOME_RECEIVER_GIFT_SUCCESS, homeReceiverService.getGiftInfo(giftId, gift)); | ||
} | ||
} |