1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
| @Autowired private UserMapper commonMapper; @Autowired private UserService commonService; private final String id = "User_id";
@Operation(summary = "列表") @PostMapping("/list") public Result listUser() { return Result.success("数据列表", commonService.list()); }
@Operation(summary = "存在") @PostMapping("/exist") public Result existUser(@RequestBody @Validated IdParam param) { QueryWrapper<UserEntity> wrapper = new QueryWrapper<>(); wrapper.eq(id.toLowerCase(Locale.ROOT), param.getId()); long count = commonService.count(wrapper); if (count == 0) return Result.success("ID不存在", false); return Result.success("ID已存在", true); }
@Operation(summary = "保存") @PostMapping("/insert") public Result insertUser(@RequestBody @Validated UserEntity param) { QueryWrapper<UserEntity> wrapper = new QueryWrapper<>(); wrapper.eq(id.toLowerCase(Locale.ROOT), param.getUserId()); if (commonService.count(wrapper) != 0) return Result.failure("ID已存在", new Date()); if (commonService.save(param)) return Result.success("保存成功", new Date()); return Result.failure("保存失败", new Date()); }
@Operation(summary = "删除") @PostMapping("/delete") public Result deleteUser(@RequestBody @Validated IdParam param) { QueryWrapper<UserEntity> wrapper = new QueryWrapper<>(); wrapper.eq(id.toLowerCase(Locale.ROOT), param.getId()); if (commonService.count(wrapper) == 0) return Result.failure("ID不存在", param.getId()); if (commonService.remove(wrapper)) return Result.success("删除成功", param.getId()); return Result.failure("删除失败", param.getId()); }
@Operation(summary = "修改") @PostMapping("/update") public Result updateUser(@RequestBody @Validated UserEntity param) { QueryWrapper<UserEntity> wrapper = new QueryWrapper<>(); wrapper.eq(id.toLowerCase(Locale.ROOT), param.getUserId()); if (commonService.count(wrapper) == 0) return Result.failure("ID不存在", new Date()); if (commonService.updateById(param)) return Result.success("修改成功", new Date()); return Result.failure("修改失败", new Date()); }
@Operation(summary = "查询") @PostMapping("/select") public Result selectUser(@RequestBody @Validated IdParam param) { QueryWrapper<UserEntity> wrapper = new QueryWrapper<>(); wrapper.eq(id.toLowerCase(Locale.ROOT), param.getId()); if (commonService.count(wrapper) == 0) return Result.failure("ID不存在", param.getId()); return Result.success(commonService.getOne(wrapper)); }
@Operation(summary = "查询byAcc") @PostMapping("/selectByAcc/{param}") public Result selectUserByAcc(@PathVariable @Validated String param) { QueryWrapper<UserEntity> wrapper = new QueryWrapper<>(); wrapper.eq("user_acc", param); if (commonService.count(wrapper) == 0) return Result.failure("ID不存在", param); return Result.success(commonService.getOne(wrapper)); }
@Operation(summary = "列表分页") @PostMapping("/listPage") public Result listPageUser(@RequestParam Integer page, @RequestParam Integer pageSize) { Page<UserEntity> rowPage = new Page(page, pageSize); LambdaQueryWrapper<UserEntity> queryWrapper = new LambdaQueryWrapper<>(); rowPage = commonMapper.selectPage(rowPage, queryWrapper); return Result.success("数据列表", rowPage); }
@Operation(summary = "导出数据") @PostMapping("exportExcel") public void exportExcelUser(HttpServletResponse response) throws IOException { response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); response.setCharacterEncoding("utf-8"); String fileName = URLEncoder.encode("轮播图", StandardCharsets.UTF_8).replaceAll("\\+", "%20"); List<UserEntity> list = commonService.list(); response.setHeader("Content-disposition", "attachment;filename*=" + fileName + ".xlsx"); EasyExcel.write(response.getOutputStream(), UserEntity.class).sheet("轮播图").doWrite(list); }
@Operation(summary = "导入数据") @PostMapping("/importExcel") public Result importExcelUser(MultipartFile file) { try { InputStream inputStream = file.getInputStream(); List<UserEntity> list = EasyExcel.read(inputStream) .head(UserEntity.class) .sheet(0) .headRowNumber(1) .doReadSync(); for (UserEntity entity : list) { commonService.saveOrUpdate(entity); } return Result.success("导入成功", new Date()); } catch (IOException exception) { throw new RuntimeException(exception); } }
|