package com.izouma.nineth.web; import com.izouma.nineth.service.CacheService; import com.izouma.nineth.service.StatisticService; import lombok.AllArgsConstructor; import org.springframework.cache.annotation.CachePut; import org.springframework.cache.annotation.Cacheable; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.math.BigDecimal; import java.time.LocalDate; import java.time.LocalDateTime; import java.time.LocalTime; import java.util.Map; @RestController @RequestMapping("/statistic") @AllArgsConstructor public class StatisticController { private StatisticService statisticService; private CacheService cacheService; @GetMapping("/total") @Cacheable("total") public Map total() { return statisticService.total(); } @GetMapping("/userTrend") @Cacheable("userTrend") public Map userTrend(int day) { return statisticService.userTrend(day); } @GetMapping("/orderNumTrend") @Cacheable("orderNumTrend") public Map> orderNumTrend(int day) { return statisticService.orderNumTrend(day); } @GetMapping("/orderPriceTrend") @Cacheable("orderPriceTrend") public Map> orderPriceTrend(int day) { return statisticService.orderPriceTrend(day); } @GetMapping("/top") @CachePut(value = "top", key = "#month") public String top(int year, int month) { LocalDateTime start = LocalDateTime.of(year, month, 1, 0, 0, 0); LocalDateTime end = start.plusMonths(1).minusSeconds(1); return statisticService.top(start, end, 50); } @GetMapping("/weekTop") @Cacheable("weekTop") public String weekTop() { LocalDate now = LocalDate.now(); LocalDate endDate = now.minusDays(now.getDayOfWeek().getValue()); LocalDateTime start = LocalDateTime.of(endDate.minusDays(6), LocalTime.MIN); return statisticService.top(start, LocalDateTime.of(endDate, LocalTime.MAX), 50); } @Scheduled(fixedRate = 120000) public void clearTop() { int monthValue = LocalDate.now().getMonthValue(); cacheService.clearTop(monthValue); } @PreAuthorize("ADMIN") @GetMapping("/clearWeekTop") public void clearWeekTop(){ cacheService.clearWeekTop(); } }