调整个人查询无法出现类别类型

This commit is contained in:
tortoise 2024-08-30 20:14:49 +08:00
parent 8767c5c554
commit 471079f68c
9 changed files with 57 additions and 21 deletions

View File

@ -29,9 +29,7 @@ public class RbacInterceptor implements HandlerInterceptor {
}
// 获取用户角色
List<String> roles = getUserRole(request).stream().map(Role::getRoleName).toList();
List<String> allowRoles = null;
// 获取当前请求的方法上的 AuthAccess 注解从而获取允许访问的角色
if (handler instanceof HandlerMethod) {
AuthAccess annotation = ((HandlerMethod) handler).getMethodAnnotation(AuthAccess.class);

View File

@ -2,6 +2,7 @@ package top.suyiiyii.sims.controller;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
@ -51,13 +52,14 @@ RecordController {
return Result.success(recordDtos);
}
@AuthAccess(allowRoles = {"user"})
@AuthAccess(allowRoles = {"user","admin"})
@Operation(summary = "获取自己的奖惩记录")
@GetMapping("/record")
public Result<List<RecordDto>> record(@RequestParam(defaultValue = "0") int page,
@RequestParam(defaultValue = "10") int size,
HttpServletRequest request) {
String token = (String) request.getAttribute("token");
HttpSession session = request.getSession();
String token = (String) session.getAttribute("token");
String userId = JwtUtils.extractUserId(token);
List<RecordDto> recordDtos = new ArrayList<>();
List<Record> records = recordService.getMyAllRecords(page, size, userId);
@ -68,9 +70,7 @@ RecordController {
recordDtos.add(recordDto);
}
return Result.success(recordDtos);
}
@AuthAccess(allowRoles = {"admin"})
@Operation(summary = "更新单个奖惩记录")
@PutMapping("/admin/record/{id}")
@ -93,14 +93,16 @@ RecordController {
@Operation(summary = "添加奖惩记录")
@PostMapping("/admin/record")
public Result<CommonResponse> adminAddRecord(@RequestBody RecordDto recordDto) {
Integer categoryId = categoryService.getIdBySubCategoryName(recordDto.getSubCategoryName());
Record record = modelMapper.map(recordDto, Record.class);
if (categoryId == null) {
Result.error("请选择奖惩类别,以及类型");
//CategoryName不是奖励或者惩罚
if (!recordDto.getCategoryName().equals("奖励")
&& !recordDto.getCategoryName().equals("惩罚")) {
return Result.error("请选择正确奖惩类别");
}
record.setCategoryId(categoryId);
recordService.addRecord(record);
if (recordDto.getSubCategoryName().isEmpty()) {
return Result.error("请输入奖惩类型");
}
recordService.addRecord(recordDto);
return Result.msg("添加成功");
}
@AuthAccess(allowRoles = {"admin"})

View File

@ -3,6 +3,7 @@ package top.suyiiyii.sims.controller;
import cn.hutool.core.util.StrUtil;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import jakarta.validation.constraints.Max;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
@ -40,7 +41,7 @@ public class UserController {
@AuthAccess(allowRoles = {"guest"})
@PostMapping("/user/login")
public Result<LoginResponse> login(@RequestBody LoginRequest request, HttpServletRequest httpServletRequest) {
public Result<LoginResponse> login(@RequestBody LoginRequest request,HttpServletRequest httpServletRequest) {
log.info("login request:{}", request);
if (StrUtil.isBlank(request.getUsername()) || StrUtil.isBlank(request.getPassword())) {
@ -53,6 +54,8 @@ public class UserController {
}
LoginResponse response = new LoginResponse();
response.setToken(token);
HttpSession session = httpServletRequest.getSession();
session.setAttribute("token",token);
return Result.success(response);
}
@AuthAccess(allowRoles = {"guest"})

View File

@ -12,7 +12,6 @@ public class RecordDto {
// 用户ID
private Integer studentId;
private String categoryName;
private String subCategoryName;

View File

@ -2,6 +2,7 @@ package top.suyiiyii.sims.entity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.tangzc.mpe.autotable.annotation.Column;
import com.tangzc.mpe.autotable.annotation.Table;
import lombok.AllArgsConstructor;
import lombok.Data;

View File

@ -1,7 +1,9 @@
package top.suyiiyii.sims.mapper;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
/**
* @Author tortoise
@ -19,6 +21,15 @@ public interface CategoryMapper {
@Select("SELECT category_name FROM reward_punishment_category WHERE category_id=#{categoryId}")
String getSubCategoryName(Integer categoryId);
@Select("SELECT category_id FROM reward_punishment_category WHERE sub_category_name=#{subCategoryName}")
@Select("SELECT id FROM reward_punishment_category WHERE sub_category_name=#{subCategoryName}")
Integer getIdBySubCategoryName(String subCategoryName);
@Select("SELECT category_id FROM reward_punishment_category WHERE category_name=#{categoryName}")
Integer getIdByCategoryName(String categoryName);
@Select("SELECT sub_category_name FROM reward_punishment_category WHERE sub_category_name=#{subCategoryName}")
String IsSubCategoryName(String subCategoryName);
@Insert("INSERT INTO reward_punishment_category (category_name, sub_category_name) VALUES (#{categoryName}, #{subCategoryName})")
void addsubcategory(String categoryName, String subCategoryName);
//把categoryId放入对应id下
@Update("update reward_punishment_category set category_id=#{categoryId} where id=#{categoryId}")
void addCategoryId(Integer categoryId);
}

View File

@ -27,8 +27,12 @@ public class CategoryService {
}
public Integer getIdBySubCategoryName(String subCategoryName) {
return categoryMapper.getIdBySubCategoryName(subCategoryName);
public Integer getIdByCategoryName(String categoryName) {
return categoryMapper.getIdByCategoryName(categoryName);
}
public String IsSubCategoryName(String subCategoryName) {
return categoryMapper.IsSubCategoryName(subCategoryName);
}
}

View File

@ -1,10 +1,13 @@
package top.suyiiyii.sims.service;
import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.suyiiyii.sims.dto.RecordDto;
import top.suyiiyii.sims.entity.Record;
import top.suyiiyii.sims.mapper.CategoryMapper;
import top.suyiiyii.sims.mapper.RecordMapper;
import top.suyiiyii.sims.mapper.UserMapper;
@ -24,7 +27,10 @@ public class RecordService {
RecordMapper recordMapper;
@Autowired
UserMapper userMapper;
@Autowired
ModelMapper modelMapper;
@Autowired
CategoryMapper categoryMapper;
public List<Record> getAllRecords(Integer page, Integer size) {
return recordMapper.getAllRecords(page, size);
@ -44,8 +50,21 @@ public class RecordService {
recordMapper.deleteRecord(id);
}
public void addRecord(Record record) {
public void addRecord(RecordDto recordDto) {
//把recordDto转化成Record
Record record = modelMapper.map(recordDto, Record.class);
//查看数据库里面是否有这个类别
String subCategoryName = categoryMapper.IsSubCategoryName(recordDto.getCategoryName());
if(subCategoryName == null) {
//没有这个类别就加上
categoryMapper.addsubcategory(recordDto.getCategoryName(), recordDto.getSubCategoryName());
}
Integer categoryId = categoryMapper.getIdBySubCategoryName(recordDto.getSubCategoryName());
categoryMapper.addCategoryId(categoryId);
record.setCategoryId(categoryId);
recordMapper.addRecord(record);
}
public List<Record> getRecordsLike(int page, int size, Integer studentId, String userGroup, String grade) {

View File

@ -92,7 +92,6 @@ public class UserService {
throw new ServiceException("组别不能为空");
}
User user = modelMapper.map(req, User.class);
mpUserMapper.insert(user);
user = mpUserMapper.selectOne(new LambdaQueryWrapper<User>().eq(User::getUsername, req.getUsername()));
rbacService.addRoleWithUserId(user.getId(), "user");