diff --git a/src/main/java/ollie/wecare/challenge/entity/Challenge.java b/src/main/java/ollie/wecare/challenge/entity/Challenge.java index 229d3a0..83b8216 100644 --- a/src/main/java/ollie/wecare/challenge/entity/Challenge.java +++ b/src/main/java/ollie/wecare/challenge/entity/Challenge.java @@ -22,6 +22,9 @@ public class Challenge extends BaseEntity { public void updateAttendanceCode(String attendanceCode) { this.attendanceCode = attendanceCode; } + public void updateAttendanceRate(Integer attendanceRate) { + this.attendanceRate = attendanceRate; + } @Id @GeneratedValue(strategy = GenerationType.IDENTITY) diff --git a/src/main/java/ollie/wecare/challenge/service/ChallengeService.java b/src/main/java/ollie/wecare/challenge/service/ChallengeService.java index a34e12e..94a46a4 100644 --- a/src/main/java/ollie/wecare/challenge/service/ChallengeService.java +++ b/src/main/java/ollie/wecare/challenge/service/ChallengeService.java @@ -77,6 +77,9 @@ public void attendChallenge(AttendChallengeReq attendChallengeReq) throws BaseEx .challenge(challengeRepository.findById(attendChallengeReq.getChallengeIdx()).orElseThrow(()-> new BaseException(INVALID_CHALLENGE_IDX))) .build(); challengeAttendanceRepository.save(challengeAttendance); + Integer newAttendanceRate = (int) ((challenge.getAttendanceRate()/(float)100 + (1/(float)((challenge.getTotalNum()) * challenge.getParticipants().size()))) * 100); + //log.info("attendanceRate : {}", newAttendanceRate); + challenge.updateAttendanceRate(newAttendanceRate); } } @@ -128,8 +131,9 @@ private static DateDto convertToDateDto(LocalDateTime dateTime) { // 챌린지 개인 달성률 계산 private Integer calculateMyAchievementRate(User user, Challenge challenge) { Integer attendanceCount = challengeAttendanceRepository.countByUserAndChallenge(user, challenge); + log.info("attendanceCount : {}", attendanceCount); if (attendanceCount == null) return 0; - else return (attendanceCount/challenge.getTotalNum()) * 100; + else return (int)((attendanceCount/(float)challenge.getTotalNum()) * 100); } /* diff --git a/src/main/java/ollie/wecare/program/service/ProgramService.java b/src/main/java/ollie/wecare/program/service/ProgramService.java index c3046bd..1e19682 100644 --- a/src/main/java/ollie/wecare/program/service/ProgramService.java +++ b/src/main/java/ollie/wecare/program/service/ProgramService.java @@ -21,6 +21,7 @@ import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; +import java.time.Duration; import java.time.LocalDate; import java.time.LocalDateTime; import java.time.LocalTime; @@ -81,7 +82,9 @@ public void saveTag(PostProgramReq postProgramReq, Program program) { } public Challenge saveChallenge(Program program, User user) { + Duration duration = Duration.between(program.getOpenDate(), program.getDueDate()); + Integer totalNum = Math.toIntExact(duration.toDays() / 7);//TODO : 특정 요일 횟수 반영 return challengeRepository.save(Challenge.builder().program(program).name(program.getName()).attendanceRate(0) - .admin(user).host(program.getHost()).totalNum(10).build()); + .admin(user).host(program.getHost()).totalNum(totalNum).build()); } }