refactor: Dashboard拆分为标准Controller-Service-Mapper三层
This commit is contained in:
parent
76c7eeb553
commit
4da6b34930
4 changed files with 116 additions and 55 deletions
|
|
@ -1,85 +1,36 @@
|
||||||
package edu.situ.stlserver.controller;
|
package edu.situ.stlserver.controller;
|
||||||
|
|
||||||
import edu.situ.stlserver.model.vo.HttpResult;
|
import edu.situ.stlserver.model.vo.HttpResult;
|
||||||
import edu.situ.stlserver.service.CourseService;
|
import edu.situ.stlserver.service.DashboardService;
|
||||||
import edu.situ.stlserver.service.CoachService;
|
|
||||||
import edu.situ.stlserver.service.MemberService;
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.web.bind.annotation.GetMapping;
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
@RequestMapping("/dashboard")
|
@RequestMapping("/dashboard")
|
||||||
public class DashboardController {
|
public class DashboardController {
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private CoachService coachService;
|
private DashboardService dashboardService;
|
||||||
|
|
||||||
@Autowired
|
|
||||||
private MemberService memberService;
|
|
||||||
|
|
||||||
@Autowired
|
|
||||||
private CourseService courseService;
|
|
||||||
|
|
||||||
@GetMapping("/stats")
|
@GetMapping("/stats")
|
||||||
public HttpResult getStats() {
|
public HttpResult getStats() {
|
||||||
Map<String, Object> stats = new HashMap<>();
|
return HttpResult.success(dashboardService.getStats());
|
||||||
// 教练总数
|
|
||||||
stats.put("coachCount", coachService.getAll().size());
|
|
||||||
// 会员总数
|
|
||||||
stats.put("memberCount", memberService.getAll().size());
|
|
||||||
// 课程总数
|
|
||||||
stats.put("courseCount", courseService.getAll().size());
|
|
||||||
// 在职教练数
|
|
||||||
long activeCoachCount = coachService.getAll().stream()
|
|
||||||
.filter(c -> c.getQuitDate() == null)
|
|
||||||
.count();
|
|
||||||
stats.put("activeCoachCount", activeCoachCount);
|
|
||||||
// 有效会员数
|
|
||||||
long activeMemberCount = memberService.getAll().stream()
|
|
||||||
.filter(m -> m.getExpireDate() == null)
|
|
||||||
.count();
|
|
||||||
stats.put("activeMemberCount", activeMemberCount);
|
|
||||||
// 进行中课程数
|
|
||||||
long activeCourseCount = courseService.getAll().stream()
|
|
||||||
.filter(c -> "进行中".equals(c.getStatus()))
|
|
||||||
.count();
|
|
||||||
stats.put("activeCourseCount", activeCourseCount);
|
|
||||||
return HttpResult.success(stats);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping("/member-level")
|
@GetMapping("/member-level")
|
||||||
public HttpResult getMemberLevel() {
|
public HttpResult getMemberLevel() {
|
||||||
Map<String, Long> levelMap = new HashMap<>();
|
return HttpResult.success(dashboardService.getMemberLevel());
|
||||||
levelMap.put("钻石", memberService.getAll().stream().filter(m -> "钻石".equals(m.getLevel())).count());
|
|
||||||
levelMap.put("金卡", memberService.getAll().stream().filter(m -> "金卡".equals(m.getLevel())).count());
|
|
||||||
levelMap.put("银卡", memberService.getAll().stream().filter(m -> "银卡".equals(m.getLevel())).count());
|
|
||||||
levelMap.put("普通", memberService.getAll().stream().filter(m -> "普通".equals(m.getLevel())).count());
|
|
||||||
return HttpResult.success(levelMap);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping("/course-type")
|
@GetMapping("/course-type")
|
||||||
public HttpResult getCourseType() {
|
public HttpResult getCourseType() {
|
||||||
Map<String, Long> typeMap = new HashMap<>();
|
return HttpResult.success(dashboardService.getCourseType());
|
||||||
courseService.getAll().forEach(c -> {
|
|
||||||
typeMap.merge(c.getType(), 1L, Long::sum);
|
|
||||||
});
|
|
||||||
return HttpResult.success(typeMap);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping("/member-trend")
|
@GetMapping("/member-trend")
|
||||||
public HttpResult getMemberTrend() {
|
public HttpResult getMemberTrend() {
|
||||||
Map<String, Long> trendMap = new HashMap<>();
|
return HttpResult.success(dashboardService.getMemberTrend());
|
||||||
memberService.getAll().forEach(m -> {
|
|
||||||
String month = m.getJoinDate().substring(0, 7); // yyyy-MM
|
|
||||||
trendMap.merge(month, 1L, Long::sum);
|
|
||||||
});
|
|
||||||
// 按月份排序
|
|
||||||
Map<String, Long> sortedMap = new java.util.TreeMap<>(trendMap);
|
|
||||||
return HttpResult.success(sortedMap);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
38
src/main/java/edu/situ/stlserver/dao/DashboardMapper.java
Normal file
38
src/main/java/edu/situ/stlserver/dao/DashboardMapper.java
Normal file
|
|
@ -0,0 +1,38 @@
|
||||||
|
package edu.situ.stlserver.dao;
|
||||||
|
|
||||||
|
import org.apache.ibatis.annotations.Mapper;
|
||||||
|
import org.apache.ibatis.annotations.Select;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
@Mapper
|
||||||
|
public interface DashboardMapper {
|
||||||
|
|
||||||
|
@Select("SELECT COUNT(*) FROM coaches")
|
||||||
|
int countCoaches();
|
||||||
|
|
||||||
|
@Select("SELECT COUNT(*) FROM coaches WHERE quit_date IS NULL")
|
||||||
|
int countActiveCoaches();
|
||||||
|
|
||||||
|
@Select("SELECT COUNT(*) FROM members")
|
||||||
|
int countMembers();
|
||||||
|
|
||||||
|
@Select("SELECT COUNT(*) FROM members WHERE expire_date IS NULL")
|
||||||
|
int countActiveMembers();
|
||||||
|
|
||||||
|
@Select("SELECT COUNT(*) FROM courses")
|
||||||
|
int countCourses();
|
||||||
|
|
||||||
|
@Select("SELECT COUNT(*) FROM courses WHERE status = '进行中'")
|
||||||
|
int countActiveCourses();
|
||||||
|
|
||||||
|
@Select("SELECT level, COUNT(*) as count FROM members GROUP BY level")
|
||||||
|
List<Map<String, Object>> countMemberByLevel();
|
||||||
|
|
||||||
|
@Select("SELECT type, COUNT(*) as count FROM courses GROUP BY type")
|
||||||
|
List<Map<String, Object>> countCourseByType();
|
||||||
|
|
||||||
|
@Select("SELECT DATE_FORMAT(join_date, '%Y-%m') as month, COUNT(*) as count FROM members GROUP BY month ORDER BY month")
|
||||||
|
List<Map<String, Object>> countMemberByMonth();
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,11 @@
|
||||||
|
package edu.situ.stlserver.service;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
public interface DashboardService {
|
||||||
|
Map<String, Object> getStats();
|
||||||
|
Map<String, Long> getMemberLevel();
|
||||||
|
Map<String, Long> getCourseType();
|
||||||
|
Map<String, Long> getMemberTrend();
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,61 @@
|
||||||
|
package edu.situ.stlserver.service.impl;
|
||||||
|
|
||||||
|
import edu.situ.stlserver.dao.DashboardMapper;
|
||||||
|
import edu.situ.stlserver.service.DashboardService;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import java.util.LinkedHashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.TreeMap;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class DashboardServiceImpl implements DashboardService {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private DashboardMapper dashboardMapper;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Map<String, Object> getStats() {
|
||||||
|
Map<String, Object> stats = new LinkedHashMap<>();
|
||||||
|
stats.put("coachCount", dashboardMapper.countCoaches());
|
||||||
|
stats.put("activeCoachCount", dashboardMapper.countActiveCoaches());
|
||||||
|
stats.put("memberCount", dashboardMapper.countMembers());
|
||||||
|
stats.put("activeMemberCount", dashboardMapper.countActiveMembers());
|
||||||
|
stats.put("courseCount", dashboardMapper.countCourses());
|
||||||
|
stats.put("activeCourseCount", dashboardMapper.countActiveCourses());
|
||||||
|
return stats;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Map<String, Long> getMemberLevel() {
|
||||||
|
Map<String, Long> map = new LinkedHashMap<>();
|
||||||
|
map.put("钻石", 0L);
|
||||||
|
map.put("金卡", 0L);
|
||||||
|
map.put("银卡", 0L);
|
||||||
|
map.put("普通", 0L);
|
||||||
|
dashboardMapper.countMemberByLevel().forEach(row -> {
|
||||||
|
map.put((String) row.get("level"), ((Number) row.get("count")).longValue());
|
||||||
|
});
|
||||||
|
return map;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Map<String, Long> getCourseType() {
|
||||||
|
Map<String, Long> map = new LinkedHashMap<>();
|
||||||
|
dashboardMapper.countCourseByType().forEach(row -> {
|
||||||
|
map.put((String) row.get("type"), ((Number) row.get("count")).longValue());
|
||||||
|
});
|
||||||
|
return map;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Map<String, Long> getMemberTrend() {
|
||||||
|
Map<String, Long> map = new TreeMap<>();
|
||||||
|
dashboardMapper.countMemberByMonth().forEach(row -> {
|
||||||
|
map.put((String) row.get("month"), ((Number) row.get("count")).longValue());
|
||||||
|
});
|
||||||
|
return map;
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue