|
|
|
@ -2,6 +2,7 @@ package com.luoo.user.service;
|
|
|
|
|
|
|
|
|
|
import java.util.Date;
|
|
|
|
|
import java.util.EnumMap;
|
|
|
|
|
import java.util.LinkedList;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.Optional;
|
|
|
|
|
|
|
|
|
@ -15,6 +16,7 @@ import com.luoo.user.pojo.UserCollect;
|
|
|
|
|
|
|
|
|
|
import api.StatusCode;
|
|
|
|
|
import enums.CollectTypeEnum;
|
|
|
|
|
import enums.CollectTypeEnum.*;
|
|
|
|
|
import exception.BizException;
|
|
|
|
|
|
|
|
|
|
@Service
|
|
|
|
@ -22,48 +24,50 @@ public class UserCollectService {
|
|
|
|
|
@Autowired
|
|
|
|
|
private UserCollectDao userCollectDao;
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
String collectId=userId+"_"+objectId+"_"+collectType;
|
|
|
|
|
Optional<UserCollect> dbCollect = userCollectDao.findById(collectId);
|
|
|
|
|
UserCollect userCollect=null;
|
|
|
|
|
Optional<UserCollect> dbCollect = userCollectDao.findById(userId);
|
|
|
|
|
if (dbCollect.isPresent()) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
UserCollect userCollect = new UserCollect();
|
|
|
|
|
userCollect.setCollectId(collectId);
|
|
|
|
|
userCollect=dbCollect.get();
|
|
|
|
|
}else {
|
|
|
|
|
userCollect = new UserCollect();
|
|
|
|
|
userCollect.setUserId(userId);
|
|
|
|
|
userCollect.setCollectTime(new Date());
|
|
|
|
|
userCollect.setObjectId(objectId);
|
|
|
|
|
userCollect.setCollectType(collectType);
|
|
|
|
|
userCollect.setJournals(new LinkedList<>());
|
|
|
|
|
userCollect.setSongs(new LinkedList<>());
|
|
|
|
|
}
|
|
|
|
|
switch (collectTypeEnum) {
|
|
|
|
|
case SONG:
|
|
|
|
|
addOrRemove(isAdd,userCollect.getSongs(),objectId);
|
|
|
|
|
break;
|
|
|
|
|
case JOURNAL:
|
|
|
|
|
addOrRemove(isAdd,userCollect.getJournals(),objectId);
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
userCollectDao.save(userCollect);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void deleteUserCollectByUserIdAndObjectIdAndCollectType(String userId, String objectId,
|
|
|
|
|
Integer collectType) {
|
|
|
|
|
String collectId=userId+"_"+objectId+"_"+collectType;
|
|
|
|
|
userCollectDao.deleteById(collectId);
|
|
|
|
|
private void addOrRemove(boolean isAdd, LinkedList<String> list, String objectId) {
|
|
|
|
|
if(isAdd) {
|
|
|
|
|
list.addFirst(objectId);
|
|
|
|
|
}else{
|
|
|
|
|
list.removeFirstOccurrence(objectId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public EnumMap<CollectTypeEnum,Long> getUserCollectTypeMap(String userId) {
|
|
|
|
|
EnumMap<CollectTypeEnum,Long> userCollectTypeMap=new EnumMap<>(CollectTypeEnum.class);
|
|
|
|
|
/*
|
|
|
|
|
* List<UserCollectCount>
|
|
|
|
|
* userCollectCounts=userCollectDao.countByUserIdAndGroupByCollectType(userId);
|
|
|
|
|
*
|
|
|
|
|
* userCollectCounts.forEach(u->{ CollectTypeEnum collectTypeEnum =
|
|
|
|
|
* CollectTypeEnum.getByType(u.getCollectType());
|
|
|
|
|
* userCollectTypeMap.put(collectTypeEnum, u.getCount()); });
|
|
|
|
|
*/
|
|
|
|
|
return userCollectTypeMap;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public int getFansCount(String userId) {
|
|
|
|
|
return userCollectDao.countByObjectIdAndCollectType(userId,CollectTypeEnum.FOLLOW.getType());
|
|
|
|
|
public void deleteByUserIdAndObjectIdAndCollectType(String userId, String objectId,
|
|
|
|
|
Integer collectType) {
|
|
|
|
|
addOrRemove(userId,objectId,collectType,false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public int getCount(String userId, Integer type) {
|
|
|
|
|
return userCollectDao.countByUserIdAndCollectType(userId,type);
|
|
|
|
|
public Optional<UserCollect> findByUserId(String userId) {
|
|
|
|
|
return userCollectDao.findById(userId);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|