package com.hollywood.applet.controller; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.hollywood.applet.dto.PutVideoDto; import com.hollywood.applet.dto.ReportDto; import com.hollywood.applet.service.TVideoReportService; import com.hollywood.applet.service.TVideoService; import com.hollywood.applet.utils.LoginInfoUtil; import com.hollywood.common.basic.ApiResult; import com.hollywood.common.basic.PageInfo; import com.hollywood.common.model.TInformation; import com.hollywood.common.model.TShortPlay; import com.hollywood.common.model.TVideo; import com.hollywood.common.model.TVideoReport; import com.hollywood.common.redis.RedisAutoTemplate; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import io.swagger.models.auth.In; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import java.util.List; import java.util.Set; /** *

* 短视频管理 前端控制器 *

* * @author xiaochen * @since 2024-02-29 */ @RestController @RequestMapping("/tVideo") @Api(tags = "短视频") public class TVideoController { @Autowired private LoginInfoUtil loginInfoUtil; @Autowired private RedisAutoTemplate redisAutoTemplate; @Autowired private TVideoService videoService; @Autowired private TVideoReportService reportService; @ApiOperation(value = "观看短视频接口") @PutMapping("/watch") public ApiResult watch(Long videoId) { Long userId = loginInfoUtil.getUserId(); // redisAutoTemplate.addList(userId+":user:history:",videoId); // Set set = redisAutoTemplate.getSet(String.valueOf(userId)); String str = redisAutoTemplate.getStr(videoId + ":views:"); if (str ==null) { redisAutoTemplate.setStr(videoId + ":views:", "1"); }else { String str1 = redisAutoTemplate.getStr(videoId + ":views:"); redisAutoTemplate.setStr(videoId + ":views:", String.valueOf(Integer.parseInt(str1)+1)); } // System.err.println(set); return ApiResult.success(); } @ApiOperation(value = "短视频点赞") @PutMapping("/good") public ApiResult good(Long videoId) { Long userId = loginInfoUtil.getUserId(); redisAutoTemplate.addSet(userId+":user:good:",videoId); redisAutoTemplate.addSet(videoId+":video:good:",userId); Set set = redisAutoTemplate.getSet(String.valueOf(userId)); System.err.println(set); return ApiResult.success(); } @ApiOperation(value = "短视频取消点赞") @PutMapping("/cancelgood") public ApiResult cancelgood(Long videoId) { Long userId = loginInfoUtil.getUserId(); // redisAutoTemplate.removeZSet(userId+":user:good:",videoId); redisAutoTemplate.setRemove(userId+":user:good:",videoId); // redisAutoTemplate.removeZSet(videoId+":video:good:",userId); redisAutoTemplate.setRemove(videoId+":video:good:",userId); Set set = redisAutoTemplate.getSet(String.valueOf(userId)); System.err.println(set); return ApiResult.success(); } @ApiOperation(value = "获取十个推荐视频,刷十次调一次这个接口") @PutMapping("/get") public ApiResult> get() { Long userId = loginInfoUtil.getUserId(); Set userVedio = redisAutoTemplate.getSet(userId+":user:history:"); QueryWrapper id = new QueryWrapper().eq("status",1).eq("isDelete",0).orderByDesc("createTime").last("limit 10"); if (!userVedio.isEmpty()){ id.notIn("id", userVedio); } List vedios = videoService.list(id); if (vedios.size()<10){ Integer num = 10-vedios.size(); List list = videoService.list(Wrappers.lambdaQuery(TVideo.class).eq(TVideo::getStatus,1).eq(TVideo::getIsDelete,0).last("ORDER BY RAND() limit " + num)); vedios.addAll(list); } //获取用户的点赞记录 Set goods = redisAutoTemplate.getSet(userId+":user:good:"); for (TVideo vedio : vedios) { //获取该视频的点赞用户 Set goodsUser = redisAutoTemplate.getSet(vedio.getId()+":video:good:"); vedio.setLikeCount(goodsUser.size()); //判断当前用户是否点赞该视频 if (goods!=null&&goods.contains(vedio.getId().intValue())){ vedio.setIsGood(1); }else { vedio.setIsGood(0); } String str = redisAutoTemplate.getStr(vedio.getId() + ":views:"); if (str != null && str.isEmpty()) { vedio.setViews(0); }else { if (str != null) { vedio.setViews(Integer.valueOf(str)); } } } return ApiResult.success(vedios); } @ApiOperation(value = "获取已发布视频") @PutMapping("/get-has") public ApiResult> getHas(@RequestParam int pageNum, @RequestParam int pageSize) { Long userId = loginInfoUtil.getUserId(); PageInfo pageInfo = new PageInfo<>(pageNum, pageSize); List vedios = videoService.list(new QueryWrapper().eq("userId",userId).eq("status",1)); //获取用户的点赞记录 Set goods = redisAutoTemplate.getSet(userId+":user:good:"); for (TVideo vedio : vedios) { //获取该视频的点赞用户 Set goodsUser = redisAutoTemplate.getSet(vedio.getId()+":video:good:"); vedio.setLikeCount(goodsUser.size()); //判断当前用户是否点赞该视频 if (goods!=null&&goods.contains(vedio.getId().intValue())){ vedio.setIsGood(1); }else { vedio.setIsGood(0); } String str = redisAutoTemplate.getStr(vedio.getId() + ":views:"); if (str != null && str.isEmpty()) { vedio.setViews(0); }else { if (str != null) { vedio.setViews(Integer.valueOf(str)); } } } pageInfo.setRecords(vedios); return ApiResult.success(pageInfo); } @ApiOperation(value = "发布视频") @PutMapping("/put") public ApiResult put(@RequestBody PutVideoDto title) { Long userId = loginInfoUtil.getUserId(); TVideo tVideo = new TVideo(); tVideo.setVideoTitle(title.getTitle()); tVideo.setVideoFile(title.getUrl()); tVideo.setUserId(userId); tVideo.setStatus(1); videoService.save(tVideo); return ApiResult.success(tVideo); } @ApiOperation(value = "删除视频") @PostMapping("/delete/{id}") public ApiResult delete(@PathVariable Long id) { videoService.removeById(id); return ApiResult.success(); } @ApiOperation(value = "举报视频") @PostMapping("/report") public ApiResult report(@RequestBody ReportDto reportDto) { Long userId = loginInfoUtil.getUserId(); TVideoReport tVideoReport = new TVideoReport(); BeanUtils.copyProperties(reportDto,tVideoReport); tVideoReport.setUserId(userId); reportService.save(tVideoReport); return ApiResult.success(); } }