From f5fef16ae22ed6de7fba47737c001015049b8369 Mon Sep 17 00:00:00 2001 From: Minjoo-kang123 Date: Thu, 4 Jul 2024 11:35:36 +0900 Subject: [PATCH] dailyRank insert --- .../repository/DailyRankRepository.java | 12 +++++ .../java/com/hana/api/group/entity/Group.java | 5 +- .../api/group/repository/GroupRepository.java | 5 ++ .../hana/api/group/service/GroupService.java | 34 +++++++++++++- .../api/user/dto/request/SignUpRequest.java | 1 + .../repository/WeeklyRateRepository.java | 9 ++++ .../weeklyRate/service/WeeklyRateService.java | 24 ++++++++++ .../common/job/AutoTransferJobConfig.java | 1 - .../job/challenge/InsertWeeklyRateConfig.java | 46 +++++++++++++++++++ .../challenge/InsertWeeklyRateTasklet.java | 19 ++++++++ .../hana/common/scheduler/BatchScheduler.java | 16 ++++++- 11 files changed, 166 insertions(+), 6 deletions(-) create mode 100644 src/main/java/com/hana/api/dailyRank/repository/DailyRankRepository.java create mode 100644 src/main/java/com/hana/api/weeklyRate/repository/WeeklyRateRepository.java create mode 100644 src/main/java/com/hana/api/weeklyRate/service/WeeklyRateService.java create mode 100644 src/main/java/com/hana/common/job/challenge/InsertWeeklyRateConfig.java create mode 100644 src/main/java/com/hana/common/job/challenge/InsertWeeklyRateTasklet.java diff --git a/src/main/java/com/hana/api/dailyRank/repository/DailyRankRepository.java b/src/main/java/com/hana/api/dailyRank/repository/DailyRankRepository.java new file mode 100644 index 0000000..ace2f89 --- /dev/null +++ b/src/main/java/com/hana/api/dailyRank/repository/DailyRankRepository.java @@ -0,0 +1,12 @@ +package com.hana.api.dailyRank.repository; + +import com.hana.api.dailyRank.entity.DailyRank; +import com.hana.api.group.entity.Group; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; + +import java.time.LocalDate; +import java.util.List; + +public interface DailyRankRepository extends JpaRepository { + } diff --git a/src/main/java/com/hana/api/group/entity/Group.java b/src/main/java/com/hana/api/group/entity/Group.java index d414ba9..c4ccc74 100644 --- a/src/main/java/com/hana/api/group/entity/Group.java +++ b/src/main/java/com/hana/api/group/entity/Group.java @@ -9,6 +9,7 @@ import lombok.*; import org.hibernate.annotations.ColumnDefault; +import java.time.LocalDate; import java.time.LocalDateTime; import java.util.List; @@ -47,9 +48,9 @@ public class Group extends BaseEntity { private String wakeupTime; @Column - private LocalDateTime startedAt; + private LocalDate startedAt; @Column - private LocalDateTime endeddAt; + private LocalDate endedAt; @Column(nullable = false) @ColumnDefault("0") diff --git a/src/main/java/com/hana/api/group/repository/GroupRepository.java b/src/main/java/com/hana/api/group/repository/GroupRepository.java index 723ee5b..6b07d9a 100644 --- a/src/main/java/com/hana/api/group/repository/GroupRepository.java +++ b/src/main/java/com/hana/api/group/repository/GroupRepository.java @@ -2,9 +2,14 @@ import com.hana.api.group.entity.Group; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; +import java.time.LocalDate; import java.util.List; public interface GroupRepository extends JpaRepository { List findByIsPublicTrue(); + + @Query(value = "SELECT * from group where group.started_at <= ?1 and group.ended_at > ?1", nativeQuery = true) + List findActiveGroup(LocalDate tomorrow); } diff --git a/src/main/java/com/hana/api/group/service/GroupService.java b/src/main/java/com/hana/api/group/service/GroupService.java index 5602cfd..ae95243 100644 --- a/src/main/java/com/hana/api/group/service/GroupService.java +++ b/src/main/java/com/hana/api/group/service/GroupService.java @@ -1,5 +1,7 @@ package com.hana.api.group.service; +import com.hana.api.dailyRank.entity.DailyRank; +import com.hana.api.dailyRank.repository.DailyRankRepository; import com.hana.api.group.dto.GroupRequestDto; import com.hana.api.group.dto.GroupResponseDto; import com.hana.api.group.entity.Group; @@ -8,6 +10,8 @@ import com.hana.api.groupMember.entity.GroupMemberPK; import com.hana.api.groupMember.repository.GroupMemberRepository; import com.hana.api.user.entity.User; +import com.hana.api.weeklyRate.entity.WeeklyRate; +import com.hana.api.weeklyRate.repository.WeeklyRateRepository; import com.hana.common.config.BaseException; import com.hana.common.config.BaseResponseStatus; import com.hana.common.scheduler.DynamicSchedulerService; @@ -17,6 +21,8 @@ import org.springframework.stereotype.Service; import com.hana.api.user.repository.UserRepository; +import java.time.LocalDate; +import java.time.temporal.ChronoUnit; import java.util.List; import java.util.UUID; import java.util.stream.Collectors; @@ -30,10 +36,34 @@ public class GroupService { private final UserRepository userRepository; private final GroupRepository groupRepository; private final GroupMemberRepository groupMemberRepository; + private final WeeklyRateRepository weeklyRateRepository; + private final DailyRankRepository dailyRankRepository; private final DynamicSchedulerService dynamicSchedulerService; - - + @Transactional + public void insertWeeklyRateForGroupMembers() { + LocalDate tomorrow = LocalDate.now().plusDays(1); + List groups = groupRepository.findActiveGroup(tomorrow); + + for (Group group : groups) { + LocalDate start = LocalDate.from(group.getStartedAt()); + int weekly = (((int) ChronoUnit.DAYS.between(start, tomorrow)) / 7) + 1; + for (GroupMember member : group.getGroupMembers()) { + WeeklyRate weeklyRate = WeeklyRate.builder() + .user(member.getUser()) + .weekly(weekly).rate(3.5).build(); + DailyRank dailyRank = DailyRank.builder() + .user(member.getUser()) + .group(member.getGroup()) + .dailyRate(0) + .dailyDate(tomorrow.atStartOfDay()) + .totalRate(3.5) + .dailyRank(0).build(); + weeklyRateRepository.save(weeklyRate); + dailyRankRepository.save(dailyRank); + } + } + } public Group createGroup(GroupRequestDto.GroupCreateReq request, UUID userCode) { User user = userRepository.findByUserCode(userCode) diff --git a/src/main/java/com/hana/api/user/dto/request/SignUpRequest.java b/src/main/java/com/hana/api/user/dto/request/SignUpRequest.java index 5c99e8e..b63a10b 100644 --- a/src/main/java/com/hana/api/user/dto/request/SignUpRequest.java +++ b/src/main/java/com/hana/api/user/dto/request/SignUpRequest.java @@ -28,6 +28,7 @@ public User toEntity() { .password(password) .phoneNumber(phoneNumber) .status(0) + .userImg("") .identificationNumber(identificationNumber).build(); } } diff --git a/src/main/java/com/hana/api/weeklyRate/repository/WeeklyRateRepository.java b/src/main/java/com/hana/api/weeklyRate/repository/WeeklyRateRepository.java new file mode 100644 index 0000000..a675420 --- /dev/null +++ b/src/main/java/com/hana/api/weeklyRate/repository/WeeklyRateRepository.java @@ -0,0 +1,9 @@ +package com.hana.api.weeklyRate.repository; + +import com.hana.api.weeklyRate.entity.WeeklyRate; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; + +@Repository +public interface WeeklyRateRepository extends JpaRepository { +} diff --git a/src/main/java/com/hana/api/weeklyRate/service/WeeklyRateService.java b/src/main/java/com/hana/api/weeklyRate/service/WeeklyRateService.java new file mode 100644 index 0000000..2df162e --- /dev/null +++ b/src/main/java/com/hana/api/weeklyRate/service/WeeklyRateService.java @@ -0,0 +1,24 @@ +package com.hana.api.weeklyRate.service; + +import com.hana.api.challenge.entity.ChallengeRecord; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import java.time.LocalDate; + +@Service +@RequiredArgsConstructor +@Slf4j +public class WeeklyRateService { + + @Transactional + public void insertDailyChallengeRecord() { + LocalDate today = LocalDate.now(); + int randomNumber = (int) (Math.random() * 3); + ChallengeRecord challengeRecord = ChallengeRecord.builder() + .challengeDate(today.plusDays(1).atStartOfDay()) + .challengeType(randomNumber).build(); + } +} diff --git a/src/main/java/com/hana/common/job/AutoTransferJobConfig.java b/src/main/java/com/hana/common/job/AutoTransferJobConfig.java index f7c2ff3..c0c288e 100644 --- a/src/main/java/com/hana/common/job/AutoTransferJobConfig.java +++ b/src/main/java/com/hana/common/job/AutoTransferJobConfig.java @@ -45,7 +45,6 @@ public Step autoTransferStep( @Value("#{jobParameters[today]}") LocalDate today ) { log.info("== autoTransferStep =="); - //log.info(dealDate.toString()); return new StepBuilder("autoTransferStep", jobRepository) .tasklet(new com.hana.common.job.AutoTransferTasklet(autoTransferService, accountService, today), transactionManager) .build(); diff --git a/src/main/java/com/hana/common/job/challenge/InsertWeeklyRateConfig.java b/src/main/java/com/hana/common/job/challenge/InsertWeeklyRateConfig.java new file mode 100644 index 0000000..a1e0c9e --- /dev/null +++ b/src/main/java/com/hana/common/job/challenge/InsertWeeklyRateConfig.java @@ -0,0 +1,46 @@ +package com.hana.common.job.challenge; + +import com.hana.common.job.InsertDailyChallengeTasklet; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.batch.core.Job; +import org.springframework.batch.core.Step; +import org.springframework.batch.core.configuration.DuplicateJobException; +import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing; +import org.springframework.batch.core.configuration.annotation.JobScope; +import org.springframework.batch.core.configuration.support.DefaultBatchConfiguration; +import org.springframework.batch.core.job.builder.JobBuilder; +import org.springframework.batch.core.repository.JobRepository; +import org.springframework.batch.core.step.builder.StepBuilder; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.transaction.PlatformTransactionManager; + +import java.time.LocalDate; + +@Configuration +@RequiredArgsConstructor +@EnableBatchProcessing +@Slf4j +public class InsertWeeklyRateConfig extends DefaultBatchConfiguration { + + private final JobRepository jobRepository; + private final PlatformTransactionManager transactionManager; + private final InsertWeeklyRateTasklet insertWeeklyTasklet; + + @Bean + public Job insertWeeklyRateJob() { + return new JobBuilder("insertWeeklyJob", jobRepository) + .start(insertWeeklyRateStep()) + .build(); + } + + @Bean + @JobScope + public Step insertWeeklyRateStep() { + return new StepBuilder("insertWeeklyStep", jobRepository) + .tasklet(insertWeeklyTasklet, transactionManager) + .build(); + } +} diff --git a/src/main/java/com/hana/common/job/challenge/InsertWeeklyRateTasklet.java b/src/main/java/com/hana/common/job/challenge/InsertWeeklyRateTasklet.java new file mode 100644 index 0000000..eb8d51c --- /dev/null +++ b/src/main/java/com/hana/common/job/challenge/InsertWeeklyRateTasklet.java @@ -0,0 +1,19 @@ +package com.hana.common.job.challenge; + + +import lombok.RequiredArgsConstructor; +import org.springframework.batch.core.StepContribution; +import org.springframework.batch.core.scope.context.ChunkContext; +import org.springframework.batch.core.step.tasklet.Tasklet; +import org.springframework.batch.repeat.RepeatStatus; +import org.springframework.stereotype.Component; + +@RequiredArgsConstructor +@Component +public class InsertWeeklyRateTasklet implements Tasklet { + + @Override + public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception { + return null; + } +} diff --git a/src/main/java/com/hana/common/scheduler/BatchScheduler.java b/src/main/java/com/hana/common/scheduler/BatchScheduler.java index 25f5f3e..971bebd 100644 --- a/src/main/java/com/hana/common/scheduler/BatchScheduler.java +++ b/src/main/java/com/hana/common/scheduler/BatchScheduler.java @@ -85,7 +85,7 @@ public void runUpdateCardListDataJob() { } } @Scheduled(cron = "0 0 12 * * ?") // 매일 정오에 실행 - public void insert() { + public void insertDailyChallenge() { try { Job job = jobRegistry.getJob("insertDailyChallengeJob"); JobParametersBuilder jobParam = new JobParametersBuilder() @@ -96,4 +96,18 @@ public void insert() { throw new RuntimeException(e); } } + + //WeeklyRate 매주 일요일 밤 11시 55분에 실행 + @Scheduled(cron = "0 55 23 ? * SUN") // 매일 정오에 실행 + public void insertWeeklyRate() { + try { + Job job = jobRegistry.getJob("insertWeeklyRateJob"); + JobParametersBuilder jobParam = new JobParametersBuilder() + .addLocalDateTime("runAt", LocalDateTime.now()); + jobLauncher.run(job, jobParam.toJobParameters()); + } catch (NoSuchJobException | JobInstanceAlreadyCompleteException | JobExecutionAlreadyRunningException | + JobParametersInvalidException | JobRestartException e) { + throw new RuntimeException(e); + } + } }