|
|
|
@ -1,167 +0,0 @@
|
|
|
|
|
package com.luoo.user.service;
|
|
|
|
|
|
|
|
|
|
import java.util.Collections;
|
|
|
|
|
import java.util.HashSet;
|
|
|
|
|
import java.util.LinkedList;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.Optional;
|
|
|
|
|
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 com.luoo.user.dao.UserCollectDao;
|
|
|
|
|
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.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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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<UserCollect> 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<UserCollect> 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<UserCollect> 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<String> follows, String objectId, LinkedList<String> fans,
|
|
|
|
|
String userId) {
|
|
|
|
|
if (isAdd) {
|
|
|
|
|
follows.addFirst(objectId);
|
|
|
|
|
fans.addFirst(userId);
|
|
|
|
|
} else {
|
|
|
|
|
follows.removeFirstOccurrence(objectId);
|
|
|
|
|
fans.removeFirstOccurrence(userId);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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<>());
|
|
|
|
|
return userCollect;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void addOrRemove(boolean isAdd, LinkedList<String> list, String objectId) {
|
|
|
|
|
if (isAdd) {
|
|
|
|
|
list.addFirst(objectId);
|
|
|
|
|
} else {
|
|
|
|
|
list.removeFirstOccurrence(objectId);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void deleteByUserIdAndObjectIdAndCollectType(String userId, String objectId, Integer collectType) {
|
|
|
|
|
addOrRemove(userId, objectId, collectType, false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Optional<UserCollect> findByUserId(String userId) {
|
|
|
|
|
return userCollectDao.findById(userId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private List<String> getPageResult(Integer pageNum, Integer pageSize, LinkedList<String> objectIds) {
|
|
|
|
|
if (null == pageNum || null == pageSize) {
|
|
|
|
|
return objectIds;
|
|
|
|
|
}
|
|
|
|
|
int end = pageNum * pageSize;
|
|
|
|
|
if (pageNum > 0 && pageSize > 0 && end <= objectIds.size()) {
|
|
|
|
|
return objectIds.subList((pageNum - 1) * pageSize, end);
|
|
|
|
|
}
|
|
|
|
|
return objectIds;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Pair<List<String>, Set<String>> getCollectListWithBothFollowSet(String userId, Integer pageNum,
|
|
|
|
|
Integer pageSize, CollectTypeEnum collectTypeEnum) {
|
|
|
|
|
if (null == userId) {
|
|
|
|
|
return Pair.of(Collections.emptyList(), Collections.emptySet());
|
|
|
|
|
}
|
|
|
|
|
Optional<UserCollect> optional = userCollectDao.findById(userId);
|
|
|
|
|
if (!optional.isPresent()) {
|
|
|
|
|
return Pair.of(Collections.emptyList(), Collections.emptySet());
|
|
|
|
|
}
|
|
|
|
|
UserCollect userCollect = optional.get();
|
|
|
|
|
switch (collectTypeEnum) {
|
|
|
|
|
case FOLLOWS:
|
|
|
|
|
List<String> follows = getPageResult(pageNum, pageSize, userCollect.getFollows());
|
|
|
|
|
Set<String> fans = new HashSet<>(userCollect.getFans());
|
|
|
|
|
Set<String> bothFollowSet = follows.stream().filter(fans::contains).collect(Collectors.toSet());
|
|
|
|
|
return Pair.of(follows, bothFollowSet);
|
|
|
|
|
case FANS:
|
|
|
|
|
List<String> fanList = getPageResult(pageNum, pageSize, userCollect.getFans());
|
|
|
|
|
Set<String> followList = new HashSet<>(userCollect.getFollows());
|
|
|
|
|
Set<String> 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());
|
|
|
|
|
default:
|
|
|
|
|
return Pair.of(Collections.emptyList(), Collections.emptySet());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|