| | |
| | | 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.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.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 com.tcm.training.admin.common.enums.EnabledStatusEnum; |
| | | |
| | | import java.util.ArrayList; |
| | | import java.util.List; |
| | |
| | | @Service |
| | | public class LiveCourseAppService { |
| | | |
| | | private static final int LIVE_ROOM_PASSWORD_TYPE = 1; |
| | | private static final int PROJECT_INTRODUCTION_PASSWORD_TYPE = 2; |
| | | |
| | | private final LiveCourseMapper liveCourseMapper; |
| | | private final ExpertMapper expertMapper; |
| | | private final LiveSessionMapper liveSessionMapper; |
| | | private final LiveSessionQuestionMapper liveSessionQuestionMapper; |
| | | private final BCryptPasswordEncoder passwordEncoder; |
| | | private final ProjectIntroductionPasswordService projectIntroductionPasswordService; |
| | | |
| | | public LiveCourseAppService(LiveCourseMapper liveCourseMapper, |
| | | ExpertMapper expertMapper, |
| | | LiveSessionMapper liveSessionMapper, |
| | | LiveSessionQuestionMapper liveSessionQuestionMapper) { |
| | | LiveSessionQuestionMapper liveSessionQuestionMapper, |
| | | BCryptPasswordEncoder passwordEncoder, |
| | | ProjectIntroductionPasswordService projectIntroductionPasswordService) { |
| | | this.liveCourseMapper = liveCourseMapper; |
| | | this.expertMapper = expertMapper; |
| | | this.liveSessionMapper = liveSessionMapper; |
| | | this.liveSessionQuestionMapper = liveSessionQuestionMapper; |
| | | this.passwordEncoder = passwordEncoder; |
| | | this.projectIntroductionPasswordService = projectIntroductionPasswordService; |
| | | } |
| | | |
| | | /** 分页查询直播课程。 */ |
| | | 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); |
| | |
| | | return PageResponse.of(list, p.getTotal(), p.getCurrent(), p.getSize()); |
| | | } |
| | | |
| | | @Transactional |
| | | @Transactional(rollbackFor = Exception.class) |
| | | /** 创建直播课程。 */ |
| | | public LiveCourseDetailVO create(LiveCourseUpsertRequest req) { |
| | | throw new IllegalArgumentException("直播课程为初始化数据,禁止新增"); |
| | | /* |
| | |
| | | */ |
| | | } |
| | | |
| | | /** 查询直播课程详情。 */ |
| | | public LiveCourseDetailVO detail(Long id) { |
| | | LiveCourseDetailRow row = liveCourseMapper.selectCourseDetail(id); |
| | | if (row == null) { |
| | |
| | | return vo; |
| | | } |
| | | |
| | | @Transactional |
| | | @Transactional(rollbackFor = Exception.class) |
| | | /** 更新直播课程。 */ |
| | | public LiveCourseDetailVO update(Long id, LiveCourseUpsertRequest req) { |
| | | validateExpert(req.getExpertId()); |
| | | |
| | |
| | | return detail(id); |
| | | } |
| | | |
| | | @Transactional |
| | | @Transactional(rollbackFor = Exception.class) |
| | | /** 修改直播课程状态。 */ |
| | | public LiveCourseStatusVO patchStatus(Long id, LiveCourseStatusRequest req) { |
| | | LiveCourse course = liveCourseMapper.selectById(id); |
| | | if (course == null) { |
| | |
| | | return new LiveCourseStatusVO(id, req.getStatus()); |
| | | } |
| | | |
| | | @Transactional |
| | | @Transactional(rollbackFor = Exception.class) |
| | | /** 更新直播课程密码。 */ |
| | | 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.updatePasswordById(id, course.getPassword()); |
| | | } |
| | | |
| | | /** 校验直播课程访问密码。 */ |
| | | public boolean verifyPassword(LiveCoursePasswordVerifyRequest req) { |
| | | if (req.getType() == LIVE_ROOM_PASSWORD_TYPE) { |
| | | // 直播间密码验证 |
| | | 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()); |
| | | } else if (req.getType() == PROJECT_INTRODUCTION_PASSWORD_TYPE) { |
| | | // 项目介绍密码验证 |
| | | return projectIntroductionPasswordService.verifyPassword(req.getPassword()); |
| | | } else { |
| | | throw new IllegalArgumentException("不支持的密码类型"); |
| | | } |
| | | } |
| | | |
| | | @Transactional(rollbackFor = Exception.class) |
| | | /** 删除直播课程。 */ |
| | | public void delete(Long id) { |
| | | throw new IllegalArgumentException("直播课程为初始化数据,禁止删除"); |
| | | } |
| | |
| | | if (expert == null) { |
| | | throw new IllegalArgumentException("授课专家不存在"); |
| | | } |
| | | if (expert.getStatus() != null && expert.getStatus() != 1) { |
| | | if (expert.getStatus() != null && expert.getStatus() != EnabledStatusEnum.ENABLED.getValue()) { |
| | | throw new IllegalArgumentException("授课专家已禁用"); |
| | | } |
| | | } |