|
|
@@ -0,0 +1,165 @@
|
|
|
+package com.njuzr.eaibackend.service.impl;
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
+import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
|
|
|
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
+import com.njuzr.eaibackend.dto.AssignmentDTO;
|
|
|
+import com.njuzr.eaibackend.dto.AssignmentUpdateDTO;
|
|
|
+import com.njuzr.eaibackend.dto.ListeningAssignmentDTO;
|
|
|
+import com.njuzr.eaibackend.enums.AssignmentStatus;
|
|
|
+import com.njuzr.eaibackend.enums.Role;
|
|
|
+import com.njuzr.eaibackend.exception.MyException;
|
|
|
+import com.njuzr.eaibackend.mapper.AssignmentMapper;
|
|
|
+import com.njuzr.eaibackend.mapper.CourseMapper;
|
|
|
+import com.njuzr.eaibackend.mapper.ListeningAssignmentMapper;
|
|
|
+import com.njuzr.eaibackend.mapper.UserMapper;
|
|
|
+import com.njuzr.eaibackend.po.Assignment;
|
|
|
+import com.njuzr.eaibackend.po.Course;
|
|
|
+import com.njuzr.eaibackend.po.ListeningAssignment;
|
|
|
+import com.njuzr.eaibackend.po.MyUserDetails;
|
|
|
+import com.njuzr.eaibackend.service.ListeningAssignmentService;
|
|
|
+import com.njuzr.eaibackend.utils.ConvertUtil;
|
|
|
+import com.njuzr.eaibackend.vo.AssignmentVO;
|
|
|
+import com.njuzr.eaibackend.vo.ListeningAssignmentVo;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.beans.BeanUtils;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.http.HttpStatus;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+import java.util.Date;
|
|
|
+import java.util.Objects;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @author Liululin
|
|
|
+ * @date 2025/1/21 - 15:21
|
|
|
+ */
|
|
|
+@Service
|
|
|
+@Slf4j
|
|
|
+public class ListeningAssignmentServiceImpl extends ServiceImpl<ListeningAssignmentMapper, ListeningAssignment> implements ListeningAssignmentService {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ CourseMapper courseMapper;
|
|
|
+ @Autowired
|
|
|
+ AssignmentMapper assignmentMapper;
|
|
|
+ @Autowired
|
|
|
+ UserMapper userMapper;
|
|
|
+ @Override
|
|
|
+ public void createListeningAssignment(MyUserDetails user, Long courseId, ListeningAssignmentDTO listeningAssignmentDTO) {
|
|
|
+ Course course = courseMapper.selectById(courseId);
|
|
|
+ if (course == null)
|
|
|
+ throw MyException.create(HttpStatus.BAD_REQUEST, "课程不存在");
|
|
|
+
|
|
|
+ if (user.getRole() != Role.ADMIN && !course.getTeacherIds().contains(String.valueOf(user.getId())))
|
|
|
+ throw MyException.create(HttpStatus.BAD_REQUEST, "无权限创建");
|
|
|
+ Assignment assignment = new Assignment();
|
|
|
+ BeanUtils.copyProperties(listeningAssignmentDTO, assignment);
|
|
|
+ assignment.setCourseId(courseId);
|
|
|
+ assignment.setTeacherId(user.getId());
|
|
|
+ try {
|
|
|
+ assignmentMapper.insert(assignment);
|
|
|
+ Long assignmentId = assignment.getAssignmentId();
|
|
|
+ log.info("assignmentId:" + assignmentId);
|
|
|
+ ListeningAssignment listeningAssignment = new ListeningAssignment();
|
|
|
+ listeningAssignment.setAssignmentId(assignmentId);
|
|
|
+ BeanUtils.copyProperties(listeningAssignmentDTO, listeningAssignment);
|
|
|
+ if(listeningAssignmentDTO.getAudios() != null){
|
|
|
+ listeningAssignment.setAudios(ConvertUtil.listToString(listeningAssignmentDTO.getAudios()));
|
|
|
+ }
|
|
|
+ if(listeningAssignmentDTO.getQuestions() != null){
|
|
|
+ listeningAssignment.setQuestions(ConvertUtil.listToString(listeningAssignmentDTO.getQuestions()));
|
|
|
+ }
|
|
|
+ baseMapper.insert(listeningAssignment);
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error(e.getMessage());
|
|
|
+ throw MyException.create(HttpStatus.INTERNAL_SERVER_ERROR, "创建作业失败");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public ListeningAssignmentVo findAssignmentById(Long assignmentId) {
|
|
|
+ QueryWrapper<Assignment> wrapper = new QueryWrapper<>();
|
|
|
+ wrapper.eq("assignment_id", assignmentId);
|
|
|
+ Assignment assignment = assignmentMapper.selectOne(wrapper);
|
|
|
+ if (assignment == null)
|
|
|
+ throw MyException.create(HttpStatus.BAD_REQUEST, "作业不存在");
|
|
|
+ QueryWrapper<ListeningAssignment> listeningAssignmentQueryWrapper = new QueryWrapper<>();
|
|
|
+ listeningAssignmentQueryWrapper.eq("assignment_id", assignmentId);
|
|
|
+ ListeningAssignment listeningAssignment = baseMapper.selectOne(listeningAssignmentQueryWrapper);
|
|
|
+ return convertToVO(assignment, listeningAssignment);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public ListeningAssignmentVo updateAssignment(MyUserDetails user, Long assignmentId, ListeningAssignmentVo listeningAssignmentVo) {
|
|
|
+ Assignment assignment = assignmentMapper.selectById(assignmentId);
|
|
|
+ if (!Objects.equals(assignment.getTeacherId(), user.getId()))
|
|
|
+ throw MyException.create(HttpStatus.BAD_REQUEST, "无权限更新");
|
|
|
+ Assignment newAssignment = new Assignment();
|
|
|
+ BeanUtils.copyProperties(listeningAssignmentVo, newAssignment);
|
|
|
+ if(listeningAssignmentVo.getAttachments() != null){
|
|
|
+ newAssignment.setAttachments(ConvertUtil.listToString(listeningAssignmentVo.getAttachments()));
|
|
|
+ }
|
|
|
+ newAssignment.setAssignmentId(assignmentId);
|
|
|
+ ListeningAssignment listeningAssignment = new ListeningAssignment();
|
|
|
+ BeanUtils.copyProperties(listeningAssignmentVo, listeningAssignment);
|
|
|
+ listeningAssignment.setAssignmentId(assignmentId);
|
|
|
+ if(listeningAssignmentVo.getQuestions() != null){
|
|
|
+ listeningAssignment.setQuestions(ConvertUtil.listToString(listeningAssignmentVo.getQuestions()));
|
|
|
+ }
|
|
|
+ if (listeningAssignmentVo.getAudios() != null) {
|
|
|
+ listeningAssignment.setAudios(ConvertUtil.listToString(listeningAssignmentVo.getAudios()));
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ int code1 = assignmentMapper.updateById(newAssignment);
|
|
|
+ UpdateWrapper<ListeningAssignment> updateWrapper = new UpdateWrapper<>();
|
|
|
+ updateWrapper.eq("assignment_id",assignmentId);
|
|
|
+ int code2 = baseMapper.update(listeningAssignment, updateWrapper);
|
|
|
+ if ((code1 | code2) == 0)
|
|
|
+ throw MyException.create(HttpStatus.INTERNAL_SERVER_ERROR, "服务器更新失败");
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error(e.getMessage());
|
|
|
+ throw MyException.create(HttpStatus.INTERNAL_SERVER_ERROR, "服务器更新失败");
|
|
|
+ }
|
|
|
+ return listeningAssignmentVo;
|
|
|
+ }
|
|
|
+
|
|
|
+ private ListeningAssignmentVo convertToVO(Assignment assignment,ListeningAssignment listeningAssignment) {
|
|
|
+ ListeningAssignmentVo res = new ListeningAssignmentVo();
|
|
|
+ BeanUtils.copyProperties(assignment, res);
|
|
|
+ BeanUtils.copyProperties(listeningAssignment, res);
|
|
|
+ if (assignment.getAttachments() != null) {
|
|
|
+ res.setAttachments(ConvertUtil.stringToList(assignment.getAttachments(), String::valueOf));
|
|
|
+ }
|
|
|
+ if(listeningAssignment.getAudios() != null){
|
|
|
+ res.setAudios(ConvertUtil.stringToList(listeningAssignment.getAudios(), String::valueOf));
|
|
|
+ }
|
|
|
+ if(listeningAssignment.getQuestions() != null){
|
|
|
+ res.setQuestions(ConvertUtil.stringToList(listeningAssignment.getQuestions(), String::valueOf));
|
|
|
+ }
|
|
|
+ res.setStatus(calculateStatus(res.getStartTime(), res.getEndTime()));
|
|
|
+ // 组装老师姓名
|
|
|
+ res.setTeacherName(userMapper.selectNameById(assignment.getTeacherId()));
|
|
|
+ return res;
|
|
|
+ }
|
|
|
+
|
|
|
+ private Assignment convertToPO(AssignmentUpdateDTO assignmentDTO) {
|
|
|
+ Assignment assignment = new Assignment();
|
|
|
+ BeanUtils.copyProperties(assignmentDTO, assignment);
|
|
|
+ if (assignmentDTO.getAttachments() != null)
|
|
|
+ assignment.setAttachments(ConvertUtil.listToString(assignmentDTO.getAttachments()));
|
|
|
+ return assignment;
|
|
|
+ }
|
|
|
+
|
|
|
+ private AssignmentStatus calculateStatus(Date startTime, Date endTime) {
|
|
|
+ Date now = new Date();
|
|
|
+ if (now.before(startTime)) {
|
|
|
+ return AssignmentStatus.NOT_STARTED;
|
|
|
+ } else if (now.after(endTime)) {
|
|
|
+ return AssignmentStatus.FINISHED;
|
|
|
+ } else {
|
|
|
+ return AssignmentStatus.PROCEEDING;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+}
|