diff --git a/luoo_user/src/main/java/com/luoo/user/service/UserCollectService.java b/luoo_user/src/main/java/com/luoo/user/service/UserCollectService.java index 5b6c046..82f0b9f 100644 --- a/luoo_user/src/main/java/com/luoo/user/service/UserCollectService.java +++ b/luoo_user/src/main/java/com/luoo/user/service/UserCollectService.java @@ -1,8 +1,6 @@ package com.luoo.user.service; import java.util.Collections; -import java.util.Date; -import java.util.EnumMap; import java.util.HashSet; import java.util.LinkedList; import java.util.List; @@ -11,68 +9,91 @@ import java.util.Set; import java.util.stream.Collectors; import org.apache.commons.lang3.tuple.Pair; +import org.springframework.amqp.rabbit.core.RabbitTemplate; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; -import org.springframework.transaction.annotation.Transactional; - import com.luoo.user.dao.UserCollectDao; -import com.luoo.user.dto.UserCollectCount; import com.luoo.user.pojo.UserCollect; +import com.luoo.user.pojo.UserInfo; import api.StatusCode; +import constants.Constants; +import dto.UserMessageDto; import enums.CollectTypeEnum; -import enums.CollectTypeEnum.*; +import enums.MessageTypeEnum; import exception.BizException; @Service public class UserCollectService { + @Autowired + private RabbitTemplate rabbitTemplate; @Autowired private UserCollectDao userCollectDao; + @Autowired + private UserInfoService userInfoService; + public void saveCollect(String userId, String objectId, Integer collectType) { - addOrRemove(userId,objectId,collectType,true); + addOrRemove(userId, objectId, collectType, true); } private void addOrRemove(String userId, String objectId, Integer collectType, boolean isAdd) { CollectTypeEnum collectTypeEnum = CollectTypeEnum.getByType(collectType); - if (null == collectTypeEnum) { - throw new BizException(StatusCode.VALIDATE_FAILED); - } - Optional dbCollect = userCollectDao.findById(userId); - UserCollect userCollect=dbCollect.isPresent()?dbCollect.get():createUserCollect(userId); - switch (collectTypeEnum) { - case SONG: - addOrRemove(isAdd,userCollect.getSongs(),objectId); - break; - case JOURNAL: - addOrRemove(isAdd,userCollect.getJournals(),objectId); - break; - case FOLLOWS: - Optional followsDbCollect = userCollectDao.findById(objectId); - UserCollect followsUserCollect=followsDbCollect.isPresent()?followsDbCollect.get():createUserCollect(objectId); - addOrRemove(isAdd,userCollect.getFollows(),objectId,followsUserCollect.getFans(),userId); - userCollectDao.save(followsUserCollect); - break; - case FANS: - Optional fansDbCollect = userCollectDao.findById(objectId); - UserCollect fansUserCollect=fansDbCollect.isPresent()?fansDbCollect.get():createUserCollect(objectId); - addOrRemove(isAdd,userCollect.getFans(),objectId,fansUserCollect.getFollows(),userId); - userCollectDao.save(fansUserCollect); - break; - case BLACK_LIST: - addOrRemove(isAdd,userCollect.getBlackList(),objectId); - break; - default: - break; - } - userCollectDao.save(userCollect); + if (null == collectTypeEnum) { + throw new BizException(StatusCode.VALIDATE_FAILED); + } + Optional dbCollect = userCollectDao.findById(userId); + UserCollect userCollect = dbCollect.isPresent() ? dbCollect.get() : createUserCollect(userId); + switch (collectTypeEnum) { + case SONG: + addOrRemove(isAdd, userCollect.getSongs(), objectId); + break; + case JOURNAL: + addOrRemove(isAdd, userCollect.getJournals(), objectId); + break; + case FOLLOWS: + Optional followsDbCollect = userCollectDao.findById(objectId); + UserCollect followsUserCollect = followsDbCollect.isPresent() ? followsDbCollect.get() + : createUserCollect(objectId); + addOrRemove(isAdd, userCollect.getFollows(), objectId, followsUserCollect.getFans(), userId); + userCollectDao.save(followsUserCollect); + if (isAdd) { + sendMessageToFollows(userId, objectId); + } + break; + case FANS: + Optional fansDbCollect = userCollectDao.findById(objectId); + UserCollect fansUserCollect = fansDbCollect.isPresent() ? fansDbCollect.get() : createUserCollect(objectId); + addOrRemove(isAdd, userCollect.getFans(), objectId, fansUserCollect.getFollows(), userId); + userCollectDao.save(fansUserCollect); + break; + case BLACK_LIST: + addOrRemove(isAdd, userCollect.getBlackList(), objectId); + break; + default: + break; + } + userCollectDao.save(userCollect); + } + + private void sendMessageToFollows(String userId, String followId) { + UserInfo userInfo = userInfoService.findById(userId); + UserMessageDto userMessageDto = new UserMessageDto(); + userMessageDto.setType(MessageTypeEnum.FOLLOW.getType()); // 消息类型 1私信 ,2新赞,3新关注, 4新评论 5.系统消息 + userMessageDto.setUserId(followId); + userMessageDto.setContent(userInfo.getNickName() + " 关注了你"); + userMessageDto.setTitle("您有新的粉丝"); + userMessageDto.setSendUserAvatar(Constants.RESOURCE_PREFIX + userInfo.getAvatar()); + userMessageDto.setSendUserId(userId); + userMessageDto.setSendUserNickName(userInfo.getNickName()); + rabbitTemplate.convertAndSend("userMessage", userMessageDto); } private void addOrRemove(boolean isAdd, LinkedList follows, String objectId, LinkedList fans, String userId) { - if(isAdd) { + if (isAdd) { follows.addFirst(objectId); fans.addFirst(userId); - }else{ + } else { follows.removeFirstOccurrence(objectId); fans.removeFirstOccurrence(userId); } @@ -80,63 +101,41 @@ public class UserCollectService { private UserCollect createUserCollect(String userId) { UserCollect userCollect = new UserCollect(); - userCollect.setUserId(userId); - userCollect.setJournals(new LinkedList<>()); - userCollect.setSongs(new LinkedList<>()); - userCollect.setFollows(new LinkedList<>()); - userCollect.setFans(new LinkedList<>()); - userCollect.setBlackList(new LinkedList<>()); + userCollect.setUserId(userId); + userCollect.setJournals(new LinkedList<>()); + userCollect.setSongs(new LinkedList<>()); + userCollect.setFollows(new LinkedList<>()); + userCollect.setFans(new LinkedList<>()); + userCollect.setBlackList(new LinkedList<>()); return userCollect; } private void addOrRemove(boolean isAdd, LinkedList list, String objectId) { - if(isAdd) { + if (isAdd) { list.addFirst(objectId); - }else{ + } else { list.removeFirstOccurrence(objectId); } } - public void deleteByUserIdAndObjectIdAndCollectType(String userId, String objectId, - Integer collectType) { - addOrRemove(userId,objectId,collectType,false); + public void deleteByUserIdAndObjectIdAndCollectType(String userId, String objectId, Integer collectType) { + addOrRemove(userId, objectId, collectType, false); } public Optional findByUserId(String userId) { return userCollectDao.findById(userId); } - public List getCollectList(String userId, Integer pageNum, Integer pageSize, - CollectTypeEnum collectTypeEnum) { - if (null == userId) { - return Collections.emptyList(); - } - Optional optional = userCollectDao.findById(userId); - if (!optional.isPresent()) { - return Collections.emptyList(); - } - UserCollect userCollect = optional.get(); - switch (collectTypeEnum) { - case FOLLOWS: - return getPageResult(pageNum, pageSize, userCollect.getFollows()); - case FANS: - return getPageResult(pageNum, pageSize, userCollect.getFans()); - case BLACK_LIST: - return getPageResult(pageNum, pageSize, userCollect.getBlackList()); - default: - return Collections.emptyList(); - } - } private List getPageResult(Integer pageNum, Integer pageSize, LinkedList objectIds) { - int end=pageNum * pageSize; - if (null != pageNum && null != pageSize && pageNum > 0 && pageSize > 0&&end<=objectIds.size()) { + int end = pageNum * pageSize; + if (null != pageNum && null != pageSize && pageNum > 0 && pageSize > 0 && end <= objectIds.size()) { return objectIds.subList((pageNum - 1) * pageSize, end); } return objectIds; } - public Pair, Set> getCollectListWithBothFollowSet(String userId, Integer pageNum, Integer pageSize, - CollectTypeEnum collectTypeEnum) { + public Pair, Set> getCollectListWithBothFollowSet(String userId, Integer pageNum, + Integer pageSize, CollectTypeEnum collectTypeEnum) { if (null == userId) { return Pair.of(Collections.emptyList(), Collections.emptySet()); } @@ -147,14 +146,14 @@ public class UserCollectService { UserCollect userCollect = optional.get(); switch (collectTypeEnum) { case FOLLOWS: - List follows=getPageResult(pageNum, pageSize, userCollect.getFollows()); - Set fans=new HashSet<>(userCollect.getFans()); - Set bothFollowSet=follows.stream().filter(fans::contains).collect(Collectors.toSet()); + List follows = getPageResult(pageNum, pageSize, userCollect.getFollows()); + Set fans = new HashSet<>(userCollect.getFans()); + Set bothFollowSet = follows.stream().filter(fans::contains).collect(Collectors.toSet()); return Pair.of(follows, bothFollowSet); case FANS: - List fanList=getPageResult(pageNum, pageSize, userCollect.getFans()); - Set followList=new HashSet<>(userCollect.getFollows()); - Set bothFanSet=fanList.stream().filter(followList::contains).collect(Collectors.toSet()); + List fanList = getPageResult(pageNum, pageSize, userCollect.getFans()); + Set followList = new HashSet<>(userCollect.getFollows()); + Set bothFanSet = fanList.stream().filter(followList::contains).collect(Collectors.toSet()); return Pair.of(fanList, bothFanSet); case BLACK_LIST: return Pair.of(getPageResult(pageNum, pageSize, userCollect.getBlackList()), Collections.emptySet());