diff --git a/src/main/java/top/suyiiyii/sims/controller/CategoryController.java b/src/main/java/top/suyiiyii/sims/controller/CategoryController.java index ee9de62..045a35a 100644 --- a/src/main/java/top/suyiiyii/sims/controller/CategoryController.java +++ b/src/main/java/top/suyiiyii/sims/controller/CategoryController.java @@ -3,10 +3,7 @@ package top.suyiiyii.sims.controller; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import io.swagger.v3.oas.annotations.Operation; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestParam; -import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.bind.annotation.*; import top.suyiiyii.sims.common.AuthAccess; import top.suyiiyii.sims.common.Result; import top.suyiiyii.sims.entity.RewardPunishmentCategory; @@ -20,8 +17,9 @@ public class CategoryController { @Autowired CategoryService categoryService; + @GetMapping("") - @Operation(summary = "获取所有类别信息,支持分页") + @Operation(summary = "获取所有类别",description = "获取所有类别信息,支持分页") @AuthAccess(allowRoles = {"admin"}) public Result> getAllCategory( @RequestParam(value = "page", defaultValue = "1") Integer page, @@ -31,4 +29,32 @@ public class CategoryController { return Result.success(categoryService.getCateGory(pageObj)); } + @PostMapping("") + @Operation(summary = "添加类别") + @AuthAccess(allowRoles = {"admin"}) + public Result addCategory(@RequestBody RewardPunishmentCategory category) { + category.setId(null); + categoryService.addCategory(category); + return Result.success("添加成功"); + } + + @PutMapping("/{id}") + @Operation(summary = "更新类别",description = """ + 根据id更新类别信息\n当status为"disable"时,表示禁用该类别\n当status为null或"enable"时,表示启用该类别""") + @AuthAccess(allowRoles = {"admin"}) + public Result updateCategory(@RequestBody RewardPunishmentCategory category,@PathVariable Integer id) { + category.setId(id); + category.setCategoryId(id); + categoryService.updateCategory(category); + return Result.success("更新成功"); + } + + @DeleteMapping("/{id}") + @Operation(summary = "删除类别",description = "根据id删除类别") + @AuthAccess(allowRoles = {"admin"}) + public Result deleteCategory(@PathVariable Integer id) { + categoryService.deleteCategory(id); + return Result.success("删除成功"); + } + } diff --git a/src/main/java/top/suyiiyii/sims/service/CategoryService.java b/src/main/java/top/suyiiyii/sims/service/CategoryService.java index 2e433c9..6a060d1 100644 --- a/src/main/java/top/suyiiyii/sims/service/CategoryService.java +++ b/src/main/java/top/suyiiyii/sims/service/CategoryService.java @@ -5,6 +5,7 @@ import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import top.suyiiyii.sims.entity.RewardPunishmentCategory; +import top.suyiiyii.sims.exception.ServiceException; import top.suyiiyii.sims.mapper.CategoryMapper; import top.suyiiyii.sims.mapper.MpCategoryMapper; @@ -50,4 +51,26 @@ public class CategoryService { return mpCategoryMapper.selectBatchIds(ids); } + public void updateCategory(RewardPunishmentCategory category) { + if (!mpCategoryMapper.exists(new LambdaQueryWrapper().eq(RewardPunishmentCategory::getId, category.getId()))) { + throw new ServiceException("该类别不存在"); + } + RewardPunishmentCategory cateInDb = mpCategoryMapper.selectById(category.getId()); + category.setId(cateInDb.getId()); + category.setCategoryId(cateInDb.getCategoryId()); + mpCategoryMapper.updateById(category); + } + + public void addCategory(RewardPunishmentCategory category) { + mpCategoryMapper.insert(category); + category.setCategoryId(category.getId()); + mpCategoryMapper.updateById(category); + } + + public void deleteCategory(Integer id) { + if (!mpCategoryMapper.exists(new LambdaQueryWrapper().eq(RewardPunishmentCategory::getId, id))) { + throw new ServiceException("该类别不存在"); + } + mpCategoryMapper.deleteById(id); + } } diff --git a/src/main/java/top/suyiiyii/sims/service/RecordService.java b/src/main/java/top/suyiiyii/sims/service/RecordService.java index d1dc2d5..a364583 100644 --- a/src/main/java/top/suyiiyii/sims/service/RecordService.java +++ b/src/main/java/top/suyiiyii/sims/service/RecordService.java @@ -103,7 +103,7 @@ public class RecordService { List catIds = recordDtos.stream().map(RecordDto::getCategoryId).distinct().toList(); List categories = mpCategoryMapper.selectBatchIds(catIds); Map catMap = categories.stream().collect(Collectors.toMap(RewardPunishmentCategory::getId, c -> c)); - List availableCatIds = catIds.stream().filter(c -> (catMap.containsKey(c) && catMap.get(c).getStatus() == null)).toList(); + List availableCatIds = catIds.stream().filter(c -> (catMap.containsKey(c) && (catMap.get(c).getStatus() == null || catMap.get(c).getStatus().equals("enable")))).toList(); return recordDtos.stream().filter(r -> availableCatIds.contains(r.getCategoryId())).toList(); } }