feat: add stat service

This commit is contained in:
Tim
2025-08-12 09:31:27 +08:00
parent 5a5d5add23
commit 08a2678bd5
7 changed files with 228 additions and 9 deletions

View File

@@ -0,0 +1,48 @@
package com.openisle.service;
import com.openisle.repository.UserRepository;
import com.openisle.repository.PostRepository;
import com.openisle.repository.CommentRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import java.time.LocalDate;
import java.util.LinkedHashMap;
import java.util.Map;
@Service
@RequiredArgsConstructor
public class StatService {
private final UserRepository userRepository;
private final PostRepository postRepository;
private final CommentRepository commentRepository;
private Map<LocalDate, Long> toDateMap(LocalDate start, LocalDate end, java.util.List<Object[]> list) {
Map<LocalDate, Long> result = new LinkedHashMap<>();
for (var obj : list) {
LocalDate d = (LocalDate) obj[0];
Long c = ((Number) obj[1]).longValue();
result.put(d, c);
}
for (LocalDate d = start; !d.isAfter(end); d = d.plusDays(1)) {
result.putIfAbsent(d, 0L);
}
return result;
}
public Map<LocalDate, Long> countNewUsersRange(LocalDate start, LocalDate end) {
java.util.List<Object[]> list = userRepository.countDailyRange(start.atStartOfDay(), end.plusDays(1).atStartOfDay());
return toDateMap(start, end, list);
}
public Map<LocalDate, Long> countPostsRange(LocalDate start, LocalDate end) {
java.util.List<Object[]> list = postRepository.countDailyRange(start.atStartOfDay(), end.plusDays(1).atStartOfDay());
return toDateMap(start, end, list);
}
public Map<LocalDate, Long> countCommentsRange(LocalDate start, LocalDate end) {
java.util.List<Object[]> list = commentRepository.countDailyRange(start.atStartOfDay(), end.plusDays(1).atStartOfDay());
return toDateMap(start, end, list);
}
}