From e285517df2a3cb206282a41f958c80044422a552 Mon Sep 17 00:00:00 2001 From: guyue <1721849008@qq.com> Date: 星期一, 07 七月 2025 17:51:35 +0800 Subject: [PATCH] 基本增删改 --- src/main/java/com/linghu/controller/TypeController.java | 60 +++++++++++++++++++++++++++++++++--------------------------- 1 files changed, 33 insertions(+), 27 deletions(-) diff --git a/src/main/java/com/linghu/controller/TypeController.java b/src/main/java/com/linghu/controller/TypeController.java index ef03c95..9115431 100644 --- a/src/main/java/com/linghu/controller/TypeController.java +++ b/src/main/java/com/linghu/controller/TypeController.java @@ -10,6 +10,7 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; +import java.util.Date; import java.util.List; @RestController @@ -23,7 +24,7 @@ @PostMapping @ApiOperation(value = "添加类型") public ResponseResult<Type> add(@RequestBody Type type) { - type.setDelFlag(0); + type.setDel_flag(0); boolean success = typeService.save(type); if (success) { return ResponseResult.success(type); @@ -33,6 +34,8 @@ @PostMapping("/batch") public ResponseResult<Void> batchAdd(@RequestBody List<Type> types) { + types.forEach(type -> type.setDel_flag(0)); + boolean success = typeService.saveBatch(types); if (success) { return ResponseResult.success(); @@ -43,22 +46,13 @@ @DeleteMapping("/{typeId}") public ResponseResult<Void> delete(@PathVariable Integer typeId) { Type type = new Type(); - type.setTypeId(typeId); - type.setDelFlag(1); + type.setType_id(typeId); + type.setDel_flag(1); boolean success = typeService.updateById(type); if (success) { return ResponseResult.success(); } return ResponseResult.error("删除类型失败"); - } - - @DeleteMapping("/batch") - public ResponseResult<Void> batchDelete(@RequestBody List<Integer> typeIds) { - boolean success = typeService.removeBatchByIds(typeIds); - if (success) { - return ResponseResult.success(); - } - return ResponseResult.error("批量删除类型失败"); } @PutMapping @@ -82,29 +76,41 @@ @GetMapping("/{typeId}") public ResponseResult<Type> getById(@PathVariable Integer typeId) { Type type = typeService.getById(typeId); - if (type != null && type.getDelFlag() != 1) { + if (type != null && type.getDel_flag() != 1) { return ResponseResult.success(type); } return ResponseResult.error("类型不存在"); } - @GetMapping("/list") - public ResponseResult<?> list( - @RequestParam(required = false) Integer pageNum, - @RequestParam(required = false) Integer pageSize, - @RequestParam(required = false) String typeName) { - - // 不传分页参数则返回全部数据 - if (pageNum == null || pageSize == null) { - List<Type> types = typeService.listAllAvailable(); - return ResponseResult.success(types); + @DeleteMapping("/batch") + public ResponseResult<Void> batchDelete(@RequestBody List<Integer> typeIds) { + List<Type> types = typeIds.stream().map(id -> { + Type type = new Type(); + type.setType_id(id); + type.setDel_flag(1); + return type; + }).collect(java.util.stream.Collectors.toList()); + boolean success = typeService.updateBatchById(types); + if (success) { + return ResponseResult.success(); } + return ResponseResult.error("批量删除类型失败"); + } + @GetMapping("/list") + public ResponseResult<List<Type>> list( + @RequestParam(required = false) Integer page, + @RequestParam(required = false) Integer pageSize) { LambdaQueryWrapper<Type> queryWrapper = new LambdaQueryWrapper<>(); - queryWrapper.eq(Type::getDelFlag, 0) - .like(typeName != null, Type::getTypeName, typeName); + queryWrapper.eq(Type::getDel_flag, 0); - Page<Type> page = typeService.page(new Page<>(pageNum, pageSize), queryWrapper); - return ResponseResult.success(page); + if (page != null && pageSize != null) { + Page<Type> pageInfo = new Page<>(page, pageSize); + Page<Type> result = typeService.page(pageInfo, queryWrapper); + return ResponseResult.success(result.getRecords()); + } else { + List<Type> list = typeService.list(queryWrapper); + return ResponseResult.success(list); + } } } \ No newline at end of file -- Gitblit v1.7.1