Merge pull request #26 from suyiiyii/cat
Some checks failed
Docker Build and Publish / build (push) Has been cancelled
Gitea Sync / trigger-gitea-sync (push) Has been cancelled
Java CI with Maven / build (push) Has been cancelled

实现记录类别的增删改查以及查询记录的过滤
This commit is contained in:
suyiiyii 2024-09-09 00:46:44 +08:00 committed by GitHub
commit 39cfd99bad
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
10 changed files with 205 additions and 25 deletions

View File

@ -0,0 +1,24 @@
package top.suyiiyii.sims.common;
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@MapperScan("top.suyiiyii.sims.mapper")
public class MybatisPlusConfig {
/**
* 添加分页插件
*/
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL)); // 如果配置多个插件, 切记分页最后添加
// 如果有多数据源可以不配具体类型, 否则都建议配上具体的 DbType
return interceptor;
}
}

View File

@ -0,0 +1,61 @@
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.*;
import top.suyiiyii.sims.common.AuthAccess;
import top.suyiiyii.sims.common.Result;
import top.suyiiyii.sims.entity.RewardPunishmentCategory;
import top.suyiiyii.sims.service.CategoryService;
import java.util.List;
@RestController()
@RequestMapping("/category")
public class CategoryController {
@Autowired
CategoryService categoryService;
@GetMapping("")
@Operation(summary = "获取所有类别",description = "获取所有类别信息,支持分页")
@AuthAccess(allowRoles = {"admin"})
public Result<List<RewardPunishmentCategory>> getAllCategory(
@RequestParam(value = "page", defaultValue = "1") Integer page,
@RequestParam(value = "size", defaultValue = "10") Integer size
) {
Page<RewardPunishmentCategory> pageObj = new Page<>(page, size);
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,
@RequestParam Integer newId) {
categoryService.deleteCategory(id, newId);
return Result.success("删除成功");
}
}

View File

@ -2,7 +2,6 @@ package top.suyiiyii.sims.controller;
import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.Operation;
import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import lombok.Data; import lombok.Data;
import org.modelmapper.ModelMapper; import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
@ -18,7 +17,6 @@ import top.suyiiyii.sims.service.CategoryService;
import top.suyiiyii.sims.service.RecordService; import top.suyiiyii.sims.service.RecordService;
import top.suyiiyii.sims.service.RoleService; import top.suyiiyii.sims.service.RoleService;
import top.suyiiyii.sims.service.UserService; import top.suyiiyii.sims.service.UserService;
import top.suyiiyii.sims.utils.JwtUtils;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashSet; import java.util.HashSet;
@ -53,7 +51,7 @@ RecordController {
recordDto.setSubCategoryName(categoryService.getsubCategoryName(record.getCategoryId())); recordDto.setSubCategoryName(categoryService.getsubCategoryName(record.getCategoryId()));
recordDtos.add(recordDto); recordDtos.add(recordDto);
} }
return Result.success(recordDtos); return Result.success(recordService.filterRecordsDtos(recordDtos));
} }
@AuthAccess(allowRoles = {"user"}) @AuthAccess(allowRoles = {"user"})
@ -72,7 +70,7 @@ RecordController {
recordDto.setSubCategoryName(categoryService.getsubCategoryName(record.getCategoryId())); recordDto.setSubCategoryName(categoryService.getsubCategoryName(record.getCategoryId()));
recordDtos.add(recordDto); recordDtos.add(recordDto);
} }
return Result.success(recordDtos); return Result.success(recordService.filterRecordsDtos(recordDtos));
} }
@AuthAccess(allowRoles = {"admin"}) @AuthAccess(allowRoles = {"admin"})
@Operation(summary = "更新单个奖惩记录") @Operation(summary = "更新单个奖惩记录")
@ -132,7 +130,7 @@ RecordController {
studentIds.add(studentId); studentIds.add(studentId);
} }
String roleName = searchRequest.getRoleName(); String roleName = searchRequest.getRoleName();
if(roleName!="") { if(!roleName.isEmpty()) {
//rolename查用户id //rolename查用户id
Integer userId = roleService.getIdByrolename(roleName); Integer userId = roleService.getIdByrolename(roleName);
// 用户id查记录 // 用户id查记录
@ -140,7 +138,7 @@ RecordController {
studentIds.add(s1); studentIds.add(s1);
} }
String username = searchRequest.getUsername(); String username = searchRequest.getUsername();
if(username!="") { if(!username.isEmpty()) {
//username查用户StudentId //username查用户StudentId
s1= roleService.getStudentIdByUsername(username); s1= roleService.getStudentIdByUsername(username);
studentIds.add(s1); studentIds.add(s1);
@ -155,7 +153,7 @@ RecordController {
RecordDto.setSubCategoryName(categoryService.getsubCategoryName(record.getCategoryId())); RecordDto.setSubCategoryName(categoryService.getsubCategoryName(record.getCategoryId()));
RecordDtos.add(RecordDto); RecordDtos.add(RecordDto);
} }
return Result.success(RecordDtos); return Result.success(recordService.filterRecordsDtos(RecordDtos));
} }
@AuthAccess(allowRoles = {"admin"}) @AuthAccess(allowRoles = {"admin"})
@Operation(summary = "筛选查询奖惩记录") @Operation(summary = "筛选查询奖惩记录")
@ -189,7 +187,7 @@ RecordController {
RecordDto.setSubCategoryName(categoryService.getsubCategoryName(record.getCategoryId())); RecordDto.setSubCategoryName(categoryService.getsubCategoryName(record.getCategoryId()));
RecordDtos.add(RecordDto); RecordDtos.add(RecordDto);
} }
return Result.success(RecordDtos); return Result.success(recordService.filterRecordsDtos(RecordDtos));
} }
@AuthAccess(allowRoles = {"user","admin"}) @AuthAccess(allowRoles = {"user","admin"})
@ -199,12 +197,7 @@ RecordController {
@RequestParam(defaultValue = "0") int page, @RequestParam(defaultValue = "0") int page,
@RequestParam(defaultValue = "10") int size, @RequestParam(defaultValue = "10") int size,
String categoryName,HttpServletRequest request) { String categoryName,HttpServletRequest request) {
HttpSession session = request.getSession(); int userId = JwtInterceptor.getUserIdFromReq(request);
String token = (String) session.getAttribute("token");
String userId = JwtUtils.extractUserId(token);
if (userId==null){
throw new RuntimeException("请先登录");
}
List<Integer> studentIds = new ArrayList<>(); List<Integer> studentIds = new ArrayList<>();
//CategoryName不是奖励或者惩罚 //CategoryName不是奖励或者惩罚
if (!categoryName.equals("奖励") if (!categoryName.equals("奖励")
@ -221,7 +214,7 @@ RecordController {
List<Record> records=new ArrayList<>(); List<Record> records=new ArrayList<>();
HashSet<Integer> studentIds1= new HashSet<>(studentIds); HashSet<Integer> studentIds1= new HashSet<>(studentIds);
for (Integer Sid : studentIds1) { for (Integer Sid : studentIds1) {
Integer studentId1 =userService.getStudentIdByUserId(Integer.valueOf(userId)); Integer studentId1 =userService.getStudentIdByUserId(userId);
if (studentId1!= null && studentId1.equals(Sid)) { if (studentId1!= null && studentId1.equals(Sid)) {
records.addAll(recordService.getRecordsById(page, size, Sid)); records.addAll(recordService.getRecordsById(page, size, Sid));
} }
@ -233,7 +226,7 @@ RecordController {
RecordDto.setSubCategoryName(categoryService.getsubCategoryName(record.getCategoryId())); RecordDto.setSubCategoryName(categoryService.getsubCategoryName(record.getCategoryId()));
RecordDtos.add(RecordDto); RecordDtos.add(RecordDto);
} }
return Result.success(RecordDtos); return Result.success(recordService.filterRecordsDtos(RecordDtos));
} }
@Data @Data
public static class SearchRequest { public static class SearchRequest {

View File

@ -17,6 +17,8 @@ public class RecordDto {
private String categoryName; private String categoryName;
private String subCategoryName; private String subCategoryName;
// 类别id
private Integer categoryId;
// 奖惩日期 // 奖惩日期
private Long date; private Long date;
// 奖惩内容 // 奖惩内容

View File

@ -2,8 +2,9 @@ package top.suyiiyii.sims.entity;
import com.baomidou.mybatisplus.annotation.IdType; import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableId;
import com.tangzc.mpe.autotable.annotation.Column; import com.tangzc.autotable.annotation.ColumnNotNull;
import com.tangzc.mpe.autotable.annotation.Table; import com.tangzc.mpe.autotable.annotation.Table;
import com.tangzc.mpe.autotable.annotation.UniqueIndex;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.Data; import lombok.Data;
import lombok.NoArgsConstructor; import lombok.NoArgsConstructor;
@ -23,15 +24,22 @@ import java.util.Objects;
@AllArgsConstructor @AllArgsConstructor
@NoArgsConstructor @NoArgsConstructor
public class RewardPunishmentCategory { public class RewardPunishmentCategory {
@TableId(type= IdType.AUTO) @TableId(type = IdType.AUTO)
private Integer id; private Integer id;
private Integer categoryId; private Integer categoryId;
// 类别名称 // 类别名称
@ColumnNotNull
private String categoryName; private String categoryName;
@ColumnNotNull
@UniqueIndex
private String subCategoryName; private String subCategoryName;
// 类别说明 // 类别说明
private String description; private String description;
// 类别状态 null:正常 "disable":删除
private String status;
@Override @Override
public boolean equals(Object o) { public boolean equals(Object o) {
if (this == o) return true; if (this == o) return true;

View File

@ -0,0 +1,7 @@
package top.suyiiyii.sims.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import top.suyiiyii.sims.entity.RewardPunishmentCategory;
public interface MpCategoryMapper extends BaseMapper<RewardPunishmentCategory> {
}

View File

@ -0,0 +1,7 @@
package top.suyiiyii.sims.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import top.suyiiyii.sims.entity.Record;
public interface MpRecordMapper extends BaseMapper<Record> {
}

View File

@ -1,8 +1,16 @@
package top.suyiiyii.sims.service; package top.suyiiyii.sims.service;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import top.suyiiyii.sims.entity.Record;
import top.suyiiyii.sims.entity.RewardPunishmentCategory;
import top.suyiiyii.sims.exception.ServiceException;
import top.suyiiyii.sims.mapper.CategoryMapper; import top.suyiiyii.sims.mapper.CategoryMapper;
import top.suyiiyii.sims.mapper.MpCategoryMapper;
import top.suyiiyii.sims.mapper.MpRecordMapper;
import java.util.List; import java.util.List;
@ -19,6 +27,12 @@ public class CategoryService {
@Autowired @Autowired
CategoryMapper categoryMapper; CategoryMapper categoryMapper;
@Autowired
MpCategoryMapper mpCategoryMapper;
@Autowired
MpRecordMapper mpRecordMapper;
public String getCategoryName(Integer id) { public String getCategoryName(Integer id) {
return categoryMapper.getCategoryName(id); return categoryMapper.getCategoryName(id);
@ -29,12 +43,41 @@ public class CategoryService {
} }
public List<Integer> getIdByCategoryName(String categoryName) { public List<Integer> getIdByCategoryName(String categoryName) {
return categoryMapper.getIdByCategoryName(categoryName); return categoryMapper.getIdByCategoryName(categoryName);
} }
public List<RewardPunishmentCategory> getCateGory(Page pageObj) {
mpCategoryMapper.selectPage(pageObj, new LambdaQueryWrapper<>());
return pageObj.getRecords();
}
public List<RewardPunishmentCategory> getCateByIds(List<Integer> ids) {
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, Integer newId) {
if (!mpCategoryMapper.exists(new LambdaQueryWrapper<RewardPunishmentCategory>().eq(RewardPunishmentCategory::getId, id))) {
throw new ServiceException("该类别不存在");
}
mpCategoryMapper.deleteById(id);
mpRecordMapper.update(new LambdaUpdateWrapper<Record>().eq(Record::getCategoryId, id).set(Record::getCategoryId, newId));
}
} }

View File

@ -4,14 +4,17 @@ package top.suyiiyii.sims.service;
import org.modelmapper.ModelMapper; import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import top.suyiiyii.sims.dto.RecordDto; import top.suyiiyii.sims.dto.RecordDto;
import top.suyiiyii.sims.entity.Record; import top.suyiiyii.sims.entity.Record;
import top.suyiiyii.sims.entity.RewardPunishmentCategory;
import top.suyiiyii.sims.mapper.CategoryMapper; import top.suyiiyii.sims.mapper.CategoryMapper;
import top.suyiiyii.sims.mapper.MpCategoryMapper;
import top.suyiiyii.sims.mapper.RecordMapper; import top.suyiiyii.sims.mapper.RecordMapper;
import top.suyiiyii.sims.mapper.UserMapper; import top.suyiiyii.sims.mapper.UserMapper;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
/** /**
* @Author tortoise * @Author tortoise
@ -31,6 +34,9 @@ public class RecordService {
ModelMapper modelMapper; ModelMapper modelMapper;
@Autowired @Autowired
CategoryMapper categoryMapper; CategoryMapper categoryMapper;
@Autowired
MpCategoryMapper mpCategoryMapper;
public List<Record> getAllRecords(Integer page, Integer size) { public List<Record> getAllRecords(Integer page, Integer size) {
return recordMapper.getAllRecords(page, size); return recordMapper.getAllRecords(page, size);
@ -57,7 +63,7 @@ public class RecordService {
//查看数据库里面是否有这个类别 //查看数据库里面是否有这个类别
String subCategoryName = categoryMapper.IsSubCategoryName(recordDto.getSubCategoryName()); String subCategoryName = categoryMapper.IsSubCategoryName(recordDto.getSubCategoryName());
if(subCategoryName == null) { if (subCategoryName == null) {
//没有这个类别就加上 //没有这个类别就加上
categoryMapper.addsubcategory(recordDto.getCategoryName(), recordDto.getSubCategoryName()); categoryMapper.addsubcategory(recordDto.getCategoryName(), recordDto.getSubCategoryName());
} }
@ -69,12 +75,11 @@ public class RecordService {
} }
public List<Record> getRecordsLike(int page, int size, Integer studentId, String userGroup, String grade) { public List<Record> getRecordsLike(int page, int size, Integer studentId, String userGroup, String grade) {
return recordMapper.getRecordsLike(page, size, studentId, userGroup,grade); return recordMapper.getRecordsLike(page, size, studentId, userGroup, grade);
} }
public List<Integer> getSidByCategoryId(Integer i) {
public List<Integer> getSidByCategoryId(Integer i) {
return recordMapper.getSidByCategoryId(i); return recordMapper.getSidByCategoryId(i);
} }
@ -85,4 +90,20 @@ public class RecordService {
public Integer IsRecord(Integer id) { public Integer IsRecord(Integer id) {
return recordMapper.IsRecord(id); return recordMapper.IsRecord(id);
} }
public List<Record> filterRecords(List<Record> records) {
List<Integer> catIds = records.stream().map(Record::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();
return records.stream().filter(r -> availableCatIds.contains(r.getCategoryId())).toList();
}
public List<RecordDto> filterRecordsDtos(List<RecordDto> recordDtos) {
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 || catMap.get(c).getStatus().equals("enable")))).toList();
return recordDtos.stream().filter(r -> availableCatIds.contains(r.getCategoryId())).toList();
}
} }

View File

@ -0,0 +1,14 @@
# 奖惩类别的增删改查
## 需求
1. 管理员可以查看奖惩类别的数据,可以按奖惩类型查看(类别名称,奖惩类型,是否启用,类别说明,操作),需分页展示
2. 管理员可以启用或禁用某个奖惩类别,禁用后在添加奖惩记录的页面中不显示该奖惩类别 ,启动后恢复
3. 管理员可以删除某个奖惩类别,但需要为已有该奖惩类别的奖惩记录选择一个替代的奖惩类别,替换后相应的奖惩记录也会同步更新
### 需求1
基础增删改查,支持分页
### 需求2
先使用已有接口进行查询,然后再查询已有数据的类型有没有被禁用,过滤即可
### 需求3
全表替换