-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatchAggregateService.java
More file actions
53 lines (46 loc) · 2.02 KB
/
MatchAggregateService.java
File metadata and controls
53 lines (46 loc) · 2.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package com.example.moim.match.service;
import com.example.moim.match.entity.Match;
import com.example.moim.match.entity.MatchUser;
import com.example.moim.match.repository.MatchRepository;
import com.example.moim.match.repository.MatchUserRepository;
import com.example.moim.statistic.service.StatisticService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.time.LocalDateTime;
import java.util.concurrent.TimeUnit;
@Service
@RequiredArgsConstructor
@Slf4j
public class MatchAggregateService {
private final MatchRepository matchRepository;
private final MatchUserRepository matchUserRepository;
private final StatisticService statisticService;
//한시간마다 지금시간-48< 매치 끝나는시간 <지금시간이면 집계
@Scheduled(fixedRate = 1, timeUnit = TimeUnit.HOURS)
// @Scheduled(fixedRate = 10000)//테스트용. 10초마다
@Transactional
public void aggregateMatchScore() {
log.info("집계 시작");
for (Match match : matchRepository.findByEndTimeBetween(LocalDateTime.now().minusHours(48), LocalDateTime.now())) {
// 참가자들의 점수 집계
int homeScore = 0;
int awayScore = 0;
for (MatchUser matchUser : matchUserRepository.findByMatch(match)) {
if (matchUser.getScore() == 0) {
continue;
}
if (matchUser.getClub().getId().equals(match.getHomeClub().getId())) {
homeScore += matchUser.getScore();
} else {
awayScore += matchUser.getScore();
}
}
log.info("Match ID: {}, homeScore: {}, awayScore: {}", match.getId(), homeScore, awayScore);
match.setMatchScore(homeScore, awayScore);
statisticService.updateStatistic(match);
}
}
}