统计不生效问题解决
This commit is contained in:
+43
@@ -0,0 +1,43 @@
|
||||
package fun.nojava.module.blog.controller;
|
||||
|
||||
import fun.nojava.common.model.ApiResult;
|
||||
import fun.nojava.module.blog.dto.AdminDashboardStatsVO;
|
||||
import fun.nojava.module.blog.dto.DashboardStatsVO;
|
||||
import fun.nojava.module.blog.service.DashboardService;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.security.core.annotation.AuthenticationPrincipal;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@Tag(name = "用户仪表盘接口")
|
||||
@RestController
|
||||
@RequestMapping("/api/app/dashboard")
|
||||
@RequiredArgsConstructor
|
||||
public class DashboardController {
|
||||
|
||||
private final DashboardService dashboardService;
|
||||
|
||||
@Operation(summary = "获取用户仪表盘统计数据")
|
||||
@GetMapping("/stats")
|
||||
public ApiResult<DashboardStatsVO> getUserStats(@AuthenticationPrincipal Long userId) {
|
||||
return ApiResult.success(dashboardService.getUserStats(userId));
|
||||
}
|
||||
}
|
||||
|
||||
@Tag(name = "管理端仪表盘接口")
|
||||
@RestController
|
||||
@RequestMapping("/api/admin/dashboard")
|
||||
@RequiredArgsConstructor
|
||||
class AdminDashboardController {
|
||||
|
||||
private final DashboardService dashboardService;
|
||||
|
||||
@Operation(summary = "获取管理端仪表盘统计数据")
|
||||
@GetMapping("/stats")
|
||||
public ApiResult<AdminDashboardStatsVO> getAdminStats() {
|
||||
return ApiResult.success(dashboardService.getAdminStats());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package fun.nojava.module.blog.dto;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
@Data
|
||||
@Builder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class AdminDashboardStatsVO {
|
||||
|
||||
private Long userCount;
|
||||
|
||||
private Long articleCount;
|
||||
|
||||
private Long commentCount;
|
||||
|
||||
private Long todayVisits;
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package fun.nojava.module.blog.dto;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
@Data
|
||||
@Builder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class DashboardStatsVO {
|
||||
|
||||
private Long articleCount;
|
||||
|
||||
private Long totalViews;
|
||||
|
||||
private Long totalLikes;
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package fun.nojava.module.blog.service;
|
||||
|
||||
import fun.nojava.module.blog.dto.AdminDashboardStatsVO;
|
||||
import fun.nojava.module.blog.dto.DashboardStatsVO;
|
||||
|
||||
public interface DashboardService {
|
||||
|
||||
DashboardStatsVO getUserStats(Long userId);
|
||||
|
||||
AdminDashboardStatsVO getAdminStats();
|
||||
}
|
||||
+71
@@ -0,0 +1,71 @@
|
||||
package fun.nojava.module.blog.service.impl;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import fun.nojava.common.enums.ArticleStatus;
|
||||
import fun.nojava.module.blog.dto.AdminDashboardStatsVO;
|
||||
import fun.nojava.module.blog.dto.DashboardStatsVO;
|
||||
import fun.nojava.module.blog.entity.Article;
|
||||
import fun.nojava.module.blog.mapper.ArticleMapper;
|
||||
import fun.nojava.module.blog.service.DashboardService;
|
||||
import fun.nojava.module.system.mapper.UserMapper;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Slf4j
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class DashboardServiceImpl implements DashboardService {
|
||||
|
||||
private final ArticleMapper articleMapper;
|
||||
private final UserMapper userMapper;
|
||||
private final JdbcTemplate jdbcTemplate;
|
||||
|
||||
@Override
|
||||
public DashboardStatsVO getUserStats(Long userId) {
|
||||
log.info("Dashboard getUserStats called, userId={}", userId);
|
||||
|
||||
List<Article> articles = articleMapper.selectList(
|
||||
new LambdaQueryWrapper<Article>()
|
||||
.eq(Article::getUserId, userId)
|
||||
.ne(Article::getStatus, ArticleStatus.RECYCLE.getCode())
|
||||
);
|
||||
|
||||
long articleCount = articles.size();
|
||||
long totalViews = articles.stream().mapToLong(a -> a.getViewCount() != null ? a.getViewCount() : 0).sum();
|
||||
long totalLikes = articles.stream().mapToLong(a -> a.getLikeCount() != null ? a.getLikeCount() : 0).sum();
|
||||
|
||||
log.info("Dashboard stats for userId={}: articleCount={}, totalViews={}, totalLikes={}",
|
||||
userId, articleCount, totalViews, totalLikes);
|
||||
|
||||
return DashboardStatsVO.builder()
|
||||
.articleCount(articleCount)
|
||||
.totalViews(totalViews)
|
||||
.totalLikes(totalLikes)
|
||||
.build();
|
||||
}
|
||||
|
||||
@Override
|
||||
public AdminDashboardStatsVO getAdminStats() {
|
||||
Long userCount = userMapper.selectCount(null);
|
||||
Long articleCount = articleMapper.selectCount(null);
|
||||
|
||||
Long commentCount;
|
||||
try {
|
||||
commentCount = jdbcTemplate.queryForObject(
|
||||
"SELECT COUNT(*) FROM blog_comment WHERE deleted = 0", Long.class);
|
||||
} catch (Exception e) {
|
||||
commentCount = 0L;
|
||||
}
|
||||
|
||||
return AdminDashboardStatsVO.builder()
|
||||
.userCount(userCount != null ? userCount : 0)
|
||||
.articleCount(articleCount != null ? articleCount : 0)
|
||||
.commentCount(commentCount != null ? commentCount : 0)
|
||||
.todayVisits(0L)
|
||||
.build();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user