diff --git a/src/main/java/com/rymcu/forest/config/WebMvcConfigurer.java b/src/main/java/com/rymcu/forest/config/WebMvcConfigurer.java index f41fa32..7ce6c50 100644 --- a/src/main/java/com/rymcu/forest/config/WebMvcConfigurer.java +++ b/src/main/java/com/rymcu/forest/config/WebMvcConfigurer.java @@ -71,7 +71,7 @@ public class WebMvcConfigurer extends WebMvcConfigurationSupport { public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(restAuthTokenInterceptor()).addPathPatterns("/api/**") .excludePathPatterns("/api/v1/console/**", "/api/v1/article/articles/**", "/api/v1/article/detail/**" - , "/api/v1/topic/**", "/api/v1/user/**", "/api/v1/article/*/comments", "/api/v1/rule/currency/**", "/api/v1/lucene/**"); + , "/api/v1/topic/**", "/api/v1/user/**", "/api/v1/article/*/comments", "/api/v1/rule/currency/**", "/api/v1/lucene/**", "/api/v1/open-data/**"); } diff --git a/src/main/java/com/rymcu/forest/service/OpenDataService.java b/src/main/java/com/rymcu/forest/service/OpenDataService.java new file mode 100644 index 0000000..d4023e5 --- /dev/null +++ b/src/main/java/com/rymcu/forest/service/OpenDataService.java @@ -0,0 +1,26 @@ +package com.rymcu.forest.service; + +import com.rymcu.forest.dto.admin.Dashboard; + +import java.util.Map; + +/** + * Created on 2022/3/14 13:51. + * + * @author ronger + * @email ronger-x@outlook.com + * @packageName com.rymcu.forest.service + */ +public interface OpenDataService { + /** + * 获取最近 30 天开放数据 + * @return + */ + Map lastThirtyDaysData(); + + /** + * 获取统计数据 + * @return + */ + Dashboard dashboard(); +} diff --git a/src/main/java/com/rymcu/forest/service/impl/OpenDataServiceImpl.java b/src/main/java/com/rymcu/forest/service/impl/OpenDataServiceImpl.java new file mode 100644 index 0000000..29cd7e3 --- /dev/null +++ b/src/main/java/com/rymcu/forest/service/impl/OpenDataServiceImpl.java @@ -0,0 +1,78 @@ +package com.rymcu.forest.service.impl; + +import com.rymcu.forest.dto.admin.Dashboard; +import com.rymcu.forest.dto.admin.DashboardData; +import com.rymcu.forest.mapper.DashboardMapper; +import com.rymcu.forest.service.OpenDataService; +import org.springframework.stereotype.Service; + +import javax.annotation.Resource; +import java.time.LocalDate; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * Created on 2022/3/14 13:51. + * + * @author ronger + * @email ronger-x@outlook.com + * @packageName com.rymcu.forest.service.impl + */ +@Service +public class OpenDataServiceImpl implements OpenDataService { + + @Resource + private DashboardMapper dashboardMapper; + + @Override + public Map lastThirtyDaysData() { + Map map = new HashMap(5); + ArrayList dates = new ArrayList(30); + ArrayList visitData = new ArrayList(30); + ArrayList visitIpData = new ArrayList(30); + List visits = dashboardMapper.selectLastThirtyDaysVisitData(); + List visitIps = dashboardMapper.selectLastThirtyDaysVisitIpData(); + LocalDate now = LocalDate.now().plusDays(1); + LocalDate localDate = LocalDate.now().plusDays(-29); + while (now.isAfter(localDate)) { + String date = localDate.toString(); + dates.add(date); + + visits.forEach(visit -> { + if (date.equals(visit.getLabel())) { + visitData.add(visit.getValue()); + return; + } + }); + if (visitData.size() < dates.size()) { + visitData.add(0); + } + + visitIps.forEach(visitIp -> { + if (date.equals(visitIp.getLabel())) { + visitIpData.add(visitIp.getValue()); + return; + } + }); + if (visitIpData.size() < dates.size()) { + visitIpData.add(0); + } + + localDate = localDate.plusDays(1); + } + map.put("dates", dates); + map.put("visits", visitData); + map.put("visitIps", visitIpData); + return map; + } + + @Override + public Dashboard dashboard() { + Dashboard dashboard = new Dashboard(); + dashboard.setCountUserNum(dashboardMapper.selectUserCount()); + dashboard.setCountArticleNum(dashboardMapper.selectArticleCount()); + return dashboard; + } +} diff --git a/src/main/java/com/rymcu/forest/web/api/open/OpenDataController.java b/src/main/java/com/rymcu/forest/web/api/open/OpenDataController.java new file mode 100644 index 0000000..b52fb74 --- /dev/null +++ b/src/main/java/com/rymcu/forest/web/api/open/OpenDataController.java @@ -0,0 +1,40 @@ +package com.rymcu.forest.web.api.open; + +import com.rymcu.forest.core.result.GlobalResult; +import com.rymcu.forest.core.result.GlobalResultGenerator; +import com.rymcu.forest.dto.admin.Dashboard; +import com.rymcu.forest.service.OpenDataService; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import javax.annotation.Resource; +import java.util.Map; + +/** + * Created on 2022/3/14 13:49. + * + * @author ronger + * @email ronger-x@outlook.com + * @packageName com.rymcu.forest.web.api.open + */ +@RestController +@RequestMapping("/api/v1/open-data") +public class OpenDataController { + + @Resource + private OpenDataService openDataService; + + @GetMapping("/dashboard") + public GlobalResult dashboard() { + Dashboard dashboard = openDataService.dashboard(); + return GlobalResultGenerator.genSuccessResult(dashboard); + } + + @GetMapping("/last-thirty-days") + public GlobalResult LastThirtyDaysData() { + Map map = openDataService.lastThirtyDaysData(); + return GlobalResultGenerator.genSuccessResult(map); + } + +}