自定义注解@ValidateParams

全局拦截器校验参数非空
课程内容相关代码优化
master
Alan 5 months ago
parent c8df5be72e
commit 2f5c6a82ed
  1. 6
      pom.xml
  2. 2
      src/main/java/com/teaching/backend/common/ErrorCode.java
  3. 19
      src/main/java/com/teaching/backend/config/WebConfig.java
  4. 5
      src/main/java/com/teaching/backend/controller/courses/CourseObjectivesController.java
  5. 9
      src/main/java/com/teaching/backend/controller/courses/CoursesController.java
  6. 3
      src/main/java/com/teaching/backend/controller/courses/ObjectiveContentKnowController.java
  7. 5
      src/main/java/com/teaching/backend/controller/courses/ObjectiveContentsController.java
  8. 8
      src/main/java/com/teaching/backend/exception/GlobalExceptionHandler.java
  9. 51
      src/main/java/com/teaching/backend/filter/BodyReaderWrapper.java
  10. 37
      src/main/java/com/teaching/backend/filter/ReplaceStreamFilter.java
  11. 13
      src/main/java/com/teaching/backend/filter/ValidateParams.java
  12. 83
      src/main/java/com/teaching/backend/filter/ValidationInterceptor.java
  13. 2
      src/main/java/com/teaching/backend/model/dto/courses/CoursesDTO.java
  14. 2
      src/main/java/com/teaching/backend/model/entity/courses/CourseObjectives.java
  15. 4
      src/main/java/com/teaching/backend/model/vo/courses/CourseObjectivesTreeVO.java
  16. 12
      src/main/java/com/teaching/backend/service/impl/courses/CourseObjectivesServiceImpl.java
  17. 37
      src/main/java/com/teaching/backend/service/impl/courses/CoursesServiceImpl.java
  18. 2
      src/main/java/com/teaching/backend/utils/CourseCode.java

@ -190,6 +190,12 @@
<artifactId>easypoi-spring-boot-starter</artifactId> <artifactId>easypoi-spring-boot-starter</artifactId>
<version>4.1.3</version> <version>4.1.3</version>
</dependency> </dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.8.0</version>
</dependency>
</dependencies> </dependencies>

@ -36,6 +36,8 @@ public enum ErrorCode {
PARAMS_COURSE_NOTEXISTS(4004,"课程不存在"), PARAMS_COURSE_NOTEXISTS(4004,"课程不存在"),
SYSTEM_ERROR(50000, "系统内部异常"), SYSTEM_ERROR(50000, "系统内部异常"),
UNKNOW_ERROR(50002, "系统内部异常"),
OPERATION_ERROR(50001, "操作失败"); OPERATION_ERROR(50001, "操作失败");
/** /**

@ -0,0 +1,19 @@
package com.teaching.backend.config;
import com.teaching.backend.filter.ValidationInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class WebConfig implements WebMvcConfigurer {
@Autowired
private ValidationInterceptor validationInterceptor;
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(validationInterceptor).addPathPatterns("/**");
}
}

@ -4,6 +4,7 @@ package com.teaching.backend.controller.courses;
import cn.hutool.core.bean.BeanUtil; import cn.hutool.core.bean.BeanUtil;
import com.teaching.backend.common.BaseResponse; import com.teaching.backend.common.BaseResponse;
import com.teaching.backend.common.ResultUtils; import com.teaching.backend.common.ResultUtils;
import com.teaching.backend.filter.ValidateParams;
import com.teaching.backend.mapper.courses.CourseObjectivesMapper; import com.teaching.backend.mapper.courses.CourseObjectivesMapper;
import com.teaching.backend.model.dto.courses.CourseObjectivesDTO; import com.teaching.backend.model.dto.courses.CourseObjectivesDTO;
import com.teaching.backend.model.entity.courses.CourseObjectives; import com.teaching.backend.model.entity.courses.CourseObjectives;
@ -37,6 +38,7 @@ public class CourseObjectivesController {
CourseObjectivesMapper courseObjectivesMapper; CourseObjectivesMapper courseObjectivesMapper;
@ApiOperation("新增分项目标-默认第一个必须添加思政目标") @ApiOperation("新增分项目标-默认第一个必须添加思政目标")
@ValidateParams({"name","type","pid"})
@PostMapping("/addobjectives") @PostMapping("/addobjectives")
public BaseResponse<String> saveCourseObjectives(@RequestBody CourseObjectivesDTO courseObjectivesDTO){ public BaseResponse<String> saveCourseObjectives(@RequestBody CourseObjectivesDTO courseObjectivesDTO){
String data = courseObjectivesService.addObjectives(courseObjectivesDTO); String data = courseObjectivesService.addObjectives(courseObjectivesDTO);
@ -44,6 +46,7 @@ public class CourseObjectivesController {
} }
@ApiOperation("删除分项目标-只能最后一个删除思政目标-总目标只需删除关联的内容") @ApiOperation("删除分项目标-只能最后一个删除思政目标-总目标只需删除关联的内容")
@ValidateParams({"id"})
@DeleteMapping("/{id}") @DeleteMapping("/{id}")
public BaseResponse<String> deleteCourseObjectivesByIds(@PathVariable String id){ public BaseResponse<String> deleteCourseObjectivesByIds(@PathVariable String id){
String data = courseObjectivesService.deleteObjectives(id); String data = courseObjectivesService.deleteObjectives(id);
@ -54,6 +57,7 @@ public class CourseObjectivesController {
* @param courseObjectivesDTO * @param courseObjectivesDTO
* @return * @return
*/ */
//TODO:分项目标按理说不能编辑,只可以新增 删除
@ApiOperation("编辑分项目标") @ApiOperation("编辑分项目标")
@PutMapping @PutMapping
public BaseResponse<String> updateCourseObjectives(@RequestBody CourseObjectivesDTO courseObjectivesDTO){ public BaseResponse<String> updateCourseObjectives(@RequestBody CourseObjectivesDTO courseObjectivesDTO){
@ -65,6 +69,7 @@ public class CourseObjectivesController {
} }
@ApiOperation("查询课程目标内容") @ApiOperation("查询课程目标内容")
@ValidateParams({"id"})
@GetMapping("/list/{id}") @GetMapping("/list/{id}")
public BaseResponse<List<CourseObjectivesTreeVO>> queryCourseObjectivesTree(@PathVariable String id){ public BaseResponse<List<CourseObjectivesTreeVO>> queryCourseObjectivesTree(@PathVariable String id){
List<CourseObjectivesTreeVO> courseObjectivesTreeVO = courseObjectivesService.queryCourseObjectivesTree(id); List<CourseObjectivesTreeVO> courseObjectivesTreeVO = courseObjectivesService.queryCourseObjectivesTree(id);

@ -6,6 +6,7 @@ import com.teaching.backend.common.ErrorCode;
import com.teaching.backend.common.ResultUtils; import com.teaching.backend.common.ResultUtils;
import com.teaching.backend.exception.BusinessException; import com.teaching.backend.exception.BusinessException;
import com.teaching.backend.filter.ValidateParams;
import com.teaching.backend.model.dto.courses.CoursesDTO; import com.teaching.backend.model.dto.courses.CoursesDTO;
import com.teaching.backend.model.dto.courses.PageDTO; import com.teaching.backend.model.dto.courses.PageDTO;
@ -56,6 +57,7 @@ public class CoursesController {
} }
@ApiOperation("新增课程--同步新增课程的总体目标并同时添加所有的分项目标") @ApiOperation("新增课程--同步新增课程的总体目标并同时添加所有的分项目标")
@ValidateParams({"teacher","img", "category", "nature","name","code","credit","classhours","assessmenttype","assessmentway"}) // 需要校验的参数
@PostMapping("/addcourse") @PostMapping("/addcourse")
public BaseResponse<String> saveCourse(@RequestBody CoursesDTO coursesDTO){ public BaseResponse<String> saveCourse(@RequestBody CoursesDTO coursesDTO){
String data = coursesService.saveCourseWithObjective(coursesDTO); String data = coursesService.saveCourseWithObjective(coursesDTO);
@ -63,8 +65,8 @@ public class CoursesController {
} }
//TODO:此处需要调整前端的接口
@ApiOperation("查询课程列表") @ApiOperation("查询课程列表")
@ValidateParams({"username"})
@GetMapping("/page") @GetMapping("/page")
public BaseResponse<PageDTO<CoursesVO>> getCourses(CourseQuery courseQuery){ public BaseResponse<PageDTO<CoursesVO>> getCourses(CourseQuery courseQuery){
PageDTO<CoursesVO> coursesList = coursesService.queryCourses(courseQuery); PageDTO<CoursesVO> coursesList = coursesService.queryCourses(courseQuery);
@ -74,9 +76,6 @@ public class CoursesController {
@ApiOperation("根据id查询课程") @ApiOperation("根据id查询课程")
@GetMapping("/{id}") @GetMapping("/{id}")
public BaseResponse<CoursesDTO> getByIdCourse(@PathVariable String id){ public BaseResponse<CoursesDTO> getByIdCourse(@PathVariable String id){
if(id==null){
throw new BusinessException(ErrorCode.PARAMS_ERROR,"课程id为空");
}
Courses course = coursesService.getById(id); Courses course = coursesService.getById(id);
CoursesDTO coursesDTO = new CoursesDTO(); CoursesDTO coursesDTO = new CoursesDTO();
BeanUtils.copyProperties(course,coursesDTO); BeanUtils.copyProperties(course,coursesDTO);
@ -84,6 +83,7 @@ public class CoursesController {
} }
@ApiOperation("根据id修改课程") @ApiOperation("根据id修改课程")
@ValidateParams({"id","teacher","img", "category", "nature","name","code","credit","classhours","assessmenttype","assessmentway"})
@PutMapping @PutMapping
public BaseResponse<String> editCourse(@RequestBody CoursesDTO coursesDTO){ public BaseResponse<String> editCourse(@RequestBody CoursesDTO coursesDTO){
coursesService.updateCourse(coursesDTO); coursesService.updateCourse(coursesDTO);
@ -92,6 +92,7 @@ public class CoursesController {
//TODO:删除功能暂未完善,数据缺失 //TODO:删除功能暂未完善,数据缺失
@ApiOperation("根据id删除课程") @ApiOperation("根据id删除课程")
@ValidateParams({"id"})
@DeleteMapping("/{id}") @DeleteMapping("/{id}")
public BaseResponse deleteCourses(@PathVariable String id){ public BaseResponse deleteCourses(@PathVariable String id){

@ -3,6 +3,7 @@ package com.teaching.backend.controller.courses;
import com.teaching.backend.common.BaseResponse; import com.teaching.backend.common.BaseResponse;
import com.teaching.backend.common.ResultUtils; import com.teaching.backend.common.ResultUtils;
import com.teaching.backend.filter.ValidateParams;
import com.teaching.backend.model.dto.courses.CoursesDTO; import com.teaching.backend.model.dto.courses.CoursesDTO;
import com.teaching.backend.model.dto.courses.ObjectiveContentKnowDTO; import com.teaching.backend.model.dto.courses.ObjectiveContentKnowDTO;
import com.teaching.backend.model.vo.courses.ObjectiveContentKnowVO; import com.teaching.backend.model.vo.courses.ObjectiveContentKnowVO;
@ -31,6 +32,7 @@ public class ObjectiveContentKnowController {
@ApiOperation("给分项目标或者是内容添加知识点") @ApiOperation("给分项目标或者是内容添加知识点")
@ValidateParams({"objectiveId","know"})
@PostMapping("/addknows") @PostMapping("/addknows")
public BaseResponse<String> saveKnowsWithObjectiveOrContent(@RequestBody ObjectiveContentKnowDTO objectiveContentKnowDTO){ public BaseResponse<String> saveKnowsWithObjectiveOrContent(@RequestBody ObjectiveContentKnowDTO objectiveContentKnowDTO){
String data = objectiveContentKnowService.saveKnowsWithObjectiveOrContent(objectiveContentKnowDTO); String data = objectiveContentKnowService.saveKnowsWithObjectiveOrContent(objectiveContentKnowDTO);
@ -38,6 +40,7 @@ public class ObjectiveContentKnowController {
} }
@ApiOperation("针对分项目标统计,支撑分项目标知识点的个数、学时合计、占比") @ApiOperation("针对分项目标统计,支撑分项目标知识点的个数、学时合计、占比")
@ValidateParams({"objectiveId"})
@GetMapping("/{objectiveId}") @GetMapping("/{objectiveId}")
public BaseResponse<ObjectiveContentKnowVO> getCountData(@PathVariable String objectiveId){ public BaseResponse<ObjectiveContentKnowVO> getCountData(@PathVariable String objectiveId){
ObjectiveContentKnowVO objectiveContentKnowVO= objectiveContentKnowService.getCountData(objectiveId); ObjectiveContentKnowVO objectiveContentKnowVO= objectiveContentKnowService.getCountData(objectiveId);

@ -3,6 +3,7 @@ package com.teaching.backend.controller.courses;
import com.teaching.backend.common.BaseResponse; import com.teaching.backend.common.BaseResponse;
import com.teaching.backend.common.ResultUtils; import com.teaching.backend.common.ResultUtils;
import com.teaching.backend.filter.ValidateParams;
import com.teaching.backend.model.entity.courses.ObjectiveContents; import com.teaching.backend.model.entity.courses.ObjectiveContents;
import com.teaching.backend.service.courses.IObjectiveContentsService; import com.teaching.backend.service.courses.IObjectiveContentsService;
import io.swagger.annotations.Api; import io.swagger.annotations.Api;
@ -27,6 +28,7 @@ public class ObjectiveContentsController {
IObjectiveContentsService objectiveContentsService; IObjectiveContentsService objectiveContentsService;
@ApiOperation("添加目标内容") @ApiOperation("添加目标内容")
@ValidateParams({"objectiveId"})
@PostMapping("/addcontent") @PostMapping("/addcontent")
public BaseResponse<String> saveContent(@RequestBody ObjectiveContents objectiveContents){ public BaseResponse<String> saveContent(@RequestBody ObjectiveContents objectiveContents){
objectiveContentsService.save(objectiveContents); objectiveContentsService.save(objectiveContents);
@ -34,6 +36,7 @@ public class ObjectiveContentsController {
} }
@ApiOperation("删除目标内容") @ApiOperation("删除目标内容")
@ValidateParams({"id"})
@DeleteMapping("/{id}") @DeleteMapping("/{id}")
public BaseResponse<String> deleteContent(@PathVariable String id){ public BaseResponse<String> deleteContent(@PathVariable String id){
// objectiveContentsService.removeById(id); // objectiveContentsService.removeById(id);
@ -42,6 +45,7 @@ public class ObjectiveContentsController {
} }
@ApiOperation("修改目标内容") @ApiOperation("修改目标内容")
@ValidateParams({"id","objectiveId"})
@PutMapping("/update") @PutMapping("/update")
public BaseResponse<String> updateContent(@RequestBody ObjectiveContents objectiveContents){ public BaseResponse<String> updateContent(@RequestBody ObjectiveContents objectiveContents){
objectiveContentsService.updateById(objectiveContents); objectiveContentsService.updateById(objectiveContents);
@ -49,6 +53,7 @@ public class ObjectiveContentsController {
} }
@ApiOperation("根据id查询目标内容") @ApiOperation("根据id查询目标内容")
@ValidateParams({"id"})
@GetMapping("/{id}") @GetMapping("/{id}")
public BaseResponse<ObjectiveContents> updateContent(@PathVariable String id){ public BaseResponse<ObjectiveContents> updateContent(@PathVariable String id){
ObjectiveContents objectiveContent = objectiveContentsService.getById(id); ObjectiveContents objectiveContent = objectiveContentsService.getById(id);

@ -1,5 +1,8 @@
package com.teaching.backend.exception; package com.teaching.backend.exception;
import com.teaching.backend.common.BaseResponse;
import com.teaching.backend.common.ErrorCode;
import com.teaching.backend.common.ResultUtils;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.ExceptionHandler;
@ -14,6 +17,11 @@ public class GlobalExceptionHandler {
return new ResponseEntity<>(errorResponse, HttpStatus.BAD_REQUEST); return new ResponseEntity<>(errorResponse, HttpStatus.BAD_REQUEST);
} }
@ExceptionHandler(Exception.class)
public BaseResponse handleException(Exception ex) {
return ResultUtils.error(ErrorCode.UNKNOW_ERROR, ex.getMessage());
}
// 其他异常处理器可以按需添加 // 其他异常处理器可以按需添加
public static class ErrorResponse { public static class ErrorResponse {

@ -0,0 +1,51 @@
package com.teaching.backend.filter;
import org.apache.commons.io.IOUtils;
import javax.servlet.ReadListener;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import java.io.*;
public class BodyReaderWrapper extends HttpServletRequestWrapper {
private final byte[] requestBody;
public BodyReaderWrapper(HttpServletRequest request) throws IOException {
super(request);
try (InputStream inputStream = request.getInputStream()) {
requestBody = IOUtils.toByteArray(inputStream);
}
}
@Override
public ServletInputStream getInputStream() {
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(requestBody);
return new ServletInputStream() {
@Override
public boolean isFinished() {
return byteArrayInputStream.available() == 0;
}
@Override
public boolean isReady() {
return true;
}
@Override
public void setReadListener(ReadListener readListener) {
// no-op
}
@Override
public int read() throws IOException {
return byteArrayInputStream.read();
}
};
}
@Override
public BufferedReader getReader() throws IOException {
return new BufferedReader(new InputStreamReader(getInputStream()));
}
}

@ -0,0 +1,37 @@
package com.teaching.backend.filter;
import org.springframework.stereotype.Component;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
@Component
@WebFilter(urlPatterns = "/*")
public class ReplaceStreamFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) {
// no-op
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
ServletRequest requestWrapper = null;
if (request instanceof HttpServletRequest) {
requestWrapper = new BodyReaderWrapper((HttpServletRequest) request);
}
if (requestWrapper == null) {
chain.doFilter(request, response);
} else {
chain.doFilter(requestWrapper, response);
}
}
@Override
public void destroy() {
// no-op
}
}

@ -0,0 +1,13 @@
package com.teaching.backend.filter;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface ValidateParams {
String[] value(); // 要校验的参数名称
}

@ -0,0 +1,83 @@
package com.teaching.backend.filter;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
@Component
public class ValidationInterceptor implements HandlerInterceptor {
@Autowired
private ObjectMapper objectMapper;
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
if (handler instanceof HandlerMethod) {
HandlerMethod handlerMethod = (HandlerMethod) handler;
Method method = handlerMethod.getMethod();
ValidateParams validateParams = method.getAnnotation(ValidateParams.class);
if (validateParams != null) {
Map<String, Object> parameterMap = new HashMap<>();
// 处理路径变量
parameterMap.putAll(getPathVariableValues(request, handlerMethod));
// 处理请求参数
parameterMap.putAll(getRequestParamValues(request));
// 处理请求体中的参数
if ("POST".equalsIgnoreCase(request.getMethod()) || "PUT".equalsIgnoreCase(request.getMethod())) {
Map<String, Object> bodyParams = objectMapper.readValue(request.getInputStream(), Map.class);
parameterMap.putAll(bodyParams);
}
for (String paramName : validateParams.value()) {
Object paramValue = parameterMap.get(paramName);
if (paramValue == null || (paramValue instanceof String && ((String) paramValue).trim().isEmpty())) {
handleErrorResponse(response, paramName + " 参数不能为空");
return false;
}
}
}
}
return true;
}
private Map<String, Object> getPathVariableValues(HttpServletRequest request, HandlerMethod handlerMethod) {
Map<String, Object> pathVariableValues = new HashMap<>();
Map<String, String> uriTemplateVars = (Map<String, String>) request.getAttribute("org.springframework.web.servlet.HandlerMapping.uriTemplateVariables");
if (uriTemplateVars != null) {
pathVariableValues.putAll(uriTemplateVars);
}
return pathVariableValues;
}
private Map<String, Object> getRequestParamValues(HttpServletRequest request) {
Map<String, Object> requestParamValues = new HashMap<>();
Map<String, String[]> paramMap = request.getParameterMap();
for (Map.Entry<String, String[]> entry : paramMap.entrySet()) {
if (entry.getValue() != null && entry.getValue().length > 0) {
requestParamValues.put(entry.getKey(), entry.getValue()[0]);
} else {
requestParamValues.put(entry.getKey(), null);
}
}
return requestParamValues;
}
private void handleErrorResponse(HttpServletResponse response, String errorMessage) throws IOException {
response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
response.setContentType("application/json;charset=UTF-8");
response.getWriter().write("{\"error\": \"" + errorMessage + "\"}");
}
}

@ -28,7 +28,7 @@ public class CoursesDTO {
/** /**
* 课程封面 * 课程封面
*/ */
@ApiModelProperty("图片") @ApiModelProperty(value = "图片",required = true)
private String img; private String img;
/** /**

@ -39,8 +39,6 @@ public class CourseObjectives implements Serializable {
@ApiModelProperty(value = "是否有子节点") @ApiModelProperty(value = "是否有子节点")
private Integer hasChild; private Integer hasChild;
@ApiModelProperty(value = "目标名称")
private String name;
@ApiModelProperty(value = "目标类型") @ApiModelProperty(value = "目标类型")
private Integer type; private Integer type;

@ -13,6 +13,10 @@ import java.util.List;
@ApiModel(description = "课程目标参数实体") @ApiModel(description = "课程目标参数实体")
public class CourseObjectivesTreeVO extends CourseObjectives implements Serializable { public class CourseObjectivesTreeVO extends CourseObjectives implements Serializable {
@ApiModelProperty(value = "目标名称")
private String name;
@ApiModelProperty(value = "目标内容列表") @ApiModelProperty(value = "目标内容列表")
private List<ObjectiveContents> contents; private List<ObjectiveContents> contents;

@ -8,6 +8,7 @@ import com.teaching.backend.exception.BusinessException;
import com.teaching.backend.mapper.courses.CourseObjectivesMapper; import com.teaching.backend.mapper.courses.CourseObjectivesMapper;
import com.teaching.backend.mapper.courses.ObjectiveContentKnowMapper; import com.teaching.backend.mapper.courses.ObjectiveContentKnowMapper;
import com.teaching.backend.mapper.courses.ObjectiveContentsMapper; import com.teaching.backend.mapper.courses.ObjectiveContentsMapper;
import com.teaching.backend.mapper.courses.ObjectivesTypeMapper;
import com.teaching.backend.model.dto.courses.CourseObjectivesDTO; import com.teaching.backend.model.dto.courses.CourseObjectivesDTO;
import com.teaching.backend.model.entity.courses.CourseObjectives; import com.teaching.backend.model.entity.courses.CourseObjectives;
import com.teaching.backend.model.entity.courses.ObjectiveContentKnow; import com.teaching.backend.model.entity.courses.ObjectiveContentKnow;
@ -43,6 +44,8 @@ public class CourseObjectivesServiceImpl extends ServiceImpl<CourseObjectivesMap
ObjectiveContentsMapper objectiveContentsMapper; ObjectiveContentsMapper objectiveContentsMapper;
@Autowired @Autowired
ObjectiveContentKnowMapper objectiveContentKnowMapper; ObjectiveContentKnowMapper objectiveContentKnowMapper;
@Autowired
ObjectivesTypeMapper objectivesTypeMapper;
@Override @Override
@Transactional @Transactional
@ -51,9 +54,9 @@ public class CourseObjectivesServiceImpl extends ServiceImpl<CourseObjectivesMap
CourseObjectives courseObjectivesNew = new CourseObjectives(); CourseObjectives courseObjectivesNew = new CourseObjectives();
BeanUtil.copyProperties(courseObjectivesDTO,courseObjectivesNew); BeanUtil.copyProperties(courseObjectivesDTO,courseObjectivesNew);
String pid = courseObjectivesDTO.getPid(); String pid = courseObjectivesDTO.getPid();
if(pid.equals(null)){ // if(pid.equals(null)){
throw new BusinessException(ErrorCode.NOT_FOUND_ERROR,"pid父节点不能为空"); // throw new BusinessException(ErrorCode.NOT_FOUND_ERROR,"pid父节点不能为空");
} // }
Integer type = courseObjectivesDTO.getType(); Integer type = courseObjectivesDTO.getType();
//判断是否已经添加过该类型的目标 //判断是否已经添加过该类型的目标
//每个类型的目标只能有一个 //每个类型的目标只能有一个
@ -104,7 +107,7 @@ public class CourseObjectivesServiceImpl extends ServiceImpl<CourseObjectivesMap
throw new BusinessException(ErrorCode.NOT_FOUND_ERROR); throw new BusinessException(ErrorCode.NOT_FOUND_ERROR);
} }
Integer type = courseObjectives.getType(); Integer type = courseObjectives.getType();
if (type == CourseCode.Total_EXIT.getValue()){ if (type == CourseCode.TOTAL_OBJECTIVE_TYPE.getValue()){
throw new BusinessException(ErrorCode.OPERATION_ERROR,"课程总目标只需清除所列内容即可!"); throw new BusinessException(ErrorCode.OPERATION_ERROR,"课程总目标只需清除所列内容即可!");
} }
//TODO:等到删完内容后判断还是上来就判断? //TODO:等到删完内容后判断还是上来就判断?
@ -170,6 +173,7 @@ public class CourseObjectivesServiceImpl extends ServiceImpl<CourseObjectivesMap
List<CourseObjectivesTreeVO> objectives = courseObjectivesMapper.selectTreeNodes(courseObjectivesId); List<CourseObjectivesTreeVO> objectives = courseObjectivesMapper.selectTreeNodes(courseObjectivesId);
Map<String, CourseObjectivesTreeVO> map = new HashMap<>(); Map<String, CourseObjectivesTreeVO> map = new HashMap<>();
for (CourseObjectivesTreeVO objective : objectives) { for (CourseObjectivesTreeVO objective : objectives) {
objective.setName(objectivesTypeMapper.selectById(objective.getType()).getTypeName());
map.put(objective.getId(), objective); map.put(objective.getId(), objective);
} }

@ -81,10 +81,10 @@ public class CoursesServiceImpl extends ServiceImpl<CoursesMapper, Courses> impl
@Override @Override
@Transactional @Transactional
public String saveCourseWithObjective(CoursesDTO coursesDTO) { public String saveCourseWithObjective(CoursesDTO coursesDTO) {
String teacher = coursesDTO.getTeacher(); // String teacher = coursesDTO.getTeacher();
if (teacher == null || teacher.equals("")) { // if (teacher == null || teacher.equals("")) {
throw new BusinessException(ErrorCode.USERNAME_NOT_EXIT); // throw new BusinessException(ErrorCode.USERNAME_NOT_EXIT);
} // }
Courses courses = new Courses(); Courses courses = new Courses();
String courseId = UUID.randomUUID().toString().replace("-", ""); String courseId = UUID.randomUUID().toString().replace("-", "");
@ -101,7 +101,8 @@ public class CoursesServiceImpl extends ServiceImpl<CoursesMapper, Courses> impl
if (insert > 0) { if (insert > 0) {
Courses coursesNew = coursesMapper.selectById(courseId); Courses coursesNew = coursesMapper.selectById(courseId);
courseObjectives.setCourseId(courseId); courseObjectives.setCourseId(courseId);
courseObjectives.setName(coursesNew.getName() + "课程总体目标"); courseObjectives.setType(CourseCode.TOTAL_OBJECTIVE_TYPE.getValue());
// courseObjectives.setName(coursesNew.getName() + "课程总体目标");
int insertTotal = courseObjectivesMapper.insert(courseObjectives); int insertTotal = courseObjectivesMapper.insert(courseObjectives);
if (insertTotal>0){ if (insertTotal>0){
CourseObjectives courseTotalObjectives = courseObjectivesMapper.selectOne CourseObjectives courseTotalObjectives = courseObjectivesMapper.selectOne
@ -112,12 +113,14 @@ public class CoursesServiceImpl extends ServiceImpl<CoursesMapper, Courses> impl
CourseObjectives courseObjectivesF = null; CourseObjectives courseObjectivesF = null;
for (ObjectivesType objectivesType : objectivesTypes) { for (ObjectivesType objectivesType : objectivesTypes) {
Integer typeId = objectivesType.getId(); Integer typeId = objectivesType.getId();
String typeName = objectivesType.getTypeName(); if (!typeId.equals(CourseCode.TOTAL_OBJECTIVE_TYPE.getValue())){
courseObjectivesF = new CourseObjectives(); // String typeName = objectivesType.getTypeName();
courseObjectivesF.setType(typeId); courseObjectivesF = new CourseObjectives();
courseObjectivesF.setName(typeName); courseObjectivesF.setType(typeId);
courseObjectivesF.setPid(courseTotalObjectivesId); // courseObjectivesF.setName(typeName);
courseObjectivesFList.add(courseObjectivesF); courseObjectivesF.setPid(courseTotalObjectivesId);
courseObjectivesFList.add(courseObjectivesF);
}
} }
courseObjectivesMapper.insertBatch(courseObjectivesFList); courseObjectivesMapper.insertBatch(courseObjectivesFList);
courseTotalObjectives.setHasChild(1); courseTotalObjectives.setHasChild(1);
@ -133,9 +136,9 @@ public class CoursesServiceImpl extends ServiceImpl<CoursesMapper, Courses> impl
@Override @Override
public PageDTO<CoursesVO> queryCourses(CourseQuery courseQuery) { public PageDTO<CoursesVO> queryCourses(CourseQuery courseQuery) {
String username = courseQuery.getUsername(); String username = courseQuery.getUsername();
if (username == null || username.isEmpty()) { // if (username == null || username.isEmpty()) {
throw new BusinessException(ErrorCode.USERNAME_NOT_EXIT); // throw new BusinessException(ErrorCode.USERNAME_NOT_EXIT);
} // }
UmsAdmin umsAdmin = umsAdminMapper.selectOne(new LambdaQueryWrapper<UmsAdmin>() UmsAdmin umsAdmin = umsAdminMapper.selectOne(new LambdaQueryWrapper<UmsAdmin>()
.eq(UmsAdmin::getUsername, username)); .eq(UmsAdmin::getUsername, username));
@ -220,9 +223,9 @@ public class CoursesServiceImpl extends ServiceImpl<CoursesMapper, Courses> impl
@Override @Override
public void deleteBatchByIds(String id) { public void deleteBatchByIds(String id) {
if(id==null){ // if(id==null){
throw new BusinessException(ErrorCode.PARAMS_ERROR,"课程id为空"); // throw new BusinessException(ErrorCode.PARAMS_ERROR,"课程id为空");
} // }
CourseObjectives courseObjectives= courseObjectivesMapper.selectOne CourseObjectives courseObjectives= courseObjectivesMapper.selectOne
(new LambdaQueryWrapper<CourseObjectives>().eq(CourseObjectives::getCourseId, id)); (new LambdaQueryWrapper<CourseObjectives>().eq(CourseObjectives::getCourseId, id));
if(courseObjectives == null){ if(courseObjectives == null){

@ -9,6 +9,8 @@ public enum CourseCode {
SI_ZHENG_EXIT(1, "思政目标存在"), SI_ZHENG_EXIT(1, "思政目标存在"),
KNOWS_EXIT(0, "存在关联的知识点"), KNOWS_EXIT(0, "存在关联的知识点"),
TEACHER_ROLE_ID(1, "教师的角色id是1"), TEACHER_ROLE_ID(1, "教师的角色id是1"),
TOTAL_OBJECTIVE_TYPE(5, "课程总目标"),
Total_EXIT(0, "课程总目标"), Total_EXIT(0, "课程总目标"),
COURSE_UODATING(1, "课程正在修改中"); COURSE_UODATING(1, "课程正在修改中");

Loading…
Cancel
Save