Commit 3ca5c1b4 authored by liwei's avatar liwei

修改了会员点赞功能

parent 381f8618
package org.rcisoft.app.task;
import lombok.extern.slf4j.Slf4j;
import org.rcisoft.business.memInfo.bean.MemberInfoRedisBean;
import org.rcisoft.business.memInfo.dao.MemInfoRepository;
import org.rcisoft.business.memInfo.entity.MemLikeDTO;
import org.rcisoft.core.service.CyRedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.util.Map;
import java.util.Set;
@Component
@Slf4j
@EnableScheduling
public class likeTask {
@Autowired
private CyRedisService cyRedisService;
@Autowired
private StringRedisTemplate redisTemplate;
@Autowired
private MemInfoRepository memInfoRepository;
/**
* 用户点赞数据定时任务 将点赞数据同步到对应的表中
* 会员表 点赞表
*/
@Scheduled(cron = "0 0 10 * * ?")
public void userLikeSchedule() {
//将redis中存储的点赞数同步到opm_user_like表中
Set<String> keys = redisTemplate.keys(MemberInfoRedisBean.MEMBERINFO_USERLIKE + "*");
if (keys != null && !keys.isEmpty()) {
for (String key : keys) {
//截取key最后一个:后的值,为userId
int lastIndex = key.lastIndexOf(':');
Integer userId = Integer.valueOf(key.substring(lastIndex + 1));
//获取key的所有value
Map<Object, Object> hmget = cyRedisService.hmget(key);
if (hmget != null && !hmget.isEmpty()) {
//遍历map
for (Map.Entry<Object, Object> entry : hmget.entrySet()) {
//获取目标
String targetId = (String) entry.getKey();
//获取点赞数
Integer likeCount = (Integer) entry.getValue();
//将redis中存储的点赞数同步到opm_user_like表中
MemLikeDTO memLikeDTO = new MemLikeDTO();
memLikeDTO.setUserId(userId);
memLikeDTO.setLikeCount(likeCount);
memLikeDTO.setTargetId(Integer.valueOf(targetId));
//对点赞表 进行增加点赞数
memInfoRepository.addLikeCount(memLikeDTO);
//对会员表 进行增加被点赞数
memInfoRepository.addMemLikeCount(memLikeDTO);
}
}
}
} else {
System.out.println("No keys found.");
}
log.info("用户点赞数据定时任务执行中...");
}
}
...@@ -9,7 +9,7 @@ package org.rcisoft.business.memInfo.bean; ...@@ -9,7 +9,7 @@ package org.rcisoft.business.memInfo.bean;
*/ */
public class MemberInfoRedisBean { public class MemberInfoRedisBean {
//总共的点赞key //总共的点赞key
public static final String MEMBERINFO_USERLIKE_ALL = "memberInfo:userLike:all:"; public static final String MEMBERINFO_USERLIKEALL = "memberInfo:userLikeAll:";
//对每个人点赞的key //对每个人点赞的key
public static final String MEMBERINFO_USERLIKE = "memberInfo:userLike:"; public static final String MEMBERINFO_USERLIKE = "memberInfo:userLike:";
} }
...@@ -38,15 +38,19 @@ public interface MemInfoRepository extends CyBaseMapper<MemInfo> { ...@@ -38,15 +38,19 @@ public interface MemInfoRepository extends CyBaseMapper<MemInfo> {
*/ */
List<Integer> selectRoleByKey(@Param("roleKey")String roleKey); List<Integer> selectRoleByKey(@Param("roleKey")String roleKey);
//校验手机号是否重复 /**
* 校验手机号是否重复
*/
SysUserRbacDTO checkPhoneRepeat(@Param("userId")Integer userId, @Param("phone")String phone); SysUserRbacDTO checkPhoneRepeat(@Param("userId")Integer userId, @Param("phone")String phone);
//校验身份证是否重复 /**
* 校验身份证是否重复
*/
SysUserRbacDTO checkIdcardRepeat(@Param("userId")Integer userId, @Param("Idcard")String Idcard); SysUserRbacDTO checkIdcardRepeat(@Param("userId")Integer userId, @Param("Idcard")String Idcard);
/** /**
* 查询详情 * 查询详情
* @param id * @param businessId
* @return * @return
*/ */
MemInfo selectDetailById(@Param("businessId")int businessId); MemInfo selectDetailById(@Param("businessId")int businessId);
...@@ -118,6 +122,16 @@ public interface MemInfoRepository extends CyBaseMapper<MemInfo> { ...@@ -118,6 +122,16 @@ public interface MemInfoRepository extends CyBaseMapper<MemInfo> {
*/ */
Integer addLike(@Param("entity") MemLikeDTO likeDTO); Integer addLike(@Param("entity") MemLikeDTO likeDTO);
/**
* 点赞表 增加点赞数
*/
Integer addLikeCount(@Param("entity") MemLikeDTO likeDTO);
/**
* 会员表 增加点赞数
*/
Integer addMemLikeCount(@Param("entity") MemLikeDTO likeDTO);
/** /**
* 查询关注的id集合 * 查询关注的id集合
*/ */
......
...@@ -553,13 +553,13 @@ public class MemInfoServiceImpl extends ServiceImpl<MemInfoRepository,MemInfo> ...@@ -553,13 +553,13 @@ public class MemInfoServiceImpl extends ServiceImpl<MemInfoRepository,MemInfo>
//一天最多给某个人点赞的总次数 //一天最多给某个人点赞的总次数
Integer dicCountToPersonal = Integer.valueOf(userLikeConfig.get(1).getDictValue()); Integer dicCountToPersonal = Integer.valueOf(userLikeConfig.get(1).getDictValue());
//redis中 该用户对所有用户的点赞次数 //redis中 该用户对所有用户的点赞次数
Object redisCountToAll = cyRedisServiceImpl.get(MemberInfoRedisBean.MEMBERINFO_USERLIKE_ALL + userId); Object redisCountToAll = cyRedisServiceImpl.get(MemberInfoRedisBean.MEMBERINFO_USERLIKEALL + userId);
//redis中 该用户对目标用户的点赞次数 //redis中 该用户对目标用户的点赞次数
Object redisCountToPersonal = cyRedisServiceImpl.hget(MemberInfoRedisBean.MEMBERINFO_USERLIKE + userId, String.valueOf(likeDTO.getTargetId())); Object redisCountToPersonal = cyRedisServiceImpl.hget(MemberInfoRedisBean.MEMBERINFO_USERLIKE + userId, String.valueOf(likeDTO.getTargetId()));
if (redisCountToAll == null && redisCountToPersonal == null){ if (redisCountToAll == null && redisCountToPersonal == null){
//用户第一次进行点赞 设置redis缓存 24小时 //用户第一次进行点赞 设置redis缓存 24小时
//对所有用户的点赞次数存储 //对所有用户的点赞次数存储
cyRedisServiceImpl.set(MemberInfoRedisBean.MEMBERINFO_USERLIKE_ALL + userId,1,86400L); cyRedisServiceImpl.set(MemberInfoRedisBean.MEMBERINFO_USERLIKEALL + userId,1,86400L);
cyRedisServiceImpl.hset(MemberInfoRedisBean.MEMBERINFO_USERLIKE + userId, String.valueOf(likeDTO.getTargetId()),1,86400L); cyRedisServiceImpl.hset(MemberInfoRedisBean.MEMBERINFO_USERLIKE + userId, String.valueOf(likeDTO.getTargetId()),1,86400L);
likeDTO.setUserId(userId); likeDTO.setUserId(userId);
int line = baseMapper.addLike(likeDTO); int line = baseMapper.addLike(likeDTO);
...@@ -569,7 +569,7 @@ public class MemInfoServiceImpl extends ServiceImpl<MemInfoRepository,MemInfo> ...@@ -569,7 +569,7 @@ public class MemInfoServiceImpl extends ServiceImpl<MemInfoRepository,MemInfo>
//判断点赞总数限制 //判断点赞总数限制
if (((int)redisCountToAll < dicCountToAll)){ if (((int)redisCountToAll < dicCountToAll)){
//条件限制都满足 点赞次数都+1 //条件限制都满足 点赞次数都+1
cyRedisServiceImpl.incr(MemberInfoRedisBean.MEMBERINFO_USERLIKE_ALL + userId, 1L); cyRedisServiceImpl.incr(MemberInfoRedisBean.MEMBERINFO_USERLIKEALL + userId, 1L);
cyRedisServiceImpl.hincr(MemberInfoRedisBean.MEMBERINFO_USERLIKE + userId, String.valueOf(likeDTO.getTargetId()), (double) 1L); cyRedisServiceImpl.hincr(MemberInfoRedisBean.MEMBERINFO_USERLIKE + userId, String.valueOf(likeDTO.getTargetId()), (double) 1L);
likeDTO.setUserId(userId); likeDTO.setUserId(userId);
int line = baseMapper.addLike(likeDTO); int line = baseMapper.addLike(likeDTO);
...@@ -583,7 +583,7 @@ public class MemInfoServiceImpl extends ServiceImpl<MemInfoRepository,MemInfo> ...@@ -583,7 +583,7 @@ public class MemInfoServiceImpl extends ServiceImpl<MemInfoRepository,MemInfo>
//判断个人点赞次数限制 //判断个人点赞次数限制
if ((int)redisCountToPersonal < dicCountToPersonal){ if ((int)redisCountToPersonal < dicCountToPersonal){
//条件限制都满足 点赞次数都+1 //条件限制都满足 点赞次数都+1
cyRedisServiceImpl.incr(MemberInfoRedisBean.MEMBERINFO_USERLIKE_ALL + userId, 1L); cyRedisServiceImpl.incr(MemberInfoRedisBean.MEMBERINFO_USERLIKEALL + userId, 1L);
cyRedisServiceImpl.hincr(MemberInfoRedisBean.MEMBERINFO_USERLIKE + userId, String.valueOf(likeDTO.getTargetId()), (double) 1L); cyRedisServiceImpl.hincr(MemberInfoRedisBean.MEMBERINFO_USERLIKE + userId, String.valueOf(likeDTO.getTargetId()), (double) 1L);
} else { } else {
throw new CyServiceException("点赞失败,今日对该用户的点赞次数已达上限"); throw new CyServiceException("点赞失败,今日对该用户的点赞次数已达上限");
......
...@@ -561,4 +561,17 @@ ...@@ -561,4 +561,17 @@
insert into opm_user_like (target_id,user_id,create_date,like_count) insert into opm_user_like (target_id,user_id,create_date,like_count)
values(#{entity.targetId},#{entity.userId},NOW(),#{entity.likeCount}) values(#{entity.targetId},#{entity.userId},NOW(),#{entity.likeCount})
</insert> </insert>
<insert id="addLikeCount">
update opm_user_like
set like_count = like_count + #{entity.likeCount}
where 1=1
and target_id = #{entity.targetId}
and user_id = #{entity.userId}
</insert>
<insert id="addMemLikeCount">
update mem_info
set mem_liked_count = mem_liked_count + #{entity.likeCount}
where 1=1
and user_id = #{entity.targetId}
</insert>
</mapper> </mapper>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment