Skip to content

Commit

Permalink
Merge pull request #48 from Do-Farming/feat/minjoo/dailyMissionSchedu…
Browse files Browse the repository at this point in the history
…lar/#39

dailyRank insert
  • Loading branch information
Minjoo-kang123 authored Jul 4, 2024
2 parents d07caab + c90c4b4 commit 22a6353
Show file tree
Hide file tree
Showing 11 changed files with 167 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -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<DailyRank, Long> {
}
10 changes: 4 additions & 6 deletions src/main/java/com/hana/api/group/entity/Group.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import lombok.*;
import org.hibernate.annotations.ColumnDefault;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

Expand Down Expand Up @@ -47,13 +48,10 @@ public class Group extends BaseEntity {
private String wakeupTime;

@Column
@Setter
private LocalDateTime startedAt;

private LocalDate startedAt;
@Column
@Setter
private LocalDateTime endedAt;

private LocalDate endedAt;

@Column(nullable = false)
@ColumnDefault("0")
@Setter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Group, Long> {
List<Group> findByIsPublicTrue();

@Query(value = "SELECT * from group where group.started_at <= ?1 and group.ended_at > ?1", nativeQuery = true)
List<Group> findActiveGroup(LocalDate tomorrow);
}
34 changes: 32 additions & 2 deletions src/main/java/com/hana/api/group/service/GroupService.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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<Group> 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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public User toEntity() {
.password(password)
.phoneNumber(phoneNumber)
.status(0)
.userImg("")
.identificationNumber(identificationNumber).build();
}
}
Original file line number Diff line number Diff line change
@@ -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<WeeklyRate, Long> {
}
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
16 changes: 15 additions & 1 deletion src/main/java/com/hana/common/scheduler/BatchScheduler.java
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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);
}
}
}

0 comments on commit 22a6353

Please sign in to comment.