diff --git a/luoo_user/src/main/java/com/luoo/user/controller/MyController.java b/luoo_user/src/main/java/com/luoo/user/controller/MyController.java index 014af0a..7a9507a 100644 --- a/luoo_user/src/main/java/com/luoo/user/controller/MyController.java +++ b/luoo_user/src/main/java/com/luoo/user/controller/MyController.java @@ -3,20 +3,19 @@ package com.luoo.user.controller; import java.io.IOException; import java.util.Collections; import java.util.Date; -import java.util.HashSet; import java.util.List; -import java.util.Optional; import java.util.Set; +import java.util.function.Function; import java.util.stream.Collectors; import com.luoo.user.dto.TotalCommentVo; import com.luoo.user.pojo.Feedback; -import com.luoo.user.pojo.UserCollect; -import com.luoo.user.service.UserCollectService; - -import org.apache.commons.lang3.tuple.Pair; +import com.luoo.user.pojo.UserCollectInfo; import org.springframework.beans.BeanUtils; 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.aggregation.Aggregation; 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.service.FeedbackService; import com.luoo.user.service.S3Service; +import com.luoo.user.service.UserCollectInfoService; import com.luoo.user.service.UserInfoService; import com.luoo.user.util.IpUtil; @@ -76,8 +76,8 @@ public class MyController extends BaseController { @Autowired private IdWorker idWorker; @Autowired -// private UserCollectInfoService userCollectInfoService; - private UserCollectService userCollectService; + private UserCollectInfoService userCollectInfoService; + @Autowired private FeedbackService feedbackService; @@ -166,41 +166,12 @@ public class MyController extends BaseController { userRespDTO.setIpLocation(IpUtil.getIpLocation(user.getLastLoginIp())); UserLoginDto userLoginDto = getUserLoginDto(authorization); - - Optional loginOptional = userCollectService.findByUserId(userLoginDto.getUserId()); - if (loginOptional.isPresent()) { - UserCollect loginCollect = loginOptional.get(); - Set loginBlackList = new HashSet<>(loginCollect.getBlackList()); - if (loginBlackList.contains(userId)) { - userRespDTO.setRelation(UserRelationEnum.SELF_BLACK_LIST.getStatus()); - return Result.success(userRespDTO); - } - } - Optional otherOptional = userCollectService.findByUserId(userId); - if (otherOptional.isPresent()) { - UserCollect otherCollect = otherOptional.get(); - Set otherBlackList = new HashSet<>(otherCollect.getBlackList()); - if (otherBlackList.contains(userLoginDto.getUserId())) { - userRespDTO.setRelation(UserRelationEnum.OTHER_BLACK_LIST.getStatus()); - return Result.success(userRespDTO); - } - - Set follows = new HashSet<>(otherCollect.getFollows()); - Set 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); - } + //若当前登录用户与查询他人 用户 id一致,则为我的主页,不查询与他人关系 + if(userId.equals(userLoginDto.getUserId())) { + return Result.success(userRespDTO); } - userRespDTO.setRelation(UserRelationEnum.NOT_FOLLOW.getStatus()); - return Result.success(userRespDTO); + userRespDTO.setRelation(userCollectInfoService.getRelation(userLoginDto.getUserId(),userId).getStatus()); + return Result.success(userRespDTO); } @ApiOperation(value = "5.查询黑名单") @@ -283,20 +254,24 @@ public class MyController extends BaseController { private Result> getCollectedUserInfo(String userId, Integer pageNum, Integer pageSize, CollectTypeEnum collectTypeEnum) { - Pair, Set> pair = userCollectService.getCollectListWithBothFollowSet(userId, pageNum, - pageSize, collectTypeEnum); - List objectIds = pair.getKey(); + Pageable pageable = PageRequest.of(pageNum-1,pageSize); + Page userCollectInfos = isGetFan(collectTypeEnum)?userCollectInfoService.findByObjectIdAndCollectType(userId,CollectTypeEnum.FOLLOWS,pageable):userCollectInfoService.findByUserIdAndCollectType(userId,collectTypeEnum,pageable); + Function idFunction=isGetFan(collectTypeEnum)?UserCollectInfo::getUserId:UserCollectInfo::getObjectId; + List objectIds=userCollectInfos.stream().map(idFunction).collect(Collectors.toList()); if (objectIds.isEmpty()) { return Result.success(new PageResult(0L, Collections.emptyList())); } List userInfos = userInfoService.orderByField(objectIds); - - List results = userInfos.stream().map(s -> getUserRespDTO(s, false, pair.getRight())) + Set bothFollowSet=userCollectInfos.stream().filter(c->Boolean.TRUE.equals(c.getIsMutualFans())).map(UserCollectInfo::getObjectId).collect(Collectors.toSet()); + List results = userInfos.stream().map(s -> getUserRespDTO(s, false, bothFollowSet)) .collect(Collectors.toList()); - return Result.success(new PageResult(Long.valueOf(results.size()), results)); } + private boolean isGetFan(CollectTypeEnum collectTypeEnum) { + return CollectTypeEnum.FANS.equals(collectTypeEnum); + } + private UserRespDTO getUserRespDTO(UserInfo user, boolean withCount, Set bothFollowSet) { UserRespDTO userRespDTO = new UserRespDTO(); BeanUtils.copyProperties(user, userRespDTO); @@ -313,14 +288,6 @@ public class MyController extends BaseController { if (!withCount) { return userRespDTO; } - Optional 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()); - } /** * 获取点赞数和评论数 */ diff --git a/luoo_user/src/main/java/com/luoo/user/controller/UserCollectController.java b/luoo_user/src/main/java/com/luoo/user/controller/UserCollectController.java index 6cd2dbd..4b774e9 100644 --- a/luoo_user/src/main/java/com/luoo/user/controller/UserCollectController.java +++ b/luoo_user/src/main/java/com/luoo/user/controller/UserCollectController.java @@ -3,18 +3,12 @@ package com.luoo.user.controller; import annotation.GlobalInterceptor; import annotation.VerifyParam; import api.Result; -import com.luoo.user.pojo.UserInfo; -import com.luoo.user.service.UserCollectService; -import com.luoo.user.service.UserInfoService; -import constants.Constants; +import com.luoo.user.service.UserCollectInfoService; import dto.UserLoginDto; -import dto.UserMessageDto; -import enums.MessageTypeEnum; import io.swagger.annotations.Api; import io.swagger.annotations.ApiImplicitParam; import io.swagger.annotations.ApiImplicitParams; import io.swagger.annotations.ApiOperation; -import org.springframework.amqp.rabbit.core.RabbitTemplate; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import util.JwtUtil; @@ -26,8 +20,7 @@ public class UserCollectController { @Autowired private JwtUtil jwtUtil; @Autowired - private UserCollectService userCollectService; - + private UserCollectInfoService userCollectInfoService; //@ApiOperation(value = "1.喜欢歌曲/收藏期刊/关注/黑名单/点赞") @ApiOperation(value = "1.喜欢歌曲/收藏期刊/关注/黑名单") @@ -42,8 +35,8 @@ public class UserCollectController { @VerifyParam(required = true) @RequestParam("objectId") String objectId, @VerifyParam(required = true) @RequestParam("collectType") Integer collectType) { 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(); } @@ -60,8 +53,8 @@ public class UserCollectController { @VerifyParam(required = true) @RequestParam("objectId") String objectId, @VerifyParam(required = true) @RequestParam("collectType") Integer collectType) { UserLoginDto loginDto = jwtUtil.getUserLoginDto(authorization); - userCollectService.deleteByUserIdAndObjectIdAndCollectType(loginDto.getUserId(), objectId, - collectType); + userCollectInfoService.cancelCollect(loginDto.getUserId(), objectId, collectType); + //userCollectService.deleteByUserIdAndObjectIdAndCollectType(loginDto.getUserId(), objectId,collectType); return Result.success(); } } diff --git a/luoo_user/src/main/java/com/luoo/user/controller/UserCollectInfoController.java b/luoo_user/src/main/java/com/luoo/user/controller/UserCollectInfoController.java deleted file mode 100644 index 6b700d4..0000000 --- a/luoo_user/src/main/java/com/luoo/user/controller/UserCollectInfoController.java +++ /dev/null @@ -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(); -// } - - - - - /** - * 收藏 type为journal 为期刊,type为song为单曲 - */ - - @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(); - } - - /** - * 取消收藏 type为journal 为期刊,type为song为单曲 - */ - @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(); - } - - -} diff --git a/luoo_user/src/main/java/com/luoo/user/dao/UserCollectInfoDao.java b/luoo_user/src/main/java/com/luoo/user/dao/UserCollectInfoDao.java index 21d7e48..13c00fc 100644 --- a/luoo_user/src/main/java/com/luoo/user/dao/UserCollectInfoDao.java +++ b/luoo_user/src/main/java/com/luoo/user/dao/UserCollectInfoDao.java @@ -1,9 +1,19 @@ package com.luoo.user.dao; 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; public interface UserCollectInfoDao extends MongoRepository { + List findByUserIdAndObjectId(String userId,String objectId); + + Page findByUserIdAndCollectTypeOrderByCreateTimeDesc(String userId, Integer collectType, + Pageable pageable); - UserCollectInfo findUserCollectInfoByUserId(String userId); + Page findByObjectIdAndCollectTypeOrderByCreateTimeDesc(String objectId, Integer collectType, + Pageable pageable); } diff --git a/luoo_user/src/main/java/com/luoo/user/pojo/UserCollectInfo.java b/luoo_user/src/main/java/com/luoo/user/pojo/UserCollectInfo.java index 8034b6d..3a3526f 100644 --- a/luoo_user/src/main/java/com/luoo/user/pojo/UserCollectInfo.java +++ b/luoo_user/src/main/java/com/luoo/user/pojo/UserCollectInfo.java @@ -7,35 +7,37 @@ import org.springframework.format.annotation.DateTimeFormat; import java.io.Serializable; import java.util.Date; -import java.util.List; -import java.util.Set; @Data public class UserCollectInfo implements Serializable { - private static final long serialVersionUID = 1L; - - /** - * 收藏ID - */ - @Id - private String collectId; - - /** - * 用户ID - */ - private String userId; - - /** - * 收藏的单曲 - */ - private Set songSet; - - - - /** - * 收藏的期刊 - */ - private Set journalSet; + private static final long serialVersionUID = 1L; + /** + * ID + */ + @Id + private String _id; + + /** + * 用户ID + */ + private String userId; + /** + * 主体ID 期刊ID,歌曲ID + */ + private String objectId; + + /** + * 0:为歌曲,1:期刊 + */ + private Integer collectType; + /** + * 是否互粉 + */ + 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; } diff --git a/luoo_user/src/main/java/com/luoo/user/pojo/UserInfo.java b/luoo_user/src/main/java/com/luoo/user/pojo/UserInfo.java index 6bb67d6..ac6c676 100644 --- a/luoo_user/src/main/java/com/luoo/user/pojo/UserInfo.java +++ b/luoo_user/src/main/java/com/luoo/user/pojo/UserInfo.java @@ -74,6 +74,22 @@ public class UserInfo implements Serializable { * 关注数 */ 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:正常 */ private int status; + /** + * 0:禁用 1:正常 + */ + private int onlineStatus; } diff --git a/luoo_user/src/main/java/com/luoo/user/service/UserCollectInfoService.java b/luoo_user/src/main/java/com/luoo/user/service/UserCollectInfoService.java index 154c97f..427caea 100644 --- a/luoo_user/src/main/java/com/luoo/user/service/UserCollectInfoService.java +++ b/luoo_user/src/main/java/com/luoo/user/service/UserCollectInfoService.java @@ -1,122 +1,318 @@ package com.luoo.user.service; - 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.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.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 util.IdWorker; - import java.util.*; +import java.util.function.Function; +import java.util.stream.Collectors; @Service 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 myCollectList=userCollectInfoDao.findByUserIdAndObjectId(userId, otherId); + if(null!=myCollectList) { + Map 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 otherCollectList=userCollectInfoDao.findByUserIdAndObjectId(otherId, userId); + if(null!=otherCollectList) { + Map 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 findByUserIdAndCollectType(String userId, CollectTypeEnum collectTypeEnum, Pageable pageable) { + return userCollectInfoDao.findByUserIdAndCollectTypeOrderByCreateTimeDesc(userId,collectTypeEnum.getType(),pageable); + } - @Autowired - private UserCollectInfoDao userCollectInfoDao; - - @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); - - } + public Page findByObjectIdAndCollectType(String objectId, CollectTypeEnum collectTypeEnum, + Pageable pageable) { + return userCollectInfoDao.findByObjectIdAndCollectTypeOrderByCreateTimeDesc(objectId,collectTypeEnum.getType(),pageable); + } }