StatisticController.java 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. package com.izouma.nineth.web;
  2. import com.izouma.nineth.service.CacheService;
  3. import com.izouma.nineth.service.StatisticService;
  4. import lombok.AllArgsConstructor;
  5. import org.springframework.cache.annotation.CachePut;
  6. import org.springframework.cache.annotation.Cacheable;
  7. import org.springframework.scheduling.annotation.Scheduled;
  8. import org.springframework.security.access.prepost.PreAuthorize;
  9. import org.springframework.web.bind.annotation.GetMapping;
  10. import org.springframework.web.bind.annotation.RequestMapping;
  11. import org.springframework.web.bind.annotation.RestController;
  12. import java.math.BigDecimal;
  13. import java.time.LocalDate;
  14. import java.time.LocalDateTime;
  15. import java.time.LocalTime;
  16. import java.util.Map;
  17. @RestController
  18. @RequestMapping("/statistic")
  19. @AllArgsConstructor
  20. public class StatisticController {
  21. private StatisticService statisticService;
  22. private CacheService cacheService;
  23. @GetMapping("/total")
  24. @Cacheable("total")
  25. public Map<String, Object> total() {
  26. return statisticService.total();
  27. }
  28. @GetMapping("/userTrend")
  29. @Cacheable("userTrend")
  30. public Map<String, Long> userTrend(int day) {
  31. return statisticService.userTrend(day);
  32. }
  33. @GetMapping("/orderNumTrend")
  34. @Cacheable("orderNumTrend")
  35. public Map<String, Map<String, Long>> orderNumTrend(int day) {
  36. return statisticService.orderNumTrend(day);
  37. }
  38. @GetMapping("/orderPriceTrend")
  39. @Cacheable("orderPriceTrend")
  40. public Map<String, Map<String, BigDecimal>> orderPriceTrend(int day) {
  41. return statisticService.orderPriceTrend(day);
  42. }
  43. @GetMapping("/top")
  44. @CachePut(value = "top", key = "#month")
  45. public String top(int year, int month) {
  46. LocalDateTime start = LocalDateTime.of(year, month, 1, 0, 0, 0);
  47. LocalDateTime end = start.plusMonths(1).minusSeconds(1);
  48. return statisticService.top(start, end, 50);
  49. }
  50. @GetMapping("/weekTop")
  51. @Cacheable("weekTop")
  52. public String weekTop() {
  53. LocalDate now = LocalDate.now();
  54. LocalDate endDate = now.minusDays(now.getDayOfWeek().getValue());
  55. LocalDateTime start = LocalDateTime.of(endDate.minusDays(6), LocalTime.MIN);
  56. return statisticService.top(start, LocalDateTime.of(endDate, LocalTime.MAX), 50);
  57. }
  58. @Scheduled(fixedRate = 120000)
  59. public void clearTop() {
  60. int monthValue = LocalDate.now().getMonthValue();
  61. cacheService.clearTop(monthValue);
  62. }
  63. @PreAuthorize("ADMIN")
  64. @GetMapping("/clearWeekTop")
  65. public void clearWeekTop(){
  66. cacheService.clearWeekTop();
  67. }
  68. }