From df4230a5d280ea2436d60e0ca9454e5e40543662 Mon Sep 17 00:00:00 2001
From: suyiiyii <suyiiyii@gmail.com>
Date: Sun, 8 Sep 2024 20:44:26 +0800
Subject: [PATCH] =?UTF-8?q?=E5=AE=9E=E7=8E=B0=E8=AE=B0=E5=BD=95=E7=9A=84?=
 =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=92=8C=E5=88=A0=E9=99=A4?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 .../sims/controller/CategoryController.java   | 36 ++++++++++++++++---
 .../sims/service/CategoryService.java         | 23 ++++++++++++
 .../suyiiyii/sims/service/RecordService.java  |  2 +-
 3 files changed, 55 insertions(+), 6 deletions(-)

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<List<RewardPunishmentCategory>> 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<String> 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<String> 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<String> 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<RewardPunishmentCategory>().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<RewardPunishmentCategory>().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<Integer> catIds = recordDtos.stream().map(RecordDto::getCategoryId).distinct().toList();
         List<RewardPunishmentCategory> categories = mpCategoryMapper.selectBatchIds(catIds);
         Map<Integer, RewardPunishmentCategory> catMap = categories.stream().collect(Collectors.toMap(RewardPunishmentCategory::getId, c -> c));
-        List<Integer> availableCatIds = catIds.stream().filter(c -> (catMap.containsKey(c) && catMap.get(c).getStatus() == null)).toList();
+        List<Integer> 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();
     }
 }