Skip to content

Commit

Permalink
Merge pull request #101 from team-Ollie/fix/73-calculateAttendanceRate
Browse files Browse the repository at this point in the history
[fix/73-calculateAttendanceRate] 챌린지 달성률 계산
  • Loading branch information
Haeun-Y authored Jul 1, 2024
2 parents b7a2d6a + c938ca7 commit 7ba9bbd
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 2 deletions.
3 changes: 3 additions & 0 deletions src/main/java/ollie/wecare/challenge/entity/Challenge.java
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -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);
}

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
}
}

0 comments on commit 7ba9bbd

Please sign in to comment.