Commit 5c032cbe by lixiaozhong

1、好友关系与好友推荐,如果已经是好友则推荐的时候忽略;

2、如果推荐好友变成了好友,则推荐关系删除
3、优化了性能,确保能走批量的一定走批量,确保全部命中索引
parent 4b4f9f39
package com.wecloud.im.friend.controller; package com.wecloud.im.friend.controller;
import com.wecloud.im.entity.ImClient; import com.wecloud.im.entity.ImClient;
import com.wecloud.im.enums.FriendStateEnum;
import com.wecloud.im.friend.param.ImFriendApplyDto; import com.wecloud.im.friend.param.ImFriendApplyDto;
import com.wecloud.im.friend.param.ImFriendBaseDto; import com.wecloud.im.friend.param.ImFriendBaseDto;
import com.wecloud.im.friend.param.ImFriendPageParam; import com.wecloud.im.friend.param.ImFriendPageParam;
...@@ -10,6 +11,7 @@ import com.wecloud.im.service.ImClientService; ...@@ -10,6 +11,7 @@ import com.wecloud.im.service.ImClientService;
import io.geekidea.springbootplus.framework.common.api.ApiCode; import io.geekidea.springbootplus.framework.common.api.ApiCode;
import io.geekidea.springbootplus.framework.common.api.ApiResult; import io.geekidea.springbootplus.framework.common.api.ApiResult;
import io.geekidea.springbootplus.framework.common.controller.BaseController; import io.geekidea.springbootplus.framework.common.controller.BaseController;
import io.geekidea.springbootplus.framework.common.enums.BaseEnum;
import io.geekidea.springbootplus.framework.core.pagination.Paging; import io.geekidea.springbootplus.framework.core.pagination.Paging;
import io.geekidea.springbootplus.framework.log.annotation.OperationLog; import io.geekidea.springbootplus.framework.log.annotation.OperationLog;
import io.geekidea.springbootplus.framework.log.enums.OperationLogType; import io.geekidea.springbootplus.framework.log.enums.OperationLogType;
...@@ -79,15 +81,15 @@ public class ImFriendController extends BaseController { ...@@ -79,15 +81,15 @@ public class ImFriendController extends BaseController {
if(currentClient.getId().equals(friendClient.getId())) { if(currentClient.getId().equals(friendClient.getId())) {
return ApiResult.fail(ApiCode.PARAMETER_EXCEPTION, null); return ApiResult.fail(ApiCode.PARAMETER_EXCEPTION, null);
} }
imFriendService.applyFriend(currentClient.getId(), friendClient.getId(), friendName, requestRemark); imFriendService.applyFriend(currentClient, friendClient, friendName, requestRemark);
return ApiResult.ok(); return ApiResult.ok();
} }
/** /**
* 待接受的好友请求列表 * 待接受的好友请求列表,最多只返回1000个
*/ */
@PostMapping("/unsureFriends") @PostMapping("/unsureFriends")
@ApiOperation(value = "待接受的好友请求列表") @ApiOperation(value = "待接受的好友请求列表,最多只返回1000个")
public ApiResult<Paging<ImFriendApplyDto>> getUnsureFriends() { public ApiResult<Paging<ImFriendApplyDto>> getUnsureFriends() {
ImClient currentClient = imClientService.getCurentClient(); ImClient currentClient = imClientService.getCurentClient();
if(currentClient == null) { if(currentClient == null) {
...@@ -165,6 +167,11 @@ public class ImFriendController extends BaseController { ...@@ -165,6 +167,11 @@ public class ImFriendController extends BaseController {
if(CollectionUtils.isEmpty(recommendFriends)) { if(CollectionUtils.isEmpty(recommendFriends)) {
return ApiResult.fail(ApiCode.PARAMETER_EXCEPTION, null); return ApiResult.fail(ApiCode.PARAMETER_EXCEPTION, null);
} }
for (ImFriendRecommendDto p : recommendFriends) {
if (BaseEnum.valueOf(FriendStateEnum.class, p.getSource()) == null) {
return ApiResult.fail(ApiCode.PARAMETER_EXCEPTION, null);
}
}
imFriendService.batchCreateRecommend(currentClient, recommendFriends); imFriendService.batchCreateRecommend(currentClient, recommendFriends);
return ApiResult.ok(); return ApiResult.ok();
} }
......
...@@ -19,11 +19,19 @@ import java.util.List; ...@@ -19,11 +19,19 @@ import java.util.List;
@Repository @Repository
public interface ImFriendMapper extends BaseMapper<ImFriend> { public interface ImFriendMapper extends BaseMapper<ImFriend> {
IPage<ImFriendBaseDto> getImFriendPageList(@Param("page") Page<ImFriendBaseDto> page, @Param("clientId") Long clientId); IPage<ImFriendBaseDto> getImFriendPageList(@Param("page") Page<ImFriendBaseDto> page, @Param("clientId") Long fkClientId);
IPage<ImFriendApplyDto> getUnsureFriendPageList(@Param("page") Page<ImFriendApplyDto> page, @Param("clientId") Long clientId); IPage<ImFriendApplyDto> getUnsureFriendPageList(@Param("page") Page<ImFriendApplyDto> page, @Param("clientId") Long fkClientId);
ImFriendApplyDto getFriendInfo(@Param("clientId") Long clientId, @Param("friendClientId") Long friendClientId); ImFriendApplyDto getFriendInfo(@Param("clientId") Long fkClientId, @Param("friendClientId") Long fkFriendClientId);
void batchDeleteFriend(@Param("clientId") Long clientId, @Param("fkAppId") Long fkAppId, @Param("friendClientIds") List<String> friendClientIds); /**
* 过滤得到真的好友,非好友的friendId将被它忽略
* @param fkClientId
* @param fkFriendClientIds
* @return
*/
List<Long> filterNegativeFriends(@Param("clientId") Long fkClientId, @Param("friendClientIds") List<Long> fkFriendClientIds);
void batchDeleteFriend(@Param("clientId") Long fkClientId, @Param("fkAppId") Long fkAppId, @Param("friendClientIds") List<String> friendClientIds);
} }
...@@ -23,7 +23,7 @@ public class ImFriendBaseDto implements Serializable { ...@@ -23,7 +23,7 @@ public class ImFriendBaseDto implements Serializable {
@ApiModelProperty("好友的昵称备注") @ApiModelProperty("好友的昵称备注")
private String friendName; private String friendName;
@ApiModelProperty("好友关系状态") @ApiModelProperty("好友关系状态,1:待确定,2:已确认,3:已拒绝,4:已删除")
private Integer state; private Integer state;
} }
package com.wecloud.im.friend.service; package com.wecloud.im.friend.service;
import java.util.Arrays;
import java.util.Collections;
import java.util.Date; import java.util.Date;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
...@@ -70,23 +72,23 @@ public class ImFriendService extends BaseServiceImpl<ImFriendMapper, ImFriend> { ...@@ -70,23 +72,23 @@ public class ImFriendService extends BaseServiceImpl<ImFriendMapper, ImFriend> {
/** /**
* 好友申请 * 好友申请
* @param currentClientId * @param currentClient
* @param friendClientId * @param friendClient
* @param friendName * @param friendName
* @param requestRemark * @param requestRemark
* @return * @return
*/ */
@Transactional @Transactional
public Boolean applyFriend(Long currentClientId, Long friendClientId, String friendName, String requestRemark) { public Boolean applyFriend(ImClient currentClient,ImClient friendClient, String friendName, String requestRemark) {
//好友关系有维护两条,我和他,他和我 //好友关系有维护两条,我和他,他和我
ImFriend my = new ImFriend(); ImFriend my = new ImFriend();
my.setFkClientId(currentClientId); my.setFkClientId(currentClient.getId());
my.setFkClientIdFriend(friendClientId); my.setFkClientIdFriend(friendClient.getId());
my.setFkClientIdClaimer(currentClientId); my.setFkClientIdClaimer(currentClient.getId());
my.setFriendName(friendName); my.setFriendName(friendName);
my.setRequestRemark(requestRemark); my.setRequestRemark(requestRemark);
my.setState(FriendStateEnum.UNSURE.getCode()); my.setState(FriendStateEnum.UNSURE.getCode());
if(getByKey(currentClientId, friendClientId) != null) { if(getByKey(currentClient.getId(), friendClient.getId()) != null) {
if(StringUtils.isNotEmpty(requestRemark)) { if(StringUtils.isNotEmpty(requestRemark)) {
my.setRequestRemark(requestRemark); my.setRequestRemark(requestRemark);
} }
...@@ -101,12 +103,12 @@ public class ImFriendService extends BaseServiceImpl<ImFriendMapper, ImFriend> { ...@@ -101,12 +103,12 @@ public class ImFriendService extends BaseServiceImpl<ImFriendMapper, ImFriend> {
} }
ImFriend friend = new ImFriend(); ImFriend friend = new ImFriend();
friend.setFkClientId(friendClientId); friend.setFkClientId(friendClient.getId());
friend.setFkClientIdFriend(currentClientId); friend.setFkClientIdFriend(currentClient.getId());
friend.setFkClientIdClaimer(currentClientId); friend.setFkClientIdClaimer(currentClient.getId());
friend.setRequestRemark(requestRemark); friend.setRequestRemark(requestRemark);
friend.setState(FriendStateEnum.UNSURE.getCode()); friend.setState(FriendStateEnum.UNSURE.getCode());
if(getByKey(friendClientId, currentClientId) != null) { if(getByKey(friendClient.getId(), currentClient.getId()) != null) {
if(StringUtils.isNotEmpty(requestRemark)) { if(StringUtils.isNotEmpty(requestRemark)) {
friend.setRequestRemark(requestRemark); friend.setRequestRemark(requestRemark);
} }
...@@ -120,6 +122,10 @@ public class ImFriendService extends BaseServiceImpl<ImFriendMapper, ImFriend> { ...@@ -120,6 +122,10 @@ public class ImFriendService extends BaseServiceImpl<ImFriendMapper, ImFriend> {
this.save(friend); this.save(friend);
} }
//既然申请好友了,就删除好友推荐
this.batchDeleteRecommend(currentClient, Collections.singletonList(friendClient.getClientId()));
this.batchDeleteRecommend(friendClient, Collections.singletonList(currentClient.getClientId()));
return true; return true;
} }
...@@ -215,20 +221,33 @@ public class ImFriendService extends BaseServiceImpl<ImFriendMapper, ImFriend> { ...@@ -215,20 +221,33 @@ public class ImFriendService extends BaseServiceImpl<ImFriendMapper, ImFriend> {
List<ImFriendRecommendDto> friends = recommendFriends.stream().filter(p -> !currentClient.getClientId().equals(p.getFriendClientId())).collect(Collectors.toList()); List<ImFriendRecommendDto> friends = recommendFriends.stream().filter(p -> !currentClient.getClientId().equals(p.getFriendClientId())).collect(Collectors.toList());
List<String> friendClientIds = friends.stream().map(ImFriendRecommendDto::getFriendClientId).collect(Collectors.toList()); List<String> friendClientIds = friends.stream().map(ImFriendRecommendDto::getFriendClientId).collect(Collectors.toList());
// 批量获取clientId 对应的内表 id主键 // 批量获取clientId 对应的内表 id主键
List<ImClientSimpleDto> simpleClients = imClientService.getSimpleClients(currentClient.getFkAppid(), friendClientIds); List<ImClientSimpleDto> simpleFriendClients = imClientService.getSimpleClients(currentClient.getFkAppid(), friendClientIds);
Map<String, Long> friendIdMap = simpleClients.stream().collect(Collectors.toMap(ImClientSimpleDto::getClientId, if(CollectionUtils.isEmpty(simpleFriendClients)) {
return;
}
//对已经存在的好友过滤掉
List<Long> maybeFriendIds = simpleFriendClients.stream().map(ImClientSimpleDto::getId).collect(Collectors.toList());
List<Long> alreadyFriendIds = imFriendMapper.filterNegativeFriends(currentClient.getId(), maybeFriendIds);
Map<String, Long> friendIdMap = simpleFriendClients.stream().collect(Collectors.toMap(ImClientSimpleDto::getClientId,
ImClientSimpleDto::getId, (a, b) -> a)); ImClientSimpleDto::getId, (a, b) -> a));
List<ImFriendRecommend> list = new ArrayList<>(); List<ImFriendRecommend> list = new ArrayList<>();
for (ImFriendRecommendDto p : friends) { for (ImFriendRecommendDto p : friends) {
Long fkFriendCliendId = friendIdMap.get(p.getFriendClientId());
// 只有client存在的数据才是真数据
if(fkFriendCliendId == null) {
continue;
}
// 如果推荐的人已经是好友,则忽略。
if (alreadyFriendIds.contains(fkFriendCliendId)) {
continue;
}
ImFriendRecommend recommend = new ImFriendRecommend(); ImFriendRecommend recommend = new ImFriendRecommend();
recommend.setId(SnowflakeUtil.getId()); recommend.setId(SnowflakeUtil.getId());
recommend.setFkClientId(currentClient.getId()); recommend.setFkClientId(currentClient.getId());
// 已经是好友的,不再推荐了。如果推荐的人已经是好友,则忽略。推荐的人如果多,走批量比较好,记个 todo
ImFriendApplyDto friendInfo = imFriendMapper.getFriendInfo(currentClient.getId(), friendIdMap.get(p.getFriendClientId())); recommend.setFkClientIdFriend(fkFriendCliendId);
if (friendInfo != null) {
continue;
}
recommend.setFkClientIdFriend(friendIdMap.get(p.getFriendClientId()));
recommend.setSource(p.getSource()); recommend.setSource(p.getSource());
recommend.setDelFlag(false); recommend.setDelFlag(false);
list.add(recommend); list.add(recommend);
...@@ -236,6 +255,7 @@ public class ImFriendService extends BaseServiceImpl<ImFriendMapper, ImFriend> { ...@@ -236,6 +255,7 @@ public class ImFriendService extends BaseServiceImpl<ImFriendMapper, ImFriend> {
if(CollectionUtils.isEmpty(list)) { if(CollectionUtils.isEmpty(list)) {
return; return;
} }
// 如果数据重复,只会更新updateTime
imFriendRecommendMapper.batchCreateRecommend(list); imFriendRecommendMapper.batchCreateRecommend(list);
} }
} }
...@@ -30,6 +30,16 @@ ...@@ -30,6 +30,16 @@
where fk_client_id = #{clientId} and state = 1 where fk_client_id = #{clientId} and state = 1
</select> </select>
<select id="filterNegativeFriends" resultType="java.lang.Long">
select
fk_client_id_friend
from im_friend
where fk_client_id = #{clientId} and fk_client_id_friend in
<foreach collection="friendClientIds" item="friendClientId" open="(" close=")" separator=",">
#{friendClientId}
</foreach>
</select>
<update id="batchDeleteFriend"> <update id="batchDeleteFriend">
update im_friend set state = 4 where fk_client_id = #{clientId} and fk_client_id_friend in ( update im_friend set state = 4 where fk_client_id = #{clientId} and fk_client_id_friend in (
select id from im_client where (fk_appid, client_id) in select id from im_client where (fk_appid, client_id) in
......
...@@ -249,7 +249,7 @@ CREATE TABLE `im_friend` ( ...@@ -249,7 +249,7 @@ CREATE TABLE `im_friend` (
`friend_name` varchar(255) DEFAULT NULL COMMENT '好友名称备注', `friend_name` varchar(255) DEFAULT NULL COMMENT '好友名称备注',
`reject_remark` varchar(255) DEFAULT NULL COMMENT '拒绝说明', `reject_remark` varchar(255) DEFAULT NULL COMMENT '拒绝说明',
`request_remark` varchar(255) DEFAULT NULL COMMENT '好友请求说明', `request_remark` varchar(255) DEFAULT NULL COMMENT '好友请求说明',
`state` tinyint NOT NULL DEFAULT '0' COMMENT '好友状态,1:待确定,2:已确认,3:已删除', `state` tinyint NOT NULL DEFAULT '0' COMMENT '好友状态,1:待确定,2:已确认,3:已拒绝,4:已删除',
`create_time` timestamp NULL DEFAULT NULL COMMENT '创建时间', `create_time` timestamp NULL DEFAULT NULL COMMENT '创建时间',
`update_time` timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP COMMENT '修改时间', `update_time` timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP COMMENT '修改时间',
PRIMARY KEY (`id`) USING BTREE, PRIMARY KEY (`id`) USING BTREE,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment