Commit 64e44947 authored by liwei's avatar liwei

修改了点赞定时任务

parent 86d79efd
...@@ -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 USER_LIKE_ALL = "user:like:all:"; public static final String USER_LIKE_ALL = "user:likeAll:";
//对每个人点赞的key //对每个人点赞的key
public static final String USER_LIKE = "user:like:"; public static final String USER_LIKE = "user:like:";
} }
...@@ -29,4 +29,14 @@ public class MemLikeDTO { ...@@ -29,4 +29,14 @@ public class MemLikeDTO {
* wxOpenid * wxOpenid
*/ */
private String wxOpenid; private String wxOpenid;
/**
* 开始时间
*/
private String beginTime;
/**
* 结束时间
*/
private String endTime;
} }
...@@ -44,6 +44,9 @@ import java.math.BigInteger; ...@@ -44,6 +44,9 @@ import java.math.BigInteger;
import java.net.URLDecoder; import java.net.URLDecoder;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.time.LocalDate; import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.*; import java.util.*;
import java.util.stream.Collectors; import java.util.stream.Collectors;
...@@ -658,7 +661,6 @@ public class MemInfoServiceImpl extends ServiceImpl<MemInfoRepository,MemInfo> ...@@ -658,7 +661,6 @@ public class MemInfoServiceImpl extends ServiceImpl<MemInfoRepository,MemInfo>
} }
} }
//没超出免费点赞次数 //没超出免费点赞次数
//插入对话表成功,则更新redis
this.redisTemplate.opsForHash().increment(MemberInfoRedisBean.USER_LIKE + userId, String.valueOf(likeDTO.getTargetId()), (double) 1L); this.redisTemplate.opsForHash().increment(MemberInfoRedisBean.USER_LIKE + userId, String.valueOf(likeDTO.getTargetId()), (double) 1L);
this.redisTemplate.opsForValue().increment(MemberInfoRedisBean.USER_LIKE_ALL + userId, (long) 1L); this.redisTemplate.opsForValue().increment(MemberInfoRedisBean.USER_LIKE_ALL + userId, (long) 1L);
return new CyPersistModel(1); return new CyPersistModel(1);
...@@ -666,6 +668,10 @@ public class MemInfoServiceImpl extends ServiceImpl<MemInfoRepository,MemInfo> ...@@ -666,6 +668,10 @@ public class MemInfoServiceImpl extends ServiceImpl<MemInfoRepository,MemInfo>
//今日第一次对该用户进行点赞 //今日第一次对该用户进行点赞
cyRedisServiceImpl.hset(MemberInfoRedisBean.USER_LIKE + userId, String.valueOf(likeDTO.getTargetId()), 1L); cyRedisServiceImpl.hset(MemberInfoRedisBean.USER_LIKE + userId, String.valueOf(likeDTO.getTargetId()), 1L);
this.redisTemplate.opsForValue().increment(MemberInfoRedisBean.USER_LIKE_ALL + userId, (long) 1L); this.redisTemplate.opsForValue().increment(MemberInfoRedisBean.USER_LIKE_ALL + userId, (long) 1L);
//新增点赞数据
likeDTO.setUserId(Integer.valueOf(userId));
likeDTO.setLikeCount(0);
memInfoRepository.addLike(likeDTO);
return new CyPersistModel(1); return new CyPersistModel(1);
} else { } else {
throw new CyServiceException(1001,"今日对该用户点赞次数超出限制"); throw new CyServiceException(1001,"今日对该用户点赞次数超出限制");
...@@ -675,6 +681,10 @@ public class MemInfoServiceImpl extends ServiceImpl<MemInfoRepository,MemInfo> ...@@ -675,6 +681,10 @@ public class MemInfoServiceImpl extends ServiceImpl<MemInfoRepository,MemInfo>
cyRedisServiceImpl.hset(MemberInfoRedisBean.USER_LIKE + userId, String.valueOf(likeDTO.getTargetId()), 1L); cyRedisServiceImpl.hset(MemberInfoRedisBean.USER_LIKE + userId, String.valueOf(likeDTO.getTargetId()), 1L);
cyRedisServiceImpl.set(MemberInfoRedisBean.USER_LIKE_ALL + userId,1L, Long.valueOf(24 * 60 * 60)); cyRedisServiceImpl.set(MemberInfoRedisBean.USER_LIKE_ALL + userId,1L, Long.valueOf(24 * 60 * 60));
cyRedisServiceImpl.expire(MemberInfoRedisBean.USER_LIKE + userId, 24 * 60 * 60); // 24小时 cyRedisServiceImpl.expire(MemberInfoRedisBean.USER_LIKE + userId, 24 * 60 * 60); // 24小时
//新增点赞数据
likeDTO.setUserId(Integer.valueOf(userId));
likeDTO.setLikeCount(0);
memInfoRepository.addLike(likeDTO);
return new CyPersistModel(1); return new CyPersistModel(1);
} else { } else {
//超出总次数限制 //超出总次数限制
...@@ -697,14 +707,35 @@ public class MemInfoServiceImpl extends ServiceImpl<MemInfoRepository,MemInfo> ...@@ -697,14 +707,35 @@ public class MemInfoServiceImpl extends ServiceImpl<MemInfoRepository,MemInfo>
* 用户点赞 定时任务 * 用户点赞 定时任务
*/ */
public void userLikeSchedule() { public void userLikeSchedule() {
log.info("用户点赞数据定时任务执行中...");
//获取当前时间
// 获取当前日期
LocalDate today = LocalDate.now();
// 获取今天的最小时间(00:00:00)
LocalDateTime startOfDay = today.atStartOfDay();
// 获取今天的最大时间(23:59:59)
LocalDateTime endOfDay = today.atTime(LocalTime.MAX);
// 定义日期时间格式
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
// 最大时间 和 最小时间
String beginTime = startOfDay.format(formatter);
String endTime = endOfDay.format(formatter);
//将redis中存储的点赞数同步到opm_user_like表中 //将redis中存储的点赞数同步到opm_user_like表中
Set<String> keys = redisTemplate.keys(MemberInfoRedisBean.USER_LIKE + "*"); Set<String> keys = redisTemplate.keys(MemberInfoRedisBean.USER_LIKE + "*");
//将redis中存储的点赞数同步到opm_user_like表中
Set<String> keys1 = redisTemplate.keys(MemberInfoRedisBean.USER_LIKE_ALL + "*");
//删除总数key
keys1.forEach(key -> {
redisTemplate.delete(key);
});
//删除1对多的key 并更新数据库
if (keys != null && !keys.isEmpty()) { if (keys != null && !keys.isEmpty()) {
for (String key : keys) { for (String key : keys) {
//截取key最后一个:后的值,为userId //截取key最后一个:后的值,为userId
int lastIndex = key.lastIndexOf(':'); int lastIndex = key.lastIndexOf(':');
Integer userId = Integer.valueOf(key.substring(lastIndex + 1)); Integer userId = Integer.valueOf(key.substring(lastIndex + 1));
//获取key的所有value //获取hash里的所有key value
Map<Object, Object> hmget = cyRedisService.hmget(key); Map<Object, Object> hmget = cyRedisService.hmget(key);
if (hmget != null && !hmget.isEmpty()) { if (hmget != null && !hmget.isEmpty()) {
//遍历map //遍历map
...@@ -718,17 +749,20 @@ public class MemInfoServiceImpl extends ServiceImpl<MemInfoRepository,MemInfo> ...@@ -718,17 +749,20 @@ public class MemInfoServiceImpl extends ServiceImpl<MemInfoRepository,MemInfo>
memLikeDTO.setUserId(userId); memLikeDTO.setUserId(userId);
memLikeDTO.setLikeCount(likeCount); memLikeDTO.setLikeCount(likeCount);
memLikeDTO.setTargetId(Integer.valueOf(targetId)); memLikeDTO.setTargetId(Integer.valueOf(targetId));
memLikeDTO.setBeginTime(beginTime);
memLikeDTO.setEndTime(endTime);
//对点赞表 进行增加点赞数 //对点赞表 进行增加点赞数
memInfoRepository.addLikeCount(memLikeDTO); Integer integer = memInfoRepository.addLikeCount(memLikeDTO);
//对会员表 进行增加被点赞数 //对会员表 进行增加被点赞数
memInfoRepository.addMemLikeCount(memLikeDTO); Integer integer1 = memInfoRepository.addMemLikeCount(memLikeDTO);
} }
} }
redisTemplate.delete(key);
} }
} else { } else {
System.out.println("No keys found."); System.out.println("No keys found.");
} }
log.info("用户点赞数据定时任务执行中..."); log.info("用户点赞数据定时任务结束---");
} }
......
...@@ -11,5 +11,5 @@ public class MemLeaveMessageRedisBean { ...@@ -11,5 +11,5 @@ public class MemLeaveMessageRedisBean {
//对个人的留言次数 //对个人的留言次数
public static final String USER_LEAVE_MESSAGE = "user:leaveMessage:"; public static final String USER_LEAVE_MESSAGE = "user:leaveMessage:";
//对所有人的留言次数 //对所有人的留言次数
public static final String USER_LEAVE_MESSAGE_All = "user:leaveMessage:all:"; public static final String USER_LEAVE_MESSAGE_All = "user:leaveMessageAll:";
} }
...@@ -667,6 +667,8 @@ ...@@ -667,6 +667,8 @@
where 1=1 where 1=1
and target_id = #{entity.targetId} and target_id = #{entity.targetId}
and user_id = #{entity.userId} and user_id = #{entity.userId}
and create_date &gt;= #{entity.beginTime}
and create_date &lt;= #{entity.endTime}
</insert> </insert>
<insert id="addMemLikeCount"> <insert id="addMemLikeCount">
update mem_info update mem_info
......
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