|
@@ -0,0 +1,129 @@
|
|
|
|
|
+package com.mirage.mirageservice.service;
|
|
|
|
|
+
|
|
|
|
|
+import com.google.common.collect.Lists;
|
|
|
|
|
+import com.mirage.core.utils.GsonUtil;
|
|
|
|
|
+import com.mirage.mirageservice.domain.FamilyMemberInfo;
|
|
|
|
|
+import com.mirage.mirageservice.domain.FamilyMoments;
|
|
|
|
|
+import com.mirage.mirageservice.enums.MomentsVisibleTypeEnum;
|
|
|
|
|
+import com.mirage.mirageservice.mapper.mysql.FamilyMemberInfoMapper;
|
|
|
|
|
+import com.mirage.mirageservice.meta.NewsRedDodInfo;
|
|
|
|
|
+import com.mirage.mirageservice.meta.UserAndMemberIdInfo;
|
|
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
+import org.springframework.data.redis.core.StringRedisTemplate;
|
|
|
|
|
+import org.springframework.scheduling.annotation.Async;
|
|
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
|
|
+
|
|
|
|
|
+import javax.annotation.Resource;
|
|
|
|
|
+import java.util.Date;
|
|
|
|
|
+import java.util.List;
|
|
|
|
|
+import java.util.Objects;
|
|
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
|
+
|
|
|
|
|
+/**
|
|
|
|
|
+ * Created by hzlinhai on 2025/10/27.
|
|
|
|
|
+ */
|
|
|
|
|
+@Service
|
|
|
|
|
+@Slf4j
|
|
|
|
|
+public class AsyncService {
|
|
|
|
|
+
|
|
|
|
|
+ private static final String REDIS_MOMENTS_NEWS_RED_DOT = "redis_moments_news_red_dot_";
|
|
|
|
|
+ @Resource
|
|
|
|
|
+ private StringRedisTemplate stringRedisTemplate;
|
|
|
|
|
+ @Resource
|
|
|
|
|
+ private FamilyMemberInfoMapper familyMemberInfoMapper;
|
|
|
|
|
+
|
|
|
|
|
+ @Async("asyncPublishEventExecutor")
|
|
|
|
|
+ public void commentAndLikePublishEvent(Long uid, Long commentId, FamilyMoments familyMoments){
|
|
|
|
|
+ if(null == uid
|
|
|
|
|
+ || null == familyMoments){
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+// REDIS_MOMENTS_NEWS_RED_DOT
|
|
|
|
|
+ List<UserAndMemberIdInfo> userAndMemberIdInfos = Lists.newArrayList();
|
|
|
|
|
+ // 可见范围:0:全员 1:支系堂派 2:聚落 3:家族内男性 4:出生地
|
|
|
|
|
+ if(Objects.equals(familyMoments.getVisibleType(), MomentsVisibleTypeEnum.ALL_MEMBER.getType())){
|
|
|
|
|
+ userAndMemberIdInfos = familyMemberInfoMapper.selectUidUserInnerJoin(null, null, null, null);
|
|
|
|
|
+ } else if(Objects.equals(familyMoments.getVisibleType(), MomentsVisibleTypeEnum.SPECIAL_BRANCH_HALL.getType())){
|
|
|
|
|
+ userAndMemberIdInfos = familyMemberInfoMapper.selectUidUserInnerJoin(familyMoments.getVisibleValue(), null, null, null);
|
|
|
|
|
+ } else if(Objects.equals(familyMoments.getVisibleType(), MomentsVisibleTypeEnum.SPECIAL_CLUSTER_PLACE.getType())){
|
|
|
|
|
+ userAndMemberIdInfos = familyMemberInfoMapper.selectUidUserInnerJoin(null, null, null, familyMoments.getVisibleValue());
|
|
|
|
|
+ } else if (Objects.equals(familyMoments.getVisibleType(), MomentsVisibleTypeEnum.SPECIAL_SEX.getType())) {
|
|
|
|
|
+ userAndMemberIdInfos = familyMemberInfoMapper.selectUidUserInnerJoin(null, null, Integer.parseInt(familyMoments.getVisibleValue()), null);
|
|
|
|
|
+ } else if (Objects.equals(familyMoments.getVisibleType(), MomentsVisibleTypeEnum.SPECIAL_BIRTH_PLACE.getType())) {
|
|
|
|
|
+ userAndMemberIdInfos = familyMemberInfoMapper.selectUidUserInnerJoin(null, familyMoments.getVisibleValue(), null, null);
|
|
|
|
|
+ }
|
|
|
|
|
+ List<Long> notfiyUidList = Lists.newArrayList();
|
|
|
|
|
+ if(null == userAndMemberIdInfos || userAndMemberIdInfos.isEmpty()){
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+ FamilyMemberInfo memberInfo = familyMemberInfoMapper.selectByUid(uid);
|
|
|
|
|
+ if(null == memberInfo){
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+ notfiyUidList = userAndMemberIdInfos.stream().map(UserAndMemberIdInfo::getUid).collect(Collectors.toList());
|
|
|
|
|
+ for(Long notifyUid : notfiyUidList){
|
|
|
|
|
+ if(Objects.equals(notifyUid, uid)){
|
|
|
|
|
+ continue;
|
|
|
|
|
+ }
|
|
|
|
|
+ NewsRedDodInfo newsRedDodInfo = new NewsRedDodInfo();
|
|
|
|
|
+ newsRedDodInfo.setType(1);
|
|
|
|
|
+ newsRedDodInfo.setEventTime(new Date());
|
|
|
|
|
+ newsRedDodInfo.setUid(uid);
|
|
|
|
|
+ newsRedDodInfo.setCommentId(commentId);
|
|
|
|
|
+ newsRedDodInfo.setName(memberInfo.getName());
|
|
|
|
|
+ newsRedDodInfo.setMomentId(familyMoments.getId());
|
|
|
|
|
+ newsRedDodInfo.setHeadImgUrl(memberInfo.getHeadImgUrl());
|
|
|
|
|
+ // 从队列右侧入队(rpush:FIFO队列,若用lpush则为栈结构)
|
|
|
|
|
+ stringRedisTemplate.opsForList().rightPush(REDIS_MOMENTS_NEWS_RED_DOT + notifyUid, GsonUtil.toJson(newsRedDodInfo));
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public List<NewsRedDodInfo> batchGetAndRemoveRedDotData(Long uid){
|
|
|
|
|
+ List<NewsRedDodInfo> result = Lists.newArrayList();
|
|
|
|
|
+ Long count = this.getRedDotQueueSize(uid);
|
|
|
|
|
+ if(null == count || count <= 0){
|
|
|
|
|
+ return result;
|
|
|
|
|
+ }
|
|
|
|
|
+ List<String> queueValueList = this.batchGetAndRemoveRedDotData(uid, count);
|
|
|
|
|
+ if(null == queueValueList || queueValueList.isEmpty()){
|
|
|
|
|
+ return result;
|
|
|
|
|
+ }
|
|
|
|
|
+ for(String cacheValue : queueValueList){
|
|
|
|
|
+ result.add(GsonUtil.fromJson(cacheValue, NewsRedDodInfo.class));
|
|
|
|
|
+ }
|
|
|
|
|
+ return result;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 从指定用户的红点队列中取出一条数据(出队,同时删除)
|
|
|
|
|
+ * @param uid 用户ID
|
|
|
|
|
+ * @return 取出的数据(无数据时返回null)
|
|
|
|
|
+ */
|
|
|
|
|
+ private String getAndRemoveRedDotData(Long uid) {
|
|
|
|
|
+ String key = REDIS_MOMENTS_NEWS_RED_DOT + uid;
|
|
|
|
|
+ // 从队列左侧出队(lpop:对应rightPush的FIFO顺序,出队后数据自动删除)
|
|
|
|
|
+ return stringRedisTemplate.opsForList().leftPop(key);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 批量取出指定用户的红点队列数据(出队,同时删除)
|
|
|
|
|
+ * @param uid 用户ID
|
|
|
|
|
+ * @param count 要取出的数量
|
|
|
|
|
+ * @return 取出的数据列表(无数据时返回空列表)
|
|
|
|
|
+ */
|
|
|
|
|
+ private List<String> batchGetAndRemoveRedDotData(Long uid, long count) {
|
|
|
|
|
+ String key = REDIS_MOMENTS_NEWS_RED_DOT + uid;
|
|
|
|
|
+ // 批量从左侧出队(最多返回count条,出队后数据自动删除)
|
|
|
|
|
+ return stringRedisTemplate.opsForList().leftPop(key, count);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 获取队列当前长度
|
|
|
|
|
+ * @param uid 用户ID
|
|
|
|
|
+ * @return 队列长度
|
|
|
|
|
+ */
|
|
|
|
|
+ private Long getRedDotQueueSize(Long uid) {
|
|
|
|
|
+ String key = REDIS_MOMENTS_NEWS_RED_DOT + uid;
|
|
|
|
|
+ return stringRedisTemplate.opsForList().size(key);
|
|
|
|
|
+ }
|
|
|
|
|
+}
|