diff --git a/Server/src/main/java/JGS/CasperEvent/domain/event/controller/adminController/AdminController.java b/Server/src/main/java/JGS/CasperEvent/domain/event/controller/adminController/AdminController.java index dba71514..0df1f00e 100644 --- a/Server/src/main/java/JGS/CasperEvent/domain/event/controller/adminController/AdminController.java +++ b/Server/src/main/java/JGS/CasperEvent/domain/event/controller/adminController/AdminController.java @@ -144,7 +144,7 @@ public ResponseEntity getRushEventOptions(@Path .status(HttpStatus.OK) .body(adminService.getRushEventOptions(rushEventId)); } - + // 추첨 이벤트 삭제 @DeleteMapping("/event/lottery") public ResponseEntity deleteLotteryEvent() { @@ -176,4 +176,12 @@ public ResponseEntity deleteLotteryEventExpectation(@PathVariable("casperI return ResponseEntity.noContent().build(); } + + // 추첨 이벤트 당첨자 추첨 + @PostMapping("/event/lottery/winner") + public ResponseEntity pickWinners(){ + return ResponseEntity + .status(HttpStatus.CREATED) + .body(adminService.pickWinners()); + } } diff --git a/Server/src/main/java/JGS/CasperEvent/domain/event/dto/ResponseDto/lotteryEventResponseDto/LotteryEventParticipantsResponseDto.java b/Server/src/main/java/JGS/CasperEvent/domain/event/dto/ResponseDto/lotteryEventResponseDto/LotteryEventParticipantsResponseDto.java index 4de9c490..35acc0ab 100644 --- a/Server/src/main/java/JGS/CasperEvent/domain/event/dto/ResponseDto/lotteryEventResponseDto/LotteryEventParticipantsResponseDto.java +++ b/Server/src/main/java/JGS/CasperEvent/domain/event/dto/ResponseDto/lotteryEventResponseDto/LotteryEventParticipantsResponseDto.java @@ -3,7 +3,6 @@ import JGS.CasperEvent.domain.event.entity.participants.LotteryParticipants; import java.time.LocalDate; -import java.time.LocalDateTime; import java.time.LocalTime; public record LotteryEventParticipantsResponseDto( diff --git a/Server/src/main/java/JGS/CasperEvent/domain/event/dto/ResponseDto/rushEventResponseDto/RushEventParticipantResponseDto.java b/Server/src/main/java/JGS/CasperEvent/domain/event/dto/ResponseDto/rushEventResponseDto/RushEventParticipantResponseDto.java index 7eb080cb..6d40224a 100644 --- a/Server/src/main/java/JGS/CasperEvent/domain/event/dto/ResponseDto/rushEventResponseDto/RushEventParticipantResponseDto.java +++ b/Server/src/main/java/JGS/CasperEvent/domain/event/dto/ResponseDto/rushEventResponseDto/RushEventParticipantResponseDto.java @@ -3,7 +3,6 @@ import JGS.CasperEvent.domain.event.entity.participants.RushParticipants; import java.time.LocalDate; -import java.time.LocalDateTime; import java.time.LocalTime; public record RushEventParticipantResponseDto(Long id, String phoneNumber, 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 39037862..6f2ec115 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 @@ -1,5 +1,6 @@ package JGS.CasperEvent.domain.event.entity.participants; +import JGS.CasperEvent.global.entity.BaseEntity; import JGS.CasperEvent.global.entity.BaseUser; import com.fasterxml.jackson.annotation.JsonBackReference; import jakarta.persistence.*; @@ -7,7 +8,7 @@ @Getter @Entity -public class LotteryParticipants { +public class LotteryParticipants extends BaseEntity { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; @@ -33,7 +34,7 @@ public LotteryParticipants() { } public void expectationAdded() { - if(expectations == 0) expectations++; + if (expectations == 0) expectations++; appliedCount = Math.min(10, 1 + expectations + linkClickedCount); } diff --git a/Server/src/main/java/JGS/CasperEvent/domain/event/entity/participants/LotteryWinners.java b/Server/src/main/java/JGS/CasperEvent/domain/event/entity/participants/LotteryWinners.java new file mode 100644 index 00000000..9b63cfcc --- /dev/null +++ b/Server/src/main/java/JGS/CasperEvent/domain/event/entity/participants/LotteryWinners.java @@ -0,0 +1,32 @@ +package JGS.CasperEvent.domain.event.entity.participants; + +import jakarta.persistence.Entity; +import jakarta.persistence.Id; + +import java.time.LocalDateTime; + +@Entity +public class LotteryWinners { + @Id + private long id; + private String phoneNumber; + private int linkClickedCount; + private int expectation; + private int appliedCount; + private LocalDateTime createdAt; + private LocalDateTime updatedAt; + + public LotteryWinners(LotteryParticipants lotteryParticipants) { + this.id = lotteryParticipants.getId(); + this.phoneNumber = lotteryParticipants.getBaseUser().getId(); + this.linkClickedCount = lotteryParticipants.getLinkClickedCount(); + this.expectation = lotteryParticipants.getExpectations(); + this.appliedCount = lotteryParticipants.getAppliedCount(); + this.createdAt = lotteryParticipants.getCreatedAt(); + this.updatedAt = lotteryParticipants.getUpdatedAt(); + } + + public LotteryWinners() { + + } +} diff --git a/Server/src/main/java/JGS/CasperEvent/domain/event/repository/participantsRepository/LotteryWinnerRepository.java b/Server/src/main/java/JGS/CasperEvent/domain/event/repository/participantsRepository/LotteryWinnerRepository.java new file mode 100644 index 00000000..6956709a --- /dev/null +++ b/Server/src/main/java/JGS/CasperEvent/domain/event/repository/participantsRepository/LotteryWinnerRepository.java @@ -0,0 +1,9 @@ +package JGS.CasperEvent.domain.event.repository.participantsRepository; + +import JGS.CasperEvent.domain.event.entity.participants.LotteryWinners; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; + +@Repository +public interface LotteryWinnerRepository extends JpaRepository { +} 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 f19f81bb..ed502ef3 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 @@ -16,6 +16,7 @@ import JGS.CasperEvent.domain.event.entity.event.RushEvent; import JGS.CasperEvent.domain.event.entity.event.RushOption; import JGS.CasperEvent.domain.event.entity.participants.LotteryParticipants; +import JGS.CasperEvent.domain.event.entity.participants.LotteryWinners; import JGS.CasperEvent.domain.event.entity.participants.RushParticipants; import JGS.CasperEvent.domain.event.repository.AdminRepository; import JGS.CasperEvent.domain.event.repository.CasperBotRepository; @@ -23,6 +24,7 @@ import JGS.CasperEvent.domain.event.repository.eventRepository.RushEventRepository; import JGS.CasperEvent.domain.event.repository.eventRepository.RushOptionRepository; import JGS.CasperEvent.domain.event.repository.participantsRepository.LotteryParticipantsRepository; +import JGS.CasperEvent.domain.event.repository.participantsRepository.LotteryWinnerRepository; import JGS.CasperEvent.domain.event.repository.participantsRepository.RushParticipantsRepository; import JGS.CasperEvent.global.enums.CustomErrorCode; import JGS.CasperEvent.global.enums.Position; @@ -58,6 +60,7 @@ public class AdminService { private final RushOptionRepository rushOptionRepository; private final S3Service s3Service; private final CasperBotRepository casperBotRepository; + private final LotteryWinnerRepository lotteryWinnerRepository; public Admin verifyAdmin(AdminRequestDto adminRequestDto) { return adminRepository.findByIdAndPassword(adminRequestDto.getAdminId(), adminRequestDto.getPassword()).orElseThrow(NoSuchElementException::new); @@ -316,6 +319,24 @@ private LotteryEvent getCurrentLotteryEvent() { return lotteryEventList.get(0); } + @Transactional + public ResponseDto pickWinners() { + if(lotteryWinnerRepository.count() > 1) throw new CustomException(CustomErrorCode.LOTTERY_EVENT_ALREADY_DRAWN); + LotteryEvent lotteryEvent = getCurrentLotteryEvent(); + + int winnerCount = lotteryEvent.getWinnerCount(); + + Pageable pageable = PageRequest.of(0, winnerCount); + + //todo 당첨자 추첨 알고리즘 변경해야함 + Page lotteryWinners = lotteryParticipantsRepository.findAll(pageable); + for (LotteryParticipants lotteryWinner : lotteryWinners) { + lotteryWinnerRepository.save(new LotteryWinners(lotteryWinner)); + } + + return new ResponseDto("추첨이 완료되었습니다."); + } + @Transactional public List updateRushEvents(List rushEventRequestDtoList) { LocalDateTime now = LocalDateTime.now(); diff --git a/Server/src/main/java/JGS/CasperEvent/global/enums/CustomErrorCode.java b/Server/src/main/java/JGS/CasperEvent/global/enums/CustomErrorCode.java index fde4cba7..78e760c9 100644 --- a/Server/src/main/java/JGS/CasperEvent/global/enums/CustomErrorCode.java +++ b/Server/src/main/java/JGS/CasperEvent/global/enums/CustomErrorCode.java @@ -29,7 +29,8 @@ public enum CustomErrorCode { EVENT_BEFORE_START_TIME("이벤트 시작 시간은 현재 시간 이후로 설정해야 합니다.", 400), EVENT_END_TIME_BEFORE_START_TIME("종료 시간은 시작 시간 이후로 설정해야 합니다.", 400), INVALID_RUSH_EVENT_OPTION("밸런스 게임 선택지가 유효하지 않습니다."), - EVENT_IN_PROGRESS_CANNOT_DELETE("진행중인 이벤트를 삭제할 수 없습니다.", 409); + EVENT_IN_PROGRESS_CANNOT_DELETE("진행중인 이벤트를 삭제할 수 없습니다.", 409), + LOTTERY_EVENT_ALREADY_DRAWN("추첨 이벤트의 당첨자가 이미 추첨되었습니다.", 409),;