封装的参数类
@Data
@Schema(description = "关注粉丝分页列表")
public class PageFollowsRequest {
@Schema(description = "用户ID")
private Long accountId;
@Schema(description = "用户ID")
private int pagesize;
@Schema(description = "用户ID")
private int currentPage;
}
第一种方法
@Operation(summary = "查看用户的粉丝用户列表")
@GetMapping("/AccountFans")
public Result<List<AccountResponse>> AccountFans(@Validated PageFollowsRequest pageFollowsRequest) {
Long accountId=pageFollowsRequest.getAccountId();
int pagesize=pageFollowsRequest.getPagesize();
int currentPage=pageFollowsRequest.getCurrentPage();
return Result.ok(accountService.getAccountRelationsFan(accountId,pagesize,currentPage));
}
//粉丝列表
@Override
public List<AccountResponse> getAccountRelationsFan(Long accountId, int pagesize, int currentPage) {
//用来查看当前用户
Long currentLoginAccountId = loginContext.getAccountId();
// 查看关注用户id
List<AccountFollow> followList = accountFollowService.list(
Wrappers.<AccountFollow>lambdaQuery()
.eq(AccountFollow::getAccountId, accountId)
);
// 获取粉丝用户id
List<AccountFollow> fansList = accountFollowService.list(
Wrappers.<AccountFollow>lambdaQuery()
.eq(AccountFollow::getFollowId, accountId)
);
// 获取关注用户id
Set<Long> followIds = followList.stream().map(AccountFollow::getFollowId).collect(Collectors.toSet());
// 获取粉丝用户id
Set<Long> fansIds = fansList.stream().map(AccountFollow::getAccountId).collect(Collectors.toSet());
if (CollUtil.isEmpty(fansIds)) {
return Collections.emptyList();
}
// 查询粉丝列表(最终返回结果)----做分页处理
List<AccountResponse> followAccounts = accountMapper.selectFollow(fansIds);
// 交集 互相关注
Set<Long> intersectionIds = CollUtil.intersectionDistinct(followIds, fansIds);
// 如果是当前登录的用户
if (currentLoginAccountId.equals(accountId)) {
return followAccounts.stream()
.peek(accountResponse -> {
if (intersectionIds.contains(accountResponse.getId())) {
accountResponse.setRelation(AccountResponse.MUTUAL_FOLLOW);
} else {
accountResponse.setRelation(AccountResponse.UNFOLLOW);
}
}).collect(Collectors.toList());
}
//查看我的关注用户id
List<AccountFollow> currentFollowList = accountFollowService.list(
Wrappers.<AccountFollow>lambdaQuery()
.eq(AccountFollow::getAccountId, currentLoginAccountId)
);
//查看我的粉丝用户id
List<AccountFollow> currentFansList = accountFollowService.list(
Wrappers.<AccountFollow>lambdaQuery()
.eq(AccountFollow::getFollowId, currentLoginAccountId)
);
//我的关注ID列表
Set<Long> myFollowIds = currentFollowList.stream().map(AccountFollow::getFollowId).collect(Collectors.toSet());
//我的粉思Id列表
Set<Long> myFansIds = currentFansList.stream().map(AccountFollow::getAccountId).collect(Collectors.toSet());
//我的交集
Set<Long> myIntersection = CollUtil.intersectionDistinct(myFollowIds, myFansIds);
// 所有交集关系 -- 关注关系
Set<Long> allIntersection = CollUtil.intersectionDistinct(fansIds, myFollowIds);
// 互相关注关系
Set<Long> mutualIntersection = CollUtil.intersectionDistinct(allIntersection, myIntersection);
allIntersection.removeAll(mutualIntersection);
for (AccountResponse followAccount : followAccounts) {
Long id = followAccount.getId();
if (allIntersection.contains(id)) {
// 已关注
followAccount.setRelation(AccountResponse.FOLLOWED);
} else if (mutualIntersection.contains(id)) {
// 互相关注
followAccount.setRelation(AccountResponse.MUTUAL_FOLLOW);
} else {
// 未关注
followAccount.setRelation(AccountResponse.UNFOLLOW);
}
}
//分页处理
List<AccountResponse> currentPageList = new ArrayList<>();
if (followAccounts != null && followAccounts.size() > 0) {
int currIdx = (currentPage > 1 ? (currentPage - 1) * pagesize : 0);
for (int i = 0; i < pagesize && i < followAccounts.size() - currIdx; i++) {
AccountResponse data = followAccounts.get(currIdx + i);
currentPageList.add(data);
}
}
return currentPageList;
}
第二种方法
@Operation(summary = "查看用户的关注用户列表")
@GetMapping("/AccountFollow")
public Result<List<AccountResponse>> AccountsFollow(@Validated PageFollowsRequest pageFollowsRequest) {
Long accountId=pageFollowsRequest.getAccountId();
int pagesize=pageFollowsRequest.getPagesize();
int currentPage=pageFollowsRequest.getCurrentPage();
return Result.ok(accountService.getAccountRelationsFollow(accountId,pagesize,currentPage));
}
//关注列表
@Override
public List<AccountResponse> getAccountRelationsFollow(Long accountId, int pagesize, int currentPage) {
//用来查看当前用户
Long currentLoginAccountId = loginContext.getAccountId();
// 查看关注用户id
List<AccountFollow> followList = accountFollowService.list(
Wrappers.<AccountFollow>lambdaQuery()
.eq(AccountFollow::getAccountId, accountId)
);
// 获取粉丝用户id
List<AccountFollow> fansList = accountFollowService.list(
Wrappers.<AccountFollow>lambdaQuery()
.eq(AccountFollow::getFollowId, accountId)
);
// 获取关注用户id
Set<Long> followIds = followList.stream().map(AccountFollow::getFollowId).collect(Collectors.toSet());
// 获取粉丝用户id
Set<Long> fansIds = fansList.stream().map(AccountFollow::getAccountId).collect(Collectors.toSet());
if (CollUtil.isEmpty(followIds)) {
return Collections.emptyList();
}
// 查询关注列表
List<AccountResponse> followAccounts = accountMapper.selectFollow(followIds);
// 交集 互相关注
Set<Long> intersectionIds = CollUtil.intersectionDistinct(followIds, fansIds);
// 如果是当前登录的用户
if (currentLoginAccountId.equals(accountId)) {
return followAccounts.stream()
.peek(accountResponse -> {
if (intersectionIds.contains(accountResponse.getId())) {
accountResponse.setRelation(AccountResponse.MUTUAL_FOLLOW);
} else {
accountResponse.setRelation(AccountResponse.FOLLOWED);
}
}).collect(Collectors.toList());
}
//查看我的关注用户id
List<AccountFollow> currentFollowList = accountFollowService.list(
Wrappers.<AccountFollow>lambdaQuery()
.eq(AccountFollow::getAccountId, currentLoginAccountId)
);
//查看我的粉丝用户id
List<AccountFollow> currentFansList = accountFollowService.list(
Wrappers.<AccountFollow>lambdaQuery()
.eq(AccountFollow::getFollowId, currentLoginAccountId)
);
//我的关注ID列表
Set<Long> myFollowIds = currentFollowList.stream().map(AccountFollow::getFollowId).collect(Collectors.toSet());
//我的粉思Id列表
Set<Long> myFansIds = currentFansList.stream().map(AccountFollow::getAccountId).collect(Collectors.toSet());
//我的交集
Set<Long> myIntersection = CollUtil.intersectionDistinct(myFollowIds, myFansIds);
// 所有交集关系 -- 关注关系
Set<Long> allIntersection = CollUtil.intersectionDistinct(followIds, myFollowIds);
// 互相关注关系
Set<Long> mutualIntersection = CollUtil.intersectionDistinct(allIntersection, myIntersection);
allIntersection.removeAll(mutualIntersection);
for (AccountResponse followAccount : followAccounts) {
Long id = followAccount.getId();
if (allIntersection.contains(id)) {
// 已关注
followAccount.setRelation(AccountResponse.FOLLOWED);
} else if (mutualIntersection.contains(id)) {
// 互相关注
followAccount.setRelation(AccountResponse.MUTUAL_FOLLOW);
} else {
// 未关注
followAccount.setRelation(AccountResponse.UNFOLLOW);
}
}
//分页
int totalcount = followAccounts.size();
int pagecount = 0;
List<AccountResponse> subList;
int m = totalcount % pagesize;
if (m > 0) {
pagecount = totalcount / pagesize + 1;
} else {
pagecount = totalcount / pagesize;
}
if (m == 0) {
subList = followAccounts.subList((currentPage - 1) * pagesize, pagesize * (currentPage));
} else {
if (currentPage == pagecount) {
subList = followAccounts.subList((currentPage - 1) * pagesize, totalcount);
} else {
subList = followAccounts.subList((currentPage - 1) * pagesize, pagesize * (currentPage));
}
}
return subList;
}