diff --git a/src/main/java/com/teaching/backend/controller/chapter/ChapterController2.java b/src/main/java/com/teaching/backend/controller/chapter/ChapterController2.java index 6f0229d..bc5b912 100644 --- a/src/main/java/com/teaching/backend/controller/chapter/ChapterController2.java +++ b/src/main/java/com/teaching/backend/controller/chapter/ChapterController2.java @@ -15,7 +15,9 @@ import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; import javax.servlet.http.HttpServletResponse; +import javax.validation.Valid; import java.io.IOException; +import java.math.BigDecimal; import java.time.LocalDateTime; import java.util.List; @@ -61,9 +63,13 @@ public BaseResponse> getCourseChapters2(@RequestParam String cours @ApiOperation("添加章节") @PostMapping("/add") - public BaseResponse addChapter2(@RequestBody ChapterDTO chapterDTO){ - chapterService.saveChapter(chapterDTO); - return ResultUtils.success("添加成功!!!!!!!!"); + public BaseResponse addChapter2(@Valid @RequestBody ChapterDTO chapterDTO) { + BigDecimal remainingHours = chapterService.saveChapter(chapterDTO); + if (remainingHours.compareTo(BigDecimal.ZERO) == 0) { + return ResultUtils.success("添加成功!所有学时分配完成"); + } else { + return ResultUtils.success("添加成功!还剩下" + remainingHours + "学时待分配"); + } } @ApiOperation("插入章节") diff --git a/src/main/java/com/teaching/backend/controller/thumb/SeCourseThumbController.java b/src/main/java/com/teaching/backend/controller/thumb/SeCourseThumbController.java index af19e27..414d475 100644 --- a/src/main/java/com/teaching/backend/controller/thumb/SeCourseThumbController.java +++ b/src/main/java/com/teaching/backend/controller/thumb/SeCourseThumbController.java @@ -1,65 +1,65 @@ -//package com.teaching.backend.controller.thumb; -// -//import com.teaching.backend.common.BaseResponse; -//import com.teaching.backend.common.ErrorCode; -//import com.teaching.backend.exception.BusinessException; -//import com.teaching.backend.model.dto.thumb.coursethumb.CourseThumbAddRequest; -//import com.teaching.backend.model.dto.thumb.coursethumb.CourseThumbAllRequest; -//import com.teaching.backend.service.thumb.SeCourseThumbService; -//import lombok.extern.slf4j.Slf4j; -//import org.apache.commons.lang3.StringUtils; -//import org.springframework.web.bind.annotation.PostMapping; -//import org.springframework.web.bind.annotation.RequestBody; -//import org.springframework.web.bind.annotation.RequestMapping; -//import org.springframework.web.bind.annotation.RestController; -// -//import javax.annotation.Resource; -// -///** -// * @Author:youhang -// * @Date:2024-05-30-20:22 -// * @Description: -// */ -//@RestController -//@RequestMapping("/api/course_thumb") -//@Slf4j -//public class SeCourseThumbController { -// -// @Resource -// private SeCourseThumbService seCourseThumbService; -// -// -// /** -// * 点赞/取消点赞 -// * -// * @param courseThumbAddRequest -// * @return result 执行情况 -// */ -// @PostMapping("/") -// // HttpServletRequest request 在一个fitter里面进行鉴权操作 -// public BaseResponse doThumb(@RequestBody CourseThumbAddRequest courseThumbAddRequest) { -// -// return seCourseThumbService.doCourseThumb(courseThumbAddRequest.getCourseId(), courseThumbAddRequest.getUserId()); -// -// } -// -// -// /** -// * 课程点赞总数 -// * -// * @param courseThumbAddRequest -// * @return result 课程点赞总数 -// */ -// @PostMapping("/count") -// // HttpServletRequest request 在一个fitter里面进行鉴权操作 -// public BaseResponse thumbCount(@RequestBody CourseThumbAddRequest courseThumbAddRequest) { -// if (StringUtils.isAnyBlank(courseThumbAddRequest.getCourseId())) { -// throw new BusinessException(ErrorCode.PARAMS_ERROR, "请求参数不能为空"); -// } -// -// return seCourseThumbService.thumbCount(courseThumbAddRequest.getCourseId()); -// -// } -// -// -//} +package com.teaching.backend.controller.thumb; + +import com.teaching.backend.common.BaseResponse; +import com.teaching.backend.common.ErrorCode; +import com.teaching.backend.exception.BusinessException; +import com.teaching.backend.model.dto.thumb.coursethumb.CourseThumbAddRequest; +import com.teaching.backend.model.dto.thumb.coursethumb.CourseThumbAllRequest; +import com.teaching.backend.service.thumb.SeCourseThumbService; +import lombok.extern.slf4j.Slf4j; +import org.apache.commons.lang3.StringUtils; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import javax.annotation.Resource; + +/** + * @Author:youhang + * @Date:2024-05-30-20:22 + * @Description: + */ +@RestController +@RequestMapping("/api/course_thumb") +@Slf4j +public class SeCourseThumbController { + + @Resource + private SeCourseThumbService seCourseThumbService; + + + /** + * 点赞/取消点赞 + * + * @param courseThumbAddRequest + * @return result 执行情况 + */ + @PostMapping("/") + // HttpServletRequest request 在一个fitter里面进行鉴权操作 + public BaseResponse doThumb(@RequestBody CourseThumbAddRequest courseThumbAddRequest) { + + return seCourseThumbService.doCourseThumb(courseThumbAddRequest.getCourseId(), courseThumbAddRequest.getUserId()); + + } + + + /** + * 课程点赞总数 + * + * @param courseThumbAddRequest + * @return result 课程点赞总数 + */ + @PostMapping("/count") + // HttpServletRequest request 在一个fitter里面进行鉴权操作 + public BaseResponse thumbCount(@RequestBody CourseThumbAddRequest courseThumbAddRequest) { + if (StringUtils.isAnyBlank(courseThumbAddRequest.getCourseId())) { + throw new BusinessException(ErrorCode.PARAMS_ERROR, "请求参数不能为空"); + } + + return seCourseThumbService.thumbCount(courseThumbAddRequest.getCourseId()); + + } + + +} diff --git a/src/main/java/com/teaching/backend/exception/GlobalExceptionHandler.java b/src/main/java/com/teaching/backend/exception/GlobalExceptionHandler.java index b52916b..56c5f3e 100644 --- a/src/main/java/com/teaching/backend/exception/GlobalExceptionHandler.java +++ b/src/main/java/com/teaching/backend/exception/GlobalExceptionHandler.java @@ -5,12 +5,26 @@ import com.teaching.backend.common.ErrorCode; import com.teaching.backend.common.ResultUtils; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; +import org.springframework.validation.FieldError; +import org.springframework.web.bind.MethodArgumentNotValidException; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.RestControllerAdvice; +import java.util.HashMap; +import java.util.Map; + @RestControllerAdvice public class GlobalExceptionHandler { - + @ExceptionHandler(MethodArgumentNotValidException.class) + public ResponseEntity> handleValidationExceptions(MethodArgumentNotValidException ex) { + Map errors = new HashMap<>(); + ex.getBindingResult().getAllErrors().forEach((error) -> { + String fieldName = ((FieldError) error).getField(); + String errorMessage = error.getDefaultMessage(); + errors.put(fieldName, errorMessage); + }); + return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(errors); + } @ExceptionHandler(BusinessException.class) public ResponseEntity handleBusinessException(BusinessException ex) { ErrorResponse errorResponse = new ErrorResponse(ex.getCode(), ex.getMessage()); diff --git a/src/main/java/com/teaching/backend/mapper/chapter/ChapterMapper.java b/src/main/java/com/teaching/backend/mapper/chapter/ChapterMapper.java index 1866cc8..09ace62 100644 --- a/src/main/java/com/teaching/backend/mapper/chapter/ChapterMapper.java +++ b/src/main/java/com/teaching/backend/mapper/chapter/ChapterMapper.java @@ -7,6 +7,7 @@ import io.lettuce.core.dynamic.annotation.Param; import org.apache.ibatis.annotations.Select; import org.apache.ibatis.annotations.Update; +import java.math.BigDecimal; import java.util.List; /** @@ -51,4 +52,7 @@ public interface ChapterMapper extends BaseMapper { Chapter selectByNameAndCourseId(@Param("name") String name, @Param("courseId") String courseId); @Select("select pid ,course_id,total_class_hours from chapter where pid = #{chapterId} and course_id = #{courseId} ") List selectByIdAndPid(@Param("chapterId") Long chapterId, @Param("courseId")String courseId); + + @Select("select total_class_hours from chapter where id=#{pid}") + BigDecimal getChapterHours(@Param("pid") Long pid); } diff --git a/src/main/java/com/teaching/backend/mapper/courses/CoursesMapper.java b/src/main/java/com/teaching/backend/mapper/courses/CoursesMapper.java index cf5b179..824e963 100644 --- a/src/main/java/com/teaching/backend/mapper/courses/CoursesMapper.java +++ b/src/main/java/com/teaching/backend/mapper/courses/CoursesMapper.java @@ -3,12 +3,14 @@ package com.teaching.backend.mapper.courses; import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; +import com.teaching.backend.model.entity.chapter.Chapter; import com.teaching.backend.model.entity.courses.Courses; import com.teaching.backend.model.vo.courses.CoursesVO; import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Param; import org.apache.ibatis.annotations.Select; +import java.math.BigDecimal; import java.util.List; /** @@ -24,5 +26,7 @@ public interface CoursesMapper extends BaseMapper { @Select("SELECT * FROM courses LIMIT #{start}, #{pageSize}") List getItemsByPage(@Param("start") int start, @Param("pageSize") int pageSize); + @Select("select classhours from courses where id=#{id}") + BigDecimal getClassHoursById(@Param("id") String id); } diff --git a/src/main/java/com/teaching/backend/model/dto/chapter/ChapterDTO.java b/src/main/java/com/teaching/backend/model/dto/chapter/ChapterDTO.java index da34698..e05365b 100644 --- a/src/main/java/com/teaching/backend/model/dto/chapter/ChapterDTO.java +++ b/src/main/java/com/teaching/backend/model/dto/chapter/ChapterDTO.java @@ -7,6 +7,9 @@ import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; + +import javax.validation.constraints.Digits; +import javax.validation.constraints.Pattern; import java.io.Serializable; import java.time.LocalDateTime; @@ -25,6 +28,10 @@ import java.time.LocalDateTime; public class ChapterDTO implements Serializable { private static final long serialVersionUID = 1L; + @Digits(integer = 10, fraction = 1, message = "总学时只能保留一位小数") + @ApiModelProperty(value = "总学时") + private double totalClassHours; + @ApiModelProperty(value = "创建人") private String createBy; @@ -61,8 +68,7 @@ public class ChapterDTO implements Serializable { @ApiModelProperty(value = "课程目标") private String courseObjectivesId; - @ApiModelProperty(value = "总学时") - private double totalClassHours; + @ApiModelProperty(value = "要求") private String requirement; diff --git a/src/main/java/com/teaching/backend/model/dto/chapter/ChapterExcelDTO.java b/src/main/java/com/teaching/backend/model/dto/chapter/ChapterExcelDTO.java index f74249b..ddbb469 100644 --- a/src/main/java/com/teaching/backend/model/dto/chapter/ChapterExcelDTO.java +++ b/src/main/java/com/teaching/backend/model/dto/chapter/ChapterExcelDTO.java @@ -9,6 +9,8 @@ import lombok.NoArgsConstructor; import java.io.Serializable; import java.time.LocalDateTime; +import java.util.ArrayList; +import java.util.List; /** * ClassName: ChapterExcelDto @@ -29,6 +31,7 @@ public class ChapterExcelDTO implements Serializable { private String createBy; + @ApiModelProperty(value = "创建日期") @JsonFormat(pattern = "yyyy/M/d HH:mm:ss") private LocalDateTime createTime; @@ -94,7 +97,10 @@ public class ChapterExcelDTO implements Serializable { @TableField(exist = false) private Long ExcelId; + public void setIsSection(boolean b) { this.isSection=b; } + // 添加获取和设置方法 + } \ No newline at end of file diff --git a/src/main/java/com/teaching/backend/model/entity/chapter/Chapter.java b/src/main/java/com/teaching/backend/model/entity/chapter/Chapter.java index 5f2e46d..9a2f6f6 100644 --- a/src/main/java/com/teaching/backend/model/entity/chapter/Chapter.java +++ b/src/main/java/com/teaching/backend/model/entity/chapter/Chapter.java @@ -11,6 +11,7 @@ import lombok.Data; import lombok.EqualsAndHashCode; import lombok.experimental.Accessors; +import javax.validation.constraints.Digits; import java.io.Serializable; import java.time.LocalDateTime; import java.util.List; @@ -70,6 +71,9 @@ public class Chapter implements Serializable { private String courseObjectivesId; @ApiModelProperty(value = "总学时") + //使用参数合法性校验 + @Digits(integer = 10, fraction = 1, message = "总学时只能保留一位小数") +// package com.teaching.backend.exception;需要在这个里面添加异常处理 private double totalClassHours; @ApiModelProperty(value = "要求") diff --git a/src/main/java/com/teaching/backend/service/chapter/IChapterService.java b/src/main/java/com/teaching/backend/service/chapter/IChapterService.java index 875a4aa..491d60c 100644 --- a/src/main/java/com/teaching/backend/service/chapter/IChapterService.java +++ b/src/main/java/com/teaching/backend/service/chapter/IChapterService.java @@ -10,6 +10,7 @@ import com.teaching.backend.model.vo.chapter.ChapterVo; import javax.servlet.http.HttpServletResponse; import java.io.IOException; +import java.math.BigDecimal; import java.util.LinkedList; import java.util.List; @@ -33,7 +34,7 @@ public interface IChapterService extends IService { List getChapterTree(String courseId); - void saveChapter(ChapterDTO chapterDTO); + BigDecimal saveChapter(ChapterDTO chapterDTO); diff --git a/src/main/java/com/teaching/backend/service/favour/SeCourseFavourService.java b/src/main/java/com/teaching/backend/service/favour/SeCourseFavourService.java index a8d1076..c748c7f 100644 --- a/src/main/java/com/teaching/backend/service/favour/SeCourseFavourService.java +++ b/src/main/java/com/teaching/backend/service/favour/SeCourseFavourService.java @@ -8,6 +8,7 @@ import com.teaching.backend.model.entity.courses.Courses; import com.teaching.backend.model.entity.favour.SeCourseFavour; import com.teaching.backend.model.vo.courses.CoursesVO; import com.teaching.backend.model.vo.favour.CourseFavourDetailVO; +import org.springframework.stereotype.Service; import java.util.List; @@ -16,6 +17,7 @@ import java.util.List; * @Date:2024-05-30-20:22 * @Description: */ + public interface SeCourseFavourService extends IService { /** diff --git a/src/main/java/com/teaching/backend/service/impl/chapter/ChapterExcelServiceImpl.java b/src/main/java/com/teaching/backend/service/impl/chapter/ChapterExcelServiceImpl.java index a1a7af4..7f1d6d1 100644 --- a/src/main/java/com/teaching/backend/service/impl/chapter/ChapterExcelServiceImpl.java +++ b/src/main/java/com/teaching/backend/service/impl/chapter/ChapterExcelServiceImpl.java @@ -26,7 +26,9 @@ public class ChapterExcelServiceImpl { public List uploadExcel(MultipartFile file, String courseId) { List validationErrors = new ArrayList<>(); - try (InputStream inputStream = file.getInputStream()) { + try ( + InputStream inputStream = file.getInputStream() + ) { validationErrors = excelParser.parse(inputStream, courseId); } catch (Exception e) { validationErrors.add("系统错误: " + e.getMessage()); diff --git a/src/main/java/com/teaching/backend/service/impl/chapter/ChapterServiceImpl.java b/src/main/java/com/teaching/backend/service/impl/chapter/ChapterServiceImpl.java index d330a6d..8ce1301 100644 --- a/src/main/java/com/teaching/backend/service/impl/chapter/ChapterServiceImpl.java +++ b/src/main/java/com/teaching/backend/service/impl/chapter/ChapterServiceImpl.java @@ -12,6 +12,7 @@ import com.teaching.backend.exception.BusinessException; import com.teaching.backend.mapper.Knowtemp.KnowtmpMapper; import com.teaching.backend.mapper.chapter.ChapterMapper; import com.teaching.backend.mapper.chapter.TemporaryChapterMapper; +import com.teaching.backend.mapper.courses.CoursesMapper; import com.teaching.backend.model.dto.chapter.ChapterDTO; import com.teaching.backend.model.dto.chapter.ChapterExcelDTO; @@ -29,6 +30,8 @@ import org.springframework.transaction.annotation.Transactional; import javax.servlet.http.HttpServletResponse; import java.io.IOException; +import java.math.BigDecimal; +import java.math.RoundingMode; import java.time.LocalDateTime; import java.util.*; import java.util.stream.Collectors; @@ -44,6 +47,8 @@ public class ChapterServiceImpl extends ServiceImpl impl @Autowired private ChapterMapper chapterMapper; + @Autowired + private CoursesMapper coursesMapper; @Autowired private TemporaryChapterMapper temporaryChapterMapper; @Autowired @@ -228,8 +233,7 @@ public class ChapterServiceImpl extends ServiceImpl impl } @Override - public void saveChapter(ChapterDTO chapterDTO) { - + public BigDecimal saveChapter(ChapterDTO chapterDTO) { Chapter chapter = new Chapter(); try { BeanUtils.copyProperties(chapterDTO, chapter); @@ -237,48 +241,97 @@ public class ChapterServiceImpl extends ServiceImpl impl throw new RuntimeException("复制数据出错", e); } Long pid = chapter.getPid(); + + BigDecimal remainingHours = BigDecimal.ZERO; // 初始化剩余学时 + if (pid == null) { - chapter.setPid(0L); - LambdaQueryWrapper maxSortWrapper = new LambdaQueryWrapper<>(); - maxSortWrapper.eq(Chapter::getPid, 0).orderByDesc(Chapter::getOrderNum).last("limit 1"); - Chapter maxSortChapter = this.getOne(maxSortWrapper); - double newSort = 1; - if (maxSortChapter != null) { - newSort = maxSortChapter.getOrderNum() + 1; - } - chapter.setOrderNum(newSort); - chapter.setCreateTime(LocalDateTime.now()); - chapter.setUpdateTime(LocalDateTime.now()); + BigDecimal classHours = coursesMapper.getClassHoursById(chapterDTO.getCourseId()); + LambdaQueryWrapper lambdaQueryWrapper = new LambdaQueryWrapper<>(); + lambdaQueryWrapper.eq(Chapter::getPid, 0) + .eq(Chapter::getCourseId, chapterDTO.getCourseId()); - this.save(chapter); - } else { + List chapterList = chapterMapper.selectList(lambdaQueryWrapper); - Chapter parentChapter = this.getById(pid); - if (parentChapter != null) { + BigDecimal totalChapterHours = chapterList.stream() + .map(Chapter::getTotalClassHours) + .map(BigDecimal::new) + .reduce(BigDecimal.ZERO, BigDecimal::add); - LambdaQueryWrapper maxSortWrapper = new LambdaQueryWrapper<>(); - maxSortWrapper.eq(Chapter::getPid, pid).orderByDesc(Chapter::getOrderNum).last("limit 1"); + remainingHours = classHours.subtract(totalChapterHours).setScale(1, RoundingMode.HALF_UP); - parentChapter.setUpdateTime(LocalDateTime.now()); + + + if (totalChapterHours.compareTo(classHours) < 0) { + chapter.setPid(0L); + LambdaQueryWrapper maxSortWrapper = new LambdaQueryWrapper<>(); + maxSortWrapper.eq(Chapter::getPid, 0).orderByDesc(Chapter::getOrderNum).last("limit 1"); Chapter maxSortChapter = this.getOne(maxSortWrapper); double newSort = 1; if (maxSortChapter != null) { newSort = maxSortChapter.getOrderNum() + 1; } - chapter.setOrderNum(newSort); - - this.updateById(parentChapter); - chapter.setCreateTime(LocalDateTime.now()); chapter.setUpdateTime(LocalDateTime.now()); this.save(chapter); } else { - throw new BusinessException(ErrorCode.PARAMS_ERROR, "未找到父章节,ID 为: " + pid); + throw new BusinessException(ErrorCode.PARAMS_ERROR, "当前课程的总学时已经分配完成,请先修改章节学时,或者课程总学时"); + } + } else { + + BigDecimal chapterHours = chapterMapper.getChapterHours(pid); + + + LambdaQueryWrapper chapterLambdaQueryWrapper = new LambdaQueryWrapper<>(); + chapterLambdaQueryWrapper.eq(Chapter::getPid, pid) + .eq(Chapter::getCourseId, chapterDTO.getCourseId()); + + List chapterList = chapterMapper.selectList(chapterLambdaQueryWrapper); + + BigDecimal hours = chapterList.stream() + .map(Chapter::getTotalClassHours) + .map(BigDecimal::new) + .reduce(BigDecimal.ZERO, BigDecimal::add); + + BigDecimal totalClassHours = new BigDecimal(chapterDTO.getTotalClassHours()).setScale(1, RoundingMode.HALF_UP); + + + + + remainingHours = chapterHours.subtract(hours).setScale(1, RoundingMode.HALF_UP); + + + + if (remainingHours.compareTo(totalClassHours) >= 0) { + Chapter parentChapter = this.getById(pid); + if (parentChapter != null) { + LambdaQueryWrapper maxSortWrapper = new LambdaQueryWrapper<>(); + maxSortWrapper.eq(Chapter::getPid, pid).orderByDesc(Chapter::getOrderNum).last("limit 1"); + parentChapter.setUpdateTime(LocalDateTime.now()); + Chapter maxSortChapter = this.getOne(maxSortWrapper); + double newSort = 1; + if (maxSortChapter != null) { + newSort = maxSortChapter.getOrderNum() + 1; + } + chapter.setOrderNum(newSort); + this.updateById(parentChapter); + chapter.setCreateTime(LocalDateTime.now()); + chapter.setUpdateTime(LocalDateTime.now()); + this.save(chapter); + remainingHours = remainingHours.subtract(totalClassHours).setScale(1, RoundingMode.HALF_UP); + } else { + throw new BusinessException(ErrorCode.PARAMS_ERROR, "未找到父章节,ID 为: " + pid); + } + } else { + throw new BusinessException(ErrorCode.PARAMS_ERROR, "所添加的章节学时大于其父章节剩余学时,所添加学时为:" + totalClassHours + " 父章节学时为:" + chapterHours + + " 当前所剩学时为:" + remainingHours); } } + + return remainingHours; } + @Override public Long saveExcelChapter2(ChapterDTO chapterDTO, boolean isParent) { Chapter chapter = new Chapter(); @@ -477,6 +530,7 @@ public class ChapterServiceImpl extends ServiceImpl impl @Transactional(rollbackFor = Exception.class) public void moveDataFromTemporaryToFinal() { List temporaryChapters = temporaryChapterMapper.selectAll(); + Map tempIdToFinalIdMap = new HashMap<>(); for (TemporaryChapter tempChapter : temporaryChapters) { @@ -537,7 +591,7 @@ public class ChapterServiceImpl extends ServiceImpl impl temporaryChapter.setPid(parentChapter.getId()); setChapterOrder(temporaryChapter, pid); } else { - throw new BusinessException(ErrorCode.PARAMS_ERROR, "未找到父章节,ID 为: " + pid); + throw new BusinessException(ErrorCode.PARAMS_ERROR, "在这里报的错 未找到父章节,ID 为: " + pid); } } // 执行 diff --git a/src/main/java/com/teaching/backend/service/impl/thumb/SeCourseThumbServiceImpl.java b/src/main/java/com/teaching/backend/service/impl/thumb/SeCourseThumbServiceImpl.java index 0edff1b..23721a0 100644 --- a/src/main/java/com/teaching/backend/service/impl/thumb/SeCourseThumbServiceImpl.java +++ b/src/main/java/com/teaching/backend/service/impl/thumb/SeCourseThumbServiceImpl.java @@ -1,108 +1,100 @@ -//package com.teaching.backend.service.impl.thumb; -// -//import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; -//import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; -//import com.teaching.backend.common.BaseResponse; -//import com.teaching.backend.common.ErrorCode; -//import com.teaching.backend.common.ResultUtils; -//import com.teaching.backend.exception.BusinessException; -//import com.teaching.backend.mapper.courses.CoursesMapper; -//import com.teaching.backend.mapper.thumb.SeCourseThumbMapper; -//import com.teaching.backend.model.entity.courses.Courses; -//import com.teaching.backend.model.entity.thumb.SeCourseThumb; -//import com.teaching.backend.model.entity.thumb.SeKnowThumb; -//import com.teaching.backend.model.entity.thumb.SeResourceThumb; -//import com.teaching.backend.service.thumb.SeCourseThumbService; -//import com.teaching.backend.service.thumb.SeKnowThumbService; -//import com.teaching.backend.service.thumb.SeResourceThumbService; -//import org.springframework.beans.factory.annotation.Autowired; -//import org.springframework.stereotype.Service; -// -//import javax.annotation.Resource; -//import java.util.concurrent.locks.Lock; -//import java.util.concurrent.locks.ReentrantLock; -// -// -///** -// * @Author:youhang -// * @Date:2024-05-30-20:23 -// * @Description: -// */ -//@Service -//public class SeCourseThumbServiceImpl extends ServiceImpl implements SeCourseThumbService { -// -// -// @Resource -// private SeResourceThumbService seResourceThumbService; -// -// @Resource -// SeKnowThumbService seKnowThumbService; -// -// -// @Autowired -// CoursesMapper coursesMapper; -// -// @Override -// public BaseResponse doCourseThumb(String courseId, String userId) { -// -// //从数据库中校验是否存在courseid -// Courses courses = coursesMapper.selectById(courseId); -// boolean result = false; -// if(courses == null){ -// return ResultUtils.error(ErrorCode.PARAMS_COURSE_NOTEXISTS); -// } -// -// // 先查询数据库记录,该用户是否点赞 -// SeCourseThumb seCourseThumb = new SeCourseThumb(); -// seCourseThumb.setCourseId(courseId); -// seCourseThumb.setUserId(userId); -// QueryWrapper thumbQueryWrapper = new QueryWrapper<>(seCourseThumb); -// SeCourseThumb oldSeCourseThumb = this.getOne(thumbQueryWrapper); -// //已点赞 -// if (oldSeCourseThumb != null) { -// // 取消点赞 删除记录 -// result = this.remove(thumbQueryWrapper); -// if (result) { -// System.out.println("取消点赞成功"); -// return ResultUtils.success(result); -// } else { -// throw new BusinessException(ErrorCode.SYSTEM_ERROR); -// } -// } else { -// // 每个用户串行点赞 -// // 锁必须要包裹住事务方法 -// Lock lock = new ReentrantLock(); -// lock.lock(); -// try { -// result = this.save(seCourseThumb); -// } catch (Exception e) { -// throw new BusinessException(ErrorCode.OPERATION_ERROR); -// } finally { -// lock.unlock(); -// System.out.println("点赞成功"); -// return ResultUtils.success(result); -// } -// } -// } -// -// @Override -// public BaseResponse thumbCount(String courseId) { -// -// //从数据库中校验是否存在courseid -// Courses courses = coursesMapper.selectById(courseId); -// if(courses == null){ -// return ResultUtils.error(ErrorCode.PARAMS_COURSE_NOTEXISTS); -// } -// -// QueryWrapper thumbQueryWrapper = new QueryWrapper<>(); -// thumbQueryWrapper.eq("course_id", courseId); -// long count = this.count(thumbQueryWrapper); -// if (count == 0) { -// throw new BusinessException(ErrorCode.SYSTEM_ERROR, "查询参数不存在"); -// } else { -// return ResultUtils.success(count); -// } -// } -// -// -//} +package com.teaching.backend.service.impl.thumb; + +import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; +import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; +import com.teaching.backend.common.BaseResponse; +import com.teaching.backend.common.ErrorCode; +import com.teaching.backend.common.ResultUtils; +import com.teaching.backend.exception.BusinessException; +import com.teaching.backend.mapper.courses.CoursesMapper; +import com.teaching.backend.mapper.thumb.SeCourseThumbMapper; +import com.teaching.backend.model.entity.courses.Courses; +import com.teaching.backend.model.entity.thumb.SeCourseThumb; +import com.teaching.backend.model.entity.thumb.SeKnowThumb; +import com.teaching.backend.model.entity.thumb.SeResourceThumb; +import com.teaching.backend.service.thumb.SeCourseThumbService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import javax.annotation.Resource; +import java.util.concurrent.locks.Lock; +import java.util.concurrent.locks.ReentrantLock; + + +/** + * @Author:youhang + * @Date:2024-05-30-20:23 + * @Description: + */ +@Service +public class SeCourseThumbServiceImpl extends ServiceImpl implements SeCourseThumbService { + + + + @Autowired + CoursesMapper coursesMapper; + + @Override + public BaseResponse doCourseThumb(String courseId, String userId) { + + //从数据库中校验是否存在courseid + Courses courses = coursesMapper.selectById(courseId); + boolean result = false; + if(courses == null){ + return ResultUtils.error(ErrorCode.PARAMS_COURSE_NOTEXISTS); + } + + // 先查询数据库记录,该用户是否点赞 + SeCourseThumb seCourseThumb = new SeCourseThumb(); + seCourseThumb.setCourseId(courseId); + seCourseThumb.setUserId(userId); + QueryWrapper thumbQueryWrapper = new QueryWrapper<>(seCourseThumb); + SeCourseThumb oldSeCourseThumb = this.getOne(thumbQueryWrapper); + //已点赞 + if (oldSeCourseThumb != null) { + // 取消点赞 删除记录 + result = this.remove(thumbQueryWrapper); + if (result) { + System.out.println("取消点赞成功"); + return ResultUtils.success(result); + } else { + throw new BusinessException(ErrorCode.SYSTEM_ERROR); + } + } else { + // 每个用户串行点赞 + // 锁必须要包裹住事务方法 + Lock lock = new ReentrantLock(); + lock.lock(); + try { + result = this.save(seCourseThumb); + } catch (Exception e) { + throw new BusinessException(ErrorCode.OPERATION_ERROR); + } finally { + lock.unlock(); + System.out.println("点赞成功"); + return ResultUtils.success(result); + } + } + } + + @Override + public BaseResponse thumbCount(String courseId) { + + //从数据库中校验是否存在courseid + Courses courses = coursesMapper.selectById(courseId); + if(courses == null){ + return ResultUtils.error(ErrorCode.PARAMS_COURSE_NOTEXISTS); + } + + QueryWrapper thumbQueryWrapper = new QueryWrapper<>(); + thumbQueryWrapper.eq("course_id", courseId); + long count = this.count(thumbQueryWrapper); + if (count == 0) { + throw new BusinessException(ErrorCode.SYSTEM_ERROR, "查询参数不存在"); + } else { + return ResultUtils.success(count); + } + } + + +} diff --git a/src/main/java/com/teaching/backend/service/thumb/SeCourseThumbService.java b/src/main/java/com/teaching/backend/service/thumb/SeCourseThumbService.java index bfeaaf5..a42e93f 100644 --- a/src/main/java/com/teaching/backend/service/thumb/SeCourseThumbService.java +++ b/src/main/java/com/teaching/backend/service/thumb/SeCourseThumbService.java @@ -1,33 +1,33 @@ -//package com.teaching.backend.service.thumb; -// -//import com.baomidou.mybatisplus.extension.service.IService; -//import com.teaching.backend.common.BaseResponse; -//import com.teaching.backend.model.entity.thumb.SeCourseThumb; -// -///** -// * @Author:youhang -// * @Date:2024-05-30-20:22 -// * @Description: -// */ -//public interface SeCourseThumbService extends IService { -// -// /** -// * 点赞 -// * -// * @param courseId -// * @param userId -// * @return -// */ -// BaseResponse doCourseThumb(String courseId, String userId); -// -// -// /** -// * 点赞总数 -// * -// * @param courseId -// * @return -// */ -// BaseResponse thumbCount(String courseId); -// -// -//} +package com.teaching.backend.service.thumb; + +import com.baomidou.mybatisplus.extension.service.IService; +import com.teaching.backend.common.BaseResponse; +import com.teaching.backend.model.entity.thumb.SeCourseThumb; + +/** + * @Author:youhang + * @Date:2024-05-30-20:22 + * @Description: + */ +public interface SeCourseThumbService extends IService { + + /** + * 点赞 + * + * @param courseId + * @param userId + * @return + */ + BaseResponse doCourseThumb(String courseId, String userId); + + + /** + * 点赞总数 + * + * @param courseId + * @return + */ + BaseResponse thumbCount(String courseId); + + +} diff --git a/src/main/java/com/teaching/backend/utils/Chapter/ExcelParser.java b/src/main/java/com/teaching/backend/utils/Chapter/ExcelParser.java index 612e073..27ed7f3 100644 --- a/src/main/java/com/teaching/backend/utils/Chapter/ExcelParser.java +++ b/src/main/java/com/teaching/backend/utils/Chapter/ExcelParser.java @@ -13,13 +13,10 @@ package com.teaching.backend.utils.Chapter; import com.teaching.backend.mapper.chapter.TemporaryChapterMapper; import com.teaching.backend.service.chapter.IChapterService; import org.apache.poi.openxml4j.opc.OPCPackage; -import org.apache.poi.ss.usermodel.Sheet; -import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.xssf.eventusermodel.XSSFReader; import org.apache.poi.xssf.eventusermodel.XSSFSheetXMLHandler; import org.apache.poi.xssf.model.SharedStringsTable; import org.apache.poi.xssf.model.StylesTable; -import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import org.xml.sax.InputSource; diff --git a/src/main/java/com/teaching/backend/utils/Chapter/SheetHandler.java b/src/main/java/com/teaching/backend/utils/Chapter/SheetHandler.java index 9b1e47a..f9c7bbe 100644 --- a/src/main/java/com/teaching/backend/utils/Chapter/SheetHandler.java +++ b/src/main/java/com/teaching/backend/utils/Chapter/SheetHandler.java @@ -9,6 +9,7 @@ import org.apache.poi.xssf.eventusermodel.XSSFSheetXMLHandler; import org.apache.poi.xssf.usermodel.XSSFComment; import org.springframework.transaction.annotation.Transactional; +import java.math.BigDecimal; import java.util.*; /** @@ -25,7 +26,6 @@ public class SheetHandler implements XSSFSheetXMLHandler.SheetContentsHandler { private Long currentChapterPid; private final TemporaryChapterMapper temporaryChapterMapper; - private Double chapterHour; private final IChapterService chapterService; private final String courseId; private ChapterExcelDTO chapterDTO; @@ -34,6 +34,9 @@ public class SheetHandler implements XSSFSheetXMLHandler.SheetContentsHandler { private boolean hasErrors = false; private int totalRows; + // 用于存储章节的学时总和 + private BigDecimal allHours = BigDecimal.ZERO; + private BigDecimal expectedTotalClassHours=BigDecimal.ZERO; public SheetHandler(TemporaryChapterMapper temporaryChapterMapper, IChapterService chapterService, String courseId,int totalRows) { @@ -48,6 +51,8 @@ public class SheetHandler implements XSSFSheetXMLHandler.SheetContentsHandler { if (rowIndex < totalRows - 1) { if (rowIndex == 0 || rowIndex == 1) { chapterDTO = null; + allHours = BigDecimal.ZERO; // 重置学时总和 + expectedTotalClassHours = BigDecimal.ZERO; // 重置预期学时 } else { chapterDTO = new ChapterExcelDTO(); chapterDTO.setCourseId(courseId); @@ -60,6 +65,9 @@ public class SheetHandler implements XSSFSheetXMLHandler.SheetContentsHandler { @Transactional(rollbackFor = Exception.class) public List finalizeProcess() { + // 校验学时 + validateTotalClassHours(); + if (hasErrors) { return validationErrors; } @@ -68,6 +76,16 @@ public class SheetHandler implements XSSFSheetXMLHandler.SheetContentsHandler { return Collections.emptyList(); } + private void validateTotalClassHours() { + + if (allHours.subtract(expectedTotalClassHours).abs().compareTo(BigDecimal.valueOf(0.01)) > 0) { + validationErrors.add("学时总和不匹配: 实际总和为 " + allHours + ", 预期为 " + expectedTotalClassHours); + temporaryChapterMapper.deleteAll(); + hasErrors = true; + + } + } + private void saveToTemporaryTable(ChapterExcelDTO chapterDTO) { Long pid = chapterDTO.getParentExcelId(); if (pid != null && pid != 0) { @@ -100,7 +118,6 @@ public class SheetHandler implements XSSFSheetXMLHandler.SheetContentsHandler { if (chapterDTO.isSection()) { if (currentChapterPid != null) { - chapterDTO.setTotalClassHours(chapterHour); chapterDTO.setPid(currentChapterPid); saveToTemporaryTable(chapterDTO); } else { @@ -127,7 +144,12 @@ public class SheetHandler implements XSSFSheetXMLHandler.SheetContentsHandler { case "C": if (!chapterDTO.isSection()) { - chapterDTO.setTotalClassHours(Double.parseDouble(formattedValue)); + // 当前章节的学时 + BigDecimal classHours = new BigDecimal(formattedValue); + chapterDTO.setTotalClassHours(classHours.doubleValue()); + + expectedTotalClassHours = expectedTotalClassHours.add(classHours); // 累加预期学时 + saveToTemporaryTable(chapterDTO); currentChapterPid = excelIdToDatabaseIdMap.get(chapterDTO.getExcelId()); } @@ -146,16 +168,18 @@ public class SheetHandler implements XSSFSheetXMLHandler.SheetContentsHandler { case "F": if (chapterDTO.isSection()) { - chapterDTO.setTotalClassHours(Double.parseDouble(formattedValue)); - chapterHour = chapterDTO.getTotalClassHours(); + // 子章节的学时 + BigDecimal sectionHours = new BigDecimal(formattedValue); + chapterDTO.setTotalClassHours(sectionHours.doubleValue()); + + // 只对当前章节的学时进行累加 + allHours = allHours.add(sectionHours); + System.out.println("这是累加的学时" + allHours); } break; - } } - - } } \ No newline at end of file