From 15a7e993823dea750eceaffc730f74de6316d612 Mon Sep 17 00:00:00 2001 From: nnijgnus Date: Wed, 21 Aug 2024 13:45:32 +0900 Subject: [PATCH 1/3] =?UTF-8?q?feat:=20=EC=B6=94=EC=B2=A8=20=EC=95=8C?= =?UTF-8?q?=EA=B3=A0=EB=A6=AC=EC=A6=98=20=EC=BD=94=EB=93=9C=20=EB=A1=9C?= =?UTF-8?q?=EC=A7=81=20=EA=B0=9C=EC=84=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/adminService/AdminService.java | 57 +++++++++++-------- 1 file changed, 32 insertions(+), 25 deletions(-) diff --git a/Server/src/main/java/JGS/CasperEvent/domain/event/service/adminService/AdminService.java b/Server/src/main/java/JGS/CasperEvent/domain/event/service/adminService/AdminService.java index d3a27033..53d24367 100644 --- a/Server/src/main/java/JGS/CasperEvent/domain/event/service/adminService/AdminService.java +++ b/Server/src/main/java/JGS/CasperEvent/domain/event/service/adminService/AdminService.java @@ -68,7 +68,6 @@ public Admin verifyAdmin(AdminRequestDto adminRequestDto) { // 어드민 생성 public ResponseDto postAdmin(AdminRequestDto adminRequestDto) { String adminId = adminRequestDto.getAdminId(); - //Todo: 비밀번호 암호화 필요 String password = adminRequestDto.getPassword(); Admin admin = adminRepository.findById(adminId).orElse(null); @@ -338,41 +337,49 @@ public ResponseDto pickLotteryEventWinners() { int winnerCount = lotteryEvent.getWinnerCount(); - List lotteryParticipants = lotteryParticipantsRepository.findAll(); + List lotteryParticipants = lotteryParticipantsRepository.findIdAndAppliedCounts(); - if(winnerCount >= lotteryParticipants.size()){ - for (LotteryParticipants lotteryParticipant : lotteryParticipants) { - lotteryWinnerRepository.save(new LotteryWinners(lotteryParticipant)); + if (winnerCount >= lotteryParticipants.size()) { + for (Object[] lotteryParticipant : lotteryParticipants) { + lotteryWinnerRepository.save(new LotteryWinners( + lotteryParticipantsRepository.findById((Long) lotteryParticipant[0]).get() + )); } return new ResponseDto("추첨이 완료되었습니다."); } - Set lotteryEventWinners = new HashSet<>(); - int totalWeight; - Random random = new Random(); - while (lotteryEventWinners.size() < winnerCount) { - totalWeight = 0; - for (LotteryParticipants lotteryParticipant : lotteryParticipants) { - totalWeight += lotteryParticipant.getAppliedCount(); - } + int appliedCount; + List appliedParticipants = new ArrayList<>(); - int randomValue = random.nextInt(totalWeight) + 1; - - int cumulativeSum = 0; - for (LotteryParticipants lotteryParticipant : lotteryParticipants) { - cumulativeSum += lotteryParticipant.getAppliedCount(); - if (randomValue <= cumulativeSum) { - lotteryEventWinners.add(lotteryParticipant); - lotteryParticipants.remove(lotteryParticipant); - break; - } + for (Object[] lotteryParticipant : lotteryParticipants) { + appliedCount = (int) lotteryParticipant[1]; + for (int i = 0; i < appliedCount; i++) { + appliedParticipants.add((long) lotteryParticipant[0]); } } - for (LotteryParticipants lotteryEventWinner : lotteryEventWinners) { - lotteryWinnerRepository.save(new LotteryWinners(lotteryEventWinner)); + // Fisher-Yates Shuffle Algorithm + Random random = new Random(); + for (int i = appliedParticipants.size() - 1; i > 0; i--) { + int j = random.nextInt(i + 1); + Long temp = appliedParticipants.get(i); + appliedParticipants.set(i, appliedParticipants.get(j)); + appliedParticipants.set(j, temp); } + Set lotteryEventWinners = new HashSet<>(); + while (lotteryEventWinners.size() < winnerCount) { + Long winnerId = appliedParticipants.remove(0); + if (lotteryEventWinners.contains(winnerId)) continue; + lotteryEventWinners.add(winnerId); + } + + List winnersToSave = lotteryEventWinners.stream() + .map(winnerId -> new LotteryWinners(lotteryParticipantsRepository.findById(winnerId).get())) + .collect(Collectors.toList()); + + lotteryWinnerRepository.saveAll(winnersToSave); + return new ResponseDto("추첨이 완료되었습니다."); } From 2f24041dc7b43e224901fa0324d3c586631c5582 Mon Sep 17 00:00:00 2001 From: nnijgnus Date: Wed, 21 Aug 2024 13:45:53 +0900 Subject: [PATCH 2/3] =?UTF-8?q?feat:=20=EB=A7=A4=ED=95=91=20=ED=8C=A8?= =?UTF-8?q?=EC=B9=98=20=ED=83=80=EC=9E=85=20Lazy=EB=A1=9C=20=EB=B3=80?= =?UTF-8?q?=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../domain/event/entity/participants/LotteryParticipants.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Server/src/main/java/JGS/CasperEvent/domain/event/entity/participants/LotteryParticipants.java b/Server/src/main/java/JGS/CasperEvent/domain/event/entity/participants/LotteryParticipants.java index 9479942f..93939241 100644 --- a/Server/src/main/java/JGS/CasperEvent/domain/event/entity/participants/LotteryParticipants.java +++ b/Server/src/main/java/JGS/CasperEvent/domain/event/entity/participants/LotteryParticipants.java @@ -14,9 +14,8 @@ public class LotteryParticipants extends BaseEntity { @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; - @OneToOne // mappedBy 이용하면 둘 다 저장 안해도 됨 + @OneToOne(fetch = FetchType.LAZY) // mappedBy 이용하면 둘 다 저장 안해도 됨 @JoinColumn(name = "base_user_id") - //todo: 왜이런지 알아보기 @JsonBackReference @JsonIgnore private BaseUser baseUser; From b9d2b0751397b12e042195f98109df37034d3d2b Mon Sep 17 00:00:00 2001 From: nnijgnus Date: Wed, 21 Aug 2024 14:01:52 +0900 Subject: [PATCH 3/3] =?UTF-8?q?feat:=20=EC=A1=B0=ED=9A=8C=EC=8B=9C=20?= =?UTF-8?q?=EC=9C=A0=EC=A0=80=20id=EC=99=80=20=EC=9D=91=EB=AA=A8=20?= =?UTF-8?q?=ED=9A=9F=EC=88=98=EB=A7=8C=20=EB=B0=98=ED=99=98=ED=95=98?= =?UTF-8?q?=EB=8F=84=EB=A1=9D=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../participantsRepository/LotteryParticipantsRepository.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Server/src/main/java/JGS/CasperEvent/domain/event/repository/participantsRepository/LotteryParticipantsRepository.java b/Server/src/main/java/JGS/CasperEvent/domain/event/repository/participantsRepository/LotteryParticipantsRepository.java index b2a2327c..2634710e 100644 --- a/Server/src/main/java/JGS/CasperEvent/domain/event/repository/participantsRepository/LotteryParticipantsRepository.java +++ b/Server/src/main/java/JGS/CasperEvent/domain/event/repository/participantsRepository/LotteryParticipantsRepository.java @@ -9,6 +9,7 @@ import org.springframework.data.jpa.repository.Query; import org.springframework.stereotype.Repository; +import java.util.List; import java.util.Optional; @Repository @@ -20,5 +21,8 @@ public interface LotteryParticipantsRepository extends JpaRepository findIdAndAppliedCounts(); }