diff --git a/demo/demo/README.md b/demo/demo/README.md index 36e7c1e..4cc70b3 100644 --- a/demo/demo/README.md +++ b/demo/demo/README.md @@ -1,4 +1,4 @@ -用户管理 +一、用户管理 1、注册时要往用户表和用户_角色关联表里面添加信息 @@ -10,7 +10,7 @@ 注意:1、2、3要开启事务,一致性 -角色管理 +二、角色管理 1、新增时直接新增即可 @@ -20,7 +20,7 @@ 4、查询时,直接查询即可 -用例树表 +三、用例树表 1、主键类型改为tinyint,自增 @@ -36,8 +36,20 @@ 查:传入参数有【节点ID、节点名称】(至少一个) -用例信息表 +四、用例信息表 1、主键类型改为tinyint,自增 -暂且:默认所有用例名称唯一 \ No newline at end of file +暂且:默认所有用例名称唯一 + +五、统计相关 + +1、按用户维度,统计每个用户的用例数 + +2、按时间维度,统计每天的例数 + +3、统计到指定日期,每个用户的用例数 + +若无指定,则统计到当天日期 + +4、统计角色关联的用户数 \ No newline at end of file diff --git a/demo/demo/src/main/java/demo/Controller/CountController.java b/demo/demo/src/main/java/demo/Controller/CountController.java new file mode 100644 index 0000000..0df953f --- /dev/null +++ b/demo/demo/src/main/java/demo/Controller/CountController.java @@ -0,0 +1,53 @@ +package demo.Controller; + +import demo.Entity.CountCaseInfo; +import demo.Entity.CountRoleUser; +import demo.Entity.Result; +import demo.Service.CaseInfoService; +import demo.Service.UserRoleService; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.validation.annotation.Validated; +import org.springframework.web.bind.annotation.*; + +import java.util.Date; +import java.util.List; + +/** + * @统计相关 + */ +@Slf4j +@RestController +@RequestMapping("/count") +@Validated +public class CountController { + + @Autowired + private CaseInfoService caseInfoService; + + @Autowired + private UserRoleService userRoleService; + + @GetMapping("/userCaseInfo") + public Result> userCaseInfo() { + return Result.success(caseInfoService.countUserCaseInfo()); + } + + @GetMapping("/dayCaseInfo") + public Result> dayCaseInfo() { + return Result.success(caseInfoService.countCreateTime()); + } + + @PostMapping("/untilDayCaseInfo") + public Result> countUntilDate(@RequestParam(required = false) Date date) { + if(date == null){ + date = new Date(); + } + return Result.success(caseInfoService.countUntilDate(date)); + } + + @GetMapping("/countRoleUser") + public Result> countRoleUser() { + return Result.success(userRoleService.countRoleUser()); + } +} \ No newline at end of file diff --git a/demo/demo/src/main/java/demo/Entity/CountCaseInfo.java b/demo/demo/src/main/java/demo/Entity/CountCaseInfo.java new file mode 100644 index 0000000..6d537c6 --- /dev/null +++ b/demo/demo/src/main/java/demo/Entity/CountCaseInfo.java @@ -0,0 +1,12 @@ +package demo.Entity; + +import lombok.Data; + +import java.util.Date; + +@Data +public class CountCaseInfo { + private String user; + private Date date; + private int count; +} diff --git a/demo/demo/src/main/java/demo/Entity/CountRoleUser.java b/demo/demo/src/main/java/demo/Entity/CountRoleUser.java new file mode 100644 index 0000000..eab7b75 --- /dev/null +++ b/demo/demo/src/main/java/demo/Entity/CountRoleUser.java @@ -0,0 +1,9 @@ +package demo.Entity; + +import lombok.Data; + +@Data +public class CountRoleUser { + private int roleId; + private int count; +} diff --git a/demo/demo/src/main/java/demo/Mapper/CaseInfoMapper.java b/demo/demo/src/main/java/demo/Mapper/CaseInfoMapper.java index 1d2658b..c14c62f 100644 --- a/demo/demo/src/main/java/demo/Mapper/CaseInfoMapper.java +++ b/demo/demo/src/main/java/demo/Mapper/CaseInfoMapper.java @@ -1,8 +1,10 @@ package demo.Mapper; import demo.Entity.CaseInfo; +import demo.Entity.CountCaseInfo; import org.apache.ibatis.annotations.*; +import java.util.Date; import java.util.List; @Mapper @@ -29,4 +31,13 @@ public interface CaseInfoMapper { @Select("SELECT * FROM case_info WHERE CASE_NAME LIKE CONCAT('%', #{likeCaseName}, '%')") List findByLikeCaseName(@Param("likeCaseName") String likeCaseName); + + @Select("SELECT CREATOR AS user, COUNT(*) AS count FROM case_info GROUP BY CREATOR") + List countUserCaseInfo(); + + @Select("SELECT DATE(CREATE_TIME) AS date, COUNT(*) AS count FROM case_info GROUP BY DATE(CREATE_TIME)") + List countCreateTime(); + + @Select("SELECT CREATOR AS user, COUNT(*) AS count FROM case_info WHERE DATE(CREATE_TIME) <= #{date} GROUP BY CREATOR") + List countUntilDate(@Param("date") Date date); } diff --git a/demo/demo/src/main/java/demo/Mapper/UserRoleMapper.java b/demo/demo/src/main/java/demo/Mapper/UserRoleMapper.java index 361077f..9a3e0d2 100644 --- a/demo/demo/src/main/java/demo/Mapper/UserRoleMapper.java +++ b/demo/demo/src/main/java/demo/Mapper/UserRoleMapper.java @@ -1,5 +1,6 @@ package demo.Mapper; +import demo.Entity.CountRoleUser; import demo.Entity.Role; import demo.Entity.UserRole; import org.apache.ibatis.annotations.*; @@ -23,4 +24,7 @@ public interface UserRoleMapper { @Select("SELECT * FROM user_role_relation WHERE USER_ID = #{userId}") UserRole findByUserId(@Param("userId") int userId); + + @Select("SELECT ROLE_ID AS roleId, COUNT(DISTINCT USER_ID) AS count FROM user_role_relation GROUP BY ROLE_ID") + List countRoleUser(); } diff --git a/demo/demo/src/main/java/demo/Service/CaseInfoService.java b/demo/demo/src/main/java/demo/Service/CaseInfoService.java index bb6ceaf..deb0ab0 100644 --- a/demo/demo/src/main/java/demo/Service/CaseInfoService.java +++ b/demo/demo/src/main/java/demo/Service/CaseInfoService.java @@ -2,12 +2,12 @@ package demo.Service; import demo.Entity.CaseInfo; import demo.Entity.CaseInfoVO; +import demo.Entity.CountCaseInfo; import demo.Mapper.CaseInfoMapper; -import org.apache.ibatis.annotations.Param; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; -import org.springframework.web.bind.annotation.PutMapping; +import java.util.Date; import java.util.List; @Service @@ -43,59 +43,15 @@ public class CaseInfoService { return caseInfoMapper.findByLikeCaseName(likeCaseName); } -// @PersistenceContext -// private EntityManager entityManager; -// -// public void addCase(String caseName, String nodeId, String creator) { -// // 确认节点为叶子节点 -// Optional optionalNode = entityManager.createQuery( -// "SELECT n FROM CaseTree n WHERE n.id = :id", CaseTree.class) -// .setParameter("id", nodeId).getResultList().stream().findFirst(); -// -// if (optionalNode.isPresent() && "1".equals(optionalNode.get().getIsLeaf())) { -// String id = generateNewId(); -// CaseInfo newCase = new CaseInfo(); -// newCase.setId(id); -// newCase.setCaseName(caseName); -// newCase.setNodeId(nodeId); -// newCase.setCreator(creator); -// newCase.setCreateTime(new Date()); -// newCase.setEditor(creator); -// newCase.setUpdateTime(new Date()); -// -// entityManager.persist(newCase); -// } else { -// throw new IllegalArgumentException("节点不是叶子节点,不能新增用例"); -// } -// } -// -// @Transactional -// public void deleteCase(String id) { -// CaseInfo caseInfo = entityManager.find(CaseInfo.class, id); -// if (caseInfo != null) { -// entityManager.remove(caseInfo); -// } -// } -// -// public void updateCase(String id, String caseName, String editor) { -// Optional optionalCase = entityManager.createQuery( -// "SELECT c FROM CaseInfo c WHERE c.id = :id", CaseInfo.class) -// .setParameter("id", id).getResultList().stream().findFirst(); -// -// optionalCase.ifPresent(caseInfo -> { -// caseInfo.setCaseName(caseName); -// caseInfo.setEditor(editor); -// caseInfo.setUpdateTime(new Date()); -// entityManager.merge(caseInfo); -// }); -// } -// -// public List getCases() { -// return entityManager.createQuery("FROM CaseInfo", CaseInfo.class).getResultList(); -// } -// -// private String generateNewId() { -// // 实现生成唯一ID的逻辑 -// return UUID.randomUUID().toString().replace("-", ""); -// } + public List countUserCaseInfo(){ + return caseInfoMapper.countUserCaseInfo(); + } + + public List countCreateTime(){ + return caseInfoMapper.countCreateTime(); + } + + public List countUntilDate(Date date){ + return caseInfoMapper.countUntilDate(date); + } } \ No newline at end of file diff --git a/demo/demo/src/main/java/demo/Service/UserRoleService.java b/demo/demo/src/main/java/demo/Service/UserRoleService.java index 947cfbb..2058e9f 100644 --- a/demo/demo/src/main/java/demo/Service/UserRoleService.java +++ b/demo/demo/src/main/java/demo/Service/UserRoleService.java @@ -1,9 +1,6 @@ package demo.Service; -import demo.Entity.Role; -import demo.Entity.RoleVO; -import demo.Entity.User; -import demo.Entity.UserRole; +import demo.Entity.*; import demo.Mapper.RoleMapper; import demo.Mapper.UserMapper; import demo.Mapper.UserRoleMapper; @@ -43,4 +40,8 @@ public class UserRoleService { public UserRole findByUserId(int userId) { return userroleMapper.findByUserId(userId); } + + public List countRoleUser() { + return userroleMapper.countRoleUser(); + } }