Skip to content

Commit

Permalink
Merge pull request #118 from Ong-gi-Jong-gi/feature/TSK-55/prepare
Browse files Browse the repository at this point in the history
[TSK-55] RECAP 통계 수정, 사진 병합 UUID 제거
  • Loading branch information
minjoon-98 authored Jul 24, 2024
2 parents 02a6fd7 + b291378 commit ee59cd4
Show file tree
Hide file tree
Showing 9 changed files with 181 additions and 181 deletions.
2 changes: 2 additions & 0 deletions src/main/java/ongjong/namanmoo/NamanmooApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableAsync;

@SpringBootApplication
@EnableAsync
public class NamanmooApplication {

public static void main(String[] args) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public class RecapController {
private final AwsS3Service awsS3Service;
private final FFmpegService ffmpegService;
private final SharedFileService sharedFileService;
private final AnswerServiceImpl answerServiceImpl;

// 행운이 리스트
@GetMapping("/list")
Expand Down Expand Up @@ -85,10 +86,10 @@ public ApiResponse<List<Map<String, Object>>> getStatistics(@RequestParam("lucky
mostViewedData.put("challengeTitle", mostViewedChallenge != null ? mostViewedChallenge.getChallengeTitle() : "");

// 모두가 가장 빨리 답한 챌린지
Challenge fastestAnsweredChallenge = challengeService.findFastestAnsweredChallenge(lucky);
Challenge fastestAnsweredChallenge = answerService.findFastestAnsweredChallenge(lucky);
Map<String, Object> fastestAnsweredData = new HashMap<>();
fastestAnsweredData.put("topic", "fastestAnswered");
fastestAnsweredData.put("topicResult", fastestAnsweredChallenge != null ? challengeService.calculateLatestResponseTime(lucky, fastestAnsweredChallenge) : 0);
fastestAnsweredData.put("topicResult", fastestAnsweredChallenge != null ? answerService.calculateLatestResponseTime(answerService.findAnswersByChallenges(fastestAnsweredChallenge, memberService.findMemberByLoginId())) : 0);
fastestAnsweredData.put("challengeId", fastestAnsweredChallenge != null ? fastestAnsweredChallenge.getChallengeId() : "");
fastestAnsweredData.put("challengeType", fastestAnsweredChallenge != null ? fastestAnsweredChallenge.getChallengeType() : "");
fastestAnsweredData.put("challengeNumber", fastestAnsweredChallenge != null ? fastestAnsweredChallenge.getChallengeNum() : "");
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/ongjong/namanmoo/service/AnswerService.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package ongjong.namanmoo.service;

import ongjong.namanmoo.domain.Family;
import ongjong.namanmoo.domain.Lucky;
import ongjong.namanmoo.domain.Member;
import ongjong.namanmoo.domain.answer.Answer;
import ongjong.namanmoo.domain.challenge.Challenge;
Expand Down Expand Up @@ -45,6 +46,10 @@ public interface AnswerService {

List<MemberDto> getAnswersByMember(Long luckyId, int challengeNum1, int challengeNum2, Class<? extends MemberDto> dtoClass) throws Exception;

Challenge findFastestAnsweredChallenge(Lucky lucky) throws Exception;

long calculateLatestResponseTime(List<Answer> answers);

List<MemberYouthAnswerDto> getYouthByMember(Long luckyId, int challengeNum1, int challengeNum2) throws Exception;

List<MemberAppreciationDto> getAppreciationByMember(Long luckyId, int challengeNum1, int challengeNum2) throws Exception;
Expand Down
90 changes: 90 additions & 0 deletions src/main/java/ongjong/namanmoo/service/AnswerServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
Expand Down Expand Up @@ -322,6 +324,94 @@ public List<MemberDto> getAnswersByMember(Long luckyId, int challengeNum1, int c
return memberDtoList;
}

// 모든 가족 구성원이 가장 빠르게 답한 챌린지를 반환
@Override
public Challenge findFastestAnsweredChallenge(Lucky lucky) throws Exception {
Family family = lucky.getFamily();

// Lucky 기간 동안의 모든 답변 가져오기
String startDate = lucky.getChallengeStartDate();
String endDate = DateUtil.getInstance().addDaysToStringDate(startDate, lucky.getLifetime().getDays());
List<Answer> answersWithinDateRange = answerRepository.findByMemberFamilyAndCreateDateBetween(family, startDate, endDate);

// createDate를 기준으로 그룹화
Map<String, List<Answer>> answersByCreateDate = answersWithinDateRange.stream()
.collect(Collectors.groupingBy(Answer::getCreateDate));

Challenge fastestChallenge = null;
long shortestTime = Long.MAX_VALUE;

for (Map.Entry<String, List<Answer>> entry : answersByCreateDate.entrySet()) {
List<Answer> answers = entry.getValue();

// 모든 가족 구성원이 해당 챌린지에 대해 답변했는지 확인
if (answers.size() == family.getMembers().size() &&
answers.stream().allMatch(answer ->
answer.getAnswerContent() != null && !answer.getAnswerContent().isEmpty() &&
answer.getModifiedDate() != null && !answer.getModifiedDate().isEmpty())) {

// 각 챌린지에 대해 답변 확인 및 가장 늦은 응답 시간 계산
long timeToAnswer = calculateLatestResponseTime(answers);
log.info("Create Date: " + entry.getKey() + ", Time to answer: " + timeToAnswer);

if (timeToAnswer < shortestTime) {
shortestTime = timeToAnswer;
fastestChallenge = answers.get(0).getChallenge();
}
}
}

log.info("Fastest Challenge ID: " + (fastestChallenge != null ? fastestChallenge.getChallengeId() : "None"));
return fastestChallenge;
}

// 챌린지에 달린 답변 중, 가장 늦게 달린 답변 시간을 계산
public long calculateLatestResponseTime(List<Answer> answers) {
long latestTime = Long.MIN_VALUE; // 해당 챌린지의 가장 늦은 응답시간을 저장할 변수

SimpleDateFormat format4 = new SimpleDateFormat(DateUtil.FORMAT_4);
SimpleDateFormat format9 = new SimpleDateFormat(DateUtil.FORMAT_9);

for (Answer answer : answers) {
try {
String createDate = answer.getCreateDate();
String modifiedDate = answer.getModifiedDate();

if (createDate == null || modifiedDate == null) {
log.warn("답변 ID: {}에서 Null 타임스탬프를 찾았습니다.", answer.getAnswerId());
continue;
}

Date createTime = format4.parse(createDate);
Date modifiedTime = format9.parse(modifiedDate);
log.info("답변 수정 시간: {}, 답변 생성 시간: {}", modifiedTime.getTime(), createTime.getTime());

long responseTime = Math.abs(modifiedTime.getTime() - createTime.getTime()); // 응답 시간 계산
log.info("응답 시간: {}", responseTime);

if (responseTime > latestTime) {
latestTime = responseTime;
}
} catch (ParseException e) {
log.error("답변 ID: {}의 날짜 파싱 에러", answer.getAnswerId(), e);
}
}

if (latestTime == Long.MIN_VALUE) {
return Long.MAX_VALUE;
}
long totalSeconds = latestTime / 1000;
long totalMinutes = totalSeconds / 60;
long totalHours = totalMinutes / 60;
long days = totalHours / 24;
long seconds = totalSeconds % 60;
long minutes = totalMinutes % 60;
long hours = totalHours % 24;
log.info("가장 늦은 응답 시간: " + days + "일 " + hours + "시간 " + minutes + "분 " + seconds + "초");
return latestTime;
}


// DTO 생성 로직을 처리하는 메서드
public MemberDto createDto(Class<? extends MemberDto> dtoClass, Member member, String answer1, String answer2) {
if (dtoClass == MemberAppreciationDto.class) {
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/ongjong/namanmoo/service/AwsS3Service.java
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,9 @@ private String determineFileType(MultipartFile multipartFile) {
* @throws IOException 파일 변환 중 발생하는 예외
*/
private Optional<File> convertFile(MultipartFile file) throws IOException {
String fileName = UUID.randomUUID() + "_" + file.getOriginalFilename();
// String fileName = UUID.randomUUID() + "_" + file.getOriginalFilename();
String fileName = file.getOriginalFilename();
assert fileName != null;
File convertFile = new File(fileName);

if (convertFile.createNewFile()) {
Expand Down
Loading

0 comments on commit ee59cd4

Please sign in to comment.