1.update userinfo table schema

main
Gary 10 months ago
parent 25eecf77bf
commit d3b0b7e0ba

@ -3,20 +3,19 @@ package com.luoo.user.controller;
import java.io.IOException; import java.io.IOException;
import java.util.Collections; import java.util.Collections;
import java.util.Date; import java.util.Date;
import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Optional;
import java.util.Set; import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import com.luoo.user.dto.TotalCommentVo; import com.luoo.user.dto.TotalCommentVo;
import com.luoo.user.pojo.Feedback; import com.luoo.user.pojo.Feedback;
import com.luoo.user.pojo.UserCollect; import com.luoo.user.pojo.UserCollectInfo;
import com.luoo.user.service.UserCollectService;
import org.apache.commons.lang3.tuple.Pair;
import org.springframework.beans.BeanUtils; import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.mongodb.core.MongoTemplate; import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.Aggregation; import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.AggregationResults; import org.springframework.data.mongodb.core.aggregation.AggregationResults;
@ -40,6 +39,7 @@ import com.luoo.user.dto.response.UserRespDTO;
import com.luoo.user.pojo.UserInfo; import com.luoo.user.pojo.UserInfo;
import com.luoo.user.service.FeedbackService; import com.luoo.user.service.FeedbackService;
import com.luoo.user.service.S3Service; import com.luoo.user.service.S3Service;
import com.luoo.user.service.UserCollectInfoService;
import com.luoo.user.service.UserInfoService; import com.luoo.user.service.UserInfoService;
import com.luoo.user.util.IpUtil; import com.luoo.user.util.IpUtil;
@ -76,8 +76,8 @@ public class MyController extends BaseController {
@Autowired @Autowired
private IdWorker idWorker; private IdWorker idWorker;
@Autowired @Autowired
// private UserCollectInfoService userCollectInfoService; private UserCollectInfoService userCollectInfoService;
private UserCollectService userCollectService;
@Autowired @Autowired
private FeedbackService feedbackService; private FeedbackService feedbackService;
@ -166,41 +166,12 @@ public class MyController extends BaseController {
userRespDTO.setIpLocation(IpUtil.getIpLocation(user.getLastLoginIp())); userRespDTO.setIpLocation(IpUtil.getIpLocation(user.getLastLoginIp()));
UserLoginDto userLoginDto = getUserLoginDto(authorization); UserLoginDto userLoginDto = getUserLoginDto(authorization);
//若当前登录用户与查询他人 用户 id一致则为我的主页不查询与他人关系
Optional<UserCollect> loginOptional = userCollectService.findByUserId(userLoginDto.getUserId()); if(userId.equals(userLoginDto.getUserId())) {
if (loginOptional.isPresent()) { return Result.success(userRespDTO);
UserCollect loginCollect = loginOptional.get();
Set<String> loginBlackList = new HashSet<>(loginCollect.getBlackList());
if (loginBlackList.contains(userId)) {
userRespDTO.setRelation(UserRelationEnum.SELF_BLACK_LIST.getStatus());
return Result.success(userRespDTO);
}
}
Optional<UserCollect> otherOptional = userCollectService.findByUserId(userId);
if (otherOptional.isPresent()) {
UserCollect otherCollect = otherOptional.get();
Set<String> otherBlackList = new HashSet<>(otherCollect.getBlackList());
if (otherBlackList.contains(userLoginDto.getUserId())) {
userRespDTO.setRelation(UserRelationEnum.OTHER_BLACK_LIST.getStatus());
return Result.success(userRespDTO);
}
Set<String> follows = new HashSet<>(otherCollect.getFollows());
Set<String> fans = new HashSet<>(otherCollect.getFans());
userRespDTO.setFollowCount(follows.size());
userRespDTO.setFansCount(fans.size());
boolean isFollow = fans.contains(userLoginDto.getUserId());
boolean isFan = follows.contains(userLoginDto.getUserId());
if (isFollow && isFan) {
userRespDTO.setRelation(UserRelationEnum.BOTH_FOLLOW.getStatus());
return Result.success(userRespDTO);
} else if (isFollow && !isFan) {
userRespDTO.setRelation(UserRelationEnum.FOLLOW.getStatus());
return Result.success(userRespDTO);
}
} }
userRespDTO.setRelation(UserRelationEnum.NOT_FOLLOW.getStatus()); userRespDTO.setRelation(userCollectInfoService.getRelation(userLoginDto.getUserId(),userId).getStatus());
return Result.success(userRespDTO); return Result.success(userRespDTO);
} }
@ApiOperation(value = "5.查询黑名单") @ApiOperation(value = "5.查询黑名单")
@ -283,20 +254,24 @@ public class MyController extends BaseController {
private Result<PageResult<UserRespDTO>> getCollectedUserInfo(String userId, Integer pageNum, Integer pageSize, private Result<PageResult<UserRespDTO>> getCollectedUserInfo(String userId, Integer pageNum, Integer pageSize,
CollectTypeEnum collectTypeEnum) { CollectTypeEnum collectTypeEnum) {
Pair<List<String>, Set<String>> pair = userCollectService.getCollectListWithBothFollowSet(userId, pageNum, Pageable pageable = PageRequest.of(pageNum-1,pageSize);
pageSize, collectTypeEnum); Page<UserCollectInfo> userCollectInfos = isGetFan(collectTypeEnum)?userCollectInfoService.findByObjectIdAndCollectType(userId,CollectTypeEnum.FOLLOWS,pageable):userCollectInfoService.findByUserIdAndCollectType(userId,collectTypeEnum,pageable);
List<String> objectIds = pair.getKey(); Function<UserCollectInfo,String> idFunction=isGetFan(collectTypeEnum)?UserCollectInfo::getUserId:UserCollectInfo::getObjectId;
List<String> objectIds=userCollectInfos.stream().map(idFunction).collect(Collectors.toList());
if (objectIds.isEmpty()) { if (objectIds.isEmpty()) {
return Result.success(new PageResult<UserRespDTO>(0L, Collections.emptyList())); return Result.success(new PageResult<UserRespDTO>(0L, Collections.emptyList()));
} }
List<UserInfo> userInfos = userInfoService.orderByField(objectIds); List<UserInfo> userInfos = userInfoService.orderByField(objectIds);
Set<String> bothFollowSet=userCollectInfos.stream().filter(c->Boolean.TRUE.equals(c.getIsMutualFans())).map(UserCollectInfo::getObjectId).collect(Collectors.toSet());
List<UserRespDTO> results = userInfos.stream().map(s -> getUserRespDTO(s, false, pair.getRight())) List<UserRespDTO> results = userInfos.stream().map(s -> getUserRespDTO(s, false, bothFollowSet))
.collect(Collectors.toList()); .collect(Collectors.toList());
return Result.success(new PageResult<UserRespDTO>(Long.valueOf(results.size()), results)); return Result.success(new PageResult<UserRespDTO>(Long.valueOf(results.size()), results));
} }
private boolean isGetFan(CollectTypeEnum collectTypeEnum) {
return CollectTypeEnum.FANS.equals(collectTypeEnum);
}
private UserRespDTO getUserRespDTO(UserInfo user, boolean withCount, Set<String> bothFollowSet) { private UserRespDTO getUserRespDTO(UserInfo user, boolean withCount, Set<String> bothFollowSet) {
UserRespDTO userRespDTO = new UserRespDTO(); UserRespDTO userRespDTO = new UserRespDTO();
BeanUtils.copyProperties(user, userRespDTO); BeanUtils.copyProperties(user, userRespDTO);
@ -313,14 +288,6 @@ public class MyController extends BaseController {
if (!withCount) { if (!withCount) {
return userRespDTO; return userRespDTO;
} }
Optional<UserCollect> optional = userCollectService.findByUserId(userRespDTO.getId());
if (optional.isPresent()) {
UserCollect userCollect = optional.get();
userRespDTO.setSongCount(userCollect.getSongs().size());
userRespDTO.setJournalCount(userCollect.getJournals().size());
userRespDTO.setFansCount(userCollect.getFans().size());
userRespDTO.setFollowCount(userCollect.getFollows().size());
}
/** /**
* *
*/ */

@ -3,18 +3,12 @@ package com.luoo.user.controller;
import annotation.GlobalInterceptor; import annotation.GlobalInterceptor;
import annotation.VerifyParam; import annotation.VerifyParam;
import api.Result; import api.Result;
import com.luoo.user.pojo.UserInfo; import com.luoo.user.service.UserCollectInfoService;
import com.luoo.user.service.UserCollectService;
import com.luoo.user.service.UserInfoService;
import constants.Constants;
import dto.UserLoginDto; import dto.UserLoginDto;
import dto.UserMessageDto;
import enums.MessageTypeEnum;
import io.swagger.annotations.Api; import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam; import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams; import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiOperation;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import util.JwtUtil; import util.JwtUtil;
@ -26,8 +20,7 @@ public class UserCollectController {
@Autowired @Autowired
private JwtUtil jwtUtil; private JwtUtil jwtUtil;
@Autowired @Autowired
private UserCollectService userCollectService; private UserCollectInfoService userCollectInfoService;
//@ApiOperation(value = "1.喜欢歌曲/收藏期刊/关注/黑名单/点赞") //@ApiOperation(value = "1.喜欢歌曲/收藏期刊/关注/黑名单/点赞")
@ApiOperation(value = "1.喜欢歌曲/收藏期刊/关注/黑名单") @ApiOperation(value = "1.喜欢歌曲/收藏期刊/关注/黑名单")
@ -42,8 +35,8 @@ public class UserCollectController {
@VerifyParam(required = true) @RequestParam("objectId") String objectId, @VerifyParam(required = true) @RequestParam("objectId") String objectId,
@VerifyParam(required = true) @RequestParam("collectType") Integer collectType) { @VerifyParam(required = true) @RequestParam("collectType") Integer collectType) {
UserLoginDto loginDto = jwtUtil.getUserLoginDto(authorization); UserLoginDto loginDto = jwtUtil.getUserLoginDto(authorization);
userCollectService.saveCollect(loginDto.getUserId(), objectId, collectType); //userCollectService.saveCollect(loginDto.getUserId(), objectId, collectType);
userCollectInfoService.saveCollect(loginDto.getUserId(), objectId, collectType);
return Result.success(); return Result.success();
} }
@ -60,8 +53,8 @@ public class UserCollectController {
@VerifyParam(required = true) @RequestParam("objectId") String objectId, @VerifyParam(required = true) @RequestParam("objectId") String objectId,
@VerifyParam(required = true) @RequestParam("collectType") Integer collectType) { @VerifyParam(required = true) @RequestParam("collectType") Integer collectType) {
UserLoginDto loginDto = jwtUtil.getUserLoginDto(authorization); UserLoginDto loginDto = jwtUtil.getUserLoginDto(authorization);
userCollectService.deleteByUserIdAndObjectIdAndCollectType(loginDto.getUserId(), objectId, userCollectInfoService.cancelCollect(loginDto.getUserId(), objectId, collectType);
collectType); //userCollectService.deleteByUserIdAndObjectIdAndCollectType(loginDto.getUserId(), objectId,collectType);
return Result.success(); return Result.success();
} }
} }

@ -1,90 +0,0 @@
package com.luoo.user.controller;
import api.Result;
import com.luoo.user.pojo.UserCollectInfo;
import com.luoo.user.service.UserCollectInfoService;
import controller.BaseController;
import dto.UserLoginDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
@RestController
@RequestMapping("/userCollectInfo")
public class UserCollectInfoController extends BaseController {
@Autowired
private HttpServletRequest request;
@Autowired
private UserCollectInfoService userCollectInfoService;
//
// @PostMapping("/save")
// public Result save(){
// userCollectInfoService.save();
// return Result.success();
// }
//
// @GetMapping("/show")
// public Result show(){
// UserCollectInfo userCollectInfo =userCollectInfoService.findByUserId("111222");
// return Result.success(userCollectInfo);
// }
//
// @PutMapping("/unCollect")
// public Result unCollect() {
// userCollectInfoService.unCollect();
// return Result.success();
// }
/**
* typejournal typesong
*/
@PostMapping("/collect/{type}/{id}")
public Result collect(@PathVariable String type,@PathVariable String id) {
//验证是否登录并且拿到ID
String token = (String) request.getAttribute("claims_user");
UserLoginDto userLoginDto = getUserLoginDto(token);
if (null == userLoginDto) {
return Result.unauthorized(null);
}
String userid = userLoginDto.getUserId();
if("journal".equals(type)) {
userCollectInfoService.collectJournal(userid,id);
}
if("song".equals(type)) {
userCollectInfoService.collectSong(userid,id);
}
return Result.success();
}
/**
* typejournal typesong
*/
@PostMapping("/unCollect/{type}/{id}")
public Result unCollect(@PathVariable String type,@PathVariable String id) {
//验证是否登录并且拿到ID
String token = (String) request.getAttribute("claims_user");
UserLoginDto userLoginDto = getUserLoginDto(token);
if (null == userLoginDto) {
return Result.unauthorized(null);
}
String userid = userLoginDto.getUserId();
if("journal".equals(type)) {
userCollectInfoService.unCollectJournal(userid,id);
}
if("song".equals(type)) {
userCollectInfoService.unCollectSong(userid,id);
}
return Result.success();
}
}

@ -1,9 +1,19 @@
package com.luoo.user.dao; package com.luoo.user.dao;
import com.luoo.user.pojo.UserCollectInfo; import com.luoo.user.pojo.UserCollectInfo;
import java.util.List;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.mongodb.repository.MongoRepository; import org.springframework.data.mongodb.repository.MongoRepository;
public interface UserCollectInfoDao extends MongoRepository<UserCollectInfo, String> { public interface UserCollectInfoDao extends MongoRepository<UserCollectInfo, String> {
List<UserCollectInfo> findByUserIdAndObjectId(String userId,String objectId);
Page<UserCollectInfo> findByUserIdAndCollectTypeOrderByCreateTimeDesc(String userId, Integer collectType,
Pageable pageable);
UserCollectInfo findUserCollectInfoByUserId(String userId); Page<UserCollectInfo> findByObjectIdAndCollectTypeOrderByCreateTimeDesc(String objectId, Integer collectType,
Pageable pageable);
} }

@ -7,35 +7,37 @@ import org.springframework.format.annotation.DateTimeFormat;
import java.io.Serializable; import java.io.Serializable;
import java.util.Date; import java.util.Date;
import java.util.List;
import java.util.Set;
@Data @Data
public class UserCollectInfo implements Serializable { public class UserCollectInfo implements Serializable {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
/**
/** * ID
* ID */
*/ @Id
@Id private String _id;
private String collectId;
/**
/** * ID
* ID */
*/ private String userId;
private String userId; /**
* ID ID,ID
/** */
* private String objectId;
*/
private Set songSet; /**
* 0:,1:
*/
private Integer collectType;
/** /**
* *
*/ */
private Set journalSet; private Boolean isMutualFans;
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private Date createTime;
} }

@ -74,6 +74,22 @@ public class UserInfo implements Serializable {
* *
*/ */
private int followCount; private int followCount;
/**
*
*/
private int thumbUpCount;
/**
*
*/
private int songCount;
/**
*
*/
private int journalCount;
/**
*
*/
private int commentReplyCount;
/** /**
* *
*/ */
@ -114,4 +130,8 @@ public class UserInfo implements Serializable {
* 0: 1: * 0: 1:
*/ */
private int status; private int status;
/**
* 0: 1:
*/
private int onlineStatus;
} }

@ -1,122 +1,318 @@
package com.luoo.user.service; package com.luoo.user.service;
import com.luoo.user.dao.UserCollectInfoDao; import com.luoo.user.dao.UserCollectInfoDao;
import com.luoo.user.dto.UserCollectSongDto; import com.luoo.user.dao.UserInfoDao;
import com.luoo.user.pojo.UserCollectInfo; import com.luoo.user.pojo.UserCollectInfo;
import com.luoo.user.pojo.UserInfo;
import api.StatusCode;
import constants.Constants;
import dto.UserMessageDto;
import enums.CollectTypeEnum;
import enums.MessageTypeEnum;
import enums.UserRelationEnum;
import exception.BizException;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.annotation.CreatedDate; import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import util.IdWorker;
import java.util.*; import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
@Service @Service
public class UserCollectInfoService { public class UserCollectInfoService {
@Autowired
private RabbitTemplate rabbitTemplate;
@Autowired
private MongoTemplate mongoTemplate;
@Autowired
private UserCollectInfoDao userCollectInfoDao;
@Autowired
private UserInfoDao userInfoDao;
public void saveCollect(String userId, String objectId, Integer collectType) {
//自己不关注自己,自己不把自己列入黑名单
if(objectId.equals(userId)) {
return;
}
CollectTypeEnum collectTypeEnum = CollectTypeEnum.getByType(collectType);
if (null == collectTypeEnum) {
throw new BizException(StatusCode.VALIDATE_FAILED);
}
// 构建查询条件
Criteria criteria = Criteria.where("userId").is(userId).and("objectId").is(objectId).and("collectType")
.is(collectType);
// 创建查询对象并应用查询条件
Query query = new Query(criteria);
boolean isExists = mongoTemplate.exists(query, UserCollectInfo.class);
// 重复保存,此处不报错
if (isExists) {
return;
}
// 特殊处理,若为关注,查看用户是否在我的/他人 黑名单中
if (CollectTypeEnum.FOLLOWS.equals(collectTypeEnum)) {
// 构建黑名单查询条件
// 我的黑名单
Criteria blackListCriteria=new Criteria();
Criteria myBlackListCriteria = Criteria.where("userId").is(userId).and("objectId").is(objectId)
.and("collectType").is(CollectTypeEnum.BLACK_LIST.getType());
// 他人黑名单
Criteria otherBlackListCriteria = Criteria.where("userId").is(objectId).and("objectId").is(userId)
.and("collectType").is(CollectTypeEnum.BLACK_LIST.getType());
blackListCriteria.orOperator(myBlackListCriteria,otherBlackListCriteria);
// 创建查询对象并应用查询条件
Query blackListQuery = new Query(blackListCriteria);
boolean isExistsBlackList = mongoTemplate.exists(blackListQuery, UserCollectInfo.class);
// 在我的/他人黑名单中,无法关注
if (isExistsBlackList) {
return;
}
}
// 存入mongodb
UserCollectInfo userCollectInfo = new UserCollectInfo();
userCollectInfo.setUserId(userId);
userCollectInfo.setObjectId(objectId);
userCollectInfo.setCollectType(collectType);
userCollectInfo.setCreateTime(new Date());
// 特殊处理,若为关注,查看他人是否为我的粉丝,是否为互粉关系
if (CollectTypeEnum.FOLLOWS.equals(collectTypeEnum)) {
Criteria fanCriteria = Criteria.where("userId").is(objectId).and("objectId").is(userId).and("collectType")
.is(collectType);
// 创建查询对象并应用查询条件
Query fanQuery = new Query(fanCriteria);
if (mongoTemplate.exists(fanQuery, UserCollectInfo.class)) {
userCollectInfo.setIsMutualFans(true);
//更新他人与我 由粉丝 变为互关
Update update = new Update().set("isMutualFans", true);
mongoTemplate.updateFirst(fanQuery, update, UserCollectInfo.class);
}
}
mongoTemplate.save(userCollectInfo);
// 更新mysql 中的count
UserInfo user = userInfoDao.getById(userId);
if (null == user) {
return;
}
switch (collectTypeEnum) {
case SONG:
addSongCount(user);
break;
case JOURNAL:
addJournalCount(user);
break;
case FOLLOWS:
addFollowCount(user, objectId);
break;
case BLACK_LIST:
adjustFollowFanCount(user, objectId);
break;
default:
break;
}
}
public void cancelCollect(String userId, String objectId, Integer collectType) {
CollectTypeEnum collectTypeEnum = CollectTypeEnum.getByType(collectType);
if (null == collectTypeEnum) {
throw new BizException(StatusCode.VALIDATE_FAILED);
}
// 构建查询条件
Criteria criteria = Criteria.where("userId").is(userId).and("objectId").is(objectId).and("collectType")
.is(collectType);
// 创建查询对象并应用查询条件
Query query = new Query(criteria);
boolean isExists = mongoTemplate.exists(query, UserCollectInfo.class);
// 未保存,无法取消
if (!isExists) {
return;
}
// 在mongodb 中 删除
mongoTemplate.remove(query, UserCollectInfo.class);
// 更新mysql 中的count
UserInfo user = userInfoDao.getById(userId);
if (null == user) {
return;
}
switch (collectTypeEnum) {
case SONG:
reduceSongCount(user);
break;
case JOURNAL:
reduceJournalCount(user);
break;
case FOLLOWS:
reduceFollowCount(user, objectId);
break;
case FANS:
reduceFanCount(user, objectId);
break;
default:
break;
}
}
private void reduceFanCount(UserInfo user, String objectId) {
UserInfo other = userInfoDao.getById(objectId);
if (null == other) {
return;
}
// 我的粉丝-1
user.setFansCount(user.getFansCount() - 1);
userInfoDao.save(user);
// 他人关注-1
other.setFollowCount(other.getFollowCount() - 1);
userInfoDao.save(other);
}
private void reduceFollowCount(UserInfo user, String objectId) {
UserInfo other = userInfoDao.getById(objectId);
if (null == other) {
return;
}
// 我的关注-1
user.setFollowCount(user.getFollowCount() - 1);
userInfoDao.save(user);
// 他人粉丝-1
other.setFansCount(other.getFansCount() - 1);
userInfoDao.save(other);
}
private void adjustFollowFanCount(UserInfo user, String objectId) {
UserInfo other = userInfoDao.getById(objectId);
if (null == other) {
return;
}
// 若为我的关注,删除关注关系,我的关注数-1他人粉丝数-1
// 我的关注
Criteria myFollowCriteria = Criteria.where("userId").is(user.getId()).and("objectId").is(objectId)
.and("collectType").is(CollectTypeEnum.FOLLOWS.getType());
Query myFollowQuery = new Query(myFollowCriteria);
boolean isMyFollow = mongoTemplate.exists(myFollowQuery, UserCollectInfo.class);
if (isMyFollow) {
mongoTemplate.remove(myFollowQuery, UserCollectInfo.class);
// 我的关注-1
user.setFollowCount(user.getFollowCount() - 1);
userInfoDao.save(user);
// 他人粉丝-1
other.setFansCount(other.getFansCount() - 1);
userInfoDao.save(other);
}
// 若为我的粉丝,删除粉丝关系,我的粉丝数-1他人关注数-1
// 我的粉丝
Criteria myFanCriteria = Criteria.where("userId").is(objectId).and("objectId").is(user.getId())
.and("collectType").is(CollectTypeEnum.FOLLOWS.getType());
Query myFanQuery = new Query(myFanCriteria);
boolean isMyFan = mongoTemplate.exists(myFanQuery, UserCollectInfo.class);
if (isMyFan) {
mongoTemplate.remove(myFanQuery, UserCollectInfo.class);
// 我的粉丝-1
user.setFansCount(user.getFansCount() - 1);
userInfoDao.save(user);
// 他人关注-1
other.setFollowCount(other.getFollowCount() - 1);
userInfoDao.save(other);
}
}
private void addFollowCount(UserInfo user, String objectId) {
// 查询我的和他人的黑名单,如果在黑名单中,无法关注
UserInfo other = userInfoDao.getById(objectId);
if (null == other) {
return;
}
// 我的关注+1
user.setFollowCount(user.getFollowCount() + 1);
userInfoDao.save(user);
// 他人粉丝+1
other.setFansCount(other.getFansCount() + 1);
userInfoDao.save(other);
//向他人发送 新粉丝 消息
sendMessageToFollows(user, objectId);
}
private void sendMessageToFollows(UserInfo userInfo, String followId) {
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(userInfo.getId());
userMessageDto.setSendUserNickName(userInfo.getNickName());
rabbitTemplate.convertAndSend("userMessage", userMessageDto);
}
private void addJournalCount(UserInfo user) {
user.setJournalCount(user.getJournalCount() + 1);
userInfoDao.save(user);
}
private void addSongCount(UserInfo user) {
user.setSongCount(user.getSongCount() + 1);
userInfoDao.save(user);
}
private void reduceJournalCount(UserInfo user) {
user.setJournalCount(user.getJournalCount() - 1);
userInfoDao.save(user);
}
private void reduceSongCount(UserInfo user) {
user.setSongCount(user.getSongCount() - 1);
userInfoDao.save(user);
}
public UserRelationEnum getRelation(String userId, String otherId) {
List<UserCollectInfo> myCollectList=userCollectInfoDao.findByUserIdAndObjectId(userId, otherId);
if(null!=myCollectList) {
Map<Integer, UserCollectInfo> myCollectTypeMap=myCollectList.stream().collect(Collectors.toMap(UserCollectInfo::getCollectType, Function.identity()));
//他人是否在自己黑名单中
if(myCollectTypeMap.containsKey(CollectTypeEnum.BLACK_LIST.getType())) {
return UserRelationEnum.SELF_BLACK_LIST;
}
//是否互关/仅关注
UserCollectInfo followCollectInfo=myCollectTypeMap.get(CollectTypeEnum.FOLLOWS.getType());
if(null!=followCollectInfo&&Boolean.TRUE.equals(followCollectInfo.getIsMutualFans())) {
return UserRelationEnum.BOTH_FOLLOW;
}else if(null!=followCollectInfo) {
return UserRelationEnum.FOLLOW;
}
}
List<UserCollectInfo> otherCollectList=userCollectInfoDao.findByUserIdAndObjectId(otherId, userId);
if(null!=otherCollectList) {
Map<Integer, UserCollectInfo> otherCollectTypeMap=otherCollectList.stream().collect(Collectors.toMap(UserCollectInfo::getCollectType, Function.identity()));
//自己是否在他人黑名单中
if(otherCollectTypeMap.containsKey(CollectTypeEnum.BLACK_LIST.getType())) {
return UserRelationEnum.OTHER_BLACK_LIST;
}
}
return UserRelationEnum.NOT_FOLLOW;
}
public Page<UserCollectInfo> findByUserIdAndCollectType(String userId, CollectTypeEnum collectTypeEnum, Pageable pageable) {
return userCollectInfoDao.findByUserIdAndCollectTypeOrderByCreateTimeDesc(userId,collectTypeEnum.getType(),pageable);
}
@Autowired public Page<UserCollectInfo> findByObjectIdAndCollectType(String objectId, CollectTypeEnum collectTypeEnum,
private UserCollectInfoDao userCollectInfoDao; Pageable pageable) {
return userCollectInfoDao.findByObjectIdAndCollectTypeOrderByCreateTimeDesc(objectId,collectTypeEnum.getType(),pageable);
@Autowired }
private IdWorker idWorker;
public void save() {
UserCollectInfo userCollectInfo = new UserCollectInfo();
UserCollectSongDto userCollectSongDto = new UserCollectSongDto();
userCollectSongDto.setName("smell like teensprit");
userCollectSongDto.setArtist("Nirvana");
UserCollectSongDto userCollectSongDto1 = new UserCollectSongDto();
userCollectSongDto1.setName("the unforgiven");
userCollectSongDto1.setArtist("Metallica");
List list = new ArrayList();
list.add(userCollectSongDto);
list.add(userCollectSongDto1);
// userCollectInfo.setSongList(list);
userCollectInfo.setCollectId(idWorker.nextId()+"");
userCollectInfo.setUserId("111222");
userCollectInfoDao.save(userCollectInfo);
}
public UserCollectInfo findByUserId(String userId){
return userCollectInfoDao.findUserCollectInfoByUserId(userId);
}
public void collectSong(String userid, String id) {
UserCollectInfo userCollectInfo = userCollectInfoDao.findUserCollectInfoByUserId(userid);
if (null == userCollectInfo) {
userCollectInfo = new UserCollectInfo();
userCollectInfo.setUserId(userid);
Set songSet = new HashSet();
songSet.add(id);
userCollectInfo.setSongSet(songSet);
userCollectInfoDao.save(userCollectInfo);
} else {
Set songSet = userCollectInfo.getSongSet();
if(null == songSet) {
songSet = new HashSet<>();
}
songSet.add(id);
userCollectInfo.setSongSet(songSet);
userCollectInfoDao.save(userCollectInfo);
}
}
public void collectJournal(String userid, String id) {
UserCollectInfo userCollectInfo = userCollectInfoDao.findUserCollectInfoByUserId(userid);
if (null == userCollectInfo) {
userCollectInfo = new UserCollectInfo();
userCollectInfo.setUserId(userid);
Set journalSet = new HashSet();
journalSet.add(id);
userCollectInfo.setJournalSet(journalSet);
userCollectInfoDao.save(userCollectInfo);
} else {
Set journalSet =userCollectInfo.getJournalSet();
if(null== journalSet) {
journalSet = new HashSet();
}
journalSet.add(id);
userCollectInfo.setJournalSet(journalSet);
userCollectInfoDao.save(userCollectInfo);
}
}
/**
*
*/
public void unCollect(){
UserCollectInfo userCollectInfo = userCollectInfoDao.findUserCollectInfoByUserId("111222");
UserCollectSongDto userCollectSongDto = new UserCollectSongDto();
userCollectSongDto.setName("smell like teensprit");
userCollectSongDto.setArtist("Nirvana");
// userCollectInfo.getSongList().remove(userCollectSongDto);
userCollectInfoDao.save(userCollectInfo);
}
public void unCollectJournal(String userid, String id) {
UserCollectInfo userCollectInfo = userCollectInfoDao.findUserCollectInfoByUserId(userid);
userCollectInfo.getJournalSet().remove(id);
userCollectInfoDao.save(userCollectInfo);
}
public void unCollectSong(String userid, String id) {
UserCollectInfo userCollectInfo = userCollectInfoDao.findUserCollectInfoByUserId(userid);
userCollectInfo.getSongSet().remove(id);
userCollectInfoDao.save(userCollectInfo);
}
} }

Loading…
Cancel
Save