From 2027d8f220fa2287925fe5b0872f3699c1fa550e Mon Sep 17 00:00:00 2001 From: zhongzb <972627721@qq.com> Date: Sun, 1 Oct 2023 21:00:16 +0800 Subject: [PATCH] =?UTF-8?q?fix:=E4=BB=A3=E7=A0=81=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/mallchat.sql | 3 ++- .../chat/service/impl/RoomAppServiceImpl.java | 2 +- .../domain/vo/request/CursorPageBaseReq.java | 2 +- .../cache/AbstractRedisStringCache.java | 5 +++++ .../common/common/utils/CursorUtils.java | 11 +++++++--- .../user/controller/FriendController.java | 6 +++--- .../mallchat/common/user/dao/UserDao.java | 5 ++--- .../user/domain/dto/SummeryInfoDTO.java | 2 ++ .../common/user/domain/entity/UserFriend.java | 12 +++++++---- .../domain/vo/response/friend/FriendResp.java | 10 +++++++--- .../common/user/service/FriendService.java | 4 ++-- .../user/service/adapter/FriendAdapter.java | 18 +++++++++++++++++ .../common/user/service/cache/UserCache.java | 6 +++++- .../user/service/impl/FriendServiceImpl.java | 20 +++++++++---------- .../user/service/impl/UserServiceImpl.java | 6 +++--- 15 files changed, 77 insertions(+), 35 deletions(-) diff --git a/docs/mallchat.sql b/docs/mallchat.sql index 6bea229..b6a9773 100644 --- a/docs/mallchat.sql +++ b/docs/mallchat.sql @@ -218,7 +218,8 @@ CREATE TABLE `user_apply` ( `create_time` datetime(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3) COMMENT '创建时间', `update_time` datetime(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3) COMMENT '修改时间', PRIMARY KEY (`id`) USING BTREE, - KEY `idx_target_id_uid_status` (`target_id`,`uid`,`status`) USING BTREE, + KEY `idx_uid_target_id` (`uid`,`target_id`) USING BTREE, + KEY `idx_target_id_read_status` (`target_id`,`read_status`) USING BTREE, KEY `idx_target_id` (`target_id`) USING BTREE, KEY `idx_create_time` (`create_time`) USING BTREE, KEY `idx_update_time` (`update_time`) USING BTREE diff --git a/mallchat-chat-server/src/main/java/com/abin/mallchat/common/chat/service/impl/RoomAppServiceImpl.java b/mallchat-chat-server/src/main/java/com/abin/mallchat/common/chat/service/impl/RoomAppServiceImpl.java index 9bd4d9a..48f22ae 100644 --- a/mallchat-chat-server/src/main/java/com/abin/mallchat/common/chat/service/impl/RoomAppServiceImpl.java +++ b/mallchat-chat-server/src/main/java/com/abin/mallchat/common/chat/service/impl/RoomAppServiceImpl.java @@ -162,7 +162,7 @@ public class RoomAppServiceImpl implements RoomAppService { public CursorPageBaseResp getMemberPage(MemberReq request) { Room room = roomCache.get(request.getRoomId()); AssertUtil.isNotEmpty(room, "房间号有误"); - List memberUidList = null; + List memberUidList; if (isHotGroup(room)) {//全员群展示所有用户 memberUidList = null; } else {//只展示房间内的群成员 diff --git a/mallchat-chat-server/src/main/java/com/abin/mallchat/common/common/domain/vo/request/CursorPageBaseReq.java b/mallchat-chat-server/src/main/java/com/abin/mallchat/common/common/domain/vo/request/CursorPageBaseReq.java index 248d7d9..e27a44a 100644 --- a/mallchat-chat-server/src/main/java/com/abin/mallchat/common/common/domain/vo/request/CursorPageBaseReq.java +++ b/mallchat-chat-server/src/main/java/com/abin/mallchat/common/common/domain/vo/request/CursorPageBaseReq.java @@ -31,7 +31,7 @@ public class CursorPageBaseReq { private String cursor; public Page plusPage() { - return new Page(1, this.pageSize); + return new Page(1, this.pageSize, false); } @JsonIgnore diff --git a/mallchat-chat-server/src/main/java/com/abin/mallchat/common/common/service/cache/AbstractRedisStringCache.java b/mallchat-chat-server/src/main/java/com/abin/mallchat/common/common/service/cache/AbstractRedisStringCache.java index f1b80df..488d1a9 100644 --- a/mallchat-chat-server/src/main/java/com/abin/mallchat/common/common/service/cache/AbstractRedisStringCache.java +++ b/mallchat-chat-server/src/main/java/com/abin/mallchat/common/common/service/cache/AbstractRedisStringCache.java @@ -38,9 +38,13 @@ public abstract class AbstractRedisStringCache implements BatchCache(); } + //去重 req = req.stream().distinct().collect(Collectors.toList()); + //组装key List keys = req.stream().map(this::getKey).collect(Collectors.toList()); + //批量get List valueList = RedisUtils.mget(keys, outClass); + //差集计算 List loadReqs = new ArrayList<>(); for (int i = 0; i < valueList.size(); i++) { if (Objects.isNull(valueList.get(i))) { @@ -50,6 +54,7 @@ public abstract class AbstractRedisStringCache implements BatchCache load = new HashMap<>(); //不足的重新加载进redis if (CollectionUtil.isNotEmpty(loadReqs)) { + //批量load load = load(loadReqs); Map loadMap = load.entrySet().stream() .map(a -> Pair.of(getKey(a.getKey()), a.getValue())) diff --git a/mallchat-chat-server/src/main/java/com/abin/mallchat/common/common/utils/CursorUtils.java b/mallchat-chat-server/src/main/java/com/abin/mallchat/common/common/utils/CursorUtils.java index 6984a5d..0b2e3e6 100644 --- a/mallchat-chat-server/src/main/java/com/abin/mallchat/common/common/utils/CursorUtils.java +++ b/mallchat-chat-server/src/main/java/com/abin/mallchat/common/common/utils/CursorUtils.java @@ -47,20 +47,25 @@ public class CursorUtils { } public static CursorPageBaseResp getCursorPageByMysql(IService mapper, CursorPageBaseReq request, Consumer> initWrapper, SFunction cursorColumn) { + //游标字段类型 Class cursorType = LambdaUtils.getReturnType(cursorColumn); LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>(); + //额外条件 initWrapper.accept(wrapper); + //游标条件 if (StrUtil.isNotBlank(request.getCursor())) { wrapper.lt(cursorColumn, parseCursor(request.getCursor(), cursorType)); } + //游标方向 wrapper.orderByDesc(cursorColumn); - Page pageReq = request.plusPage(); - pageReq.setSearchCount(false); - Page page = mapper.page(pageReq, wrapper); + + Page page = mapper.page(request.plusPage(), wrapper); + //取出游标 String cursor = Optional.ofNullable(CollectionUtil.getLast(page.getRecords())) .map(cursorColumn) .map(CursorUtils::toCursor) .orElse(null); + //判断是否最后一页 Boolean isLast = page.getRecords().size() != request.getPageSize(); return new CursorPageBaseResp<>(cursor, isLast, page.getRecords()); } diff --git a/mallchat-chat-server/src/main/java/com/abin/mallchat/common/user/controller/FriendController.java b/mallchat-chat-server/src/main/java/com/abin/mallchat/common/user/controller/FriendController.java index 5c0ef63..01d3164 100644 --- a/mallchat-chat-server/src/main/java/com/abin/mallchat/common/user/controller/FriendController.java +++ b/mallchat-chat-server/src/main/java/com/abin/mallchat/common/user/controller/FriendController.java @@ -13,8 +13,8 @@ import com.abin.mallchat.common.user.domain.vo.request.friend.FriendCheckReq; import com.abin.mallchat.common.user.domain.vo.request.friend.FriendDeleteReq; import com.abin.mallchat.common.user.domain.vo.response.friend.FriendApplyResp; import com.abin.mallchat.common.user.domain.vo.response.friend.FriendCheckResp; +import com.abin.mallchat.common.user.domain.vo.response.friend.FriendResp; import com.abin.mallchat.common.user.domain.vo.response.friend.FriendUnreadResp; -import com.abin.mallchat.common.user.domain.vo.response.ws.ChatMemberResp; import com.abin.mallchat.common.user.service.FriendService; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; @@ -78,7 +78,7 @@ public class FriendController { } @PutMapping("/apply") - @ApiOperation("审批申请") + @ApiOperation("审批同意") public ApiResult applyApprove(@Valid @RequestBody FriendApproveReq request) { friendService.applyApprove(RequestHolder.get().getUid(), request); return ApiResult.success(); @@ -86,7 +86,7 @@ public class FriendController { @GetMapping("/page") @ApiOperation("联系人列表") - public ApiResult> friendList(@Valid CursorPageBaseReq request) { + public ApiResult> friendList(@Valid CursorPageBaseReq request) { Long uid = RequestHolder.get().getUid(); return ApiResult.success(friendService.friendList(uid, request)); } diff --git a/mallchat-chat-server/src/main/java/com/abin/mallchat/common/user/dao/UserDao.java b/mallchat-chat-server/src/main/java/com/abin/mallchat/common/user/dao/UserDao.java index ce9d558..cbf620a 100644 --- a/mallchat-chat-server/src/main/java/com/abin/mallchat/common/user/dao/UserDao.java +++ b/mallchat-chat-server/src/main/java/com/abin/mallchat/common/user/dao/UserDao.java @@ -60,11 +60,10 @@ public class UserDao extends ServiceImpl { } - public List getUserList(List uids) { + public List getFriendList(List uids) { return lambdaQuery() .in(User::getId, uids) - .orderByDesc(User::getId) - .select(User::getId, User::getActiveStatus, User::getLastOptTime) + .select(User::getId, User::getActiveStatus, User::getName, User::getAvatar) .list(); } diff --git a/mallchat-chat-server/src/main/java/com/abin/mallchat/common/user/domain/dto/SummeryInfoDTO.java b/mallchat-chat-server/src/main/java/com/abin/mallchat/common/user/domain/dto/SummeryInfoDTO.java index 60cb132..83273ce 100644 --- a/mallchat-chat-server/src/main/java/com/abin/mallchat/common/user/domain/dto/SummeryInfoDTO.java +++ b/mallchat-chat-server/src/main/java/com/abin/mallchat/common/user/domain/dto/SummeryInfoDTO.java @@ -1,5 +1,6 @@ package com.abin.mallchat.common.user.domain.dto; +import com.fasterxml.jackson.annotation.JsonInclude; import io.swagger.annotations.ApiModelProperty; import lombok.AllArgsConstructor; import lombok.Builder; @@ -18,6 +19,7 @@ import java.util.List; @Builder @AllArgsConstructor @NoArgsConstructor +@JsonInclude(JsonInclude.Include.NON_NULL) public class SummeryInfoDTO { @ApiModelProperty(value = "用户id") private Long uid; diff --git a/mallchat-chat-server/src/main/java/com/abin/mallchat/common/user/domain/entity/UserFriend.java b/mallchat-chat-server/src/main/java/com/abin/mallchat/common/user/domain/entity/UserFriend.java index a587ca3..67b5a70 100644 --- a/mallchat-chat-server/src/main/java/com/abin/mallchat/common/user/domain/entity/UserFriend.java +++ b/mallchat-chat-server/src/main/java/com/abin/mallchat/common/user/domain/entity/UserFriend.java @@ -1,9 +1,6 @@ package com.abin.mallchat.common.user.domain.entity; -import com.baomidou.mybatisplus.annotation.IdType; -import com.baomidou.mybatisplus.annotation.TableField; -import com.baomidou.mybatisplus.annotation.TableId; -import com.baomidou.mybatisplus.annotation.TableName; +import com.baomidou.mybatisplus.annotation.*; import lombok.Data; import lombok.EqualsAndHashCode; @@ -43,6 +40,13 @@ public class UserFriend implements Serializable { @TableField("friend_uid") private Long friendUid; + /** + * 逻辑删除(0-正常,1-删除) + */ + @TableField("delete_status") + @TableLogic(value = "0", delval = "1") + private Integer deleteStatus; + /** * 创建时间 */ diff --git a/mallchat-chat-server/src/main/java/com/abin/mallchat/common/user/domain/vo/response/friend/FriendResp.java b/mallchat-chat-server/src/main/java/com/abin/mallchat/common/user/domain/vo/response/friend/FriendResp.java index 3423fc5..ee60ed0 100644 --- a/mallchat-chat-server/src/main/java/com/abin/mallchat/common/user/domain/vo/response/friend/FriendResp.java +++ b/mallchat-chat-server/src/main/java/com/abin/mallchat/common/user/domain/vo/response/friend/FriendResp.java @@ -18,7 +18,11 @@ import lombok.NoArgsConstructor; @NoArgsConstructor public class FriendResp { - @ApiModelProperty("申请列表的未读数") - private Integer unReadCount; - + @ApiModelProperty("好友uid") + private Long uid; + /** + * @see com.abin.mallchat.common.user.domain.enums.ChatActiveStatusEnum + */ + @ApiModelProperty("在线状态 1在线 2离线") + private Integer activeStatus; } diff --git a/mallchat-chat-server/src/main/java/com/abin/mallchat/common/user/service/FriendService.java b/mallchat-chat-server/src/main/java/com/abin/mallchat/common/user/service/FriendService.java index c275cd6..800690f 100644 --- a/mallchat-chat-server/src/main/java/com/abin/mallchat/common/user/service/FriendService.java +++ b/mallchat-chat-server/src/main/java/com/abin/mallchat/common/user/service/FriendService.java @@ -9,8 +9,8 @@ import com.abin.mallchat.common.user.domain.vo.request.friend.FriendApproveReq; import com.abin.mallchat.common.user.domain.vo.request.friend.FriendCheckReq; import com.abin.mallchat.common.user.domain.vo.response.friend.FriendApplyResp; import com.abin.mallchat.common.user.domain.vo.response.friend.FriendCheckResp; +import com.abin.mallchat.common.user.domain.vo.response.friend.FriendResp; import com.abin.mallchat.common.user.domain.vo.response.friend.FriendUnreadResp; -import com.abin.mallchat.common.user.domain.vo.response.ws.ChatMemberResp; /** * @author : limeng @@ -69,5 +69,5 @@ public interface FriendService { */ void deleteFriend(Long uid, Long friendUid); - CursorPageBaseResp friendList(Long uid, CursorPageBaseReq request); + CursorPageBaseResp friendList(Long uid, CursorPageBaseReq request); } diff --git a/mallchat-chat-server/src/main/java/com/abin/mallchat/common/user/service/adapter/FriendAdapter.java b/mallchat-chat-server/src/main/java/com/abin/mallchat/common/user/service/adapter/FriendAdapter.java index bd99eb5..541768f 100644 --- a/mallchat-chat-server/src/main/java/com/abin/mallchat/common/user/service/adapter/FriendAdapter.java +++ b/mallchat-chat-server/src/main/java/com/abin/mallchat/common/user/service/adapter/FriendAdapter.java @@ -1,10 +1,15 @@ package com.abin.mallchat.common.user.service.adapter; +import com.abin.mallchat.common.user.domain.entity.User; import com.abin.mallchat.common.user.domain.entity.UserApply; +import com.abin.mallchat.common.user.domain.entity.UserFriend; import com.abin.mallchat.common.user.domain.vo.request.friend.FriendApplyReq; import com.abin.mallchat.common.user.domain.vo.response.friend.FriendApplyResp; +import com.abin.mallchat.common.user.domain.vo.response.friend.FriendResp; import java.util.List; +import java.util.Map; +import java.util.Objects; import java.util.stream.Collectors; import static com.abin.mallchat.common.user.domain.enums.ApplyReadStatusEnum.UNREAD; @@ -41,4 +46,17 @@ public class FriendAdapter { return friendApplyResp; }).collect(Collectors.toList()); } + + public static List buildFriend(List list, List userList) { + Map userMap = userList.stream().collect(Collectors.toMap(User::getId, user -> user)); + return list.stream().map(userFriend -> { + FriendResp resp = new FriendResp(); + resp.setUid(userFriend.getFriendUid()); + User user = userMap.get(userFriend.getFriendUid()); + if (Objects.nonNull(user)) { + resp.setActiveStatus(user.getActiveStatus()); + } + return resp; + }).collect(Collectors.toList()); + } } diff --git a/mallchat-chat-server/src/main/java/com/abin/mallchat/common/user/service/cache/UserCache.java b/mallchat-chat-server/src/main/java/com/abin/mallchat/common/user/service/cache/UserCache.java index 0705d17..dd517ab 100644 --- a/mallchat-chat-server/src/main/java/com/abin/mallchat/common/user/service/cache/UserCache.java +++ b/mallchat-chat-server/src/main/java/com/abin/mallchat/common/user/service/cache/UserCache.java @@ -123,15 +123,19 @@ public class UserCache { * 获取用户信息,盘路缓存模式 */ public Map getUserInfoBatch(Set uids) { + //批量组装key List keys = uids.stream().map(a -> RedisKey.getKey(RedisKey.USER_INFO_STRING, a)).collect(Collectors.toList()); + //批量get List mget = RedisUtils.mget(keys, User.class); Map map = mget.stream().filter(Objects::nonNull).collect(Collectors.toMap(User::getId, Function.identity())); - //还需要load更新的uid + //发现差集——还需要load更新的uid List needLoadUidList = uids.stream().filter(a -> !map.containsKey(a)).collect(Collectors.toList()); if (CollUtil.isNotEmpty(needLoadUidList)) { + //批量load List needLoadUserList = userDao.listByIds(needLoadUidList); Map redisMap = needLoadUserList.stream().collect(Collectors.toMap(a -> RedisKey.getKey(RedisKey.USER_INFO_STRING, a.getId()), Function.identity())); RedisUtils.mset(redisMap, 5 * 60); + //加载回redis map.putAll(needLoadUserList.stream().collect(Collectors.toMap(User::getId, Function.identity()))); } return map; diff --git a/mallchat-chat-server/src/main/java/com/abin/mallchat/common/user/service/impl/FriendServiceImpl.java b/mallchat-chat-server/src/main/java/com/abin/mallchat/common/user/service/impl/FriendServiceImpl.java index 011e209..e59ea5d 100644 --- a/mallchat-chat-server/src/main/java/com/abin/mallchat/common/user/service/impl/FriendServiceImpl.java +++ b/mallchat-chat-server/src/main/java/com/abin/mallchat/common/user/service/impl/FriendServiceImpl.java @@ -6,7 +6,6 @@ import com.abin.mallchat.common.chat.domain.entity.RoomFriend; import com.abin.mallchat.common.chat.service.ChatService; import com.abin.mallchat.common.chat.service.ContactService; import com.abin.mallchat.common.chat.service.RoomService; -import com.abin.mallchat.common.chat.service.adapter.MemberAdapter; import com.abin.mallchat.common.chat.service.adapter.MessageAdapter; import com.abin.mallchat.common.common.annotation.RedissonLock; import com.abin.mallchat.common.common.domain.vo.request.CursorPageBaseReq; @@ -26,8 +25,8 @@ import com.abin.mallchat.common.user.domain.vo.request.friend.FriendApproveReq; import com.abin.mallchat.common.user.domain.vo.request.friend.FriendCheckReq; import com.abin.mallchat.common.user.domain.vo.response.friend.FriendApplyResp; import com.abin.mallchat.common.user.domain.vo.response.friend.FriendCheckResp; +import com.abin.mallchat.common.user.domain.vo.response.friend.FriendResp; import com.abin.mallchat.common.user.domain.vo.response.friend.FriendUnreadResp; -import com.abin.mallchat.common.user.domain.vo.response.ws.ChatMemberResp; import com.abin.mallchat.common.user.service.FriendService; import com.abin.mallchat.common.user.service.adapter.FriendAdapter; import com.baomidou.mybatisplus.core.metadata.IPage; @@ -138,12 +137,16 @@ public class FriendServiceImpl implements FriendService { return PageBaseResp.empty(); } //将这些申请列表设为已读 + readApples(uid, userApplyIPage); + //返回消息 + return PageBaseResp.init(userApplyIPage, FriendAdapter.buildFriendApplyList(userApplyIPage.getRecords())); + } + + private void readApples(Long uid, IPage userApplyIPage) { List applyIds = userApplyIPage.getRecords() .stream().map(UserApply::getId) .collect(Collectors.toList()); userApplyDao.readApples(uid, applyIds); - //返回消息 - return PageBaseResp.init(userApplyIPage, FriendAdapter.buildFriendApplyList(userApplyIPage.getRecords())); } /** @@ -171,9 +174,6 @@ public class FriendServiceImpl implements FriendService { createFriend(uid, userApply.getUid()); //创建一个聊天房间 RoomFriend roomFriend = roomService.createFriendRoom(Arrays.asList(uid, userApply.getUid())); -// //创建双方的会话 -// contactService.createContact(uid, roomFriend.getRoomId()); -// contactService.createContact(userApply.getUid(), roomFriend.getRoomId()); //发送一条同意消息。。我们已经是好友了,开始聊天吧 chatService.sendMsg(MessageAdapter.buildAgreeMsg(roomFriend.getRoomId()), uid); } @@ -199,7 +199,7 @@ public class FriendServiceImpl implements FriendService { } @Override - public CursorPageBaseResp friendList(Long uid, CursorPageBaseReq request) { + public CursorPageBaseResp friendList(Long uid, CursorPageBaseReq request) { CursorPageBaseResp friendPage = userFriendDao.getFriendPage(uid, request); if (CollectionUtils.isEmpty(friendPage.getList())) { return CursorPageBaseResp.empty(); @@ -207,8 +207,8 @@ public class FriendServiceImpl implements FriendService { List friendUids = friendPage.getList() .stream().map(UserFriend::getFriendUid) .collect(Collectors.toList()); - List userList = userDao.getUserList(friendUids); - return CursorPageBaseResp.init(friendPage, MemberAdapter.buildMember(friendPage.getList(), userList)); + List userList = userDao.getFriendList(friendUids); + return CursorPageBaseResp.init(friendPage, FriendAdapter.buildFriend(friendPage.getList(), userList)); } private void createFriend(Long uid, Long targetUid) { diff --git a/mallchat-chat-server/src/main/java/com/abin/mallchat/common/user/service/impl/UserServiceImpl.java b/mallchat-chat-server/src/main/java/com/abin/mallchat/common/user/service/impl/UserServiceImpl.java index 66d84db..a6b9d6c 100644 --- a/mallchat-chat-server/src/main/java/com/abin/mallchat/common/user/service/impl/UserServiceImpl.java +++ b/mallchat-chat-server/src/main/java/com/abin/mallchat/common/user/service/impl/UserServiceImpl.java @@ -167,16 +167,16 @@ public class UserServiceImpl implements UserService { } private List getNeedSyncUidList(List reqList) { - List result = new ArrayList<>(); + List needSyncUidList = new ArrayList<>(); List userModifyTime = userCache.getUserModifyTime(reqList.stream().map(SummeryInfoReq.infoReq::getUid).collect(Collectors.toList())); for (int i = 0; i < reqList.size(); i++) { SummeryInfoReq.infoReq infoReq = reqList.get(i); Long modifyTime = userModifyTime.get(i); if (Objects.isNull(infoReq.getLastModifyTime()) || (Objects.nonNull(modifyTime) && modifyTime > infoReq.getLastModifyTime())) { - result.add(infoReq.getUid()); + needSyncUidList.add(infoReq.getUid()); } } - return result; + return needSyncUidList; } public void blackIp(String ip) {