“mitao”
2026-04-17 d699da385dda6257677ee93bef47388c0635f64e
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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
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.setDurationSeconds(s.getDurationSeconds());
            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.setDurationSeconds(req.getDurationSeconds());
        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.setDurationSeconds(req.getDurationSeconds());
        s.setBaseReserveCount(safeInt(req.getBaseReserveCount()));
        s.setBaseWatchCount(safeInt(req.getBaseWatchCount()));
        liveSessionMapper.updateById(s);
 
        // 场次题目属于内嵌配置数据,编辑时直接物理删除后重建,避免逻辑删除记录占用唯一索引。
        liveSessionQuestionMapper.hardDeleteBySessionId(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.setDurationSeconds(src.getDurationSeconds());
        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 session = requireSession(courseId, sessionId);
        return markSessionLiving(session, LocalDateTime.now(), true);
    }
 
    /**
     * 定时任务将已到开播时间的场次切换为直播中。
     */
    @Transactional
    public LiveSessionStatusVO autoStart(Long sessionId, LocalDateTime actualStartTime) {
        LiveSession session = liveSessionMapper.selectById(sessionId);
        LocalDateTime resolvedStartTime = actualStartTime == null ? LocalDateTime.now() : actualStartTime;
        return markSessionLiving(session, resolvedStartTime, false);
    }
 
    /**
     * 定时任务将已到结束时间的直播中场次切换为已结束。
     */
    @Transactional
    public LiveSessionStatusVO autoEnd(Long sessionId, LocalDateTime actualEndTime) {
        LiveSession session = liveSessionMapper.selectById(sessionId);
        LocalDateTime resolvedEndTime = actualEndTime == null ? LocalDateTime.now() : actualEndTime;
        return markSessionEnded(session, resolvedEndTime, false);
    }
 
    /**
     * 手动结束正在直播中的场次。
     */
    @Transactional
    public LiveSessionStatusVO end(Long courseId, Long sessionId) {
        LiveSession s = requireSession(courseId, sessionId);
        return markSessionEnded(s, LocalDateTime.now(), true);
    }
 
    /**
     * 收敛同课程的直播中场次,只保留时间线上当前有效的一场。
     */
    @Transactional
    public int reconcileCourseLivingSessions(Long courseId, LocalDateTime reconcileTime) {
        if (courseId == null) {
            return 0;
        }
        LocalDateTime resolvedReconcileTime = reconcileTime == null ? LocalDateTime.now() : reconcileTime;
        List<LiveSession> sessions = liveSessionMapper.selectList(new LambdaQueryWrapper<LiveSession>()
                .eq(LiveSession::getLiveCourseId, courseId)
                .eq(LiveSession::getDeleted, 0)
                .orderByAsc(LiveSession::getStartTime)
                .orderByAsc(LiveSession::getId));
        if (sessions == null || sessions.isEmpty()) {
            return 0;
        }
 
        LiveSession effectiveLivingSession = resolveEffectiveLivingSession(sessions, resolvedReconcileTime);
        int repairedCount = 0;
        for (LiveSession session : sessions) {
            if (session == null || safeInt(session.getLiveStatus()) != 1) {
                continue;
            }
            if (effectiveLivingSession != null && effectiveLivingSession.getId() != null
                    && effectiveLivingSession.getId().equals(session.getId())) {
                continue;
            }
            // 同课程历史直播中残留数据会污染 APP 展示态,这里统一收敛为已结束。
            session.setLiveStatus(2);
            if (session.getActualEndTime() == null) {
                session.setActualEndTime(resolvedReconcileTime);
            }
            liveSessionMapper.updateById(session);
            repairedCount++;
        }
        return repairedCount;
    }
 
    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 LiveSessionStatusVO markSessionLiving(LiveSession session, LocalDateTime actualStartTime, boolean failWhenInvalid) {
        if (session == null) {
            if (failWhenInvalid) {
                throw new IllegalArgumentException("场次不存在");
            }
            return null;
        }
 
        // 使用带状态条件的更新语句保证人工开播与定时开播并发时仍然幂等。
        int affected = liveSessionMapper.markLivingIfPending(session.getId(), actualStartTime);
        if (affected <= 0) {
            if (failWhenInvalid) {
                throw new IllegalArgumentException("场次状态不允许开播");
            }
            return null;
        }
 
        reconcileCourseLivingSessions(session.getLiveCourseId(), actualStartTime);
        LiveSession startedSession = liveSessionMapper.selectById(session.getId());
        if (startedSession == null) {
            session.setLiveStatus(1);
            if (session.getActualStartTime() == null) {
                session.setActualStartTime(actualStartTime);
            }
            return new LiveSessionStatusVO(session.getId(), session.getLiveStatus(), session.getActualStartTime(), session.getActualEndTime());
        }
        return new LiveSessionStatusVO(
                startedSession.getId(),
                startedSession.getLiveStatus(),
                startedSession.getActualStartTime(),
                startedSession.getActualEndTime()
        );
    }
 
    /**
     * 执行直播中场次到已结束的公共状态迁移。
     */
    private LiveSessionStatusVO markSessionEnded(LiveSession session, LocalDateTime actualEndTime, boolean failWhenInvalid) {
        if (session == null) {
            if (failWhenInvalid) {
                throw new IllegalArgumentException("场次不存在");
            }
            return null;
        }
        if (session.getLiveStatus() == null || session.getLiveStatus().intValue() != 1) {
            if (failWhenInvalid) {
                throw new IllegalArgumentException("场次状态不允许结束");
            }
            return null;
        }
        LocalDateTime resolvedEndTime = actualEndTime == null ? LocalDateTime.now() : actualEndTime;
        session.setLiveStatus(2);
        session.setActualEndTime(resolvedEndTime);
        liveSessionMapper.updateById(session);
        reconcileCourseLivingSessions(session.getLiveCourseId(), resolvedEndTime);
        return new LiveSessionStatusVO(session.getId(), session.getLiveStatus(), session.getActualStartTime(), session.getActualEndTime());
    }
 
    /**
     * 校验课程存在性。
     */
    private LiveCourse requireCourse(Long courseId) {
        LiveCourse c = liveCourseMapper.selectById(courseId);
        if (c == null) {
            throw new IllegalArgumentException("课程不存在");
        }
        return c;
    }
 
    /**
     * 解析同课程时间线下当前有效的直播中场次。
     */
    private LiveSession resolveEffectiveLivingSession(List<LiveSession> sessions, LocalDateTime now) {
        LiveSession latestStartedSession = null;
        for (LiveSession session : sessions) {
            if (session == null || session.getStartTime() == null || session.getStartTime().isAfter(now)) {
                continue;
            }
            if (latestStartedSession == null || compareTimelineSession(latestStartedSession, session) < 0) {
                latestStartedSession = session;
            }
        }
        if (latestStartedSession == null || safeInt(latestStartedSession.getLiveStatus()) != 1) {
            return null;
        }
        return latestStartedSession;
    }
 
    /**
     * 比较两场已开始场次在课程时间线中的先后,开始时间更晚者优先。
     */
    private int compareTimelineSession(LiveSession left, LiveSession right) {
        int startTimeCompare = compareSessionTime(left == null ? null : left.getStartTime(), right == null ? null : right.getStartTime());
        if (startTimeCompare != 0) {
            return startTimeCompare;
        }
        Long leftId = left == null ? null : left.getId();
        Long rightId = right == null ? null : right.getId();
        if (leftId == null && rightId == null) {
            return 0;
        }
        if (leftId == null) {
            return -1;
        }
        if (rightId == null) {
            return 1;
        }
        return leftId.compareTo(rightId);
    }
 
    /**
     * 比较两个时间值,null 视为更早的时间。
     */
    private int compareSessionTime(LocalDateTime left, LocalDateTime right) {
        if (left == null && right == null) {
            return 0;
        }
        if (left == null) {
            return -1;
        }
        if (right == null) {
            return 1;
        }
        return left.compareTo(right);
    }
 
    /**
     * 校验场次存在且属于指定课程。
     */
    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.setDurationSeconds(s.getDurationSeconds());
        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;
    }
}