实现记录的增加和删除

This commit is contained in:
suyiiyii 2024-09-08 20:44:26 +08:00
parent b8b14f7af9
commit df4230a5d2
3 changed files with 55 additions and 6 deletions

View File

@ -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("删除成功");
}
}

View File

@ -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);
}
}

View File

@ -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();
}
}