|
|
|
@ -1,17 +1,26 @@ |
|
|
|
package com.bnyer.img.service.impl; |
|
|
|
|
|
|
|
import com.bnyer.common.core.utils.RedisKeyUtil; |
|
|
|
import com.bnyer.common.redis.service.RedisService; |
|
|
|
import com.bnyer.img.constants.PlatformConstant; |
|
|
|
import com.bnyer.img.constants.RedisKeyConstant; |
|
|
|
import com.bnyer.img.domain.Creator; |
|
|
|
import com.bnyer.img.mapper.CreatorMapper; |
|
|
|
import com.bnyer.img.service.FollowService; |
|
|
|
import com.bnyer.img.vo.CreatorFollowVo; |
|
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
|
import org.springframework.data.redis.core.RedisTemplate; |
|
|
|
import org.springframework.stereotype.Service; |
|
|
|
import org.springframework.transaction.annotation.Transactional; |
|
|
|
|
|
|
|
import java.util.*; |
|
|
|
|
|
|
|
/** |
|
|
|
* @author chengkun |
|
|
|
* @date 2022/9/17 16:18 |
|
|
|
*/ |
|
|
|
@Service |
|
|
|
@Slf4j |
|
|
|
public class FollowServiceImpl implements FollowService { |
|
|
|
|
|
|
|
@Autowired |
|
|
|
@ -20,18 +29,98 @@ public class FollowServiceImpl implements FollowService { |
|
|
|
@Autowired |
|
|
|
private RedisTemplate redisTemplate; |
|
|
|
|
|
|
|
@Autowired |
|
|
|
private CreatorMapper creatorMapper; |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Transactional(rollbackFor = Exception.class) |
|
|
|
public void follow(Long userId, Long creatorId, String platform) { |
|
|
|
//关注键
|
|
|
|
String followeeKey = RedisKeyUtil.getFolloweeKey(userId, creatorId, platform); |
|
|
|
//粉丝键
|
|
|
|
String followerKey = RedisKeyUtil.getFollowerKey(userId, creatorId, platform); |
|
|
|
|
|
|
|
String redisKey = null; |
|
|
|
if(platform.equals(PlatformConstant.TIKTOK)){ |
|
|
|
redisKey = RedisKeyConstant.TIKTOK_USER_FOLLOW_KEY + userId; |
|
|
|
}else if(platform.equals(PlatformConstant.FAST_HAND)){ |
|
|
|
redisKey = RedisKeyConstant.FH_USER_FOLLOW_KEY + userId; |
|
|
|
}else{ |
|
|
|
redisKey = RedisKeyConstant.WECHAT_USER_FOLLOW_KEY + userId; |
|
|
|
} |
|
|
|
//添加粉丝关注艺术家
|
|
|
|
Set<Long> creatorSet = new HashSet<>(); |
|
|
|
creatorSet.add(creatorId); |
|
|
|
redisService.setCacheSet(redisKey, creatorSet); |
|
|
|
//添加艺术家粉丝数量
|
|
|
|
redisService.hashIncr(RedisKeyConstant.CREATOR_FANS_NUM_KEY, String.valueOf(creatorId),1); |
|
|
|
log.debug("平台{}用户{}关注了艺术家{}",platform,userId,creatorId); |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
@Transactional(rollbackFor = Exception.class) |
|
|
|
public void unFollow(Long userId, Long creatorId, String platform) { |
|
|
|
String redisKey = null; |
|
|
|
if(platform.equals(PlatformConstant.TIKTOK)){ |
|
|
|
redisKey = RedisKeyConstant.TIKTOK_USER_FOLLOW_KEY + userId; |
|
|
|
}else if(platform.equals(PlatformConstant.FAST_HAND)){ |
|
|
|
redisKey = RedisKeyConstant.FH_USER_FOLLOW_KEY + userId; |
|
|
|
}else{ |
|
|
|
redisKey = RedisKeyConstant.WECHAT_USER_FOLLOW_KEY + userId; |
|
|
|
} |
|
|
|
//取消粉丝关注艺术家
|
|
|
|
if(redisService.hasSet(redisKey,creatorId)){ |
|
|
|
redisService.removeSet(redisKey,creatorId); |
|
|
|
//取消艺术家粉丝数量
|
|
|
|
redisService.hashIncr(RedisKeyConstant.CREATOR_FANS_NUM_KEY, String.valueOf(creatorId),-1); |
|
|
|
log.debug("平台{}用户{}取消关注了艺术家{}",platform,userId,creatorId); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public boolean checkFollow(Long userId, Long creatorId, String platform) { |
|
|
|
String redisKey = null; |
|
|
|
if(platform.equals(PlatformConstant.TIKTOK)){ |
|
|
|
redisKey = RedisKeyConstant.TIKTOK_USER_FOLLOW_KEY + userId; |
|
|
|
}else if(platform.equals(PlatformConstant.FAST_HAND)){ |
|
|
|
redisKey = RedisKeyConstant.FH_USER_FOLLOW_KEY + userId; |
|
|
|
}else{ |
|
|
|
redisKey = RedisKeyConstant.WECHAT_USER_FOLLOW_KEY + userId; |
|
|
|
} |
|
|
|
if(redisService.hasSet(redisKey,creatorId)){ |
|
|
|
return true; |
|
|
|
}else{ |
|
|
|
return false; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public List<CreatorFollowVo> queryFollowCreatorList(Long userId, String platform) { |
|
|
|
String redisKey = null; |
|
|
|
if(platform.equals(PlatformConstant.TIKTOK)){ |
|
|
|
redisKey = RedisKeyConstant.TIKTOK_USER_FOLLOW_KEY + userId; |
|
|
|
}else if(platform.equals(PlatformConstant.FAST_HAND)){ |
|
|
|
redisKey = RedisKeyConstant.FH_USER_FOLLOW_KEY + userId; |
|
|
|
}else{ |
|
|
|
redisKey = RedisKeyConstant.WECHAT_USER_FOLLOW_KEY + userId; |
|
|
|
} |
|
|
|
//TODO 此处需要优化
|
|
|
|
if(redisService.hasKey(redisKey)){ |
|
|
|
List<CreatorFollowVo> creatorList = new ArrayList<>(); |
|
|
|
Set<Long> cacheSet = redisService.getCacheSet(redisKey); |
|
|
|
for (Long aLong : cacheSet) { |
|
|
|
CreatorFollowVo followVo = creatorMapper.queryFollowCreatorBySet(aLong); |
|
|
|
creatorList.add(followVo); |
|
|
|
} |
|
|
|
return creatorList; |
|
|
|
}else{ |
|
|
|
return new ArrayList<>(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public Integer queryFansNum(Long creatorId) { |
|
|
|
String redisKey = RedisKeyConstant.CREATOR_FANS_NUM_KEY; |
|
|
|
if(redisService.hasHashKey(redisKey,String.valueOf(creatorId))){ |
|
|
|
return redisService.getCacheMapValue(redisKey, String.valueOf(creatorId)); |
|
|
|
}else{ |
|
|
|
return 0; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|