StatisticController.java 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. package com.izouma.nineth.web;
  2. import com.izouma.nineth.service.CacheService;
  3. import com.izouma.nineth.service.StatisticService;
  4. import com.izouma.nineth.utils.SecurityUtils;
  5. import lombok.AllArgsConstructor;
  6. import org.springframework.cache.annotation.CachePut;
  7. import org.springframework.cache.annotation.Cacheable;
  8. import org.springframework.scheduling.annotation.Scheduled;
  9. import org.springframework.security.access.prepost.PreAuthorize;
  10. import org.springframework.web.bind.annotation.GetMapping;
  11. import org.springframework.web.bind.annotation.RequestMapping;
  12. import org.springframework.web.bind.annotation.RestController;
  13. import java.math.BigDecimal;
  14. import java.time.LocalDate;
  15. import java.time.LocalDateTime;
  16. import java.time.LocalTime;
  17. import java.time.YearMonth;
  18. import java.util.Map;
  19. @RestController
  20. @RequestMapping("/statistic")
  21. @AllArgsConstructor
  22. public class StatisticController {
  23. private StatisticService statisticService;
  24. private CacheService cacheService;
  25. @GetMapping("/total")
  26. @Cacheable("total")
  27. @PreAuthorize("hasAnyRole('ADMIN','COMPANY','ORDERINFO')")
  28. public Map<String, Object> total() {
  29. return statisticService.total(SecurityUtils.getAuthenticatedUser().getId());
  30. }
  31. @GetMapping("/userTrend")
  32. @Cacheable("userTrend")
  33. @PreAuthorize("hasAnyRole('ADMIN','ORDERINFO')")
  34. public Map<String, Long> userTrend(int day) {
  35. return statisticService.userTrend(day);
  36. }
  37. @GetMapping("/userTrendV2")
  38. @Cacheable(value = "userTrendV2", key = "#yearMonth")
  39. @PreAuthorize("hasAnyRole('ADMIN','ORDERINFO')")
  40. public Map<String, Long> userTrendV2(String yearMonth) {
  41. return statisticService.userTrendV2(yearMonth);
  42. }
  43. @GetMapping("/orderNumTrend")
  44. @Cacheable("orderNumTrend")
  45. @PreAuthorize("hasAnyRole('ADMIN','COMPANY','ORDERINFO')")
  46. public Map<String, Map<String, Long>> orderNumTrend(int day) {
  47. return statisticService.orderNumTrend(day, SecurityUtils.getAuthenticatedUser().getId());
  48. }
  49. @GetMapping("/orderPriceTrend")
  50. @Cacheable("orderPriceTrend")
  51. @PreAuthorize("hasAnyRole('ADMIN','COMPANY','ORDERINFO')")
  52. public Map<String, Map<String, BigDecimal>> orderPriceTrend(int day) {
  53. return statisticService.orderPriceTrend(day, SecurityUtils.getAuthenticatedUser().getId());
  54. }
  55. @GetMapping("/orderPriceTrendV2")
  56. @Cacheable(value = "orderPriceTrendV2", key = "#yearMonth")
  57. @PreAuthorize("hasAnyRole('ADMIN','COMPANY','ORDERINFO')")
  58. public Map<String, BigDecimal> orderPriceTrendV2(String yearMonth) {
  59. return statisticService.orderPriceTrendV2(yearMonth, SecurityUtils.getAuthenticatedUser().getId());
  60. }
  61. @GetMapping("/top")
  62. @CachePut(value = "top", key = "#month")
  63. @PreAuthorize("hasAnyRole('ADMIN','ORDERINFO')")
  64. public String top(int year, int month) {
  65. LocalDateTime start = LocalDateTime.of(year, month, 1, 0, 0, 0);
  66. LocalDateTime end = start.plusMonths(1).minusSeconds(1);
  67. return statisticService.top(start, end, 50);
  68. }
  69. @GetMapping("/weekTop")
  70. @Cacheable("weekTop")
  71. public String weekTop() {
  72. LocalDate now = LocalDate.now();
  73. LocalDate endDate = now.minusDays(now.getDayOfWeek().getValue());
  74. LocalDateTime start = LocalDateTime.of(endDate.minusDays(6), LocalTime.MIN);
  75. return statisticService.top(start, LocalDateTime.of(endDate, LocalTime.MAX), 50);
  76. }
  77. @Scheduled(fixedRate = 120000)
  78. public void clearTop() {
  79. int monthValue = LocalDate.now().getMonthValue();
  80. cacheService.clearTop(monthValue);
  81. }
  82. @PreAuthorize("hasRole('ADMIN')")
  83. @GetMapping("/clearWeekTop")
  84. public void clearWeekTop() {
  85. cacheService.clearWeekTop();
  86. }
  87. @PreAuthorize("hasRole('ADMIN')")
  88. @GetMapping("/clearUser")
  89. public void clearUser() {
  90. cacheService.clearUser();
  91. }
  92. @GetMapping("/fixedTop")
  93. @Cacheable("fixedTop")
  94. public String fixedTop() {
  95. LocalDateTime start = LocalDateTime.of(2022, 5, 30, 20, 0, 0);
  96. return statisticService.top(start, LocalDateTime.now(), 50);
  97. }
  98. }