From 51c5cf07122df78686b0f37e255c9c49863a4849 Mon Sep 17 00:00:00 2001 From: tianfeng <769204422@qq.com> Date: Fri, 12 Dec 2025 11:36:06 +0800 Subject: [PATCH] =?UTF-8?q?cpList=20=E7=BC=93=E5=AD=98=E9=87=8D=E5=86=99?= =?UTF-8?q?=EF=BC=8C=E5=AE=9A=E6=97=B6=E4=BB=BB=E5=8A=A1=E6=9B=B4=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../command/user/DismissCpApplyCmdExe.java | 4 +- .../app/command/user/ProcessCpApplyCmd.java | 4 +- .../user/query/UserProfileUseQryExe.java | 16 +-- .../gift/strategy/RankCountStrategy.java | 5 +- .../CleanExpiredDismissingCpTask.java | 10 +- .../database/cache/key/user/UserKey.java | 8 +- .../cp/CpRelationshipCacheService.java | 24 ++++ .../impl/CpRelationshipCacheServiceImpl.java | 126 ++++++++++++++++++ .../user/user/CpRelationshipService.java | 6 +- .../user/impl/CpRelationshipServiceImpl.java | 5 +- .../UserRunProfileTransportGatewayImpl.java | 65 --------- 11 files changed, 185 insertions(+), 88 deletions(-) create mode 100644 rc-service/rc-service-other/other-infrastructure/src/main/java/com/red/circle/other/infra/database/cache/service/cp/CpRelationshipCacheService.java create mode 100644 rc-service/rc-service-other/other-infrastructure/src/main/java/com/red/circle/other/infra/database/cache/service/cp/impl/CpRelationshipCacheServiceImpl.java diff --git a/rc-service/rc-service-other/other-application/src/main/java/com/red/circle/other/app/command/user/DismissCpApplyCmdExe.java b/rc-service/rc-service-other/other-application/src/main/java/com/red/circle/other/app/command/user/DismissCpApplyCmdExe.java index ea375fca..49a9b3a3 100644 --- a/rc-service/rc-service-other/other-application/src/main/java/com/red/circle/other/app/command/user/DismissCpApplyCmdExe.java +++ b/rc-service/rc-service-other/other-application/src/main/java/com/red/circle/other/app/command/user/DismissCpApplyCmdExe.java @@ -6,6 +6,7 @@ import com.red.circle.other.app.dto.cmd.user.relation.cp.CpApplyDismissCmd; import com.red.circle.other.app.service.user.user.UserProfileService; import com.red.circle.other.domain.gateway.user.UserProfileGateway; import com.red.circle.other.infra.common.user.UserGiftBackpackCommon; +import com.red.circle.other.infra.database.cache.service.cp.CpRelationshipCacheService; import com.red.circle.other.infra.database.cache.service.user.UserCpValueCacheService; import com.red.circle.other.infra.database.mongo.service.user.count.WeekCpValueCountService; import com.red.circle.other.infra.database.rds.entity.user.user.CpRelationship; @@ -40,6 +41,7 @@ public class DismissCpApplyCmdExe { private final CpBlessRecordService cpBlessRecordService; private final CpCabinAchieveService cpCabinAchieveService; private final UserProfileGateway userProfileGateway; + private final CpRelationshipCacheService cpRelationshipCacheService; public void execute(CpApplyDismissCmd cmd) { @@ -54,7 +56,7 @@ public class DismissCpApplyCmdExe { // 更新CP关系为分手中,不删除数据 cpRelationshipService.updateToDismissing(cmd.requiredReqUserId(), cmd.getCpUserId()); - userProfileGateway.removeCacheAll(Arrays.asList(cmd.requiredReqUserId(), cmd.getCpUserId())); + cpRelationshipCacheService.remove(Arrays.asList(cmd.requiredReqUserId(), cmd.getCpUserId())); // 以下数据不删除,7天后由定时任务清理 // weekCpValueCountService.removeThisWeek(...) diff --git a/rc-service/rc-service-other/other-application/src/main/java/com/red/circle/other/app/command/user/ProcessCpApplyCmd.java b/rc-service/rc-service-other/other-application/src/main/java/com/red/circle/other/app/command/user/ProcessCpApplyCmd.java index 6c735669..10e4d5b7 100644 --- a/rc-service/rc-service-other/other-application/src/main/java/com/red/circle/other/app/command/user/ProcessCpApplyCmd.java +++ b/rc-service/rc-service-other/other-application/src/main/java/com/red/circle/other/app/command/user/ProcessCpApplyCmd.java @@ -8,6 +8,7 @@ import com.red.circle.mq.business.model.event.user.CpApplyEvent; import com.red.circle.mq.rocket.business.producer.UserMqMessageService; import com.red.circle.other.app.dto.cmd.user.relation.cp.ProcessApplyCmd; import com.red.circle.other.domain.gateway.user.UserProfileGateway; +import com.red.circle.other.infra.database.cache.service.cp.CpRelationshipCacheService; import com.red.circle.other.infra.database.rds.entity.user.user.CpApply; import com.red.circle.other.infra.database.rds.entity.user.user.CpRelationship; import com.red.circle.other.infra.database.rds.entity.user.user.CpValue; @@ -44,6 +45,7 @@ public class ProcessCpApplyCmd { private final UserMqMessageService userMqMessageService; private final CpRelationshipService cpRelationshipService; private final UserProfileGateway userProfileGateway; + private final CpRelationshipCacheService cpRelationshipCacheService; public void execute(ProcessApplyCmd cmd) { process(cmd); @@ -96,7 +98,7 @@ public class ProcessCpApplyCmd { cmd.requireReqSysOrigin()); } - userProfileGateway.removeCacheAll(Arrays.asList(cpApply.getSendApplyUserId(), cpApply.getAcceptApplyUserId())); + cpRelationshipCacheService.remove(Arrays.asList(cpApply.getSendApplyUserId(), cpApply.getAcceptApplyUserId())); } private void refuseStatusWait(ProcessApplyCmd cmd, CpApply cpApply) { diff --git a/rc-service/rc-service-other/other-application/src/main/java/com/red/circle/other/app/command/user/query/UserProfileUseQryExe.java b/rc-service/rc-service-other/other-application/src/main/java/com/red/circle/other/app/command/user/query/UserProfileUseQryExe.java index b4bbf149..566d4123 100644 --- a/rc-service/rc-service-other/other-application/src/main/java/com/red/circle/other/app/command/user/query/UserProfileUseQryExe.java +++ b/rc-service/rc-service-other/other-application/src/main/java/com/red/circle/other/app/command/user/query/UserProfileUseQryExe.java @@ -14,6 +14,8 @@ import com.red.circle.other.app.service.user.relation.UserSupportRelationService import com.red.circle.other.domain.gateway.user.UserProfileGateway; import com.red.circle.other.domain.gateway.user.ability.UserRegionGateway; import com.red.circle.other.domain.model.user.UserProfile; +import com.red.circle.other.infra.database.cache.key.user.UserKey; +import com.red.circle.other.infra.database.cache.service.cp.CpRelationshipCacheService; import com.red.circle.other.infra.database.mongo.entity.live.RoomProfileManager; import com.red.circle.other.infra.database.mongo.service.live.RoomProfileManagerService; import com.red.circle.other.infra.database.rds.service.user.user.CpValueService; @@ -51,6 +53,7 @@ public class UserProfileUseQryExe { private final RedisService redisService; private final UserSupportRelationService userRelationInterviewService; private final CpValueService cpValueService; + private final CpRelationshipCacheService cpRelationshipCacheService; public UserProfileDTO execute(UserProfileQryCmd cmd) { UserProfile userProfile = getUserProfile(cmd); @@ -96,18 +99,9 @@ public class UserProfileUseQryExe { userRelationInterviewService.addVisitor(userIdCmd); } - // 重新赋值cpValue - if (CollectionUtil.isNotEmpty(userProfileDTO.getCpList())) { - Set cpValIdSet = userProfileDTO.getCpList().stream().map(CpSimpleUserProfileCO::getCpValId).collect(Collectors.toSet()); - Map mapCpVal = cpValueService.mapCpVal(cpValIdSet); - userProfileDTO.getCpList().forEach(cp -> { - BigDecimal cpValue = mapCpVal.get(cp.getCpValId()); - if (Objects.nonNull(cpValue)) { - cp.setCpValue(cpValue); - } - }); - } + // 填充用户CP信息 + userProfileDTO.setCpList(cpRelationshipCacheService.getUserCpList(userProfile.getId())); return userProfileDTO; } diff --git a/rc-service/rc-service-other/other-application/src/main/java/com/red/circle/other/app/listener/gift/strategy/RankCountStrategy.java b/rc-service/rc-service-other/other-application/src/main/java/com/red/circle/other/app/listener/gift/strategy/RankCountStrategy.java index d3e59f1b..28639519 100644 --- a/rc-service/rc-service-other/other-application/src/main/java/com/red/circle/other/app/listener/gift/strategy/RankCountStrategy.java +++ b/rc-service/rc-service-other/other-application/src/main/java/com/red/circle/other/app/listener/gift/strategy/RankCountStrategy.java @@ -13,6 +13,7 @@ import com.red.circle.other.domain.ranking.RankingActivityType; import com.red.circle.other.domain.ranking.RankingCycleType; import com.red.circle.other.domain.ranking.RankingDimension; import com.red.circle.other.infra.common.game.GameKtvCommon; +import com.red.circle.other.infra.database.cache.service.cp.CpRelationshipCacheService; import com.red.circle.other.infra.database.cache.service.user.UserCpValueCacheService; import com.red.circle.other.infra.database.mongo.dto.TemporaryActivityCountParam; import com.red.circle.other.infra.database.mongo.entity.gift.GiftAcceptUser; @@ -79,6 +80,7 @@ public class RankCountStrategy implements GiftStrategy { private final GiftGiveRunningWaterService giftGiveRunningWaterService; private final TemporaryActivityCountService temporaryActivityCountService; private final RankingActivityService rankingActivityService; + private final CpRelationshipCacheService cpRelationshipCacheService; @Override public void processor(OfflineProcessGiftEvent event) { @@ -340,8 +342,9 @@ public class RankCountStrategy implements GiftStrategy { userCpValueCacheService.incr(cp.getUserId(), cp.getCpUserId(), acceptAmount.longValue()); - }); + cpRelationshipCacheService.remove(Arrays.asList(cp.getUserId(), cp.getCpUserId())); + }); // 更新cp用户赠送的礼物价值 // cpRelationshipService.updateAccountByUserId(cpRelationship.getUserId(), // cpRelationship.getAmount().add(acceptAmount)); diff --git a/rc-service/rc-service-other/other-application/src/main/java/com/red/circle/other/app/scheduler/CleanExpiredDismissingCpTask.java b/rc-service/rc-service-other/other-application/src/main/java/com/red/circle/other/app/scheduler/CleanExpiredDismissingCpTask.java index 50cb9285..4a41f9ae 100644 --- a/rc-service/rc-service-other/other-application/src/main/java/com/red/circle/other/app/scheduler/CleanExpiredDismissingCpTask.java +++ b/rc-service/rc-service-other/other-application/src/main/java/com/red/circle/other/app/scheduler/CleanExpiredDismissingCpTask.java @@ -1,7 +1,9 @@ package com.red.circle.other.app.scheduler; import com.google.common.collect.Sets; +import com.red.circle.other.domain.gateway.user.UserProfileGateway; import com.red.circle.other.infra.common.user.UserGiftBackpackCommon; +import com.red.circle.other.infra.database.cache.service.cp.CpRelationshipCacheService; import com.red.circle.other.infra.database.cache.service.user.UserCpValueCacheService; import com.red.circle.other.infra.database.mongo.service.user.count.WeekCpValueCountService; import com.red.circle.other.infra.database.rds.entity.user.user.CpRelationship; @@ -10,6 +12,8 @@ import com.red.circle.other.infra.database.rds.service.user.user.CpCabinAchieveS import com.red.circle.other.infra.database.rds.service.user.user.CpRelationshipService; import com.red.circle.other.infra.database.rds.service.user.user.CpValueService; import com.red.circle.tool.core.collection.CollectionUtils; + +import java.util.Arrays; import java.util.List; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; @@ -33,6 +37,8 @@ public class CleanExpiredDismissingCpTask { private final UserCpValueCacheService userCpValueCacheService; private final CpCabinAchieveService cpCabinAchieveService; private final CpBlessRecordService cpBlessRecordService; + private final UserProfileGateway userProfileGateway; + private final CpRelationshipCacheService cpRelationshipCacheService; /** * 每天凌晨3点执行清理任务. @@ -58,7 +64,7 @@ public class CleanExpiredDismissingCpTask { private void cleanCpData(CpRelationship cpRelationship) { try { // 删除CP关系 - cpRelationshipService.deleteCpRelationship(cpRelationship.getUserId()); + cpRelationshipService.deleteById(cpRelationship.getId()); // 删除周统计 weekCpValueCountService.removeThisWeek(cpRelationship.getUserId(), @@ -80,6 +86,8 @@ public class CleanExpiredDismissingCpTask { // 删除祝福记录 cpBlessRecordService.deleteByCpValId(cpRelationship.getCpValId()); + cpRelationshipCacheService.remove(Arrays.asList(cpRelationship.getUserId(), cpRelationship.getCpUserId())); + } catch (Exception e) { log.error("CleanExpiredDismissingCpTask cleanCpData error, userId={}, cpUserId={}", cpRelationship.getUserId(), cpRelationship.getCpUserId(), e); diff --git a/rc-service/rc-service-other/other-infrastructure/src/main/java/com/red/circle/other/infra/database/cache/key/user/UserKey.java b/rc-service/rc-service-other/other-infrastructure/src/main/java/com/red/circle/other/infra/database/cache/key/user/UserKey.java index aedbe8ec..c2c0fe82 100644 --- a/rc-service/rc-service-other/other-infrastructure/src/main/java/com/red/circle/other/infra/database/cache/key/user/UserKey.java +++ b/rc-service/rc-service-other/other-infrastructure/src/main/java/com/red/circle/other/infra/database/cache/key/user/UserKey.java @@ -62,7 +62,13 @@ public enum UserKey implements RedisKeys { /** * 系统用户友谊卡配置. */ - FRIENDSHIP_CARD_CONF; + FRIENDSHIP_CARD_CONF, + + /** + * 用户CP + */ + CP, + ; @Override public String businessPrefix() { diff --git a/rc-service/rc-service-other/other-infrastructure/src/main/java/com/red/circle/other/infra/database/cache/service/cp/CpRelationshipCacheService.java b/rc-service/rc-service-other/other-infrastructure/src/main/java/com/red/circle/other/infra/database/cache/service/cp/CpRelationshipCacheService.java new file mode 100644 index 00000000..163f86a2 --- /dev/null +++ b/rc-service/rc-service-other/other-infrastructure/src/main/java/com/red/circle/other/infra/database/cache/service/cp/CpRelationshipCacheService.java @@ -0,0 +1,24 @@ +package com.red.circle.other.infra.database.cache.service.cp; + +import com.red.circle.other.inner.model.dto.user.CpSimpleUserProfileCO; + +import java.util.List; +import java.util.function.Function; + +/** + * CP关系缓存服务. + */ +public interface CpRelationshipCacheService { + + /** + * 获取用户CP列表. + */ + List getUserCpList(Long userId); + + /** + * 移除用户CP列表缓存. + * + * @param userId 用户id + */ + void remove(List userId); +} diff --git a/rc-service/rc-service-other/other-infrastructure/src/main/java/com/red/circle/other/infra/database/cache/service/cp/impl/CpRelationshipCacheServiceImpl.java b/rc-service/rc-service-other/other-infrastructure/src/main/java/com/red/circle/other/infra/database/cache/service/cp/impl/CpRelationshipCacheServiceImpl.java new file mode 100644 index 00000000..f2dafca0 --- /dev/null +++ b/rc-service/rc-service-other/other-infrastructure/src/main/java/com/red/circle/other/infra/database/cache/service/cp/impl/CpRelationshipCacheServiceImpl.java @@ -0,0 +1,126 @@ +package com.red.circle.other.infra.database.cache.service.cp.impl; + +import com.alibaba.fastjson.JSON; +import com.red.circle.component.redis.service.RedisService; +import com.red.circle.other.infra.database.cache.key.user.UserKey; +import com.red.circle.other.infra.database.cache.service.cp.CpRelationshipCacheService; +import com.red.circle.other.infra.database.rds.entity.user.user.BaseInfo; +import com.red.circle.other.infra.database.rds.entity.user.user.CpRelationship; +import com.red.circle.other.infra.database.rds.service.user.user.BaseInfoService; +import com.red.circle.other.infra.database.rds.service.user.user.CpRelationshipService; +import com.red.circle.other.infra.database.rds.service.user.user.CpValueService; +import com.red.circle.other.inner.model.dto.user.CpSimpleUserProfileCO; +import com.red.circle.tool.core.collection.CollectionUtils; +import com.red.circle.tool.core.date.DateUtils; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Service; + +import java.math.BigDecimal; +import java.time.ZoneId; +import java.util.*; +import java.util.concurrent.TimeUnit; +import java.util.function.Function; +import java.util.stream.Collectors; + +/** + * CP关系缓存服务实现. + */ +@Slf4j +@Service +@RequiredArgsConstructor +public class CpRelationshipCacheServiceImpl implements CpRelationshipCacheService { + + private final CpRelationshipService cpRelationshipService; + private final RedisService redisService; + private final BaseInfoService baseInfoService; + private final CpValueService cpValueService; + + + private static final long CACHE_EXPIRE_SECONDS = 7200L; + + @Override + public List getUserCpList(Long userId) { + String key = getKey(userId); + + String cacheData = redisService.getString(key); + if (cacheData != null) { + return JSON.parseArray(cacheData, CpSimpleUserProfileCO.class); + } + + + try { + List cpRelationshipList = cpRelationshipService.getByUserId(userId); + if (CollectionUtils.isEmpty(cpRelationshipList)) { + redisService.setString(key, JSON.toJSONString(Collections.emptyList()), CACHE_EXPIRE_SECONDS, TimeUnit.SECONDS); + return Collections.emptyList(); + } + + // 批量获取CP用户信息 + Set cpUserIds = cpRelationshipList.stream() + .map(CpRelationship::getCpUserId) + .collect(Collectors.toSet()); + List cpUserProfiles = baseInfoService.listByUserId(cpUserIds); + Map cpUserProfileMap = cpUserProfiles.stream() + .collect(Collectors.toMap(BaseInfo::getId, Function.identity())); + BaseInfo baseInfo = baseInfoService.getById(userId); + + // 批量获取CP值 + Set cpValIds = cpRelationshipList.stream() + .map(CpRelationship::getCpValId) + .collect(Collectors.toSet()); + Map cpValueMap = cpValueService.mapCpVal(cpValIds); + + // 组装CP列表 + List cpList = cpRelationshipList.stream() + .map(cpRelationship -> { + BaseInfo cpUser = cpUserProfileMap.get(cpRelationship.getCpUserId()); + if (cpUser == null) { + return null; + } + + BigDecimal cpValue = cpValueMap.getOrDefault(cpRelationship.getCpValId(), BigDecimal.ZERO); + Long days = DateUtils.toDurationDays(cpRelationship.getCreateTime(), DateUtils.now()); + String firstDay = cpRelationship.getCreateTime().toInstant() + .atZone(ZoneId.systemDefault()).toLocalDate().toString(); + + return new CpSimpleUserProfileCO() + .setMeUserId(baseInfo.getId()) + .setMeAccount(baseInfo.getAccount()) + .setMeUserAvatar(baseInfo.getUserAvatar()) + .setMeUserNickname(baseInfo.getUserNickname()) + .setCpUserId(cpUser.getId()) + .setCpAccount(cpUser.getAccount()) + .setCpUserAvatar(cpUser.getUserAvatar()) + .setCpUserNickname(cpUser.getUserNickname()) + .setCpValId(cpRelationship.getCpValId()) + .setCpValue(cpValue) + .setCreateTime(cpRelationship.getCreateTime()) + .setDays(days) + .setFirstDay(firstDay); + }) + .filter(Objects::nonNull) + .sorted(Comparator.comparing(CpSimpleUserProfileCO::getCpValue).reversed() + .thenComparing(CpSimpleUserProfileCO::getCreateTime)) + .toList(); + + redisService.setString(key, JSON.toJSONString(cpList), CACHE_EXPIRE_SECONDS, TimeUnit.SECONDS); + return cpList; + } catch (Exception e) { + log.error("填充CP数据失败, userId={}", userId, e); + } + + return Collections.emptyList(); + } + + private static String getKey(Long userId) { + return UserKey.CP.getKey(userId); + } + + @Override + public void remove(List userIds) { + userIds.forEach(userId -> { + redisService.delete(getKey(userId)); + }); + } +} diff --git a/rc-service/rc-service-other/other-infrastructure/src/main/java/com/red/circle/other/infra/database/rds/service/user/user/CpRelationshipService.java b/rc-service/rc-service-other/other-infrastructure/src/main/java/com/red/circle/other/infra/database/rds/service/user/user/CpRelationshipService.java index 5387c29a..3dd49367 100644 --- a/rc-service/rc-service-other/other-infrastructure/src/main/java/com/red/circle/other/infra/database/rds/service/user/user/CpRelationshipService.java +++ b/rc-service/rc-service-other/other-infrastructure/src/main/java/com/red/circle/other/infra/database/rds/service/user/user/CpRelationshipService.java @@ -63,11 +63,9 @@ public interface CpRelationshipService extends BaseService { Long getCpValByUserId(Long userId); /** - * 删除cp关系. - * - * @param userId 用户id + * 根据id删除 */ - void deleteCpRelationship(Long userId); + void deleteById(Long id); /** * 组建cp. diff --git a/rc-service/rc-service-other/other-infrastructure/src/main/java/com/red/circle/other/infra/database/rds/service/user/user/impl/CpRelationshipServiceImpl.java b/rc-service/rc-service-other/other-infrastructure/src/main/java/com/red/circle/other/infra/database/rds/service/user/user/impl/CpRelationshipServiceImpl.java index 59e1fef5..55248994 100644 --- a/rc-service/rc-service-other/other-infrastructure/src/main/java/com/red/circle/other/infra/database/rds/service/user/user/impl/CpRelationshipServiceImpl.java +++ b/rc-service/rc-service-other/other-infrastructure/src/main/java/com/red/circle/other/infra/database/rds/service/user/user/impl/CpRelationshipServiceImpl.java @@ -114,9 +114,8 @@ public class CpRelationshipServiceImpl extends } @Override - public void deleteCpRelationship(Long userId) { - delete().eq(CpRelationship::getCpUserId, userId).last(PageConstant.LIMIT_ONE).execute(); - delete().eq(CpRelationship::getUserId, userId).last(PageConstant.LIMIT_ONE).execute(); + public void deleteById(Long id) { + removeById(id); } @Override diff --git a/rc-service/rc-service-other/other-infrastructure/src/main/java/com/red/circle/other/infra/gateway/user/UserRunProfileTransportGatewayImpl.java b/rc-service/rc-service-other/other-infrastructure/src/main/java/com/red/circle/other/infra/gateway/user/UserRunProfileTransportGatewayImpl.java index e9faf39c..7c6b5ae9 100644 --- a/rc-service/rc-service-other/other-infrastructure/src/main/java/com/red/circle/other/infra/gateway/user/UserRunProfileTransportGatewayImpl.java +++ b/rc-service/rc-service-other/other-infrastructure/src/main/java/com/red/circle/other/infra/gateway/user/UserRunProfileTransportGatewayImpl.java @@ -283,9 +283,6 @@ public class UserRunProfileTransportGatewayImpl implements UserRunProfileTranspo // 用户是否首充处理 fillUserFirstRecharge(newUserRunProfile); - // 填充CP列表数据 - fillUserCpList(newUserRunProfile); - newUserRunProfile.addUseProps(userUsePropsEntityMap.get(userBaseInfo.getId())); newUserRunProfile.addUseProps(userUseRoomTheme.get(userBaseInfo.getId())); newUserRunProfile.setOwnSpecialId(userSpecialIdMap.get(userBaseInfo.getId())); @@ -322,68 +319,6 @@ public class UserRunProfileTransportGatewayImpl implements UserRunProfileTranspo } } - /** - * 填充用户CP列表数据 - */ - private void fillUserCpList(UserRunProfile userRunProfile) { - try { - List cpRelationshipList = cpRelationshipService.getByUserId(userRunProfile.getId()); - if (CollectionUtils.isEmpty(cpRelationshipList)) { - return; - } - - // 批量获取CP用户信息 - Set cpUserIds = cpRelationshipList.stream() - .map(CpRelationship::getCpUserId) - .collect(Collectors.toSet()); - List cpUserProfiles = baseInfoService.listByUserId(cpUserIds); - Map cpUserProfileMap = cpUserProfiles.stream() - .collect(Collectors.toMap(BaseInfo::getId, Function.identity())); - - // 批量获取CP值 - Set cpValIds = cpRelationshipList.stream() - .map(CpRelationship::getCpValId) - .collect(Collectors.toSet()); - Map cpValueMap = cpValueService.mapCpVal(cpValIds); - - // 组装CP列表 - List cpList = cpRelationshipList.stream() - .map(cpRelationship -> { - BaseInfo cpUser = cpUserProfileMap.get(cpRelationship.getCpUserId()); - if (cpUser == null) { - return null; - } - BigDecimal cpValue = cpValueMap.getOrDefault(cpRelationship.getCpValId(), BigDecimal.ZERO); - Long days = DateUtils.toDurationDays(cpRelationship.getCreateTime(), DateUtils.now()); - String firstDay = cpRelationship.getCreateTime().toInstant() - .atZone(ZoneId.systemDefault()).toLocalDate().toString(); - - return new CpSimpleUserProfileCO() - .setMeUserId(userRunProfile.getId()) - .setMeAccount(userRunProfile.getAccount()) - .setMeUserAvatar(userRunProfile.getUserAvatar()) - .setMeUserNickname(userRunProfile.getUserNickname()) - .setCpUserId(cpUser.getId()) - .setCpAccount(cpUser.getAccount()) - .setCpUserAvatar(cpUser.getUserAvatar()) - .setCpUserNickname(cpUser.getUserNickname()) - .setCpValId(cpRelationship.getCpValId()) - .setCpValue(cpValue) - .setCreateTime(cpRelationship.getCreateTime()) - .setDays(days) - .setFirstDay(firstDay); - }) - .filter(Objects::nonNull) - .sorted(Comparator.comparing(CpSimpleUserProfileCO::getCpValue).reversed() - .thenComparing(CpSimpleUserProfileCO::getCreateTime)) - .toList(); - - userRunProfile.setCpList(cpList); - } catch (Exception e) { - log.error("填充CP数据失败, userId={}", userRunProfile.getId(), e); - } - } - private Map> mapUserUseBadgeEntity(String sysOrigin, Set userIds) { return Optional.ofNullable(this.listUserUseBadgeEntity(sysOrigin, userIds))