1.remove unsed method and add sendMessageToFollows

main
Gary 12 months ago
parent 0ca110f4df
commit 77e539ffb1

@ -1,8 +1,6 @@
package com.luoo.user.service; package com.luoo.user.service;
import java.util.Collections; import java.util.Collections;
import java.util.Date;
import java.util.EnumMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
@ -11,25 +9,31 @@ import java.util.Set;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import org.apache.commons.lang3.tuple.Pair; import org.apache.commons.lang3.tuple.Pair;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.luoo.user.dao.UserCollectDao; import com.luoo.user.dao.UserCollectDao;
import com.luoo.user.dto.UserCollectCount;
import com.luoo.user.pojo.UserCollect; import com.luoo.user.pojo.UserCollect;
import com.luoo.user.pojo.UserInfo;
import api.StatusCode; import api.StatusCode;
import constants.Constants;
import dto.UserMessageDto;
import enums.CollectTypeEnum; import enums.CollectTypeEnum;
import enums.CollectTypeEnum.*; import enums.MessageTypeEnum;
import exception.BizException; import exception.BizException;
@Service @Service
public class UserCollectService { public class UserCollectService {
@Autowired
private RabbitTemplate rabbitTemplate;
@Autowired @Autowired
private UserCollectDao userCollectDao; private UserCollectDao userCollectDao;
@Autowired
private UserInfoService userInfoService;
public void saveCollect(String userId, String objectId, Integer collectType) { 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) { private void addOrRemove(String userId, String objectId, Integer collectType, boolean isAdd) {
@ -38,28 +42,32 @@ public class UserCollectService {
throw new BizException(StatusCode.VALIDATE_FAILED); throw new BizException(StatusCode.VALIDATE_FAILED);
} }
Optional<UserCollect> dbCollect = userCollectDao.findById(userId); Optional<UserCollect> dbCollect = userCollectDao.findById(userId);
UserCollect userCollect=dbCollect.isPresent()?dbCollect.get():createUserCollect(userId); UserCollect userCollect = dbCollect.isPresent() ? dbCollect.get() : createUserCollect(userId);
switch (collectTypeEnum) { switch (collectTypeEnum) {
case SONG: case SONG:
addOrRemove(isAdd,userCollect.getSongs(),objectId); addOrRemove(isAdd, userCollect.getSongs(), objectId);
break; break;
case JOURNAL: case JOURNAL:
addOrRemove(isAdd,userCollect.getJournals(),objectId); addOrRemove(isAdd, userCollect.getJournals(), objectId);
break; break;
case FOLLOWS: case FOLLOWS:
Optional<UserCollect> followsDbCollect = userCollectDao.findById(objectId); Optional<UserCollect> followsDbCollect = userCollectDao.findById(objectId);
UserCollect followsUserCollect=followsDbCollect.isPresent()?followsDbCollect.get():createUserCollect(objectId); UserCollect followsUserCollect = followsDbCollect.isPresent() ? followsDbCollect.get()
addOrRemove(isAdd,userCollect.getFollows(),objectId,followsUserCollect.getFans(),userId); : createUserCollect(objectId);
addOrRemove(isAdd, userCollect.getFollows(), objectId, followsUserCollect.getFans(), userId);
userCollectDao.save(followsUserCollect); userCollectDao.save(followsUserCollect);
if (isAdd) {
sendMessageToFollows(userId, objectId);
}
break; break;
case FANS: case FANS:
Optional<UserCollect> fansDbCollect = userCollectDao.findById(objectId); Optional<UserCollect> fansDbCollect = userCollectDao.findById(objectId);
UserCollect fansUserCollect=fansDbCollect.isPresent()?fansDbCollect.get():createUserCollect(objectId); UserCollect fansUserCollect = fansDbCollect.isPresent() ? fansDbCollect.get() : createUserCollect(objectId);
addOrRemove(isAdd,userCollect.getFans(),objectId,fansUserCollect.getFollows(),userId); addOrRemove(isAdd, userCollect.getFans(), objectId, fansUserCollect.getFollows(), userId);
userCollectDao.save(fansUserCollect); userCollectDao.save(fansUserCollect);
break; break;
case BLACK_LIST: case BLACK_LIST:
addOrRemove(isAdd,userCollect.getBlackList(),objectId); addOrRemove(isAdd, userCollect.getBlackList(), objectId);
break; break;
default: default:
break; break;
@ -67,12 +75,25 @@ public class UserCollectService {
userCollectDao.save(userCollect); 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<String> follows, String objectId, LinkedList<String> fans, private void addOrRemove(boolean isAdd, LinkedList<String> follows, String objectId, LinkedList<String> fans,
String userId) { String userId) {
if(isAdd) { if (isAdd) {
follows.addFirst(objectId); follows.addFirst(objectId);
fans.addFirst(userId); fans.addFirst(userId);
}else{ } else {
follows.removeFirstOccurrence(objectId); follows.removeFirstOccurrence(objectId);
fans.removeFirstOccurrence(userId); fans.removeFirstOccurrence(userId);
} }
@ -90,53 +111,31 @@ public class UserCollectService {
} }
private void addOrRemove(boolean isAdd, LinkedList<String> list, String objectId) { private void addOrRemove(boolean isAdd, LinkedList<String> list, String objectId) {
if(isAdd) { if (isAdd) {
list.addFirst(objectId); list.addFirst(objectId);
}else{ } else {
list.removeFirstOccurrence(objectId); list.removeFirstOccurrence(objectId);
} }
} }
public void deleteByUserIdAndObjectIdAndCollectType(String userId, String objectId, public void deleteByUserIdAndObjectIdAndCollectType(String userId, String objectId, Integer collectType) {
Integer collectType) { addOrRemove(userId, objectId, collectType, false);
addOrRemove(userId,objectId,collectType,false);
} }
public Optional<UserCollect> findByUserId(String userId) { public Optional<UserCollect> findByUserId(String userId) {
return userCollectDao.findById(userId); return userCollectDao.findById(userId);
} }
public List<String> getCollectList(String userId, Integer pageNum, Integer pageSize,
CollectTypeEnum collectTypeEnum) {
if (null == userId) {
return Collections.emptyList();
}
Optional<UserCollect> 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<String> getPageResult(Integer pageNum, Integer pageSize, LinkedList<String> objectIds) { private List<String> getPageResult(Integer pageNum, Integer pageSize, LinkedList<String> objectIds) {
int end=pageNum * pageSize; int end = pageNum * pageSize;
if (null != pageNum && null != pageSize && pageNum > 0 && pageSize > 0&&end<=objectIds.size()) { if (null != pageNum && null != pageSize && pageNum > 0 && pageSize > 0 && end <= objectIds.size()) {
return objectIds.subList((pageNum - 1) * pageSize, end); return objectIds.subList((pageNum - 1) * pageSize, end);
} }
return objectIds; return objectIds;
} }
public Pair<List<String>, Set<String>> getCollectListWithBothFollowSet(String userId, Integer pageNum, Integer pageSize, public Pair<List<String>, Set<String>> getCollectListWithBothFollowSet(String userId, Integer pageNum,
CollectTypeEnum collectTypeEnum) { Integer pageSize, CollectTypeEnum collectTypeEnum) {
if (null == userId) { if (null == userId) {
return Pair.of(Collections.emptyList(), Collections.emptySet()); return Pair.of(Collections.emptyList(), Collections.emptySet());
} }
@ -147,14 +146,14 @@ public class UserCollectService {
UserCollect userCollect = optional.get(); UserCollect userCollect = optional.get();
switch (collectTypeEnum) { switch (collectTypeEnum) {
case FOLLOWS: case FOLLOWS:
List<String> follows=getPageResult(pageNum, pageSize, userCollect.getFollows()); List<String> follows = getPageResult(pageNum, pageSize, userCollect.getFollows());
Set<String> fans=new HashSet<>(userCollect.getFans()); Set<String> fans = new HashSet<>(userCollect.getFans());
Set<String> bothFollowSet=follows.stream().filter(fans::contains).collect(Collectors.toSet()); Set<String> bothFollowSet = follows.stream().filter(fans::contains).collect(Collectors.toSet());
return Pair.of(follows, bothFollowSet); return Pair.of(follows, bothFollowSet);
case FANS: case FANS:
List<String> fanList=getPageResult(pageNum, pageSize, userCollect.getFans()); List<String> fanList = getPageResult(pageNum, pageSize, userCollect.getFans());
Set<String> followList=new HashSet<>(userCollect.getFollows()); Set<String> followList = new HashSet<>(userCollect.getFollows());
Set<String> bothFanSet=fanList.stream().filter(followList::contains).collect(Collectors.toSet()); Set<String> bothFanSet = fanList.stream().filter(followList::contains).collect(Collectors.toSet());
return Pair.of(fanList, bothFanSet); return Pair.of(fanList, bothFanSet);
case BLACK_LIST: case BLACK_LIST:
return Pair.of(getPageResult(pageNum, pageSize, userCollect.getBlackList()), Collections.emptySet()); return Pair.of(getPageResult(pageNum, pageSize, userCollect.getBlackList()), Collections.emptySet());

Loading…
Cancel
Save