yl-backend/src/main/java/com/guwan/backend/service/impl/CourseServiceImpl.java

195 lines
7.1 KiB
Java

package com.guwan.backend.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guwan.backend.factory.SectionVOFactory;
import com.guwan.backend.mapper.*;
import com.guwan.backend.mapper.CourseTypeMapper;
import com.guwan.backend.pojo.entity.*;
import com.guwan.backend.pojo.response.courseDetail.BaseSectionVO;
import com.guwan.backend.pojo.response.courseDetail.ChapterVO;
import com.guwan.backend.pojo.response.courseDetail.CourseDetailVO;
import com.guwan.backend.pojo.response.courseDetail.ReviewVO;
import com.guwan.backend.service.CourseService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;
/**
* @author 12455
* @description 针对表【courses(课程表)】的数据库操作Service实现
* @createDate 2025-03-13 22:45:19
*/
@Service
@RequiredArgsConstructor
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course>
implements CourseService {
private final CourseMapper courseMapper;
private final TeacherMapper teacherMapper;
private final RatingDistributionMapper ratingDistributionMapper;
private final ChapterMapper chapterMapper;
private final SectionMapper sectionMapper;
private final SectionVOFactory sectionVOFactory;
private final ReviewMapper reviewMapper;
private final UserMapper userMapper;
private final CourseLevelMapper courseLevelMapper;
private final CourseTypeMapper courseTypeMapper;
private static final Map<String, Integer> courseStudentCounts = new ConcurrentHashMap<>();
@Override
public CourseDetailVO getCourseDetail(String courseId) {
//首选查询course表
Course course = courseMapper.selectOne(new LambdaQueryWrapper<Course>().eq(Course::getId, courseId));
if (course == null) {
return null;
}
System.out.println("course = " + course);
CourseDetailVO courseDetailVO = new CourseDetailVO();
courseDetailVO.setTitle(course.getTitle());
courseDetailVO.setDescription(course.getDescription());
courseDetailVO.setLevelName(courseLevelMapper.selectById(course.getLevelId()).getChineseName());
courseDetailVO.setTypeName(courseTypeMapper.selectById(course.getTypeId()).getChineseName());
// Course(id=1, title=黑马程序员匠心之作|C++教程从0到1入门编程, description=null, categoryId=2, categoryName=null, teacherId=1, coverImg=http://localhost:9000/photo/cover/c315f738-71aa-4329-8c7c-9092284c33c3.png, price=0.00, coursrTeacherId=null, rating=0.0, ratingCount=null, studentCount=0, videoCount=null, documentCount=null, totalDuration=null, createdAt=null, updatedAt=null)
var teacherId = course.getTeacherId();
Teacher teacher = null;
if (teacherId != null) {
teacher = teacherMapper.selectById(teacherId);
}
if (teacher != null) {
courseDetailVO.setTeacher(teacher.getName());
courseDetailVO.setTeacherAvatar(teacher.getAvatar());
}
courseDetailVO.setPrice(course.getPrice().doubleValue());
List<RatingDistribution> ratingDistributions = ratingDistributionMapper.selectList(new LambdaQueryWrapper<RatingDistribution>().eq(RatingDistribution::getCourseId, courseId));
ratingDistributions.sort(Comparator.comparingInt(RatingDistribution::getRatingLevel).reversed());
int ratingCount = 0;
int totalCount = 0;
for (RatingDistribution ratingDistribution : ratingDistributions) {
ratingCount += ratingDistribution.getCount();
totalCount += ratingDistribution.getCount() * ratingDistribution.getRatingLevel();
}
courseDetailVO.setRating((double) totalCount / ratingCount);
courseDetailVO.setRatingCount(ratingCount);
courseDetailVO.setStudentCount(0);
List<Chapter> chapters = chapterMapper.selectList(new LambdaQueryWrapper<Chapter>().eq(Chapter::getCourseId, courseId));
List<String> chapterIds = chapters.stream().map(Chapter::getId).toList();
List<Section> sections = sectionMapper.selectList(new LambdaQueryWrapper<Section>().in(Section::getChapterId, chapterIds));
int videoCount = 0;
int docCount = 0;
int totalTimeOfMinute = 0;
for (Section section : sections) {
if (Objects.equals("video", section.getType())){
videoCount ++;
totalTimeOfMinute += section.getDuration();
}else {
docCount ++;
}
}
courseDetailVO.setVideoCount(videoCount);
courseDetailVO.setDocCount(docCount);
courseDetailVO.setTotalDuration(totalTimeOfMinute / 60);
courseDetailVO.setEnrolled(false);
courseDetailVO.setCoverImg(course.getCoverImg());
List<ChapterVO> chapterVOList = new ArrayList<>();
//chapter 变 vo
chapters.sort(Comparator.comparingInt(Chapter::getOrderNumber));
for (Chapter chapter : chapters) {
System.out.println("chapter = " + chapter);
ChapterVO chapterVO = new ChapterVO();
BeanUtils.copyProperties(chapter, chapterVO);
String chapterId = chapter.getId();
List<Section> sectionList = sectionMapper.selectList(new LambdaQueryWrapper<Section>().eq(Section::getChapterId, chapterId));
sectionList.sort(Comparator.comparingInt(Section::getOrderNumber));
List<BaseSectionVO> BaseSectionVOs = sectionList.stream()
.map(sectionVOFactory::fromSection) // 用实例方法引用
.toList();
chapterVO.setSectionVOS(BaseSectionVOs);
chapterVOList.add(chapterVO);
}
courseDetailVO.setChapterVOS(chapterVOList);
List<Review> reviews = reviewMapper.selectList(new LambdaQueryWrapper<Review>().eq(Review::getCourseId, courseId));
List<ReviewVO> reviewVOList = reviews.stream().map(review -> {
ReviewVO reviewVO = new ReviewVO();
BeanUtils.copyProperties(review, reviewVO);
String userId = review.getUserId();
User user = userMapper.selectById(userId);
reviewVO.setUserName(user.getUsername());
reviewVO.setAvatar(user.getAvatar());
return reviewVO;
}).toList();
courseDetailVO.setReviewVOS(reviewVOList);
courseDetailVO.setRatingDistribution(ratingDistributions.stream().map(RatingDistribution::getCount).collect(Collectors.toList()));
System.out.println("courseDetailVO = " + courseDetailVO);
return courseDetailVO;
}
public int getStudentCount(String courseId) {
return courseStudentCounts.getOrDefault(courseId, 0);
}
public void updateStudentCount(String courseId, int count) {
courseStudentCounts.put(courseId, count);
}
public Map<String, Integer> getAllCourseCounts() {
return new HashMap<>(courseStudentCounts);
}
}