| | |
| | | package cc.mrbird.febs.video.service.impl; |
| | | |
| | | import cc.mrbird.febs.common.entity.FebsResponse; |
| | | import cc.mrbird.febs.common.entity.QueryRequest; |
| | | import cc.mrbird.febs.video.entity.VideoMasterInfoEntity; |
| | | import cc.mrbird.febs.video.mapper.VideoMasterInfoMapper; |
| | | import cc.mrbird.febs.common.exception.FebsException; |
| | | import cc.mrbird.febs.common.utils.AppContants; |
| | | import cc.mrbird.febs.common.utils.LoginUserUtil; |
| | | import cc.mrbird.febs.video.conversion.VideoConversion; |
| | | import cc.mrbird.febs.video.conversion.VideoMemberConversion; |
| | | import cc.mrbird.febs.video.dto.ApiVideoCollectionDto; |
| | | import cc.mrbird.febs.video.dto.ApiVideoHistoryDto; |
| | | import cc.mrbird.febs.video.dto.ApiVideoLikeDto; |
| | | import cc.mrbird.febs.video.dto.VideoListDto; |
| | | import cc.mrbird.febs.video.entity.*; |
| | | import cc.mrbird.febs.video.mapper.*; |
| | | import cc.mrbird.febs.video.service.IVideoMasterDataService; |
| | | import cc.mrbird.febs.video.service.IVideoMasterInfoService; |
| | | import cc.mrbird.febs.video.vo.VideoInfoItemVo; |
| | | import cc.mrbird.febs.video.vo.VideoInfoVo; |
| | | import cc.mrbird.febs.video.vo.VideoListVo; |
| | | import cn.hutool.core.collection.CollUtil; |
| | | import cn.hutool.core.util.StrUtil; |
| | | import cn.hutool.core.*; |
| | | import com.baomidou.mybatisplus.core.metadata.IPage; |
| | | import com.baomidou.mybatisplus.extension.plugins.pagination.Page; |
| | | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
| | | import lombok.RequiredArgsConstructor; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | |
| | | import java.util.ArrayList; |
| | | import java.util.Date; |
| | | import java.util.List; |
| | | |
| | | /** |
| | | * @author wzy |
| | |
| | | @RequiredArgsConstructor |
| | | public class VideoMasterInfoServiceImpl extends ServiceImpl<VideoMasterInfoMapper, VideoMasterInfoEntity> implements IVideoMasterInfoService { |
| | | |
| | | private final VideoMasterItemsMapper videoMasterItemsMapper; |
| | | private final VideoMasterSourceMapper videoMasterSourceMapper; |
| | | private final VideoMasterDataMapper videoMasterDataMapper; |
| | | private final VideoCollectionMapper videoCollectionMapper; |
| | | private final VideoCommentInfoMapper videoCommentInfoMapper; |
| | | private final VideoCommentLikeMapper videoCommentLikeMapper; |
| | | private final VideoHistoryMapper videoHistoryMapper; |
| | | private final VideoMemberMapper videoMemberMapper; |
| | | private final IVideoMasterDataService videoMasterDataService; |
| | | |
| | | @Override |
| | | public IPage<VideoMasterInfoEntity> findInPage(VideoMasterInfoEntity info, QueryRequest request) { |
| | | return null; |
| | | Page<VideoMasterInfoEntity> page = new Page<>(request.getPageNum(), request.getPageSize()); |
| | | return this.baseMapper.selectInPage(info, page); |
| | | } |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public void addVideo(VideoMasterInfoEntity info) { |
| | | |
| | | this.baseMapper.insert(info); |
| | | List<VideoMasterItemsEntity> items = info.getItems(); |
| | | if (CollUtil.isEmpty(items)) { |
| | | throw new FebsException("章节异常"); |
| | | } |
| | | |
| | | int i = 1; |
| | | for (VideoMasterItemsEntity item : items) { |
| | | if (StrUtil.isBlank(item.getName())){ |
| | | throw new FebsException("章节标题未填写"); |
| | | } |
| | | VideoMasterSourceEntity source = this.videoMasterSourceMapper.selectById(item.getSourceId()); |
| | | item.setMasterId(info.getId()); |
| | | item.setSeq(i); |
| | | item.setVideoUrl(source.getUrl()); |
| | | |
| | | this.videoMasterItemsMapper.insert(item); |
| | | i++; |
| | | } |
| | | |
| | | VideoMasterDataEntity data = new VideoMasterDataEntity(); |
| | | data.setMasterId(info.getId()); |
| | | data.setCollectCnt(0); |
| | | data.setPlayCnt(0); |
| | | data.setStarCnt(0); |
| | | this.videoMasterDataMapper.insert(data); |
| | | } |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public void updateVideo(VideoMasterInfoEntity info) { |
| | | this.baseMapper.updateById(info); |
| | | |
| | | // List<VideoMasterItemsEntity> beforeItems = this.videoMasterItemsMapper.selectItemsByMasterId(info.getId()); |
| | | List<VideoMasterItemsEntity> newItems = info.getItems(); |
| | | if (CollUtil.isEmpty(newItems)) { |
| | | throw new FebsException("章节异常"); |
| | | } |
| | | |
| | | List<Long> existID = new ArrayList<>(); |
| | | List<VideoMasterItemsEntity> newInsert = new ArrayList<>(); |
| | | int i = 1; |
| | | for (VideoMasterItemsEntity newItem : newItems) { |
| | | if (StrUtil.isBlank(newItem.getName())) { |
| | | throw new FebsException("章节标题未填写"); |
| | | } |
| | | |
| | | if (newItem.getId() != null) { |
| | | newItem.setSeq(i); |
| | | this.videoMasterItemsMapper.updateById(newItem); |
| | | existID.add(newItem.getId()); |
| | | i++; |
| | | } else { |
| | | newInsert.add(newItem); |
| | | } |
| | | } |
| | | |
| | | if (CollUtil.isNotEmpty(existID)) { |
| | | this.videoMasterItemsMapper.deleteNotInIds(existID, info.getId()); |
| | | } |
| | | |
| | | if (CollUtil.isNotEmpty(newInsert)) { |
| | | for (VideoMasterItemsEntity item : newInsert) { |
| | | VideoMasterSourceEntity source = this.videoMasterSourceMapper.selectById(item.getSourceId()); |
| | | item.setMasterId(info.getId()); |
| | | item.setVideoUrl(source.getUrl()); |
| | | item.setSeq(i); |
| | | this.videoMasterItemsMapper.insert(item); |
| | | |
| | | i++; |
| | | } |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | public VideoMasterInfoEntity findById(Long id) { |
| | | return this.baseMapper.selectEntityById(id); |
| | | } |
| | | |
| | | @Override |
| | | public void changeIsUp(Long id) { |
| | | VideoMasterInfoEntity masterInfo = this.baseMapper.selectById(id); |
| | | if (masterInfo == null) { |
| | | throw new FebsException("视频不存在"); |
| | | } |
| | | if (AppContants.FLAG_INT_Y.equals(masterInfo.getIsUp())) { |
| | | masterInfo.setIsUp(AppContants.FLAG_INT_N); |
| | | } else { |
| | | masterInfo.setIsUp(AppContants.FLAG_INT_Y); |
| | | } |
| | | |
| | | this.baseMapper.updateById(masterInfo); |
| | | } |
| | | |
| | | @Override |
| | | public List<VideoListVo> findVideoList(VideoListDto videoListDto) { |
| | | Page<VideoListVo> page = new Page<>(videoListDto.getPageNum(), videoListDto.getPageSize()); |
| | | |
| | | videoListDto.setIsUp(AppContants.FLAG_INT_Y); |
| | | IPage<VideoListVo> data = this.baseMapper.selectVideoListInPage(videoListDto, page); |
| | | return data.getRecords(); |
| | | } |
| | | |
| | | @Override |
| | | public VideoInfoVo findVideoInfo(Long videoId, Long itemId) { |
| | | VideoMasterInfoEntity videoInfoEntity = this.baseMapper.selectEntityById(videoId); |
| | | if (videoInfoEntity == null) { |
| | | throw new FebsException("视频不存在"); |
| | | } |
| | | |
| | | VideoMemberEntity loginUser = LoginUserUtil.getLoginUser(); |
| | | VideoInfoVo videoInfoVo = VideoConversion.INSTANCE.videoInfoEntityToInfoVo(videoInfoEntity); |
| | | List<VideoInfoItemVo> items = VideoConversion.INSTANCE.videoItemEntitiesToItemVoes(videoInfoEntity.getItems()); |
| | | videoInfoVo.setItems(items); |
| | | |
| | | VideoMasterDataEntity data = this.videoMasterDataMapper.selectDataByMasterId(videoInfoEntity.getId()); |
| | | videoInfoVo.setPlayCnt(data.getPlayCnt()); |
| | | videoInfoVo.setCollectCnt(data.getCollectCnt()); |
| | | videoInfoVo.setStarCnt(data.getStarCnt()); |
| | | |
| | | if (loginUser != null) { |
| | | // 是否点赞 |
| | | VideoCommentLikeEntity commentLike = this.videoCommentLikeMapper.selectCommentLikeByMemberIdAndRelateId(loginUser.getId(), videoInfoEntity.getId()); |
| | | if (commentLike != null) { |
| | | videoInfoVo.setIsLike(1); |
| | | } else { |
| | | videoInfoVo.setIsLike(2); |
| | | } |
| | | |
| | | // 是否收藏 |
| | | VideoCollectionEntity collection = this.videoCollectionMapper.selectVideoCollectionByVideoIdAndMemberId(videoInfoEntity.getId(), loginUser.getId()); |
| | | if (collection != null) { |
| | | videoInfoVo.setIsCollect(1); |
| | | } else { |
| | | videoInfoVo.setIsCollect(2); |
| | | } |
| | | } |
| | | |
| | | // 视频是否需要vip |
| | | if (AppContants.FLAG_INT_N.equals(videoInfoEntity.getIsFree())) { |
| | | // 判断用户是否登录 |
| | | if (loginUser == null) { |
| | | videoInfoVo.setNotLogin(AppContants.FLAG_INT_N); |
| | | return videoInfoVo; |
| | | } |
| | | |
| | | // 判断用户是否为vip |
| | | VideoMemberEntity member = this.videoMemberMapper.selectById(loginUser.getId()); |
| | | if (AppContants.FLAG_INT_N.equals(member.getIsVip())) { |
| | | videoInfoVo.setNotVip(AppContants.FLAG_INT_N); |
| | | return videoInfoVo; |
| | | } |
| | | } |
| | | |
| | | |
| | | VideoMasterItemsEntity currentItem = this.videoMasterItemsMapper.selectItemByVideoIdAndItemId(videoId, itemId); |
| | | if (currentItem == null) { |
| | | throw new FebsException("视频不存在"); |
| | | } |
| | | |
| | | videoInfoVo.setNotLogin(AppContants.FLAG_INT_Y); |
| | | videoInfoVo.setNotVip(AppContants.FLAG_INT_Y); |
| | | videoInfoVo.setSubTitle(currentItem.getName()); |
| | | videoInfoVo.setVideoUrl(currentItem.getVideoUrl()); |
| | | videoInfoVo.setThumb(currentItem.getThumb()); |
| | | videoInfoVo.setItemId(currentItem.getId()); |
| | | videoInfoVo.setTimeLength(currentItem.getTimeLength()); |
| | | |
| | | // 增加播放量 |
| | | this.videoMasterDataService.modifyVideoData(videoInfoEntity.getId(), 1, 1); |
| | | |
| | | // 历史记录 |
| | | if (loginUser != null) { |
| | | VideoHistoryEntity history = this.videoHistoryMapper.selectByMemberIdAndVideoId(loginUser.getId(), videoInfoEntity.getId()); |
| | | if (history == null) { |
| | | history = new VideoHistoryEntity(); |
| | | history.setVideoId(videoInfoEntity.getId()); |
| | | history.setMemberId(loginUser.getId()); |
| | | history.setVideoItemId(currentItem.getId()); |
| | | this.videoHistoryMapper.insert(history); |
| | | } else { |
| | | history.setUpdatedTime(new Date()); |
| | | history.setVideoItemId(currentItem.getId()); |
| | | this.videoHistoryMapper.updateById(history); |
| | | } |
| | | } |
| | | |
| | | return videoInfoVo; |
| | | } |
| | | |
| | | @Override |
| | | public FebsResponse videoCollection(ApiVideoCollectionDto apiVideoCollectionDto) { |
| | | Long id = LoginUserUtil.getLoginUser().getId(); |
| | | Long videoId = apiVideoCollectionDto.getVideoId(); |
| | | VideoMasterInfoEntity videoMasterInfoEntity = this.baseMapper.selectById(videoId); |
| | | if(videoMasterInfoEntity == null){ |
| | | throw new FebsException("视频不存在"); |
| | | } |
| | | VideoCollectionEntity videoCollection = videoCollectionMapper.selectVideoCollectionByVideoIdAndMemberId(videoId, id); |
| | | if(videoCollection != null){ |
| | | videoCollectionMapper.deleteById(videoCollection.getId()); |
| | | |
| | | //收藏数-1 |
| | | this.videoMasterDataService.modifyVideoData(videoMasterInfoEntity.getId(), -1, 2); |
| | | // VideoMasterDataEntity videoMasterDataEntity = videoMasterDataMapper.selectDataByMasterId(videoId); |
| | | // int collectCnt = videoMasterDataEntity.getCollectCnt(); |
| | | // videoMasterDataEntity.setCollectCnt((collectCnt - 1) <= 0 ? 0:(collectCnt - 1)); |
| | | // videoMasterDataMapper.updateById(videoMasterDataEntity); |
| | | return new FebsResponse().success().message("已取消收藏"); |
| | | }else{ |
| | | VideoCollectionEntity videoCollectionEntity = new VideoCollectionEntity(); |
| | | videoCollectionEntity.setMemberId(id); |
| | | videoCollectionEntity.setVideoId(videoId); |
| | | videoCollectionMapper.insert(videoCollectionEntity); |
| | | //收藏数+1 |
| | | |
| | | this.videoMasterDataService.modifyVideoData(videoMasterInfoEntity.getId(), 1, 2); |
| | | // VideoMasterDataEntity videoMasterDataEntity = videoMasterDataMapper.selectDataByMasterId(videoId); |
| | | // int collectCnt = videoMasterDataEntity.getCollectCnt(); |
| | | // videoMasterDataEntity.setCollectCnt((collectCnt + 1) <= 0 ? 0:(collectCnt + 1)); |
| | | // videoMasterDataMapper.updateById(videoMasterDataEntity); |
| | | return new FebsResponse().success().message("已收藏"); |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | public FebsResponse videoLike(ApiVideoLikeDto apiVideoLikeDto) { |
| | | Long id = LoginUserUtil.getLoginUser().getId(); |
| | | Integer type = apiVideoLikeDto.getType(); |
| | | Long relateId = apiVideoLikeDto.getRelateId(); |
| | | VideoCommentLikeEntity videoCommentLikeEntity = videoCommentLikeMapper.selectCommentLikeByMemberIdAndRelateId(id,relateId); |
| | | |
| | | VideoMasterInfoEntity videoMasterInfoEntity = this.baseMapper.selectById(relateId); |
| | | if(videoCommentLikeEntity != null){ |
| | | if(1 == type){ |
| | | //视频 |
| | | if(videoMasterInfoEntity == null){ |
| | | throw new FebsException("视频不存在"); |
| | | } |
| | | videoCommentLikeMapper.deleteById(videoCommentLikeEntity.getId()); |
| | | //点赞数-1 |
| | | this.videoMasterDataService.modifyVideoData(videoMasterInfoEntity.getId(), -1, 3); |
| | | // VideoMasterDataEntity videoMasterDataEntity = videoMasterDataMapper.selectDataByMasterId(relateId); |
| | | // int starCnt = videoMasterDataEntity.getStarCnt(); |
| | | // videoMasterDataEntity.setStarCnt((starCnt - 1) <= 0 ? 0:(starCnt - 1)); |
| | | // videoMasterDataMapper.updateById(videoMasterDataEntity); |
| | | return new FebsResponse().success().message("已取消点赞"); |
| | | }else if(2 == type){ |
| | | //评论 |
| | | VideoCommentInfoEntity videoCommentInfoEntity = videoCommentInfoMapper.selectById(relateId); |
| | | if(videoCommentInfoEntity == null){ |
| | | throw new FebsException("评论不存在"); |
| | | } |
| | | videoCommentLikeMapper.deleteById(videoCommentLikeEntity.getId()); |
| | | return new FebsResponse().success().message("已取消点赞"); |
| | | }else{ |
| | | throw new FebsException("网络繁忙"); |
| | | } |
| | | }else{ |
| | | VideoCommentLikeEntity videoCommentLike = new VideoCommentLikeEntity(); |
| | | videoCommentLike.setType(type); |
| | | videoCommentLike.setRelateId(relateId); |
| | | videoCommentLike.setMemberId(id); |
| | | videoCommentLikeMapper.insert(videoCommentLike); |
| | | //点赞数+1 |
| | | |
| | | this.videoMasterDataService.modifyVideoData(videoMasterInfoEntity.getId(), 1, 3); |
| | | // VideoMasterDataEntity videoMasterDataEntity = videoMasterDataMapper.selectDataByMasterId(relateId); |
| | | // int starCnt = videoMasterDataEntity.getStarCnt(); |
| | | // videoMasterDataEntity.setStarCnt((starCnt + 1) <= 0 ? 0:(starCnt + 1)); |
| | | // videoMasterDataMapper.updateById(videoMasterDataEntity); |
| | | return new FebsResponse().success().message("已点赞"); |
| | | } |
| | | |
| | | } |
| | | |
| | | @Override |
| | | public FebsResponse delVideoHistory(ApiVideoHistoryDto apiVideoHistoryDto) { |
| | | Long id = LoginUserUtil.getLoginUser().getId(); |
| | | String ids = apiVideoHistoryDto.getIds(); |
| | | List<String> idList = StrUtil.split(ids, ','); |
| | | videoHistoryMapper.delVideoHistoryByIdsAndMemberId(idList,id); |
| | | return new FebsResponse().success().message("成功"); |
| | | } |
| | | } |