licailing 3 年 前
コミット
5ea296f1f0

+ 1 - 1
src/main/java/com/izouma/nineth/repo/TokenHistoryRepo.java

@@ -41,7 +41,7 @@ public interface TokenHistoryRepo extends JpaRepository<TokenHistory, Long>, Jpa
 
     List<TokenHistory> findByOperationAndPriceNull(String oper);
 
-    @Query(nativeQuery = true, value = "select to_user_id , to_user, sum(price) total from token_history " +
+    @Query(nativeQuery = true, value = "select to_user_id , to_user, to_avatar, sum(price) total from token_history " +
             "where created_at between ?1 and ?2 group by to_user_id order by sum(price) desc limit ?3")
     List<Map<String, Object>> sumPrice(LocalDateTime start, LocalDateTime end, int size);
 }

+ 1 - 0
src/main/java/com/izouma/nineth/security/WebSecurityConfig.java

@@ -108,6 +108,7 @@ public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
                 .antMatchers("/news/get/**").permitAll()
                 .antMatchers("/druid/**").permitAll()
                 .antMatchers("/identityAuth/autoAuth").permitAll()
+                .antMatchers("/statistic/weekTop").permitAll()
                 // all other requests need to be authenticated
                 .anyRequest().authenticated().and()
                 // make sure we use stateless session; session won't be used to

+ 4 - 0
src/main/java/com/izouma/nineth/service/CacheService.java

@@ -40,4 +40,8 @@ public class CacheService {
     @CacheEvict(value = "collectionList", allEntries = true)
     public void clearCollectionList() {
     }
+
+    @CacheEvict(value = "weekTop", allEntries = true)
+    public void clearWeekTop() {
+    }
 }

+ 1 - 3
src/main/java/com/izouma/nineth/service/StatisticService.java

@@ -116,9 +116,7 @@ public class StatisticService {
         return trend;
     }
 
-    public String top(int year, int month, int size) {
-        LocalDateTime start = LocalDateTime.of(year, month, 1, 0, 0, 0);
-        LocalDateTime end = start.plusMonths(1).minusSeconds(1);
+    public String top(LocalDateTime start, LocalDateTime end, int size) {
         List<Map<String, Object>> maps = tokenHistoryRepo.sumPrice(start, end, size);
         return JSONObject.toJSONString(maps);
     }

+ 16 - 1
src/main/java/com/izouma/nineth/web/StatisticController.java

@@ -2,11 +2,15 @@ package com.izouma.nineth.web;
 
 import com.izouma.nineth.service.StatisticService;
 import lombok.AllArgsConstructor;
+import org.springframework.cache.annotation.Cacheable;
 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
@@ -37,6 +41,17 @@ public class StatisticController {
 
     @GetMapping("/top")
     public String top(int year, int month) {
-        return statisticService.top(year, month, 50);
+        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);
     }
 }

+ 14 - 3
src/test/java/com/izouma/nineth/repo/UserRepoTest.java

@@ -9,10 +9,14 @@ import org.junit.Test;
 import org.junit.runner.RunWith;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.boot.test.context.SpringBootTest;
-import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
 import org.springframework.test.context.junit4.SpringRunner;
 
-import java.util.*;
+import java.time.LocalDate;
+import java.time.LocalDateTime;
+import java.util.Arrays;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Locale;
 
 @RunWith(SpringRunner.class)
 @SpringBootTest
@@ -54,6 +58,13 @@ public class UserRepoTest {
 
     @Test
     public void test() {
-        System.out.println(userRepo.findById(150843L).orElse(null));
+//        System.out.println(userRepo.findById(150843L).orElse(null));
+        LocalDate now = LocalDate.now();
+        int dayOfWeek = now.getDayOfWeek().getValue();
+        System.out.println(dayOfWeek);
+        LocalDate end = now.minusDays(dayOfWeek);
+        System.out.println(end);
+        LocalDate start = end.minusDays(6);
+        System.out.println(start);
     }
 }