[fix]修改视频推送逻辑
This commit is contained in:
parent
53c12bcf1b
commit
6c4ffb26e9
@ -49,7 +49,7 @@ public class VlogController extends BaseInfoProperties {
|
|||||||
@Autowired
|
@Autowired
|
||||||
private IVlogPushService vlogPushService;
|
private IVlogPushService vlogPushService;
|
||||||
|
|
||||||
@PostMapping("vodCallBack")
|
@PostMapping("/vodCallBack")
|
||||||
public R<Void> vodCallBack(@RequestBody Map<String, Object> callbackData) {
|
public R<Void> vodCallBack(@RequestBody Map<String, Object> callbackData) {
|
||||||
try {
|
try {
|
||||||
// 解析回调事件类型
|
// 解析回调事件类型
|
||||||
@ -163,7 +163,7 @@ public class VlogController extends BaseInfoProperties {
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
@PostMapping("changeVlogStatus")
|
@PostMapping("/changeVlogStatus")
|
||||||
public R<Void> changeVlogStatus(@RequestParam String userId,
|
public R<Void> changeVlogStatus(@RequestParam String userId,
|
||||||
@RequestParam String vlogId,
|
@RequestParam String vlogId,
|
||||||
@RequestParam Integer status) {
|
@RequestParam Integer status) {
|
||||||
@ -171,7 +171,7 @@ public class VlogController extends BaseInfoProperties {
|
|||||||
return R.ok();
|
return R.ok();
|
||||||
}
|
}
|
||||||
|
|
||||||
@PostMapping("changeToPrivate")
|
@PostMapping("/changeToPrivate")
|
||||||
public R<Void> changeToPrivate(@RequestParam String userId,
|
public R<Void> changeToPrivate(@RequestParam String userId,
|
||||||
@RequestParam String vlogId) {
|
@RequestParam String vlogId) {
|
||||||
vlogService.changeToPrivateOrPublic(userId,
|
vlogService.changeToPrivateOrPublic(userId,
|
||||||
@ -180,7 +180,7 @@ public class VlogController extends BaseInfoProperties {
|
|||||||
return R.ok();
|
return R.ok();
|
||||||
}
|
}
|
||||||
|
|
||||||
@PostMapping("changeToPublic")
|
@PostMapping("/changeToPublic")
|
||||||
public R<Void> changeToPublic(@RequestParam String userId,
|
public R<Void> changeToPublic(@RequestParam String userId,
|
||||||
@RequestParam String vlogId) {
|
@RequestParam String vlogId) {
|
||||||
vlogService.changeToPrivateOrPublic(userId,
|
vlogService.changeToPrivateOrPublic(userId,
|
||||||
@ -193,7 +193,7 @@ public class VlogController extends BaseInfoProperties {
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
@PostMapping("totalLikedCounts")
|
@PostMapping("/totalLikedCounts")
|
||||||
public R<Integer> totalLikedCounts(@RequestParam String vlogId) {
|
public R<Integer> totalLikedCounts(@RequestParam String vlogId) {
|
||||||
return R.ok(vlogService.getVlogBeLikedCounts(vlogId));
|
return R.ok(vlogService.getVlogBeLikedCounts(vlogId));
|
||||||
}
|
}
|
||||||
|
@ -4,6 +4,7 @@ import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|||||||
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
|
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
|
||||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||||
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||||
import com.fasterxml.jackson.core.type.TypeReference;
|
import com.fasterxml.jackson.core.type.TypeReference;
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
@ -13,6 +14,7 @@ import com.wzj.soopin.content.domain.bo.*;
|
|||||||
import com.wzj.soopin.content.domain.po.MyLikedVlog;
|
import com.wzj.soopin.content.domain.po.MyLikedVlog;
|
||||||
import com.wzj.soopin.content.domain.po.Vlog;
|
import com.wzj.soopin.content.domain.po.Vlog;
|
||||||
import com.wzj.soopin.content.domain.po.Users;
|
import com.wzj.soopin.content.domain.po.Users;
|
||||||
|
import com.wzj.soopin.content.domain.vo.VlogerVO;
|
||||||
import com.wzj.soopin.member.domain.po.Member;
|
import com.wzj.soopin.member.domain.po.Member;
|
||||||
import com.wzj.soopin.content.domain.vo.IndexVlogVO;
|
import com.wzj.soopin.content.domain.vo.IndexVlogVO;
|
||||||
import com.wzj.soopin.content.enums.YesOrNo;
|
import com.wzj.soopin.content.enums.YesOrNo;
|
||||||
@ -32,12 +34,22 @@ import com.wzj.soopin.content.utils.TencentCloudUtil;
|
|||||||
import com.wzj.soopin.member.service.IFansService;
|
import com.wzj.soopin.member.service.IFansService;
|
||||||
import com.wzj.soopin.content.convert.VlogConvert;
|
import com.wzj.soopin.content.convert.VlogConvert;
|
||||||
import com.wzj.soopin.member.service.IMemberService;
|
import com.wzj.soopin.member.service.IMemberService;
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
import org.apache.commons.lang3.StringUtils;
|
import org.apache.commons.lang3.StringUtils;
|
||||||
|
import org.dromara.common.core.constant.GlobalConstants;
|
||||||
import org.dromara.common.core.domain.model.LoginUser;
|
import org.dromara.common.core.domain.model.LoginUser;
|
||||||
import org.dromara.common.core.exception.ServiceException;
|
import org.dromara.common.core.exception.ServiceException;
|
||||||
|
import org.dromara.common.mq.domain.MQMessage;
|
||||||
|
import org.dromara.common.mq.enums.MQMessageType;
|
||||||
|
import org.dromara.common.mq.enums.MessageActionEnum;
|
||||||
|
import org.dromara.common.mq.utils.MqUtil;
|
||||||
|
import org.dromara.common.mq.utils.UserRocketMQConsumerManager;
|
||||||
|
import org.dromara.common.redis.redis.RedisCache;
|
||||||
import org.dromara.common.satoken.utils.LoginHelper;
|
import org.dromara.common.satoken.utils.LoginHelper;
|
||||||
import org.springframework.beans.BeanUtils;
|
import org.springframework.beans.BeanUtils;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.cache.annotation.CacheEvict;
|
||||||
|
import org.springframework.cache.annotation.Cacheable;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
import org.dromara.common.core.utils.MapstructUtils;
|
import org.dromara.common.core.utils.MapstructUtils;
|
||||||
@ -49,36 +61,27 @@ import java.time.format.DateTimeFormatter;
|
|||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
|
||||||
|
import static com.wzj.soopin.content.domain.base.BaseInfoProperties.*;
|
||||||
|
|
||||||
@Slf4j
|
@Slf4j
|
||||||
@Service
|
@Service
|
||||||
public class VlogServiceImpl extends BaseInfoProperties implements VlogService {
|
@AllArgsConstructor
|
||||||
|
public class VlogServiceImpl extends ServiceImpl<VlogMapper, Vlog> implements VlogService {
|
||||||
|
|
||||||
|
private final VlogMapper vlogMapper;
|
||||||
|
private final VlogMapperCustom vlogMapperCustom;
|
||||||
|
private final MyLikedVlogMapper myLikedVlogMapper;
|
||||||
|
private final IFansService fansService;
|
||||||
|
private final MsgService msgService;
|
||||||
|
private final Sid sid;
|
||||||
|
private final CommentMapper commentMapper;
|
||||||
|
private final MemberMapper memberMapper;
|
||||||
|
private final RedisCache cache;
|
||||||
|
private final RedisCache redisCache;
|
||||||
|
|
||||||
@Autowired
|
|
||||||
private VlogMapper vlogMapper;
|
|
||||||
@Autowired
|
|
||||||
private VlogMapperCustom vlogMapperCustom;
|
|
||||||
@Autowired
|
|
||||||
private MyLikedVlogMapper myLikedVlogMapper;
|
|
||||||
@Autowired
|
|
||||||
private IFansService fansService;
|
|
||||||
@Autowired
|
|
||||||
private MsgService msgService;
|
|
||||||
@Autowired
|
|
||||||
private Sid sid;
|
|
||||||
@Autowired
|
|
||||||
private CommentMapper commentMapper;
|
|
||||||
@Autowired
|
|
||||||
private RedisOperator redis;
|
|
||||||
@Autowired
|
|
||||||
private TencentCloudUtil tencentCloudUtil;
|
|
||||||
@Autowired
|
|
||||||
private UsersMapper usersMapper;
|
|
||||||
@Autowired
|
|
||||||
private MemberMapper memberMapper;
|
|
||||||
@Autowired
|
|
||||||
private IMemberService memberService;
|
|
||||||
@Transactional(rollbackFor = Exception.class)
|
@Transactional(rollbackFor = Exception.class)
|
||||||
@Override
|
@Override
|
||||||
public void updateVlogStatus(String fileId, Integer status, String reason) {
|
public void updateVlogStatus(String fileId, Integer status, String reason) {
|
||||||
@ -138,143 +141,26 @@ public class VlogServiceImpl extends BaseInfoProperties implements VlogService {
|
|||||||
vlogMapper.insert(vlog);
|
vlogMapper.insert(vlog);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public Page<IndexVlogVO> getIndexVlogList(IndexListBO bo, Page page) {
|
|
||||||
String userId = bo.getMemberId();
|
|
||||||
String search = bo.getSearch();
|
|
||||||
String cityCode = bo.getCityCode();
|
|
||||||
String status = bo.getStatus();
|
|
||||||
int current = (int) page.getCurrent();
|
|
||||||
int size = (int) page.getSize();
|
|
||||||
Page<Vlog> pageParam = new Page<>(current, size);
|
|
||||||
LambdaQueryWrapper<Vlog> queryWrapper = new LambdaQueryWrapper<>();
|
|
||||||
if (StringUtils.isNotBlank(search)) {
|
|
||||||
queryWrapper.like(Vlog::getTitle, search);
|
|
||||||
}
|
|
||||||
if (StringUtils.isNotBlank(cityCode)) {
|
|
||||||
queryWrapper.eq(Vlog::getCityCode, cityCode);
|
|
||||||
}
|
|
||||||
if (StringUtils.isNotBlank(status)) {
|
|
||||||
queryWrapper.eq(Vlog::getStatus, status);
|
|
||||||
}
|
|
||||||
// 去掉黑名单的视频
|
|
||||||
if (StringUtils.isNotBlank(userId)) {
|
|
||||||
String redisKey = REDIS_VIDEO_BLOCK + ":" + userId;
|
|
||||||
if (redis.keyIsExist(redisKey)) {
|
|
||||||
List<String> blockVdList = new ArrayList<>();
|
|
||||||
ObjectMapper objectMapper = new ObjectMapper();
|
|
||||||
List<String> reports = redis.lrange(redisKey, 0, -1);
|
|
||||||
for (String report : reports) {
|
|
||||||
try {
|
|
||||||
Map<String, Object> reportMap = objectMapper.readValue(report, new TypeReference<Map<String, Object>>() {});
|
|
||||||
String vlogId = (String) reportMap.get("vlogId");
|
|
||||||
blockVdList.add(vlogId);
|
|
||||||
} catch (JsonProcessingException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (!blockVdList.isEmpty()) {
|
|
||||||
queryWrapper.notIn(Vlog::getId, blockVdList);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// 去掉黑名单的用户
|
|
||||||
if (StringUtils.isNotBlank(userId)) {
|
|
||||||
String redisKey = REDIS_USER_BLOCK + ":" + userId;
|
|
||||||
if (redis.keyIsExist(redisKey)) {
|
|
||||||
List<String> blockUserList = new ArrayList<>();
|
|
||||||
ObjectMapper objectMapper = new ObjectMapper();
|
|
||||||
List<String> reports = redis.lrange(redisKey, 0, -1);
|
|
||||||
for (String report : reports) {
|
|
||||||
try {
|
|
||||||
Map<String, Object> reportMap = objectMapper.readValue(report, new TypeReference<Map<String, Object>>() {});
|
|
||||||
String memberId = (String) reportMap.get("MemberId");
|
|
||||||
blockUserList.add(memberId);
|
|
||||||
} catch (JsonProcessingException e) {
|
|
||||||
log.error(e.getMessage());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (!blockUserList.isEmpty()) {
|
|
||||||
queryWrapper.notIn(Vlog::getMemberId, blockUserList);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// 去掉已读的视频
|
|
||||||
if (StringUtils.isNotBlank(userId)) {
|
|
||||||
String redisKey = REDIS_USER_READ_VLOG + ":" + userId;
|
|
||||||
if (redis.keyIsExist(redisKey)) {
|
|
||||||
List<String> readVlogList = new ArrayList<>();
|
|
||||||
ObjectMapper objectMapper = new ObjectMapper();
|
|
||||||
List<String> reports = redis.lrange(redisKey, 0, -1);
|
|
||||||
for (String report : reports) {
|
|
||||||
try {
|
|
||||||
Map<String, Object> reportMap = objectMapper.readValue(report, new TypeReference<Map<String, Object>>() {});
|
|
||||||
String memberId = (String) reportMap.get("MemberId");
|
|
||||||
readVlogList.add(memberId);
|
|
||||||
} catch (JsonProcessingException e) {
|
|
||||||
log.error(e.getMessage());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (!readVlogList.isEmpty()) {
|
|
||||||
queryWrapper.notIn(Vlog::getMemberId, readVlogList);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Page<Vlog> vlogPage = vlogMapper.selectPage(pageParam, queryWrapper);
|
|
||||||
List<Vlog> vlogList = vlogPage.getRecords();
|
|
||||||
List<IndexVlogVO> voList = vlogList.stream().map(v -> {
|
|
||||||
IndexVlogVO vo = MapstructUtils.convert(v, IndexVlogVO.class);
|
|
||||||
if (StringUtils.isNotBlank(userId)) {
|
|
||||||
vo.setDoIFollowVloger(fansService.queryDoIFollowVloger(userId, v.getMemberId()));
|
|
||||||
vo.setDoILikeThisVlog(doILikeVlog(userId, v.getId()));
|
|
||||||
}
|
|
||||||
vo.setLikeCounts(getVlogBeLikedCounts(v.getId()));
|
|
||||||
vo.setCommentsCounts(getVlogComment(v.getId()));
|
|
||||||
MemberVO m =memberService.getMemberInfo(vo.getMemberId());
|
|
||||||
if (m != null) {
|
|
||||||
vo.setAvatar(m.getAvatar());
|
|
||||||
vo.setNickname(m.getNickname());
|
|
||||||
}
|
|
||||||
return vo;
|
|
||||||
}).collect(Collectors.toList());
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// 封装分页结果
|
|
||||||
Page<IndexVlogVO> gridResult = new Page<IndexVlogVO>();
|
|
||||||
gridResult.setRecords(voList); // 当前页数据列表
|
|
||||||
gridResult.setCurrent(current); // 当前页码
|
|
||||||
gridResult.setTotal(vlogPage.getTotal()); // 总记录数
|
|
||||||
gridResult.setSize(vlogPage.getPages()); // 总页数
|
|
||||||
|
|
||||||
|
|
||||||
return gridResult;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Integer getVlogBeLikedCounts(String vlogId) {
|
public Integer getVlogBeLikedCounts(String vlogId) {
|
||||||
String countsStr = redis.get(REDIS_VLOG_BE_LIKED_COUNTS + ":" + vlogId);
|
Double count = cache.zSetScore(REDIS_VLOG_BE_LIKED_COUNTS, vlogId);
|
||||||
if (StringUtils.isBlank(countsStr)) {
|
|
||||||
countsStr = "0";
|
return count==null?0:count.intValue();
|
||||||
}
|
|
||||||
return Integer.valueOf(countsStr);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private Integer getVlogComment(String vlogId) {
|
private Integer getVlogComment(String vlogId) {
|
||||||
String countsStr = redis.get(REDIS_VLOG_COMMENT_COUNTS + ":" + vlogId);
|
Double count = cache.zSetScore(REDIS_VLOG_COMMENT_COUNTS, vlogId);
|
||||||
if (StringUtils.isBlank(countsStr)) {
|
return count==null?0:count.intValue();
|
||||||
countsStr = "0";
|
|
||||||
}
|
|
||||||
return Integer.valueOf(countsStr);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean doILikeVlog(String myId, String vlogId) {
|
private boolean doILikeVlog(String myId, String vlogId) {
|
||||||
String doILike = redis.get(REDIS_USER_LIKE_VLOG + ":" + myId + ":" + vlogId);
|
String doILike = cache.getCacheObject(REDIS_USER_LIKE_VLOG + ":" + myId + ":" + vlogId);
|
||||||
return StringUtils.isNotBlank(doILike) && doILike.equalsIgnoreCase("1");
|
return StringUtils.isNotBlank(doILike) && doILike.equalsIgnoreCase("1");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@Cacheable(cacheNames = GlobalConstants.VLOG_KEY, key = "#vlogId")
|
||||||
public IndexVlogVO getVlogDetailById(String vlogId) {
|
public IndexVlogVO getVlogDetailById(String vlogId) {
|
||||||
Vlog vlog = vlogMapper.selectById(vlogId);
|
Vlog vlog = vlogMapper.selectById(vlogId);
|
||||||
if (vlog == null) {
|
if (vlog == null) {
|
||||||
@ -307,6 +193,8 @@ public class VlogServiceImpl extends BaseInfoProperties implements VlogService {
|
|||||||
|
|
||||||
@Transactional(rollbackFor = Exception.class)
|
@Transactional(rollbackFor = Exception.class)
|
||||||
@Override
|
@Override
|
||||||
|
|
||||||
|
@CacheEvict(value = GlobalConstants.VLOG_KEY, key = "#vlogId")
|
||||||
public void changeToPrivateOrPublic(String userId, String vlogId, Integer yesOrNo) {
|
public void changeToPrivateOrPublic(String userId, String vlogId, Integer yesOrNo) {
|
||||||
LambdaUpdateWrapper<Vlog> updateWrapper = new LambdaUpdateWrapper<>();
|
LambdaUpdateWrapper<Vlog> updateWrapper = new LambdaUpdateWrapper<>();
|
||||||
updateWrapper.eq(Vlog::getId, vlogId)
|
updateWrapper.eq(Vlog::getId, vlogId)
|
||||||
@ -317,6 +205,7 @@ public class VlogServiceImpl extends BaseInfoProperties implements VlogService {
|
|||||||
|
|
||||||
@Transactional(rollbackFor = Exception.class)
|
@Transactional(rollbackFor = Exception.class)
|
||||||
@Override
|
@Override
|
||||||
|
@CacheEvict(value = GlobalConstants.VLOG_KEY, key = "#vlogId")
|
||||||
public void changeVlogStatus(String userId, String vlogId, Integer status) {
|
public void changeVlogStatus(String userId, String vlogId, Integer status) {
|
||||||
LambdaUpdateWrapper<Vlog> updateWrapper = new LambdaUpdateWrapper<>();
|
LambdaUpdateWrapper<Vlog> updateWrapper = new LambdaUpdateWrapper<>();
|
||||||
updateWrapper.eq(Vlog::getId, vlogId)
|
updateWrapper.eq(Vlog::getId, vlogId)
|
||||||
@ -380,7 +269,15 @@ public class VlogServiceImpl extends BaseInfoProperties implements VlogService {
|
|||||||
|
|
||||||
@Transactional(rollbackFor = Exception.class)
|
@Transactional(rollbackFor = Exception.class)
|
||||||
@Override
|
@Override
|
||||||
|
@CacheEvict(value = GlobalConstants.VLOG_KEY, key = "#vlogId")
|
||||||
public void userLikeVlog(String userId, String vlogId) {
|
public void userLikeVlog(String userId, String vlogId) {
|
||||||
|
//获取vlog
|
||||||
|
Vlog vlog = this.getVlog(vlogId);
|
||||||
|
if (vlog == null) {
|
||||||
|
throw new ServiceException("视频不存在");
|
||||||
|
}
|
||||||
|
// 我点赞的视频,关联关系保存到数据库
|
||||||
|
|
||||||
String rid = sid.nextShort();
|
String rid = sid.nextShort();
|
||||||
MyLikedVlog likedVlog = new MyLikedVlog();
|
MyLikedVlog likedVlog = new MyLikedVlog();
|
||||||
likedVlog.setId(rid);
|
likedVlog.setId(rid);
|
||||||
@ -388,36 +285,32 @@ public class VlogServiceImpl extends BaseInfoProperties implements VlogService {
|
|||||||
likedVlog.setUserId(userId);
|
likedVlog.setUserId(userId);
|
||||||
myLikedVlogMapper.insert(likedVlog);
|
myLikedVlogMapper.insert(likedVlog);
|
||||||
|
|
||||||
// 我喜欢的视频总数累加
|
// 点赞后,视频和视频发布者的获赞都会 +1
|
||||||
redis.increment(REDIS_VLOGER_BE_LIKED_COUNTS + ":" + userId, 1);
|
cache.zSetIncrement(REDIS_VLOGER_BE_LIKED_COUNTS, vlog.getMemberId(), 1);
|
||||||
// 视频被喜欢的总数累加
|
cache.zSetIncrement(REDIS_VLOG_BE_LIKED_COUNTS, vlogId, 1);
|
||||||
redis.increment(REDIS_VLOG_BE_LIKED_COUNTS + ":" + vlogId, 1);
|
|
||||||
// 保存用户和视频的喜欢关系
|
|
||||||
redis.set(REDIS_USER_LIKE_VLOG + ":" + userId + ":" + vlogId, "1");
|
|
||||||
|
|
||||||
// 发送点赞通知
|
// 我点赞的视频,需要在redis中保存关联关系
|
||||||
Vlog vlog = vlogMapper.selectById(vlogId);
|
cache.setCacheObject(REDIS_USER_LIKE_VLOG + ":" + userId + ":" + vlogId, "1");
|
||||||
if (vlog != null) {
|
Double count = cache.zSetScore(REDIS_VLOG_BE_LIKED_COUNTS, vlogId);
|
||||||
String vlogerId = vlog.getMemberId();
|
if (count > 0) {
|
||||||
if (!userId.equals(vlogerId)) {
|
//更新数据库的点赞数
|
||||||
// Long templateId = 1938491299175723009L;
|
this.flushCounts(vlogId, count.intValue());
|
||||||
// SysMessageTemplateVo template = templateService.selectTemplateById(templateId);
|
|
||||||
// if (template != null) {
|
|
||||||
// // 查询点赞用户昵称
|
|
||||||
// Member liker = memberMapper.selectById(userId);
|
|
||||||
// String likerNickname = liker != null && liker.getNickname() != null ? liker.getNickname() : "";
|
|
||||||
//
|
|
||||||
// String content = template.getTemplateContent()
|
|
||||||
// .replace("${videoTitle}", vlog.getTitle() == null ? "" : vlog.getTitle())
|
|
||||||
// .replace("${liker}", userId)
|
|
||||||
// .replace("${likerNickname}", likerNickname);
|
|
||||||
// SysMessageBo messageBo = new SysMessageBo();
|
|
||||||
// messageBo.setTitle(template.getTitle());
|
|
||||||
// messageBo.setContent(content);
|
|
||||||
// messageBo.setSenderId(Long.valueOf(userId));
|
|
||||||
// sysMessageService.sendMessageToUser(messageBo, Long.valueOf(vlogerId));
|
|
||||||
// }
|
|
||||||
}
|
}
|
||||||
|
//发送消息
|
||||||
|
if (userId != null && vlog.getMemberId() != null && !userId.equals(vlog.getMemberId())) {
|
||||||
|
// 新版:使用模板类型编号和参数
|
||||||
|
Map<String, Object> params = new HashMap<>();
|
||||||
|
params.put("userId", userId);
|
||||||
|
params.put("nickname", LoginHelper.getLoginUser().getNickname());
|
||||||
|
params.put("action", MessageActionEnum.INTERACTION_LIKE.name());
|
||||||
|
params.put("toUserId", vlog.getMemberId());
|
||||||
|
MQMessage message = MQMessage.builder()
|
||||||
|
.messageType(MQMessageType.IM.name())
|
||||||
|
.data(params)
|
||||||
|
.source("member")
|
||||||
|
.build();
|
||||||
|
// 关注消息
|
||||||
|
MqUtil.sendIMMessage(message);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -437,18 +330,28 @@ public class VlogServiceImpl extends BaseInfoProperties implements VlogService {
|
|||||||
|
|
||||||
@Transactional(rollbackFor = Exception.class)
|
@Transactional(rollbackFor = Exception.class)
|
||||||
@Override
|
@Override
|
||||||
|
@CacheEvict(value = GlobalConstants.VLOG_KEY, key = "#vlogId")
|
||||||
public void userUnLikeVlog(String userId, String vlogId) {
|
public void userUnLikeVlog(String userId, String vlogId) {
|
||||||
LambdaQueryWrapper<MyLikedVlog> queryWrapper = new LambdaQueryWrapper<>();
|
|
||||||
queryWrapper.eq(MyLikedVlog::getUserId, userId)
|
|
||||||
.eq(MyLikedVlog::getVlogId, vlogId);
|
|
||||||
myLikedVlogMapper.delete(queryWrapper);
|
|
||||||
|
|
||||||
// 我喜欢的视频总数累减
|
//获取vlog
|
||||||
redis.decrement(REDIS_VLOGER_BE_LIKED_COUNTS + ":" + userId, 1);
|
Vlog vlog = this.getVlog(vlogId);
|
||||||
// 视频被喜欢的总数累减
|
if (vlog == null) {
|
||||||
redis.decrement(REDIS_VLOG_BE_LIKED_COUNTS + ":" + vlogId, 1);
|
throw new ServiceException("视频不存在");
|
||||||
// 删除用户和视频的喜欢关系
|
}
|
||||||
redis.del(REDIS_USER_LIKE_VLOG + ":" + userId + ":" + vlogId);
|
|
||||||
|
// 我取消点赞的视频,关联关系删除
|
||||||
|
this.userUnLikeVlog(userId, vlogId);
|
||||||
|
|
||||||
|
cache.zSetDecrement(REDIS_VLOGER_BE_LIKED_COUNTS, vlog.getMemberId(), 1);
|
||||||
|
cache.zSetDecrement(REDIS_VLOG_BE_LIKED_COUNTS, vlogId, 1);
|
||||||
|
cache.deleteObject(REDIS_USER_LIKE_VLOG + ":" + userId + ":" + vlogId);
|
||||||
|
|
||||||
|
|
||||||
|
Double count = cache.zSetScore(REDIS_VLOG_BE_LIKED_COUNTS, vlogId);
|
||||||
|
if (count > 0) {
|
||||||
|
//更新数据库的点赞数
|
||||||
|
this.flushCounts(vlogId, count.intValue());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -520,7 +423,7 @@ public class VlogServiceImpl extends BaseInfoProperties implements VlogService {
|
|||||||
// 这里不再统计,避免重复计算
|
// 这里不再统计,避免重复计算
|
||||||
|
|
||||||
// 获取粉丝数量:优先 Redis,无则 MySQL
|
// 获取粉丝数量:优先 Redis,无则 MySQL
|
||||||
String fansCountsStr = redis.get(REDIS_MY_FANS_COUNTS + ":" + vlog.getMemberId());
|
String fansCountsStr = cache.getCacheObject(REDIS_MY_FANS_COUNTS + ":" + vlog.getMemberId());
|
||||||
Integer fansCounts = 0;
|
Integer fansCounts = 0;
|
||||||
if (StringUtils.isNotBlank(fansCountsStr)) {
|
if (StringUtils.isNotBlank(fansCountsStr)) {
|
||||||
fansCounts = Integer.valueOf(fansCountsStr);
|
fansCounts = Integer.valueOf(fansCountsStr);
|
||||||
@ -560,16 +463,8 @@ public class VlogServiceImpl extends BaseInfoProperties implements VlogService {
|
|||||||
result.put("comments", comments);
|
result.put("comments", comments);
|
||||||
|
|
||||||
// 从Redis获取点赞数
|
// 从Redis获取点赞数
|
||||||
String likeCountsStr = redis.get(REDIS_VLOG_BE_LIKED_COUNTS + ":" + vlog.getId());
|
int likeCounts = getVlogBeLikedCounts(vlog.getId());
|
||||||
Integer likeCounts = 0;
|
|
||||||
if (StringUtils.isNotBlank(likeCountsStr)) {
|
|
||||||
try {
|
|
||||||
likeCounts = Integer.valueOf(likeCountsStr);
|
|
||||||
} catch (NumberFormatException e) {
|
|
||||||
log.warn("Redis中视频{}的点赞数格式错误: {}", vlog.getId(), likeCountsStr);
|
|
||||||
likeCounts = 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
result.put("likeCounts", likeCounts);
|
result.put("likeCounts", likeCounts);
|
||||||
result.put("vlogId", vlog.getId());
|
result.put("vlogId", vlog.getId());
|
||||||
|
|
||||||
@ -608,20 +503,6 @@ public class VlogServiceImpl extends BaseInfoProperties implements VlogService {
|
|||||||
return myLikedVlogMapper.selectLikedUsersByVlogId(vlogId);
|
return myLikedVlogMapper.selectLikedUsersByVlogId(vlogId);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public int getLikeCounts(String vlogId) {
|
|
||||||
// 从Redis获取点赞数
|
|
||||||
String likeCountsStr = redis.get(REDIS_VLOG_BE_LIKED_COUNTS + ":" + vlogId);
|
|
||||||
if (StringUtils.isNotBlank(likeCountsStr)) {
|
|
||||||
try {
|
|
||||||
return Integer.valueOf(likeCountsStr);
|
|
||||||
} catch (NumberFormatException e) {
|
|
||||||
log.warn("Redis中视频{}的点赞数格式错误: {}", vlogId, likeCountsStr);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Map<String, String> getVlogUploaderInfo(String fileId) {
|
public Map<String, String> getVlogUploaderInfo(String fileId) {
|
||||||
@ -699,77 +580,6 @@ public class VlogServiceImpl extends BaseInfoProperties implements VlogService {
|
|||||||
return vlogMapper.selectVlogListWithAggregatedData(page, vlogBO);
|
return vlogMapper.selectVlogListWithAggregatedData(page, vlogBO);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void cacheTopLikedVlogs(int limit) {
|
|
||||||
try {
|
|
||||||
log.info("开始查询点赞最多的{}条视频", limit);
|
|
||||||
|
|
||||||
// 查询所有公开的视频列表
|
|
||||||
List<Map<String, Object>> allVlogs = vlogMapper.selectAllPublicVlogs();
|
|
||||||
|
|
||||||
if (allVlogs != null && !allVlogs.isEmpty()) {
|
|
||||||
// 从Redis获取每个视频的点赞数量
|
|
||||||
List<Map<String, Object>> vlogsWithLikeCounts = new ArrayList<>();
|
|
||||||
|
|
||||||
for (Map<String, Object> vlog : allVlogs) {
|
|
||||||
String vlogId = vlog.get("id").toString();
|
|
||||||
String redisLikeKey = REDIS_VLOG_BE_LIKED_COUNTS + ":" + vlogId;
|
|
||||||
String likeCountStr = redis.get(redisLikeKey);
|
|
||||||
|
|
||||||
// 获取Redis中的点赞数,如果没有则使用数据库中的默认值
|
|
||||||
int likeCount = 0;
|
|
||||||
if (likeCountStr != null && !likeCountStr.isEmpty()) {
|
|
||||||
try {
|
|
||||||
likeCount = Integer.parseInt(likeCountStr);
|
|
||||||
} catch (NumberFormatException e) {
|
|
||||||
log.warn("Redis中视频{}的点赞数格式错误: {}", vlogId, likeCountStr);
|
|
||||||
likeCount = 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 添加Redis中的点赞数到视频信息中
|
|
||||||
vlog.put("redis_like_count", likeCount);
|
|
||||||
vlogsWithLikeCounts.add(vlog);
|
|
||||||
}
|
|
||||||
|
|
||||||
// 先打乱顺序
|
|
||||||
Collections.shuffle(vlogsWithLikeCounts);
|
|
||||||
|
|
||||||
// 按Redis中的点赞数排序,获取前limit个
|
|
||||||
vlogsWithLikeCounts.sort((v1, v2) -> {
|
|
||||||
int count1 = (Integer) v1.get("redis_like_count");
|
|
||||||
int count2 = (Integer) v2.get("redis_like_count");
|
|
||||||
return Integer.compare(count2, count1); // 降序排列
|
|
||||||
});
|
|
||||||
|
|
||||||
// 取前limit个
|
|
||||||
List<Map<String, Object>> topLikedVlogs = vlogsWithLikeCounts.stream()
|
|
||||||
.limit(limit)
|
|
||||||
.collect(Collectors.toList());
|
|
||||||
|
|
||||||
if (!topLikedVlogs.isEmpty()) {
|
|
||||||
// 将结果存储到Redis中,使用JSON格式
|
|
||||||
ObjectMapper objectMapper = new ObjectMapper();
|
|
||||||
// 注册JavaTimeModule以支持LocalDateTime序列化
|
|
||||||
objectMapper.registerModule(new com.fasterxml.jackson.datatype.jsr310.JavaTimeModule());
|
|
||||||
objectMapper.disable(com.fasterxml.jackson.databind.SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
|
|
||||||
String jsonData = objectMapper.writeValueAsString(topLikedVlogs);
|
|
||||||
|
|
||||||
// 存储到Redis,设置24小时过期时间
|
|
||||||
String redisKey = "top_liked_vlogs:" + LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
|
|
||||||
redis.set(redisKey, jsonData, 24 * 60 * 60); // 24小时过期
|
|
||||||
|
|
||||||
log.info("成功缓存{}条点赞最多的视频到Redis,key: {}", topLikedVlogs.size(), redisKey);
|
|
||||||
} else {
|
|
||||||
log.warn("未查询到点赞最多的视频数据");
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
log.warn("未查询到公开的视频数据");
|
|
||||||
}
|
|
||||||
} catch (Exception e) {
|
|
||||||
log.error("缓存点赞最多视频到Redis失败", e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<Map<String, Object>> getRandomVlogs(int limit) {
|
public List<Map<String, Object>> getRandomVlogs(int limit) {
|
||||||
@ -783,4 +593,40 @@ public class VlogServiceImpl extends BaseInfoProperties implements VlogService {
|
|||||||
return new ArrayList<>();
|
return new ArrayList<>();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public IPage<String> getVlogForUser(Page<IndexVlogVO> page, Long memberId) {
|
||||||
|
return baseMapper.getVlogForUser(page, memberId);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
@Transactional(rollbackFor = Exception.class)
|
||||||
|
public int readVlog(Long memberId, String vlogId) {
|
||||||
|
//加入已读缓存列表,数量为1
|
||||||
|
if (!redisCache.zSetHasMember("vlog:read:" + memberId, vlogId)) {
|
||||||
|
redisCache.zSetAdd("vlog:read:" + memberId, vlogId, 1);
|
||||||
|
} else {
|
||||||
|
redisCache.zSetIncrement("vlog:read:" + memberId, vlogId, 1);
|
||||||
|
}
|
||||||
|
return baseMapper.readVlog(memberId, vlogId);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<IndexVlogVO> getIndexVlogList(Map<String, Object> paramMap) {
|
||||||
|
List<IndexVlogVO> indexVlogVOPage = vlogMapperCustom.getIndexVlogList(paramMap);
|
||||||
|
fillRedisColumn(indexVlogVOPage );
|
||||||
|
return indexVlogVOPage;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void fillRedisColumn(List<IndexVlogVO> vlogList) {
|
||||||
|
LoginUser user = LoginHelper.getLoginUser();
|
||||||
|
vlogList.parallelStream().forEach(vlog -> {
|
||||||
|
if (user != null) {
|
||||||
|
vlog.setDoIFollowVloger(fansService.queryDoIFollowVloger(user.getUserId() + "", vlog.getMemberId()));
|
||||||
|
vlog.setDoILikeThisVlog(doILikeVlog(user.getUserId() + "", vlog.getId()));
|
||||||
|
}
|
||||||
|
vlog.setLikeCounts(getVlogBeLikedCounts(vlog.getId()));
|
||||||
|
vlog.setCommentsCounts(getVlogComment(vlog.getId()));
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user