Browse Source

完成统计相关

1. 按用户维度,统计每个用户的用例数
2. 按时间维度,统计每天的例数
3. 统计到指定日期,每个用户的用例数
4. 统计角色关联的用户数
chubin
郑楚滨 3 months ago
parent
commit
08937e8170
  1. 22
      demo/demo/README.md
  2. 53
      demo/demo/src/main/java/demo/Controller/CountController.java
  3. 12
      demo/demo/src/main/java/demo/Entity/CountCaseInfo.java
  4. 9
      demo/demo/src/main/java/demo/Entity/CountRoleUser.java
  5. 11
      demo/demo/src/main/java/demo/Mapper/CaseInfoMapper.java
  6. 4
      demo/demo/src/main/java/demo/Mapper/UserRoleMapper.java
  7. 70
      demo/demo/src/main/java/demo/Service/CaseInfoService.java
  8. 9
      demo/demo/src/main/java/demo/Service/UserRoleService.java

22
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,自增
暂且:默认所有用例名称唯一
暂且:默认所有用例名称唯一
五、统计相关
1、按用户维度,统计每个用户的用例数
2、按时间维度,统计每天的例数
3、统计到指定日期,每个用户的用例数
若无指定,则统计到当天日期
4、统计角色关联的用户数

53
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<List<CountCaseInfo>> userCaseInfo() {
return Result.success(caseInfoService.countUserCaseInfo());
}
@GetMapping("/dayCaseInfo")
public Result<List<CountCaseInfo>> dayCaseInfo() {
return Result.success(caseInfoService.countCreateTime());
}
@PostMapping("/untilDayCaseInfo")
public Result<List<CountCaseInfo>> countUntilDate(@RequestParam(required = false) Date date) {
if(date == null){
date = new Date();
}
return Result.success(caseInfoService.countUntilDate(date));
}
@GetMapping("/countRoleUser")
public Result<List<CountRoleUser>> countRoleUser() {
return Result.success(userRoleService.countRoleUser());
}
}

12
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;
}

9
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;
}

11
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<CaseInfo> findByLikeCaseName(@Param("likeCaseName") String likeCaseName);
@Select("SELECT CREATOR AS user, COUNT(*) AS count FROM case_info GROUP BY CREATOR")
List<CountCaseInfo> countUserCaseInfo();
@Select("SELECT DATE(CREATE_TIME) AS date, COUNT(*) AS count FROM case_info GROUP BY DATE(CREATE_TIME)")
List<CountCaseInfo> countCreateTime();
@Select("SELECT CREATOR AS user, COUNT(*) AS count FROM case_info WHERE DATE(CREATE_TIME) <= #{date} GROUP BY CREATOR")
List<CountCaseInfo> countUntilDate(@Param("date") Date date);
}

4
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> countRoleUser();
}

70
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<CaseTree> 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<CaseInfo> 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<CaseInfo> getCases() {
// return entityManager.createQuery("FROM CaseInfo", CaseInfo.class).getResultList();
// }
//
// private String generateNewId() {
// // 实现生成唯一ID的逻辑
// return UUID.randomUUID().toString().replace("-", "");
// }
public List<CountCaseInfo> countUserCaseInfo(){
return caseInfoMapper.countUserCaseInfo();
}
public List<CountCaseInfo> countCreateTime(){
return caseInfoMapper.countCreateTime();
}
public List<CountCaseInfo> countUntilDate(Date date){
return caseInfoMapper.countUntilDate(date);
}
}

9
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> countRoleUser() {
return userroleMapper.countRoleUser();
}
}

Loading…
Cancel
Save