Commit 4ce2be724e91bf9838baa1cc9562f698bf8e2aa9

Authored by puhui999
1 parent 866b3853

MALL-KEFU: 根据 review 完善客服相关实现

yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/controller/admin/kefu/KeFuMessageController.java
1 1 package cn.iocoder.yudao.module.promotion.controller.admin.kefu;
2 2  
  3 +import cn.iocoder.yudao.framework.common.enums.UserTypeEnum;
3 4 import cn.iocoder.yudao.framework.common.pojo.CommonResult;
4 5 import cn.iocoder.yudao.framework.common.pojo.PageResult;
5 6 import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
... ... @@ -18,6 +19,7 @@ import org.springframework.validation.annotation.Validated;
18 19 import org.springframework.web.bind.annotation.*;
19 20  
20 21 import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
  22 +import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;
21 23  
22 24 @Tag(name = "管理后台 - 客服消息")
23 25 @RestController
... ... @@ -32,6 +34,7 @@ public class KeFuMessageController {
32 34 @Operation(summary = "发送客服消息")
33 35 @PreAuthorize("@ss.hasPermission('promotion:kefu-message:send')")
34 36 public CommonResult<Long> createKefuMessage(@Valid @RequestBody KeFuMessageSendReqVO sendReqVO) {
  37 + sendReqVO.setSenderId(getLoginUserId()).setSenderType(UserTypeEnum.ADMIN.getValue()); // 设置用户编号和类型
35 38 return success(messageService.sendKefuMessage(sendReqVO));
36 39 }
37 40  
... ... @@ -40,7 +43,7 @@ public class KeFuMessageController {
40 43 @Parameter(name = "conversationId", description = "会话编号", required = true)
41 44 @PreAuthorize("@ss.hasPermission('promotion:kefu-message:update')")
42 45 public CommonResult<Boolean> updateKefuMessageReadStatus(@RequestParam("conversationId") Long conversationId) {
43   - messageService.updateKefuMessageReadStatus(conversationId);
  46 + messageService.updateKefuMessageReadStatus(conversationId, getLoginUserId(), UserTypeEnum.ADMIN.getValue());
44 47 return success(true);
45 48 }
46 49  
... ...
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;
9 9 @Data
10 10 public class KeFuMessageSendReqVO {
11 11  
12   - // TODO @puhui999:貌似字段多了;1)id 不用;2)senderId、senderType 不用;3)receiverId、receiverType 也不用;原因可以想下哈
13   -
14   - @Schema(description = "编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "23202")
15   - private Long id;
16   -
17 12 @Schema(description = "会话编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "12580")
18 13 @NotNull(message = "会话编号不能为空")
19 14 private Long conversationId;
20 15  
21   - @Schema(description = "发送人编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "24571")
22   - @NotNull(message = "发送人编号不能为空")
23   - private Long senderId;
24   -
25   - @Schema(description = "发送人类型", requiredMode = Schema.RequiredMode.REQUIRED, example = "1")
26   - @NotNull(message = "发送人类型不能为空")
27   - private Integer senderType;
28   -
29   - @Schema(description = "接收人编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "29124")
30   - @NotNull(message = "接收人编号不能为空")
31   - private Long receiverId;
32   -
33   - @Schema(description = "接收人类型", requiredMode = Schema.RequiredMode.REQUIRED, example = "2")
34   - @NotNull(message = "接收人类型不能为空")
35   - private Integer receiverType;
36   -
37   - @Schema(description = "消息类型", requiredMode = Schema.RequiredMode.REQUIRED, example = "1")
38   - @NotNull(message = "消息类型不能为空")
39   - private Integer contentType;
40   -
41 16 @Schema(description = "消息", requiredMode = Schema.RequiredMode.REQUIRED)
42 17 @NotEmpty(message = "消息不能为空")
43 18 private String content;
44 19  
  20 + // ========== 后端设置的参数,前端无需传递 ==========
  21 +
  22 + @Schema(description = "发送人编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "24571", hidden = true)
  23 + private Long senderId;
  24 + @Schema(description = "发送人类型", requiredMode = Schema.RequiredMode.REQUIRED, example = "1", hidden = true)
  25 + private Integer senderType;
  26 +
45 27 }
46 28 \ No newline at end of file
... ...
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 {
43 43 @Parameter(name = "conversationId", description = "会话编号", required = true)
44 44 @PreAuthenticated
45 45 public CommonResult<Boolean> updateKefuMessageReadStatus(@RequestParam("conversationId") Long conversationId) {
46   - // TODO @puhui999:需要传递 userId;万一用户模拟一个 conversationId
47   - kefuMessageService.updateKefuMessageReadStatus(conversationId);
  46 + kefuMessageService.updateKefuMessageReadStatus(conversationId, getLoginUserId(), UserTypeEnum.MEMBER.getValue());
48 47 return success(true);
49 48 }
50 49  
... ...
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;
9 9 @Data
10 10 public class AppKeFuMessageSendReqVO {
11 11  
12   - // TODO @puhui999:应该没有传递编号哈
13   - @Schema(description = "编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "23202")
14   - private Long id;
15   -
16 12 @Schema(description = "消息类型", requiredMode = Schema.RequiredMode.REQUIRED, example = "1")
17 13 @NotNull(message = "消息类型不能为空")
18 14 private Integer contentType;
... ...
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&lt;KeFuConversationDO&gt;
22 22 .orderByDesc(KeFuConversationDO::getCreateTime));
23 23 }
24 24  
25   - // TODO @puhui999:这个不用单独搞个方法哈。Service 直接 new 一个对象,然后调用 update 方法。
26   - default void updateAdminUnreadMessageCountWithZero(Long id) {
  25 + default void updateAdminUnreadMessageCountIncrement(Long id) {
27 26 update(new LambdaUpdateWrapper<KeFuConversationDO>()
28 27 .eq(KeFuConversationDO::getId, id)
29   - .set(KeFuConversationDO::getAdminUnreadMessageCount, 0));
  28 + .setSql("admin_unread_message_count = admin_unread_message_count + 1"));
30 29 }
31 30  
32   - // TODO @puhui999:改成 updateAdminUnreadMessageCountIncrement 增加
33   - default void updateAdminUnreadMessageCount(Long id) {
34   - update(new LambdaUpdateWrapper<KeFuConversationDO>()
35   - .eq(KeFuConversationDO::getId, id)
36   - .setSql("admin_unread_message_count = admin_unread_message_count + 1"));
  31 + default KeFuConversationDO selectByUserId(Long userId){
  32 + return selectOne(KeFuConversationDO::getUserId, userId);
37 33 }
38 34  
39 35 }
40 36 \ No newline at end of file
... ...
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 {
56 56  
57 57 // 2.1 更新管理员未读消息数
58 58 if (UserTypeEnum.MEMBER.getValue().equals(kefuMessage.getSenderType())) {
59   - conversationMapper.updateAdminUnreadMessageCount(kefuMessage.getConversationId());
  59 + conversationMapper.updateAdminUnreadMessageCountIncrement(kefuMessage.getConversationId());
60 60 }
61 61 // 2.2 会员用户发送消息时,如果管理员删除过会话则进行恢复
62   - // TODO @puhui999:其实不用判断用户类型;只要be已删除,就恢复!
63   - if (UserTypeEnum.MEMBER.getValue().equals(kefuMessage.getSenderType())
64   - && Boolean.TRUE.equals(conversation.getAdminDeleted())) {
  62 + if (Boolean.TRUE.equals(conversation.getAdminDeleted())) {
65 63 updateConversationAdminDeleted(kefuMessage.getConversationId(), Boolean.FALSE);
66 64 }
67 65 }
68 66  
69 67 @Override
70 68 public void updateAdminUnreadMessageCountWithZero(Long id) {
  69 + // 校验存在
71 70 validateKefuConversationExists(id);
72   - conversationMapper.updateAdminUnreadMessageCountWithZero(id);
  71 +
  72 + // 管理员未读消息数归零
  73 + conversationMapper.updateById(new KeFuConversationDO().setId(id).setAdminUnreadMessageCount(0));
73 74 }
74 75  
75 76 @Override
... ... @@ -107,8 +108,7 @@ public class KeFuConversationServiceImpl implements KeFuConversationService {
107 108  
108 109 @Override
109 110 public KeFuConversationDO getConversationByUserId(Long userId) {
110   - // TODO @puhui999:service 不写 dao 的逻辑哈
111   - return conversationMapper.selectOne(KeFuConversationDO::getUserId, userId);
  111 + return conversationMapper.selectByUserId(userId);
112 112 }
113 113  
114 114 }
115 115 \ No newline at end of file
... ...
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 {
35 35 * 【管理员】更新消息已读状态
36 36 *
37 37 * @param conversationId 会话编号
  38 + * @param userId 用户编号
  39 + * @param userType 用户类型
38 40 */
39   - void updateKefuMessageReadStatus(Long conversationId);
  41 + void updateKefuMessageReadStatus(Long conversationId, Long userId, Integer userType);
40 42  
41 43 /**
42 44 * 获得客服消息分页
... ...
yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/service/kefu/KeFuMessageServiceImpl.java
1 1 package cn.iocoder.yudao.module.promotion.service.kefu;
2 2  
3 3 import cn.hutool.core.collection.CollUtil;
  4 +import cn.hutool.core.util.ObjUtil;
4 5 import cn.hutool.core.util.StrUtil;
5 6 import cn.hutool.extra.spring.SpringUtil;
6 7 import cn.iocoder.yudao.framework.common.enums.UserTypeEnum;
... ... @@ -22,10 +23,11 @@ import org.springframework.stereotype.Service;
22 23 import org.springframework.transaction.annotation.Transactional;
23 24 import org.springframework.validation.annotation.Validated;
24 25  
25   -import java.util.Collections;
26 26 import java.util.List;
27 27  
  28 +import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
28 29 import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.*;
  30 +import static cn.iocoder.yudao.module.promotion.enums.ErrorCodeConstants.KEFU_CONVERSATION_NOT_EXISTS;
29 31 import static cn.iocoder.yudao.module.promotion.enums.WebSocketMessageTypeConstants.KEFU_MESSAGE_ADMIN_READ;
30 32 import static cn.iocoder.yudao.module.promotion.enums.WebSocketMessageTypeConstants.KEFU_MESSAGE_TYPE;
31 33  
... ... @@ -53,18 +55,19 @@ public class KeFuMessageServiceImpl implements KeFuMessageService {
53 55 @Transactional(rollbackFor = Exception.class)
54 56 public Long sendKefuMessage(KeFuMessageSendReqVO sendReqVO) {
55 57 // 1.1 校验会话是否存在
56   - conversationService.validateKefuConversationExists(sendReqVO.getConversationId());
  58 + KeFuConversationDO conversation = conversationService.validateKefuConversationExists(sendReqVO.getConversationId());
57 59 // 1.2 校验接收人是否存在
58   - validateReceiverExist(sendReqVO.getReceiverId(), sendReqVO.getReceiverType());
  60 + validateReceiverExist(conversation.getUserId(), UserTypeEnum.MEMBER.getValue());
59 61  
60 62 // 2.1 保存消息
61 63 KeFuMessageDO kefuMessage = BeanUtils.toBean(sendReqVO, KeFuMessageDO.class);
  64 + kefuMessage.setReceiverId(conversation.getUserId()).setReceiverType(UserTypeEnum.MEMBER.getValue()); // 设置接收人
62 65 keFuMessageMapper.insert(kefuMessage);
63 66 // 2.2 更新会话消息冗余
64 67 conversationService.updateConversationLastMessage(kefuMessage);
65 68  
66 69 // 3. 发送消息
67   - getSelf().sendAsyncMessage(sendReqVO.getReceiverType(), sendReqVO.getReceiverId(), kefuMessage);
  70 + getSelf().sendAsyncMessage(UserTypeEnum.MEMBER.getValue(), conversation.getUserId(), kefuMessage);
68 71 return kefuMessage.getId();
69 72 }
70 73  
... ... @@ -86,9 +89,13 @@ public class KeFuMessageServiceImpl implements KeFuMessageService {
86 89  
87 90 @Override
88 91 @Transactional(rollbackFor = Exception.class)
89   - public void updateKefuMessageReadStatus(Long conversationId) {
  92 + public void updateKefuMessageReadStatus(Long conversationId, Long userId, Integer userType) {
90 93 // 1.1 校验会话是否存在
91   - conversationService.validateKefuConversationExists(conversationId);
  94 + KeFuConversationDO conversation = conversationService.validateKefuConversationExists(conversationId);
  95 + // 1.2 如果是会员端处理已读,需要传递 userId;万一用户模拟一个 conversationId
  96 + if (UserTypeEnum.MEMBER.getValue().equals(userType) && ObjUtil.notEqual(conversation.getUserId(), userId)) {
  97 + throw exception(KEFU_CONVERSATION_NOT_EXISTS);
  98 + }
92 99 // 1.2 查询会话所有的未读消息 (tips: 多个客服,一个人点了,就都点了)
93 100 List<KeFuMessageDO> messageList = keFuMessageMapper.selectListByConversationIdAndReadStatus(conversationId, Boolean.FALSE);
94 101 // 1.3 情况一:没有未读消息
... ...