diff --git a/jeecg-boot/jeecg-boot-module/jeecg-module-aiol/src/main/java/org/jeecg/modules/aiol/controller/AiolPaperController.java b/jeecg-boot/jeecg-boot-module/jeecg-module-aiol/src/main/java/org/jeecg/modules/aiol/controller/AiolPaperController.java index 543712b2..8c81b8d4 100644 --- a/jeecg-boot/jeecg-boot-module/jeecg-module-aiol/src/main/java/org/jeecg/modules/aiol/controller/AiolPaperController.java +++ b/jeecg-boot/jeecg-boot-module/jeecg-module-aiol/src/main/java/org/jeecg/modules/aiol/controller/AiolPaperController.java @@ -89,7 +89,7 @@ public class AiolPaperController extends JeecgController add(@RequestBody AiolPaper aiolPaper) { aiolPaperService.save(aiolPaper); - return Result.OK("添加成功!"); + return Result.OK(aiolPaper.getId()); } /** diff --git a/jeecg-boot/jeecg-boot-module/jeecg-module-aiol/src/main/java/org/jeecg/modules/aiol/controller/AiolQuestionController.java b/jeecg-boot/jeecg-boot-module/jeecg-module-aiol/src/main/java/org/jeecg/modules/aiol/controller/AiolQuestionController.java index e549de26..e3fd502d 100644 --- a/jeecg-boot/jeecg-boot-module/jeecg-module-aiol/src/main/java/org/jeecg/modules/aiol/controller/AiolQuestionController.java +++ b/jeecg-boot/jeecg-boot-module/jeecg-module-aiol/src/main/java/org/jeecg/modules/aiol/controller/AiolQuestionController.java @@ -101,7 +101,7 @@ public class AiolQuestionController extends JeecgController { @Autowired private IAiolRepoService aiolRepoService; + @Autowired + private IAiolEntityPermissionService aiolEntityPermissionService; + @Autowired + private ISysBaseAPI sysBaseApi; /** * 分页列表查询 @@ -118,7 +128,7 @@ public class AiolRepoController extends JeecgController edit(@RequestBody AiolRepo aiolRepo) { aiolRepoService.updateById(aiolRepo); - return Result.OK("编辑成功!"); + return Result.OK(aiolRepo.getId()); } /** @@ -205,6 +215,8 @@ public class AiolRepoController extends JeecgController> repoList() { - return Result.ok(aiolRepoService.list()); + @Operation(summary = "获取有权限的所有题库") + public Result> repoList(HttpServletRequest request) { + // 尝试获取token,判断用户id + String token = request.getHeader(CommonConstant.X_ACCESS_TOKEN); + if (token != null && !token.trim().isEmpty()) { + try { + String username = JwtUtil.getUsername(token); + LoginUser sysUser = sysBaseApi.getUserByName(username); + + if (sysUser != null) { + // 获取用户有权限的题库ID列表 + List list = aiolEntityPermissionService.list(new QueryWrapper() + .eq("user_id", sysUser.getId()) + .eq("entity_type", "repo") + ); + if (list.isEmpty()) { + return Result.error("该用户没有权限访问任意题库"); + } + List repoIds = list.stream().map(AiolEntityPermission::getEntityId).collect(Collectors.toList()); + // 根据ID列表查询题库 + return Result.ok(repoMapper.selectBatchIds(repoIds)); + } + } catch (Exception e) { + // token无效或解析失败,忽略错误,继续执行原有逻辑 + log.debug("Token解析失败,按未登录用户处理: {}", e.getMessage()); + } + } + return Result.error("请检查登录状态"); } @PostMapping(value = "/courseAdd") @Operation(summary = "课程新建题库") - public Result courseAdd(@RequestBody AiolRepo repo) { - return aiolRepoService.save(repo) ? Result.OK("添加成功!") : Result.error("添加失败!"); + @Transactional + public Result courseAdd(@RequestBody AiolRepo repo,HttpServletRequest request) { + aiolRepoService.save(repo); + AiolEntityPermission aiolEntityPermission = new AiolEntityPermission(); + aiolEntityPermission.setEntityId(repo.getId()); + aiolEntityPermission.setEntityType("repo"); + // 尝试获取token,判断用户id + String token = request.getHeader(CommonConstant.X_ACCESS_TOKEN); + if (token != null && !token.trim().isEmpty()) { + try { + String username = JwtUtil.getUsername(token); + LoginUser sysUser = sysBaseApi.getUserByName(username); + + if (sysUser != null) { + aiolEntityPermission.setUserId(sysUser.getId()); + } + } catch (Exception e) { + // token无效或解析失败,忽略错误,继续执行原有逻辑 + log.debug("Token解析失败,按未登录用户处理: {}", e.getMessage()); + } + } + aiolEntityPermissionService.save(aiolEntityPermission); + return Result.OK(repo.getId()); } + @GetMapping("userList") + @Operation(summary = "获取用户") + public Result> userList( + @RequestParam(required = false) String username, + @RequestParam(required = false) String employeeNumber) { + + // 创建查询条件 + LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>(); + + // 当用户名不为空时,添加用户名查询条件(模糊查询) + if (StringUtils.isNotBlank(username)) { + queryWrapper.like(SysUser::getUsername, username); + } + + // 当用户工号不为空时,添加工号查询条件(精确查询) + if (StringUtils.isNotBlank(employeeNumber)) { + queryWrapper.eq(SysUser::getWorkNo, employeeNumber); + } + + // 执行查询 + List userList = sysUserService.list(queryWrapper); + + // 返回结果 + return Result.ok(userList); + } + + + @PostMapping("addPermission") + @Operation(summary = "添加权限") + @Transactional + public Result addPermission(@RequestParam String userId, @RequestParam String repoId) { + AiolEntityPermission aiolEntityPermission = new AiolEntityPermission(); + aiolEntityPermission.setEntityId(repoId); + aiolEntityPermission.setEntityType("repo"); + aiolEntityPermission.setUserId(userId); + aiolEntityPermissionService.saveOrUpdate(aiolEntityPermission); + return Result.OK(); + } + + //获取有权限的用户 + @GetMapping("userListByRepoId/{repoId}") + @Operation(summary = "获取题库有权限的用户列表") + public Result> userListByRepoId(@PathVariable String repoId) { + // 获取用户有权限的题库ID列表 + List list = aiolEntityPermissionService.list(new QueryWrapper() + .eq("entity_id", repoId) + .eq("entity_type", "repo") + ); + if (list.isEmpty()) { + return Result.error("该题库下没有有权限用户或该题库不存在"); + } + List userIds = list.stream().map(AiolEntityPermission::getUserId).collect(Collectors.toList()); + // 根据ID列表查询用户 + return Result.ok(sysUserService.listByIds(userIds)); + } + + @DeleteMapping("deletePermission") + @Operation(summary = "删除权限") + @Transactional + public Result deletePermission(@RequestParam String userId, @RequestParam String repoId) { + AiolEntityPermission aiolEntityPermission = new AiolEntityPermission(); + aiolEntityPermission.setEntityId(repoId); + aiolEntityPermission.setEntityType("repo"); + aiolEntityPermission.setUserId(userId); + aiolEntityPermissionService.remove(new QueryWrapper<>(aiolEntityPermission)); + return Result.OK(); + } + + @GetMapping("/questionList/{repoId}") @Operation(summary = "查询题库下题目")