hejianhao
2026-08-19 84f91e46377accae3fbb66954d40116c9ca8e72b
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
package com.tcm.training.admin.live.service;
 
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tcm.training.admin.common.Jsons;
import com.tcm.training.admin.common.PageResponse;
import com.tcm.training.admin.live.dto.LiveCourseStatusRequest;
import com.tcm.training.admin.live.dto.LiveCourseUpsertRequest;
import com.tcm.training.admin.live.dto.LiveCoursePasswordRequest;
import com.tcm.training.admin.live.dto.LiveCoursePasswordVerifyRequest;
import com.tcm.training.admin.live.entity.Expert;
import com.tcm.training.admin.live.entity.LiveCourse;
import com.tcm.training.admin.live.entity.LiveSession;
import com.tcm.training.admin.live.entity.LiveSessionQuestion;
import com.tcm.training.admin.live.mapper.ExpertMapper;
import com.tcm.training.admin.live.mapper.LiveCourseMapper;
import com.tcm.training.admin.live.mapper.LiveSessionMapper;
import com.tcm.training.admin.live.mapper.LiveSessionQuestionMapper;
import com.tcm.training.admin.live.vo.ExpertVO;
import com.tcm.training.admin.live.vo.LiveCourseDetailVO;
import com.tcm.training.admin.live.vo.LiveCourseListItemVO;
import com.tcm.training.admin.live.vo.LiveCourseListRow;
import com.tcm.training.admin.live.vo.LiveCourseDetailRow;
import com.tcm.training.admin.live.vo.LiveCourseStatusVO;
import org.springframework.stereotype.Service;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.transaction.annotation.Transactional;
 
import java.util.ArrayList;
import java.util.List;
 
@Service
public class LiveCourseAppService {
 
    private static final int LIVE_ROOM_PASSWORD_TYPE = 1;
 
    private final LiveCourseMapper liveCourseMapper;
    private final ExpertMapper expertMapper;
    private final LiveSessionMapper liveSessionMapper;
    private final LiveSessionQuestionMapper liveSessionQuestionMapper;
    private final BCryptPasswordEncoder passwordEncoder;
 
    public LiveCourseAppService(LiveCourseMapper liveCourseMapper,
                                ExpertMapper expertMapper,
                                LiveSessionMapper liveSessionMapper,
                                LiveSessionQuestionMapper liveSessionQuestionMapper,
                                BCryptPasswordEncoder passwordEncoder) {
        this.liveCourseMapper = liveCourseMapper;
        this.expertMapper = expertMapper;
        this.liveSessionMapper = liveSessionMapper;
        this.liveSessionQuestionMapper = liveSessionQuestionMapper;
        this.passwordEncoder = passwordEncoder;
    }
 
    public PageResponse<LiveCourseListItemVO> page(int page, int pageSize, String keyword, Long expertId, Integer status, Integer liveStatus) {
        IPage<LiveCourseListRow> p = liveCourseMapper.selectCoursePage(new Page<LiveCourseListRow>(page, pageSize),
                keyword, expertId, status, liveStatus);
 
        List<LiveCourseListItemVO> list = new ArrayList<LiveCourseListItemVO>();
        for (LiveCourseListRow row : p.getRecords()) {
            LiveCourseListItemVO vo = new LiveCourseListItemVO();
            vo.setId(row.getId());
            vo.setTitle(row.getTitle());
            vo.setLevel(row.getLevel());
            vo.setExpert(new ExpertVO(row.getExpertId(), row.getExpertName(), row.getExpertAvatar()));
            vo.setCoverImg(row.getCoverImg());
            vo.setReserveCount(row.getReserveCount() == null ? 0L : row.getReserveCount());
            vo.setWatchCount(row.getWatchCount() == null ? 0L : row.getWatchCount());
            vo.setQuizPoints(row.getQuizPoints());
            vo.setStartTime(row.getStartTime());
            vo.setLiveStatus(row.getLiveStatus());
            vo.setStatus(row.getStatus());
            vo.setCreatedAt(row.getCreatedAt());
            list.add(vo);
        }
 
        return PageResponse.of(list, p.getTotal(), p.getCurrent(), p.getSize());
    }
 
    @Transactional
    public LiveCourseDetailVO create(LiveCourseUpsertRequest req) {
        throw new IllegalArgumentException("直播课程为初始化数据,禁止新增");
        /*
        validateExpert(req.getExpertId());
 
        LiveCourse course = new LiveCourse();
        course.setTitle(req.getTitle());
        course.setLevel(req.getLevel());
        course.setExpertId(req.getExpertId());
        course.setCoverImg(req.getCoverImg());
        course.setBannerImgs(req.getBannerImgs());
        course.setIntro(req.getIntro());
        course.setTargetUsers(req.getTargetUsers());
        course.setQuizDelayMinutes(req.getQuizDelayMinutes());
        course.setQuizPoints(req.getQuizPoints());
        course.setStatus(req.getStatus());
        liveCourseMapper.insert(course);
 
        return detail(course.getId());
        */
    }
 
    public LiveCourseDetailVO detail(Long id) {
        LiveCourseDetailRow row = liveCourseMapper.selectCourseDetail(id);
        if (row == null) {
            throw new IllegalArgumentException("课程不存在");
        }
 
        LiveCourseDetailVO vo = new LiveCourseDetailVO();
        vo.setId(row.getId());
        vo.setTitle(row.getTitle());
        vo.setLevel(row.getLevel());
        vo.setExpert(new ExpertVO(row.getExpertId(), row.getExpertName(), row.getExpertAvatar()));
        vo.setCoverImg(row.getCoverImg());
        vo.setBannerImgs(Jsons.parseStringList(row.getBannerImgs()));
        vo.setIntro(row.getIntro());
        vo.setTargetUsers(row.getTargetUsers());
        vo.setQuizDelayMinutes(row.getQuizDelayMinutes());
        vo.setQuizPoints(row.getQuizPoints());
        vo.setStartTime(row.getStartTime());
        vo.setVideoUrl(row.getVideoUrl());
        vo.setLiveStatus(row.getLiveStatus());
        vo.setStatus(row.getStatus());
        vo.setReserveCount(row.getReserveCount() == null ? 0L : row.getReserveCount());
        vo.setWatchCount(row.getWatchCount() == null ? 0L : row.getWatchCount());
        vo.setCreatedAt(row.getCreatedAt());
        vo.setUpdatedAt(row.getUpdatedAt());
        return vo;
    }
 
    @Transactional
    public LiveCourseDetailVO update(Long id, LiveCourseUpsertRequest req) {
        validateExpert(req.getExpertId());
 
        LiveCourse course = liveCourseMapper.selectById(id);
        if (course == null) {
            throw new IllegalArgumentException("课程不存在");
        }
 
        course.setTitle(req.getTitle());
        course.setLevel(req.getLevel());
        course.setExpertId(req.getExpertId());
        course.setCoverImg(req.getCoverImg());
        course.setBannerImgs(req.getBannerImgs());
        course.setIntro(req.getIntro());
        course.setTargetUsers(req.getTargetUsers());
        course.setQuizDelayMinutes(req.getQuizDelayMinutes());
        course.setQuizPoints(req.getQuizPoints());
        course.setStatus(req.getStatus());
        liveCourseMapper.updateById(course);
 
        return detail(id);
    }
 
    @Transactional
    public LiveCourseStatusVO patchStatus(Long id, LiveCourseStatusRequest req) {
        LiveCourse course = liveCourseMapper.selectById(id);
        if (course == null) {
            throw new IllegalArgumentException("课程不存在");
        }
        course.setStatus(req.getStatus());
        liveCourseMapper.updateById(course);
        return new LiveCourseStatusVO(id, req.getStatus());
    }
 
    @Transactional
    public void updatePassword(Long id, LiveCoursePasswordRequest req) {
        String password = req.getPassword();
        if (StrUtil.isNotBlank(password)) {
            if (password.length() != 6) {
                throw new IllegalArgumentException("直播间密码必须是6个字符");
            }
        }
        LiveCourse course = liveCourseMapper.selectById(id);
        if (course == null) {
            throw new IllegalArgumentException("课程不存在");
        }
        if (StrUtil.isNotBlank(password)) {
            course.setPassword(passwordEncoder.encode(req.getPassword()));
        } else {
            course.setPassword(null);
        }
        liveCourseMapper.updateById(course);
    }
 
    public boolean verifyPassword(LiveCoursePasswordVerifyRequest req) {
        if (req.getType() != LIVE_ROOM_PASSWORD_TYPE) {
            throw new IllegalArgumentException("不支持的密码类型");
        }
        LiveCourse course = liveCourseMapper.selectOne(new LambdaQueryWrapper<LiveCourse>()
                .isNotNull(LiveCourse::getPassword)
                .orderByAsc(LiveCourse::getId)
                .last("limit 1"));
        return course != null
                && course.getPassword() != null
                && !course.getPassword().isEmpty()
                && passwordEncoder.matches(req.getPassword(), course.getPassword());
    }
 
    @Transactional
    public void delete(Long id) {
        throw new IllegalArgumentException("直播课程为初始化数据,禁止删除");
    }
 
    private void validateExpert(Long expertId) {
        Expert expert = expertMapper.selectById(expertId);
        if (expert == null) {
            throw new IllegalArgumentException("授课专家不存在");
        }
        if (expert.getStatus() != null && expert.getStatus() != 1) {
            throw new IllegalArgumentException("授课专家已禁用");
        }
    }
 
}