From 4ce2be724e91bf9838baa1cc9562f698bf8e2aa9 Mon Sep 17 00:00:00 2001 From: puhui999 Date: Mon, 24 Jun 2024 15:45:05 +0800 Subject: [PATCH] =?UTF-8?q?MALL-KEFU:=20=E6=A0=B9=E6=8D=AE=20review=20?= =?UTF-8?q?=E5=AE=8C=E5=96=84=E5=AE=A2=E6=9C=8D=E7=9B=B8=E5=85=B3=E5=AE=9E?= =?UTF-8?q?=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../admin/kefu/KeFuMessageController.java | 5 ++- .../kefu/vo/message/KeFuMessageSendReqVO.java | 32 ++++--------------- .../app/kefu/AppKeFuMessageController.java | 3 +- .../vo/message/AppKeFuMessageSendReqVO.java | 4 --- .../mysql/kefu/KeFuConversationMapper.java | 14 +++----- .../kefu/KeFuConversationServiceImpl.java | 14 ++++---- .../service/kefu/KeFuMessageService.java | 4 ++- .../service/kefu/KeFuMessageServiceImpl.java | 19 +++++++---- 8 files changed, 40 insertions(+), 55 deletions(-) diff --git a/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/controller/admin/kefu/KeFuMessageController.java b/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/controller/admin/kefu/KeFuMessageController.java index 58de6c476..a5f543d79 100644 --- a/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/controller/admin/kefu/KeFuMessageController.java +++ b/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/controller/admin/kefu/KeFuMessageController.java @@ -1,5 +1,6 @@ package cn.iocoder.yudao.module.promotion.controller.admin.kefu; +import cn.iocoder.yudao.framework.common.enums.UserTypeEnum; import cn.iocoder.yudao.framework.common.pojo.CommonResult; import cn.iocoder.yudao.framework.common.pojo.PageResult; import cn.iocoder.yudao.framework.common.util.object.BeanUtils; @@ -18,6 +19,7 @@ import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.*; import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success; +import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId; @Tag(name = "管理后台 - 客服消息") @RestController @@ -32,6 +34,7 @@ public class KeFuMessageController { @Operation(summary = "发送客服消息") @PreAuthorize("@ss.hasPermission('promotion:kefu-message:send')") public CommonResult createKefuMessage(@Valid @RequestBody KeFuMessageSendReqVO sendReqVO) { + sendReqVO.setSenderId(getLoginUserId()).setSenderType(UserTypeEnum.ADMIN.getValue()); // 设置用户编号和类型 return success(messageService.sendKefuMessage(sendReqVO)); } @@ -40,7 +43,7 @@ public class KeFuMessageController { @Parameter(name = "conversationId", description = "会话编号", required = true) @PreAuthorize("@ss.hasPermission('promotion:kefu-message:update')") public CommonResult updateKefuMessageReadStatus(@RequestParam("conversationId") Long conversationId) { - messageService.updateKefuMessageReadStatus(conversationId); + messageService.updateKefuMessageReadStatus(conversationId, getLoginUserId(), UserTypeEnum.ADMIN.getValue()); return success(true); } diff --git a/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/controller/admin/kefu/vo/message/KeFuMessageSendReqVO.java b/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/controller/admin/kefu/vo/message/KeFuMessageSendReqVO.java index 0dca02410..d01b163c2 100644 --- a/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/controller/admin/kefu/vo/message/KeFuMessageSendReqVO.java +++ b/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/controller/admin/kefu/vo/message/KeFuMessageSendReqVO.java @@ -9,37 +9,19 @@ import lombok.Data; @Data public class KeFuMessageSendReqVO { - // TODO @puhui999:貌似字段多了;1)id 不用;2)senderId、senderType 不用;3)receiverId、receiverType 也不用;原因可以想下哈 - - @Schema(description = "编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "23202") - private Long id; - @Schema(description = "会话编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "12580") @NotNull(message = "会话编号不能为空") private Long conversationId; - @Schema(description = "发送人编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "24571") - @NotNull(message = "发送人编号不能为空") - private Long senderId; - - @Schema(description = "发送人类型", requiredMode = Schema.RequiredMode.REQUIRED, example = "1") - @NotNull(message = "发送人类型不能为空") - private Integer senderType; - - @Schema(description = "接收人编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "29124") - @NotNull(message = "接收人编号不能为空") - private Long receiverId; - - @Schema(description = "接收人类型", requiredMode = Schema.RequiredMode.REQUIRED, example = "2") - @NotNull(message = "接收人类型不能为空") - private Integer receiverType; - - @Schema(description = "消息类型", requiredMode = Schema.RequiredMode.REQUIRED, example = "1") - @NotNull(message = "消息类型不能为空") - private Integer contentType; - @Schema(description = "消息", requiredMode = Schema.RequiredMode.REQUIRED) @NotEmpty(message = "消息不能为空") private String content; + // ========== 后端设置的参数,前端无需传递 ========== + + @Schema(description = "发送人编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "24571", hidden = true) + private Long senderId; + @Schema(description = "发送人类型", requiredMode = Schema.RequiredMode.REQUIRED, example = "1", hidden = true) + private Integer senderType; + } \ No newline at end of file diff --git a/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/controller/app/kefu/AppKeFuMessageController.java b/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/controller/app/kefu/AppKeFuMessageController.java index 71eb8bdb1..a38f68686 100644 --- a/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/controller/app/kefu/AppKeFuMessageController.java +++ b/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/controller/app/kefu/AppKeFuMessageController.java @@ -43,8 +43,7 @@ public class AppKeFuMessageController { @Parameter(name = "conversationId", description = "会话编号", required = true) @PreAuthenticated public CommonResult updateKefuMessageReadStatus(@RequestParam("conversationId") Long conversationId) { - // TODO @puhui999:需要传递 userId;万一用户模拟一个 conversationId - kefuMessageService.updateKefuMessageReadStatus(conversationId); + kefuMessageService.updateKefuMessageReadStatus(conversationId, getLoginUserId(), UserTypeEnum.MEMBER.getValue()); return success(true); } diff --git a/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/controller/app/kefu/vo/message/AppKeFuMessageSendReqVO.java b/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/controller/app/kefu/vo/message/AppKeFuMessageSendReqVO.java index 432ed56b6..5795254f2 100644 --- a/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/controller/app/kefu/vo/message/AppKeFuMessageSendReqVO.java +++ b/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/controller/app/kefu/vo/message/AppKeFuMessageSendReqVO.java @@ -9,10 +9,6 @@ import lombok.Data; @Data public class AppKeFuMessageSendReqVO { - // TODO @puhui999:应该没有传递编号哈 - @Schema(description = "编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "23202") - private Long id; - @Schema(description = "消息类型", requiredMode = Schema.RequiredMode.REQUIRED, example = "1") @NotNull(message = "消息类型不能为空") private Integer contentType; diff --git a/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/dal/mysql/kefu/KeFuConversationMapper.java b/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/dal/mysql/kefu/KeFuConversationMapper.java index f5c4fac29..35a664e53 100644 --- a/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/dal/mysql/kefu/KeFuConversationMapper.java +++ b/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/dal/mysql/kefu/KeFuConversationMapper.java @@ -22,18 +22,14 @@ public interface KeFuConversationMapper extends BaseMapperX .orderByDesc(KeFuConversationDO::getCreateTime)); } - // TODO @puhui999:这个不用单独搞个方法哈。Service 直接 new 一个对象,然后调用 update 方法。 - default void updateAdminUnreadMessageCountWithZero(Long id) { - update(new LambdaUpdateWrapper() - .eq(KeFuConversationDO::getId, id) - .set(KeFuConversationDO::getAdminUnreadMessageCount, 0)); - } - - // TODO @puhui999:改成 updateAdminUnreadMessageCountIncrement 增加 - default void updateAdminUnreadMessageCount(Long id) { + default void updateAdminUnreadMessageCountIncrement(Long id) { update(new LambdaUpdateWrapper() .eq(KeFuConversationDO::getId, id) .setSql("admin_unread_message_count = admin_unread_message_count + 1")); } + default KeFuConversationDO selectByUserId(Long userId){ + return selectOne(KeFuConversationDO::getUserId, userId); + } + } \ No newline at end of file diff --git a/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/service/kefu/KeFuConversationServiceImpl.java b/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/service/kefu/KeFuConversationServiceImpl.java index fcd609b9d..7ad6427ed 100644 --- a/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/service/kefu/KeFuConversationServiceImpl.java +++ b/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/service/kefu/KeFuConversationServiceImpl.java @@ -56,20 +56,21 @@ public class KeFuConversationServiceImpl implements KeFuConversationService { // 2.1 更新管理员未读消息数 if (UserTypeEnum.MEMBER.getValue().equals(kefuMessage.getSenderType())) { - conversationMapper.updateAdminUnreadMessageCount(kefuMessage.getConversationId()); + conversationMapper.updateAdminUnreadMessageCountIncrement(kefuMessage.getConversationId()); } // 2.2 会员用户发送消息时,如果管理员删除过会话则进行恢复 - // TODO @puhui999:其实不用判断用户类型;只要be已删除,就恢复! - if (UserTypeEnum.MEMBER.getValue().equals(kefuMessage.getSenderType()) - && Boolean.TRUE.equals(conversation.getAdminDeleted())) { + if (Boolean.TRUE.equals(conversation.getAdminDeleted())) { updateConversationAdminDeleted(kefuMessage.getConversationId(), Boolean.FALSE); } } @Override public void updateAdminUnreadMessageCountWithZero(Long id) { + // 校验存在 validateKefuConversationExists(id); - conversationMapper.updateAdminUnreadMessageCountWithZero(id); + + // 管理员未读消息数归零 + conversationMapper.updateById(new KeFuConversationDO().setId(id).setAdminUnreadMessageCount(0)); } @Override @@ -107,8 +108,7 @@ public class KeFuConversationServiceImpl implements KeFuConversationService { @Override public KeFuConversationDO getConversationByUserId(Long userId) { - // TODO @puhui999:service 不写 dao 的逻辑哈 - return conversationMapper.selectOne(KeFuConversationDO::getUserId, userId); + return conversationMapper.selectByUserId(userId); } } \ No newline at end of file diff --git a/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/service/kefu/KeFuMessageService.java b/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/service/kefu/KeFuMessageService.java index 2563ada34..fae4566cb 100644 --- a/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/service/kefu/KeFuMessageService.java +++ b/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/service/kefu/KeFuMessageService.java @@ -35,8 +35,10 @@ public interface KeFuMessageService { * 【管理员】更新消息已读状态 * * @param conversationId 会话编号 + * @param userId 用户编号 + * @param userType 用户类型 */ - void updateKefuMessageReadStatus(Long conversationId); + void updateKefuMessageReadStatus(Long conversationId, Long userId, Integer userType); /** * 获得客服消息分页 diff --git a/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/service/kefu/KeFuMessageServiceImpl.java b/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/service/kefu/KeFuMessageServiceImpl.java index 22339e608..cf79c53d1 100644 --- a/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/service/kefu/KeFuMessageServiceImpl.java +++ b/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/service/kefu/KeFuMessageServiceImpl.java @@ -1,6 +1,7 @@ package cn.iocoder.yudao.module.promotion.service.kefu; import cn.hutool.core.collection.CollUtil; +import cn.hutool.core.util.ObjUtil; import cn.hutool.core.util.StrUtil; import cn.hutool.extra.spring.SpringUtil; import cn.iocoder.yudao.framework.common.enums.UserTypeEnum; @@ -22,10 +23,11 @@ import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.validation.annotation.Validated; -import java.util.Collections; import java.util.List; +import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.*; +import static cn.iocoder.yudao.module.promotion.enums.ErrorCodeConstants.KEFU_CONVERSATION_NOT_EXISTS; import static cn.iocoder.yudao.module.promotion.enums.WebSocketMessageTypeConstants.KEFU_MESSAGE_ADMIN_READ; import static cn.iocoder.yudao.module.promotion.enums.WebSocketMessageTypeConstants.KEFU_MESSAGE_TYPE; @@ -53,18 +55,19 @@ public class KeFuMessageServiceImpl implements KeFuMessageService { @Transactional(rollbackFor = Exception.class) public Long sendKefuMessage(KeFuMessageSendReqVO sendReqVO) { // 1.1 校验会话是否存在 - conversationService.validateKefuConversationExists(sendReqVO.getConversationId()); + KeFuConversationDO conversation = conversationService.validateKefuConversationExists(sendReqVO.getConversationId()); // 1.2 校验接收人是否存在 - validateReceiverExist(sendReqVO.getReceiverId(), sendReqVO.getReceiverType()); + validateReceiverExist(conversation.getUserId(), UserTypeEnum.MEMBER.getValue()); // 2.1 保存消息 KeFuMessageDO kefuMessage = BeanUtils.toBean(sendReqVO, KeFuMessageDO.class); + kefuMessage.setReceiverId(conversation.getUserId()).setReceiverType(UserTypeEnum.MEMBER.getValue()); // 设置接收人 keFuMessageMapper.insert(kefuMessage); // 2.2 更新会话消息冗余 conversationService.updateConversationLastMessage(kefuMessage); // 3. 发送消息 - getSelf().sendAsyncMessage(sendReqVO.getReceiverType(), sendReqVO.getReceiverId(), kefuMessage); + getSelf().sendAsyncMessage(UserTypeEnum.MEMBER.getValue(), conversation.getUserId(), kefuMessage); return kefuMessage.getId(); } @@ -86,9 +89,13 @@ public class KeFuMessageServiceImpl implements KeFuMessageService { @Override @Transactional(rollbackFor = Exception.class) - public void updateKefuMessageReadStatus(Long conversationId) { + public void updateKefuMessageReadStatus(Long conversationId, Long userId, Integer userType) { // 1.1 校验会话是否存在 - conversationService.validateKefuConversationExists(conversationId); + KeFuConversationDO conversation = conversationService.validateKefuConversationExists(conversationId); + // 1.2 如果是会员端处理已读,需要传递 userId;万一用户模拟一个 conversationId + if (UserTypeEnum.MEMBER.getValue().equals(userType) && ObjUtil.notEqual(conversation.getUserId(), userId)) { + throw exception(KEFU_CONVERSATION_NOT_EXISTS); + } // 1.2 查询会话所有的未读消息 (tips: 多个客服,一个人点了,就都点了) List messageList = keFuMessageMapper.selectListByConversationIdAndReadStatus(conversationId, Boolean.FALSE); // 1.3 情况一:没有未读消息