zhangf
2024-06-24 21362fd048558832cdcaca8ee957d2d7aa753be2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package com.gkhy.exam.institutionalaccess.service.serviceImpl;
 
import com.gkhy.exam.institutionalaccess.entity.ThCourse;
import com.gkhy.exam.institutionalaccess.entity.ThCourseChapter;
import com.gkhy.exam.institutionalaccess.model.query.ThCourseQuery;
import com.gkhy.exam.institutionalaccess.model.resp.ThCourseChapterRespDTO;
import com.gkhy.exam.institutionalaccess.model.resp.ThCourseRespDTO;
import com.gkhy.exam.institutionalaccess.model.vo.ThCourseChapterVO;
import com.gkhy.exam.institutionalaccess.model.vo.ThStatisticStudentVO;
import com.gkhy.exam.institutionalaccess.service.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
 
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
 
@Service("ThCourseManagerService")
public class ThCourseManagerServiceImpl implements ThCourseManagerService {
    @Autowired
    private ThCourseService courseService;
 
    @Autowired
    private ThCourseChapterService courseChapterService;
    @Autowired
    private ThStudentCourseService studentCourseService;
 
 
    @Override
    public List<ThCourseRespDTO> listByPage(ThCourseQuery query) {
 
        List<ThCourse> courseList = courseService.listByPage(query);
        //分许获取数据
        List<ThStatisticStudentVO> thStatisticStudentVOS = studentCourseService.statisticByCourseUuid();
 
        List<ThCourseRespDTO> courseRespDTOList = new ArrayList<>();
        if(!CollectionUtils.isEmpty(courseList)){
            //根据courseids获取所有章节
            List<ThCourseChapterVO> courseChapterVOS = new ArrayList<>();
            List<String> courseUuids = courseList.stream().map(ThCourse::getUuid).collect(Collectors.toList());
            if(!CollectionUtils.isEmpty(courseUuids)){
                courseChapterVOS = courseChapterService.listByCourseUuids(courseUuids);
            }
            for (ThCourse course : courseList) {
                ThCourseRespDTO courseRespDTO = new ThCourseRespDTO();
                BeanUtils.copyProperties(course, courseRespDTO);
                //获取章
                List<ThCourseChapterVO> finalCourseChapterVOS = courseChapterVOS;
                List<ThCourseChapterRespDTO> chapterList = courseChapterVOS
                        .stream()
                        .filter(cc -> course.getUuid().equals(cc.getCourseUuid()) && cc.getParentUuid().equals("0"))
                        .map(cc -> {
                            ThCourseChapterRespDTO courseChapterRespDTO = new ThCourseChapterRespDTO();
                            BeanUtils.copyProperties(cc, courseChapterRespDTO);
                            courseChapterRespDTO.setChildren(getChildren(finalCourseChapterVOS,cc.getUuid()));
                            return courseChapterRespDTO;
                        })
                        .collect(Collectors.toList());
                courseRespDTO.setOutline(chapterList);
                //学员数量
                List<ThStatisticStudentVO> statisticList = thStatisticStudentVOS.stream().filter(s -> s.getCourseUuid().equals(course.getUuid())).collect(Collectors.toList());
                if(statisticList.size()>0){
                    ThStatisticStudentVO thStatisticStudentVO = statisticList.get(0);
                    courseRespDTO.setStudentCount(thStatisticStudentVO.getCount());
                }else {
                    courseRespDTO.setStudentCount(0);
                }
                courseRespDTOList.add(courseRespDTO);
            }
        }
        return courseRespDTOList;
    }
 
    //获取单条课程
    @Override
    public ThCourseRespDTO findById(Long id) {
        ThCourse thCourse = courseService.getById(id);
        ThCourseRespDTO thCourseRespDTO = new ThCourseRespDTO();
        BeanUtils.copyProperties(thCourse, thCourseRespDTO);
        List<ThCourseChapterVO> thCourseChapterVOS = courseChapterService.listByCourseUuid(thCourse.getUuid());
        List<ThCourseChapterRespDTO> chapterRespDTOS = thCourseChapterVOS.stream()
                .filter(c -> c.getParentUuid().equals("0"))
                .map(c -> {
                    ThCourseChapterRespDTO chapterRespDTO = new ThCourseChapterRespDTO();
                    BeanUtils.copyProperties(c, chapterRespDTO);
                    chapterRespDTO.setChildren(getChildren(thCourseChapterVOS,c.getUuid()));
                    return chapterRespDTO;
                }).collect(Collectors.toList());
        thCourseRespDTO.setOutline(chapterRespDTOS);
        return thCourseRespDTO;
    }
 
    //获取章节
    private List<ThCourseChapterRespDTO> getChildren(List<ThCourseChapterVO> courseChapterVOS, String parentUuid) {
        List<ThCourseChapterRespDTO> chapterList = courseChapterVOS
                .stream()
                .filter(cc -> cc.getParentUuid().equals(parentUuid))
                .map(cc -> {
                    ThCourseChapterRespDTO courseChapterRespDTO = new ThCourseChapterRespDTO();
                    BeanUtils.copyProperties(cc, courseChapterRespDTO);
                    return courseChapterRespDTO;
                })
                .collect(Collectors.toList());
        return chapterList;
    }
 
}