diff --git a/src/main/java/com/rymcu/vertical/dto/admin/DashboardData.java b/src/main/java/com/rymcu/vertical/dto/admin/DashboardData.java new file mode 100644 index 0000000..76f5225 --- /dev/null +++ b/src/main/java/com/rymcu/vertical/dto/admin/DashboardData.java @@ -0,0 +1,15 @@ +package com.rymcu.vertical.dto.admin; + +import lombok.Data; + +/** + * @author ronger + */ +@Data +public class DashboardData { + + private String label; + + private Integer value; + +} diff --git a/src/main/java/com/rymcu/vertical/mapper/DashboardMapper.java b/src/main/java/com/rymcu/vertical/mapper/DashboardMapper.java index c837d3f..1245e72 100644 --- a/src/main/java/com/rymcu/vertical/mapper/DashboardMapper.java +++ b/src/main/java/com/rymcu/vertical/mapper/DashboardMapper.java @@ -1,7 +1,10 @@ package com.rymcu.vertical.mapper; +import com.rymcu.vertical.dto.admin.DashboardData; import org.apache.ibatis.annotations.Param; +import java.util.List; + /** * @author ronger */ @@ -41,4 +44,38 @@ public interface DashboardMapper { * @return */ Integer selectTodayViewNum(); + + /** + * 获取最近 30 天文章数据 + * @return + */ + List selectLastThirtyDaysArticleData(); + + /** + * 获取最近 30 天文章数据 + * @return + */ + List selectLastThirtyDaysUserData(); + + /** + * 获取最近 30 天文章数据 + * @return + */ + List selectLastThirtyDaysVisitData(); + + /** + * + * @return + */ + List selectHistoryArticleData(); + + /** + * @return + */ + List selectHistoryUserData(); + + /** + * @return + */ + List selectHistoryVisitData(); } diff --git a/src/main/java/com/rymcu/vertical/service/DashboardService.java b/src/main/java/com/rymcu/vertical/service/DashboardService.java index 33c2bb3..abc1a15 100644 --- a/src/main/java/com/rymcu/vertical/service/DashboardService.java +++ b/src/main/java/com/rymcu/vertical/service/DashboardService.java @@ -2,6 +2,8 @@ package com.rymcu.vertical.service; import com.rymcu.vertical.dto.admin.Dashboard; +import java.util.Map; + /** * @author ronger */ @@ -12,4 +14,16 @@ public interface DashboardService { * @return * */ Dashboard dashboard(); + + /** + * 统计最近三十天数据 + * @return + */ + Map lastThirtyDaysData(); + + /** + * 获取历史数据 + * @return + */ + Map history(); } diff --git a/src/main/java/com/rymcu/vertical/service/impl/DashboardServiceImpl.java b/src/main/java/com/rymcu/vertical/service/impl/DashboardServiceImpl.java index 67921dd..71efb0e 100644 --- a/src/main/java/com/rymcu/vertical/service/impl/DashboardServiceImpl.java +++ b/src/main/java/com/rymcu/vertical/service/impl/DashboardServiceImpl.java @@ -1,11 +1,14 @@ package com.rymcu.vertical.service.impl; import com.rymcu.vertical.dto.admin.Dashboard; +import com.rymcu.vertical.dto.admin.DashboardData; import com.rymcu.vertical.mapper.DashboardMapper; import com.rymcu.vertical.service.DashboardService; import org.springframework.stereotype.Service; import javax.annotation.Resource; +import java.time.LocalDate; +import java.util.*; /** * @author ronger @@ -27,4 +30,151 @@ public class DashboardServiceImpl implements DashboardService { dashboard.setTodayViewNum(dashboardMapper.selectTodayViewNum()); return dashboard; } + + @Override + public Map lastThirtyDaysData() { + Map map = new HashMap(4); + ArrayList dates = new ArrayList(30); + ArrayList articleData = new ArrayList(30); + ArrayList userData = new ArrayList(30); + ArrayList visitData = new ArrayList(30); + List articles = dashboardMapper.selectLastThirtyDaysArticleData(); + List users = dashboardMapper.selectLastThirtyDaysUserData(); + List visits = dashboardMapper.selectLastThirtyDaysVisitData(); + LocalDate now = LocalDate.now().plusDays(1); + LocalDate localDate = LocalDate.now().plusDays(-29); + while (now.isAfter(localDate)) { + String date = localDate.toString(); + dates.add(date); + + articles.forEach(article->{ + if (date.equals(article.getLabel())) { + articleData.add(article.getValue()); + return; + } + }); + if (articleData.size() < dates.size()) { + articleData.add(0); + } + + users.forEach(user->{ + if (date.equals(user.getLabel())) { + userData.add(user.getValue()); + return; + } + }); + if (userData.size() < dates.size()) { + userData.add(0); + } + + visits.forEach(visit->{ + if (date.equals(visit.getLabel())) { + visitData.add(visit.getValue()); + return; + } + }); + if (visitData.size() < dates.size()) { + visitData.add(0); + } + + localDate = localDate.plusDays(1); + } + map.put("dates", dates); + map.put("articles", articleData); + map.put("users", userData); + map.put("visits", visitData); + return map; + } + + @Override + public Map history() { + Map map = new HashMap(4); + ArrayList dates = new ArrayList(30); + ArrayList articleData = new ArrayList(30); + ArrayList userData = new ArrayList(30); + ArrayList visitData = new ArrayList(30); + List articles = dashboardMapper.selectHistoryArticleData(); + List users = dashboardMapper.selectHistoryUserData(); + List visits = dashboardMapper.selectHistoryVisitData(); + LocalDate now = LocalDate.now().plusMonths(1); + LocalDate localDate = LocalDate.now().plusYears(-1).plusMonths(1); + while (now.getYear() >= localDate.getYear()) { + if (now.getYear() == localDate.getYear()) { + if (now.getMonthValue() > localDate.getMonthValue()){ + String date = localDate.getYear() + "-" + (localDate.getMonthValue() < 10 ? "0" + localDate.getMonthValue() : localDate.getMonthValue()); + dates.add(date); + + articles.forEach(article->{ + if (date.equals(article.getLabel())) { + articleData.add(article.getValue()); + return; + } + }); + if (articleData.size() < dates.size()) { + articleData.add(0); + } + + users.forEach(user->{ + if (date.equals(user.getLabel())) { + userData.add(user.getValue()); + return; + } + }); + if (userData.size() < dates.size()) { + userData.add(0); + } + + visits.forEach(visit->{ + if (date.equals(visit.getLabel())) { + visitData.add(visit.getValue()); + return; + } + }); + if (visitData.size() < dates.size()) { + visitData.add(0); + } + } + } else { + String date = localDate.getYear() + "-" + (localDate.getMonthValue() < 10 ? "0" + localDate.getMonthValue() : localDate.getMonthValue()); + dates.add(date); + + articles.forEach(article->{ + if (date.equals(article.getLabel())) { + articleData.add(article.getValue()); + return; + } + }); + if (articleData.size() < dates.size()) { + articleData.add(0); + } + + users.forEach(user->{ + if (date.equals(user.getLabel())) { + userData.add(user.getValue()); + return; + } + }); + if (userData.size() < dates.size()) { + userData.add(0); + } + + visits.forEach(visit->{ + if (date.equals(visit.getLabel())) { + visitData.add(visit.getValue()); + return; + } + }); + if (visitData.size() < dates.size()) { + visitData.add(0); + } + } + + localDate = localDate.plusMonths(1); + } + map.put("dates", dates); + map.put("articles", articleData); + map.put("users", userData); + map.put("visits", visitData); + return map; + } } diff --git a/src/main/java/com/rymcu/vertical/web/api/admin/DashboardController.java b/src/main/java/com/rymcu/vertical/web/api/admin/DashboardController.java index 3a22b91..f039f0d 100644 --- a/src/main/java/com/rymcu/vertical/web/api/admin/DashboardController.java +++ b/src/main/java/com/rymcu/vertical/web/api/admin/DashboardController.java @@ -9,6 +9,7 @@ import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import javax.annotation.Resource; +import java.util.Map; /** * @author ronger @@ -21,8 +22,20 @@ public class DashboardController { private DashboardService dashboardService; @GetMapping - public GlobalResult dashboard(){ + public GlobalResult dashboard() { Dashboard dashboard = dashboardService.dashboard(); return GlobalResultGenerator.genSuccessResult(dashboard); } + + @GetMapping("/last-thirty-days") + public GlobalResult LastThirtyDaysData() { + Map map = dashboardService.lastThirtyDaysData(); + return GlobalResultGenerator.genSuccessResult(map); + } + + @GetMapping("/history") + public GlobalResult history() { + Map map = dashboardService.history(); + return GlobalResultGenerator.genSuccessResult(map); + } } diff --git a/src/main/java/mapper/DashboardMapper.xml b/src/main/java/mapper/DashboardMapper.xml index dfc50e5..50a8ef2 100644 --- a/src/main/java/mapper/DashboardMapper.xml +++ b/src/main/java/mapper/DashboardMapper.xml @@ -1,6 +1,10 @@ + + + + @@ -19,7 +23,31 @@ select count(*) from vertical_visit + + + + + + \ No newline at end of file