Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
W
wecloud_im_server
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
hewei
wecloud_im_server
Commits
5c032cbe
Commit
5c032cbe
authored
Jan 13, 2022
by
lixiaozhong
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
1、好友关系与好友推荐,如果已经是好友则推荐的时候忽略;
2、如果推荐好友变成了好友,则推荐关系删除 3、优化了性能,确保能走批量的一定走批量,确保全部命中索引
parent
4b4f9f39
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
73 additions
and
28 deletions
+73
-28
core/src/main/java/com/wecloud/im/friend/controller/ImFriendController.java
+10
-3
core/src/main/java/com/wecloud/im/friend/mapper/ImFriendMapper.java
+12
-4
core/src/main/java/com/wecloud/im/friend/param/ImFriendBaseDto.java
+1
-1
core/src/main/java/com/wecloud/im/friend/service/ImFriendService.java
+39
-19
core/src/main/resources/mapper/ImFriendMapper.xml
+10
-0
docs/db/wecloud_im.sql
+1
-1
No files found.
core/src/main/java/com/wecloud/im/friend/controller/ImFriendController.java
View file @
5c032cbe
package
com
.
wecloud
.
im
.
friend
.
controller
;
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.ImFriendBaseDto
;
import
com.wecloud.im.friend.param.ImFriendPageParam
;
...
...
@@ -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.ApiResult
;
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.log.annotation.OperationLog
;
import
io.geekidea.springbootplus.framework.log.enums.OperationLogType
;
...
...
@@ -79,15 +81,15 @@ public class ImFriendController extends BaseController {
if
(
currentClient
.
getId
().
equals
(
friendClient
.
getId
()))
{
return
ApiResult
.
fail
(
ApiCode
.
PARAMETER_EXCEPTION
,
null
);
}
imFriendService
.
applyFriend
(
currentClient
.
getId
(),
friendClient
.
getId
()
,
friendName
,
requestRemark
);
imFriendService
.
applyFriend
(
currentClient
,
friendClient
,
friendName
,
requestRemark
);
return
ApiResult
.
ok
();
}
/**
* 待接受的好友请求列表
* 待接受的好友请求列表
,最多只返回1000个
*/
@PostMapping
(
"/unsureFriends"
)
@ApiOperation
(
value
=
"待接受的好友请求列表"
)
@ApiOperation
(
value
=
"待接受的好友请求列表
,最多只返回1000个
"
)
public
ApiResult
<
Paging
<
ImFriendApplyDto
>>
getUnsureFriends
()
{
ImClient
currentClient
=
imClientService
.
getCurentClient
();
if
(
currentClient
==
null
)
{
...
...
@@ -165,6 +167,11 @@ public class ImFriendController extends BaseController {
if
(
CollectionUtils
.
isEmpty
(
recommendFriends
))
{
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
);
return
ApiResult
.
ok
();
}
...
...
core/src/main/java/com/wecloud/im/friend/mapper/ImFriendMapper.java
View file @
5c032cbe
...
...
@@ -19,11 +19,19 @@ import java.util.List;
@Repository
public
interface
ImFriendMapper
extends
BaseMapper
<
ImFriend
>
{
IPage
<
ImFriendBaseDto
>
getImFriendPageList
(
@Param
(
"page"
)
Page
<
ImFriendBaseDto
>
page
,
@Param
(
"clientId"
)
Long
c
lientId
);
IPage
<
ImFriendBaseDto
>
getImFriendPageList
(
@Param
(
"page"
)
Page
<
ImFriendBaseDto
>
page
,
@Param
(
"clientId"
)
Long
fkC
lientId
);
IPage
<
ImFriendApplyDto
>
getUnsureFriendPageList
(
@Param
(
"page"
)
Page
<
ImFriendApplyDto
>
page
,
@Param
(
"clientId"
)
Long
c
lientId
);
IPage
<
ImFriendApplyDto
>
getUnsureFriendPageList
(
@Param
(
"page"
)
Page
<
ImFriendApplyDto
>
page
,
@Param
(
"clientId"
)
Long
fkC
lientId
);
ImFriendApplyDto
getFriendInfo
(
@Param
(
"clientId"
)
Long
clientId
,
@Param
(
"friendClientId"
)
Long
f
riendClientId
);
ImFriendApplyDto
getFriendInfo
(
@Param
(
"clientId"
)
Long
fkClientId
,
@Param
(
"friendClientId"
)
Long
fkF
riendClientId
);
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
);
}
core/src/main/java/com/wecloud/im/friend/param/ImFriendBaseDto.java
View file @
5c032cbe
...
...
@@ -23,7 +23,7 @@ public class ImFriendBaseDto implements Serializable {
@ApiModelProperty
(
"好友的昵称备注"
)
private
String
friendName
;
@ApiModelProperty
(
"好友关系状态"
)
@ApiModelProperty
(
"好友关系状态
,1:待确定,2:已确认,3:已拒绝,4:已删除
"
)
private
Integer
state
;
}
core/src/main/java/com/wecloud/im/friend/service/ImFriendService.java
View file @
5c032cbe
package
com
.
wecloud
.
im
.
friend
.
service
;
import
java.util.Arrays
;
import
java.util.Collections
;
import
java.util.Date
;
import
com.baomidou.mybatisplus.core.conditions.query.QueryWrapper
;
...
...
@@ -70,23 +72,23 @@ public class ImFriendService extends BaseServiceImpl<ImFriendMapper, ImFriend> {
/**
* 好友申请
* @param currentClient
Id
* @param friendClient
Id
* @param currentClient
* @param friendClient
* @param friendName
* @param requestRemark
* @return
*/
@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
();
my
.
setFkClientId
(
currentClient
Id
);
my
.
setFkClientIdFriend
(
friendClient
Id
);
my
.
setFkClientIdClaimer
(
currentClient
Id
);
my
.
setFkClientId
(
currentClient
.
getId
()
);
my
.
setFkClientIdFriend
(
friendClient
.
getId
()
);
my
.
setFkClientIdClaimer
(
currentClient
.
getId
()
);
my
.
setFriendName
(
friendName
);
my
.
setRequestRemark
(
requestRemark
);
my
.
setState
(
FriendStateEnum
.
UNSURE
.
getCode
());
if
(
getByKey
(
currentClient
Id
,
friendClientId
)
!=
null
)
{
if
(
getByKey
(
currentClient
.
getId
(),
friendClient
.
getId
()
)
!=
null
)
{
if
(
StringUtils
.
isNotEmpty
(
requestRemark
))
{
my
.
setRequestRemark
(
requestRemark
);
}
...
...
@@ -101,12 +103,12 @@ public class ImFriendService extends BaseServiceImpl<ImFriendMapper, ImFriend> {
}
ImFriend
friend
=
new
ImFriend
();
friend
.
setFkClientId
(
friendClient
Id
);
friend
.
setFkClientIdFriend
(
currentClient
Id
);
friend
.
setFkClientIdClaimer
(
currentClient
Id
);
friend
.
setFkClientId
(
friendClient
.
getId
()
);
friend
.
setFkClientIdFriend
(
currentClient
.
getId
()
);
friend
.
setFkClientIdClaimer
(
currentClient
.
getId
()
);
friend
.
setRequestRemark
(
requestRemark
);
friend
.
setState
(
FriendStateEnum
.
UNSURE
.
getCode
());
if
(
getByKey
(
friendClient
Id
,
currentClientId
)
!=
null
)
{
if
(
getByKey
(
friendClient
.
getId
(),
currentClient
.
getId
()
)
!=
null
)
{
if
(
StringUtils
.
isNotEmpty
(
requestRemark
))
{
friend
.
setRequestRemark
(
requestRemark
);
}
...
...
@@ -120,6 +122,10 @@ public class ImFriendService extends BaseServiceImpl<ImFriendMapper, ImFriend> {
this
.
save
(
friend
);
}
//既然申请好友了,就删除好友推荐
this
.
batchDeleteRecommend
(
currentClient
,
Collections
.
singletonList
(
friendClient
.
getClientId
()));
this
.
batchDeleteRecommend
(
friendClient
,
Collections
.
singletonList
(
currentClient
.
getClientId
()));
return
true
;
}
...
...
@@ -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
<
String
>
friendClientIds
=
friends
.
stream
().
map
(
ImFriendRecommendDto:
:
getFriendClientId
).
collect
(
Collectors
.
toList
());
// 批量获取clientId 对应的内表 id主键
List
<
ImClientSimpleDto
>
simpleClients
=
imClientService
.
getSimpleClients
(
currentClient
.
getFkAppid
(),
friendClientIds
);
Map
<
String
,
Long
>
friendIdMap
=
simpleClients
.
stream
().
collect
(
Collectors
.
toMap
(
ImClientSimpleDto:
:
getClientId
,
List
<
ImClientSimpleDto
>
simpleFriendClients
=
imClientService
.
getSimpleClients
(
currentClient
.
getFkAppid
(),
friendClientIds
);
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
));
List
<
ImFriendRecommend
>
list
=
new
ArrayList
<>();
for
(
ImFriendRecommendDto
p
:
friends
)
{
Long
fkFriendCliendId
=
friendIdMap
.
get
(
p
.
getFriendClientId
());
// 只有client存在的数据才是真数据
if
(
fkFriendCliendId
==
null
)
{
continue
;
}
// 如果推荐的人已经是好友,则忽略。
if
(
alreadyFriendIds
.
contains
(
fkFriendCliendId
))
{
continue
;
}
ImFriendRecommend
recommend
=
new
ImFriendRecommend
();
recommend
.
setId
(
SnowflakeUtil
.
getId
());
recommend
.
setFkClientId
(
currentClient
.
getId
());
// 已经是好友的,不再推荐了。如果推荐的人已经是好友,则忽略。推荐的人如果多,走批量比较好,记个 todo
ImFriendApplyDto
friendInfo
=
imFriendMapper
.
getFriendInfo
(
currentClient
.
getId
(),
friendIdMap
.
get
(
p
.
getFriendClientId
()));
if
(
friendInfo
!=
null
)
{
continue
;
}
recommend
.
setFkClientIdFriend
(
friendIdMap
.
get
(
p
.
getFriendClientId
()));
recommend
.
setFkClientIdFriend
(
fkFriendCliendId
);
recommend
.
setSource
(
p
.
getSource
());
recommend
.
setDelFlag
(
false
);
list
.
add
(
recommend
);
...
...
@@ -236,6 +255,7 @@ public class ImFriendService extends BaseServiceImpl<ImFriendMapper, ImFriend> {
if
(
CollectionUtils
.
isEmpty
(
list
))
{
return
;
}
// 如果数据重复,只会更新updateTime
imFriendRecommendMapper
.
batchCreateRecommend
(
list
);
}
}
core/src/main/resources/mapper/ImFriendMapper.xml
View file @
5c032cbe
...
...
@@ -30,6 +30,16 @@
where fk_client_id = #{clientId} and state = 1
</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 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
...
...
docs/db/wecloud_im.sql
View file @
5c032cbe
...
...
@@ -249,7 +249,7 @@ CREATE TABLE `im_friend` (
`
friend_name
` varchar(255) DEFAULT NULL COMMENT '好友名称备注',
`
reject_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 '创建时间',
`
update_time
` timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP COMMENT '修改时间',
PRIMARY KEY (`
id
`) USING BTREE,
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment