“mitao”
2026-03-27 cde5977f64ff1dfa928d1076c059e8865c02d70b
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
package com.tcm.training.admin.live.service;
 
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.DateTimes;
import com.tcm.training.admin.common.PageResponse;
import com.tcm.training.admin.live.dto.LiveSessionQuestionRequest;
import com.tcm.training.admin.live.dto.LiveSessionUpsertRequest;
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.entity.LiveReserve;
import com.tcm.training.admin.live.mapper.LiveCourseMapper;
import com.tcm.training.admin.live.mapper.LiveReserveMapper;
import com.tcm.training.admin.live.mapper.LiveSessionMapper;
import com.tcm.training.admin.live.mapper.LiveSessionQuestionMapper;
import com.tcm.training.admin.live.vo.LivePushNotificationVO;
import com.tcm.training.admin.live.vo.LiveSessionQuestionVO;
import com.tcm.training.admin.live.vo.LiveSessionStatusVO;
import com.tcm.training.admin.live.vo.LiveSessionVO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
 
@Service
public class LiveSessionAppService {
 
    private final LiveCourseMapper liveCourseMapper;
    private final LiveSessionMapper liveSessionMapper;
    private final LiveSessionQuestionMapper liveSessionQuestionMapper;
    private final LiveReserveMapper liveReserveMapper;
 
    public LiveSessionAppService(LiveCourseMapper liveCourseMapper,
                                 LiveSessionMapper liveSessionMapper,
                                 LiveSessionQuestionMapper liveSessionQuestionMapper,
                                 LiveReserveMapper liveReserveMapper) {
        this.liveCourseMapper = liveCourseMapper;
        this.liveSessionMapper = liveSessionMapper;
        this.liveSessionQuestionMapper = liveSessionQuestionMapper;
        this.liveReserveMapper = liveReserveMapper;
    }
 
    public PageResponse<LiveSessionVO> pageSessions(Long courseId, int page, int pageSize, String startTimeFrom, String startTimeTo, Integer liveStatus) {
        requireCourse(courseId);
 
        LambdaQueryWrapper<LiveSession> qw = new LambdaQueryWrapper<LiveSession>()
                .eq(LiveSession::getLiveCourseId, courseId);
        LocalDateTime from = DateTimes.parse(startTimeFrom);
        LocalDateTime to = DateTimes.parse(startTimeTo);
        if (from != null) {
            qw.ge(LiveSession::getStartTime, from);
        }
        if (to != null) {
            qw.le(LiveSession::getStartTime, to);
        }
        if (liveStatus != null) {
            qw.eq(LiveSession::getLiveStatus, liveStatus);
        }
        qw.orderByDesc(LiveSession::getStartTime).orderByDesc(LiveSession::getId);
 
        IPage<LiveSession> p = liveSessionMapper.selectPage(new Page<LiveSession>(page, pageSize), qw);
        List<LiveSession> sessions = p.getRecords();
        if (sessions == null || sessions.isEmpty()) {
            return PageResponse.of(Collections.<LiveSessionVO>emptyList(), p.getTotal(), p.getCurrent(), p.getSize());
        }
 
        List<Long> sessionIds = new ArrayList<Long>();
        for (LiveSession s : sessions) {
            sessionIds.add(s.getId());
        }
 
        List<LiveSessionQuestion> qs = liveSessionQuestionMapper.selectList(new LambdaQueryWrapper<LiveSessionQuestion>()
                .in(LiveSessionQuestion::getSessionId, sessionIds)
                .orderByAsc(LiveSessionQuestion::getSort));
        Map<Long, List<LiveSessionQuestionVO>> map = new HashMap<Long, List<LiveSessionQuestionVO>>();
        for (LiveSessionQuestion q : qs) {
            List<LiveSessionQuestionVO> list = map.get(q.getSessionId());
            if (list == null) {
                list = new ArrayList<LiveSessionQuestionVO>();
                map.put(q.getSessionId(), list);
            }
            LiveSessionQuestionVO qvo = new LiveSessionQuestionVO();
            qvo.setId(q.getId());
            qvo.setSort(q.getSort());
            qvo.setStem(q.getStem());
            qvo.setOptionA(q.getOptionA());
            qvo.setOptionB(q.getOptionB());
            qvo.setOptionC(q.getOptionC());
            qvo.setCorrectOption(q.getCorrectOption());
            list.add(qvo);
        }
 
        List<LiveSessionVO> list = new ArrayList<LiveSessionVO>();
        int baseSort = (page - 1) * pageSize;
        for (int i = 0; i < sessions.size(); i++) {
            LiveSession s = sessions.get(i);
            LiveSessionVO vo = new LiveSessionVO();
            vo.setId(s.getId());
            vo.setSort(baseSort + i + 1);
            vo.setStartTime(s.getStartTime());
            vo.setVideoUrl(s.getVideoUrl());
            vo.setLiveStatus(s.getLiveStatus());
            vo.setBaseReserveCount(s.getBaseReserveCount());
            vo.setBaseWatchCount(s.getBaseWatchCount());
            vo.setRealReserveCount(s.getRealReserveCount());
            vo.setRealWatchCount(s.getRealWatchCount());
            vo.setReserveConvertRate(s.getReserveConvertRate());
            vo.setCompletionRate(s.getCompletionRate());
            vo.setEffectiveWatchRate(s.getEffectiveWatchRate());
            vo.setQuizCorrectCount(s.getQuizCorrectCount());
            vo.setPointsIssued(s.getPointsIssued());
            vo.setQuestions(map.get(s.getId()));
            list.add(vo);
        }
 
        return PageResponse.of(list, p.getTotal(), p.getCurrent(), p.getSize());
    }
 
    @Transactional
    public LiveSessionVO createSession(Long courseId, LiveSessionUpsertRequest req) {
        requireCourse(courseId);
        validateStartTime(req.getStartTime());
        validateQuestions(req.getQuestions());
 
        LiveSession s = new LiveSession();
        s.setLiveCourseId(courseId);
        s.setStartTime(req.getStartTime());
        s.setVideoUrl(req.getVideoUrl());
        s.setBaseReserveCount(safeInt(req.getBaseReserveCount()));
        s.setBaseWatchCount(safeInt(req.getBaseWatchCount()));
        s.setRealReserveCount(0);
        s.setRealWatchCount(0);
        s.setQuizCorrectCount(0);
        s.setPointsIssued(0);
        s.setLiveStatus(0);
        liveSessionMapper.insert(s);
 
        upsertQuestions(s.getId(), req.getQuestions());
 
        return getSessionVo(s.getId(), 1);
    }
 
    @Transactional
    public LiveSessionVO updateSession(Long courseId, Long sessionId, LiveSessionUpsertRequest req) {
        LiveSession s = requireSession(courseId, sessionId);
        if (s.getLiveStatus() != null && s.getLiveStatus() != 0) {
            throw new IllegalArgumentException("仅允许修改未开始的场次");
        }
        validateStartTime(req.getStartTime());
        validateQuestions(req.getQuestions());
 
        s.setStartTime(req.getStartTime());
        s.setVideoUrl(req.getVideoUrl());
        s.setBaseReserveCount(safeInt(req.getBaseReserveCount()));
        s.setBaseWatchCount(safeInt(req.getBaseWatchCount()));
        liveSessionMapper.updateById(s);
 
        liveSessionQuestionMapper.delete(new LambdaQueryWrapper<LiveSessionQuestion>()
                .eq(LiveSessionQuestion::getSessionId, sessionId));
        upsertQuestions(sessionId, req.getQuestions());
 
        return getSessionVo(sessionId, 1);
    }
 
    @Transactional
    public LiveSessionVO copySession(Long courseId, Long sessionId) {
        LiveSession src = requireSession(courseId, sessionId);
 
        LiveSession dst = new LiveSession();
        dst.setLiveCourseId(courseId);
        dst.setStartTime(null);
        dst.setVideoUrl(null);
        dst.setBaseReserveCount(safeInt(src.getBaseReserveCount()));
        dst.setBaseWatchCount(safeInt(src.getBaseWatchCount()));
        dst.setRealReserveCount(0);
        dst.setRealWatchCount(0);
        dst.setQuizCorrectCount(0);
        dst.setPointsIssued(0);
        dst.setLiveStatus(0);
        liveSessionMapper.insert(dst);
 
        List<LiveSessionQuestion> qs = liveSessionQuestionMapper.selectList(new LambdaQueryWrapper<LiveSessionQuestion>()
                .eq(LiveSessionQuestion::getSessionId, sessionId)
                .orderByAsc(LiveSessionQuestion::getSort));
        if (qs != null && !qs.isEmpty()) {
            for (LiveSessionQuestion q : qs) {
                LiveSessionQuestion nq = new LiveSessionQuestion();
                nq.setSessionId(dst.getId());
                nq.setSort(q.getSort());
                nq.setStem(q.getStem());
                nq.setOptionA(q.getOptionA());
                nq.setOptionB(q.getOptionB());
                nq.setOptionC(q.getOptionC());
                nq.setCorrectOption(q.getCorrectOption());
                liveSessionQuestionMapper.insert(nq);
            }
        }
 
        return getSessionVo(dst.getId(), 1);
    }
 
    @Transactional
    public void deleteSession(Long courseId, Long sessionId) {
        requireSession(courseId, sessionId);
        liveSessionMapper.deleteById(sessionId);
    }
 
    @Transactional
    public LiveSessionStatusVO start(Long courseId, Long sessionId) {
        LiveSession s = requireSession(courseId, sessionId);
        if (s.getLiveStatus() != null && s.getLiveStatus() != 0) {
            throw new IllegalArgumentException("场次状态不允许开播");
        }
        s.setLiveStatus(1);
        s.setActualStartTime(LocalDateTime.now());
        liveSessionMapper.updateById(s);
        return new LiveSessionStatusVO(s.getId(), s.getLiveStatus(), s.getActualStartTime(), null);
    }
 
    @Transactional
    public LiveSessionStatusVO end(Long courseId, Long sessionId) {
        LiveSession s = requireSession(courseId, sessionId);
        if (s.getLiveStatus() == null || s.getLiveStatus() != 1) {
            throw new IllegalArgumentException("场次状态不允许结束");
        }
        s.setLiveStatus(2);
        s.setActualEndTime(LocalDateTime.now());
        liveSessionMapper.updateById(s);
        return new LiveSessionStatusVO(s.getId(), s.getLiveStatus(), null, s.getActualEndTime());
    }
 
    public LivePushNotificationVO pushNotification(Long courseId, Long sessionId) {
        LiveSession session = requireSession(courseId, sessionId);
        if (session.getLiveStatus() != null && session.getLiveStatus() != 0) {
            throw new IllegalArgumentException("仅允许对未开始的场次推送开播提醒");
        }
        Long cnt = liveReserveMapper.selectCount(new LambdaQueryWrapper<LiveReserve>()
                .eq(LiveReserve::getLiveCourseId, courseId)
                .eq(LiveReserve::getSessionId, sessionId)
                .eq(LiveReserve::getIsCancel, 0));
        return new LivePushNotificationVO(cnt == null ? 0L : cnt, LocalDateTime.now());
    }
 
    private LiveCourse requireCourse(Long courseId) {
        LiveCourse c = liveCourseMapper.selectById(courseId);
        if (c == null) {
            throw new IllegalArgumentException("课程不存在");
        }
        return c;
    }
 
    private LiveSession requireSession(Long courseId, Long sessionId) {
        requireCourse(courseId);
        LiveSession s = liveSessionMapper.selectById(sessionId);
        if (s == null) {
            throw new IllegalArgumentException("场次不存在");
        }
        if (!courseId.equals(s.getLiveCourseId())) {
            throw new IllegalArgumentException("场次不属于该课程");
        }
        return s;
    }
 
    private LiveSessionVO getSessionVo(Long sessionId, int sort) {
        LiveSession s = liveSessionMapper.selectById(sessionId);
        if (s == null) {
            throw new IllegalArgumentException("场次不存在");
        }
 
        List<LiveSessionQuestion> qs = liveSessionQuestionMapper.selectList(new LambdaQueryWrapper<LiveSessionQuestion>()
                .eq(LiveSessionQuestion::getSessionId, sessionId)
                .orderByAsc(LiveSessionQuestion::getSort));
        List<LiveSessionQuestionVO> qvos = new ArrayList<LiveSessionQuestionVO>();
        for (LiveSessionQuestion q : qs) {
            LiveSessionQuestionVO qvo = new LiveSessionQuestionVO();
            qvo.setId(q.getId());
            qvo.setSort(q.getSort());
            qvo.setStem(q.getStem());
            qvo.setOptionA(q.getOptionA());
            qvo.setOptionB(q.getOptionB());
            qvo.setOptionC(q.getOptionC());
            qvo.setCorrectOption(q.getCorrectOption());
            qvos.add(qvo);
        }
 
        LiveSessionVO vo = new LiveSessionVO();
        vo.setId(s.getId());
        vo.setSort(sort);
        vo.setStartTime(s.getStartTime());
        vo.setVideoUrl(s.getVideoUrl());
        vo.setLiveStatus(s.getLiveStatus());
        vo.setBaseReserveCount(s.getBaseReserveCount());
        vo.setBaseWatchCount(s.getBaseWatchCount());
        vo.setRealReserveCount(s.getRealReserveCount());
        vo.setRealWatchCount(s.getRealWatchCount());
        vo.setReserveConvertRate(s.getReserveConvertRate());
        vo.setCompletionRate(s.getCompletionRate());
        vo.setEffectiveWatchRate(s.getEffectiveWatchRate());
        vo.setQuizCorrectCount(s.getQuizCorrectCount());
        vo.setPointsIssued(s.getPointsIssued());
        vo.setQuestions(qvos);
        return vo;
    }
 
    private void upsertQuestions(Long sessionId, List<LiveSessionQuestionRequest> questions) {
        if (questions == null || questions.isEmpty()) {
            return;
        }
        for (LiveSessionQuestionRequest q : questions) {
            LiveSessionQuestion nq = new LiveSessionQuestion();
            nq.setSessionId(sessionId);
            nq.setSort(q.getSort());
            nq.setStem(q.getStem());
            nq.setOptionA(q.getOptionA());
            nq.setOptionB(q.getOptionB());
            nq.setOptionC(q.getOptionC());
            nq.setCorrectOption(q.getCorrectOption());
            liveSessionQuestionMapper.insert(nq);
        }
    }
 
    private void validateQuestions(List<LiveSessionQuestionRequest> questions) {
        if (questions == null || questions.isEmpty()) {
            throw new IllegalArgumentException("题目数量必须为3道");
        }
        if (questions.size() != 3) {
            throw new IllegalArgumentException("题目数量必须为3道");
        }
        Set<Integer> sortSet = new HashSet<Integer>();
        for (LiveSessionQuestionRequest q : questions) {
            if (q == null || q.getSort() == null) {
                continue;
            }
            if (!sortSet.add(q.getSort())) {
                throw new IllegalArgumentException("题目序号重复");
            }
        }
    }
 
    private void validateStartTime(LocalDateTime startTime) {
        if (startTime == null) {
            return;
        }
        if (startTime.isBefore(LocalDateTime.now())) {
            throw new IllegalArgumentException("开始直播时间不能早于当前时间");
        }
    }
 
    private int safeInt(Integer v) {
        return v == null ? 0 : v;
    }
}