From 301df9908e43020c97bd219f25882a8412156c7c Mon Sep 17 00:00:00 2001 From: Gary Date: Sun, 25 Feb 2024 01:34:52 +0800 Subject: [PATCH] 1.fix my fans status after cancel follow --- .../luoo/user/dto/response/UserRespDTO.java | 2 +- .../user/service/UserCollectInfoService.java | 97 ++++++++++++------- 2 files changed, 64 insertions(+), 35 deletions(-) diff --git a/luoo_user/src/main/java/com/luoo/user/dto/response/UserRespDTO.java b/luoo_user/src/main/java/com/luoo/user/dto/response/UserRespDTO.java index 5eaeb26..a945690 100644 --- a/luoo_user/src/main/java/com/luoo/user/dto/response/UserRespDTO.java +++ b/luoo_user/src/main/java/com/luoo/user/dto/response/UserRespDTO.java @@ -36,7 +36,7 @@ public class UserRespDTO { @ApiModelProperty(value = "生日,格式为: yyyy.MM.dd") private String birthDay; @ApiModelProperty(value = "与他人关系,1:未关注,2 :已关注,3:互相关注, 4:他人在自己黑名单,5:自己在他人黑名单", example = "1") - private int relation; + private int relation=1; @ApiModelProperty(value = "IP 归属地,精确到省份", example = "北京") private String ipLocation; } 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 c5caaa7..dad15df 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 @@ -37,21 +37,21 @@ public class UserCollectInfoService { private RedisTemplate redisTemplate; @Autowired private UserCollectInfoDao userCollectInfoDao; - + @Autowired private UserInfoDao userInfoDao; public Set getCollectSet(String userId, CollectTypeEnum collectTypeEnum) { - Criteria criteria = Criteria.where("userId").is(userId).and("collectType") - .is(collectTypeEnum.getType()); + Criteria criteria = Criteria.where("userId").is(userId).and("collectType").is(collectTypeEnum.getType()); Query query = new Query(criteria); List userCollectInfos = mongoTemplate.find(query, UserCollectInfo.class); - return userCollectInfos.isEmpty() ? Collections.emptySet() : userCollectInfos.stream().map(UserCollectInfo::getObjectId).collect(Collectors.toSet()); + return userCollectInfos.isEmpty() ? Collections.emptySet() + : userCollectInfos.stream().map(UserCollectInfo::getObjectId).collect(Collectors.toSet()); } - + public void saveCollect(String userId, String objectId, Integer collectType) { - //自己不关注自己,自己不把自己列入黑名单 - if(objectId.equals(userId)) { + // 自己不关注自己,自己不把自己列入黑名单 + if (objectId.equals(userId)) { return; } CollectTypeEnum collectTypeEnum = CollectTypeEnum.getByType(collectType); @@ -73,14 +73,14 @@ public class UserCollectInfoService { if (CollectTypeEnum.FOLLOWS.equals(collectTypeEnum)) { // 构建黑名单查询条件 // 我的黑名单 - Criteria blackListCriteria=new Criteria(); + 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); + + blackListCriteria.orOperator(myBlackListCriteria, otherBlackListCriteria); // 创建查询对象并应用查询条件 Query blackListQuery = new Query(blackListCriteria); boolean isExistsBlackList = mongoTemplate.exists(blackListQuery, UserCollectInfo.class); @@ -103,7 +103,7 @@ public class UserCollectInfoService { 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); } @@ -188,6 +188,17 @@ public class UserCollectInfoService { // 他人关注-1 other.setFollowCount(other.getFollowCount() - 1); userInfoDao.save(other); + + // 若为我的关注,且与他人为互关状态,则更新我的关注状态 为 仅关注,而非互关 + // 我的关注 + Criteria myFollowCriteria = Criteria.where("userId").is(user.getId()).and("objectId").is(objectId) + .and("collectType").is(CollectTypeEnum.FOLLOWS.getType()).and("isMutualFans").is(true); + Query myFollowQuery = new Query(myFollowCriteria); + boolean isMyFollow = mongoTemplate.exists(myFollowQuery, UserCollectInfo.class); + if (isMyFollow) { + Update update = new Update().set("isMutualFans", false); + mongoTemplate.updateFirst(myFollowQuery, update, UserCollectInfo.class); + } } private void reduceFollowCount(UserInfo user, String objectId) { @@ -201,6 +212,17 @@ public class UserCollectInfoService { // 他人粉丝-1 other.setFansCount(other.getFansCount() - 1); userInfoDao.save(other); + + // 若为我的粉丝,且与他人为互关状态,则更新他人的关注状态 为 仅关注,而非互关 + // 他人的关注 + Criteria otherFollowCriteria = Criteria.where("userId").is(objectId).and("objectId").is(user.getId()) + .and("collectType").is(CollectTypeEnum.FOLLOWS.getType()).and("isMutualFans").is(true); + Query otherFollowQuery = new Query(otherFollowCriteria); + boolean isOtherFollow = mongoTemplate.exists(otherFollowQuery, UserCollectInfo.class); + if (isOtherFollow) { + Update update = new Update().set("isMutualFans", false); + mongoTemplate.updateFirst(otherFollowQuery, update, UserCollectInfo.class); + } } private void adjustFollowFanCount(UserInfo user, String objectId) { @@ -253,8 +275,8 @@ public class UserCollectInfoService { // 他人粉丝+1 other.setFansCount(other.getFansCount() + 1); userInfoDao.save(other); - - //向他人发送 新粉丝 消息 + + // 向他人发送 新粉丝 消息 sendMessageToFollows(user, objectId); } @@ -273,8 +295,9 @@ public class UserCollectInfoService { private void addJournalCount(UserInfo user) { user.setJournalCount(user.getJournalCount() + 1); userInfoDao.save(user); - //更新redis 中的缓存 - redisTemplate.opsForValue().set(Constants.REDIS_KEY_USER_COLLECT_JOURNAL+user.getId(),getCollectSet(user.getId(), CollectTypeEnum.JOURNAL)); + // 更新redis 中的缓存 + redisTemplate.opsForValue().set(Constants.REDIS_KEY_USER_COLLECT_JOURNAL + user.getId(), + getCollectSet(user.getId(), CollectTypeEnum.JOURNAL)); } private void addSongCount(UserInfo user) { @@ -285,8 +308,9 @@ public class UserCollectInfoService { private void reduceJournalCount(UserInfo user) { user.setJournalCount(user.getJournalCount() - 1); userInfoDao.save(user); - //更新redis 中的缓存 - redisTemplate.opsForValue().set(Constants.REDIS_KEY_USER_COLLECT_JOURNAL+user.getId(),getCollectSet(user.getId(), CollectTypeEnum.JOURNAL)); + // 更新redis 中的缓存 + redisTemplate.opsForValue().set(Constants.REDIS_KEY_USER_COLLECT_JOURNAL + user.getId(), + getCollectSet(user.getId(), CollectTypeEnum.JOURNAL)); } private void reduceSongCount(UserInfo user) { @@ -295,38 +319,43 @@ public class UserCollectInfoService { } 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())) { + 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())) { + // 是否互关/仅关注 + UserCollectInfo followCollectInfo = myCollectTypeMap.get(CollectTypeEnum.FOLLOWS.getType()); + if (null != followCollectInfo && Boolean.TRUE.equals(followCollectInfo.getIsMutualFans())) { return UserRelationEnum.BOTH_FOLLOW; - }else if(null!=followCollectInfo) { + } 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())) { + 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); + public Page findByUserIdAndCollectType(String userId, CollectTypeEnum collectTypeEnum, + Pageable pageable) { + return userCollectInfoDao.findByUserIdAndCollectTypeOrderByCreateTimeDesc(userId, collectTypeEnum.getType(), + pageable); } public Page findByObjectIdAndCollectType(String objectId, CollectTypeEnum collectTypeEnum, Pageable pageable) { - return userCollectInfoDao.findByObjectIdAndCollectTypeOrderByCreateTimeDesc(objectId,collectTypeEnum.getType(),pageable); + return userCollectInfoDao.findByObjectIdAndCollectTypeOrderByCreateTimeDesc(objectId, collectTypeEnum.getType(), + pageable); } }