MALL-KEFU: 根据代码评审完善相关方法

This commit is contained in:
puhui999 2024-06-17 10:17:46 +08:00
parent d89a19a92b
commit 285a431616
19 changed files with 144 additions and 211 deletions

View File

@ -0,0 +1,15 @@
package cn.iocoder.yudao.module.promotion.enums;
/**
* websocket 消息类型枚举类
*
* @author HUIHUI
*/
public interface WebSocketMessageTypeConstants {
//======================= mall 客服 =======================
String KEFU_MESSAGE_TYPE = "kefu_message_type"; // 客服消息类型
String KEFU_MESSAGE_ADMIN_READ = "kefu_message_read_status_change"; // 客服消息管理员已读
}

View File

@ -19,6 +19,7 @@ public enum KeFuMessageContentTypeEnum implements IntArrayValuable {
IMAGE(2, "图片消息"),
VOICE(3, "语音消息"),
VIDEO(4, "视频消息"),
SYSTEM(5, "系统消息"),
// ========== 商城特殊消息 ==========
PRODUCT(10, "商品消息"),
ORDER(11, "订单消息");

View File

@ -27,12 +27,11 @@ public class KeFuConversationController {
@Resource
private KeFuConversationService conversationService;
// TODO @puhui999updateConversationPinned
@PostMapping("/update-pinned")
@PostMapping("/update-conversation-pinned")
@Operation(summary = "置顶客服会话")
@PreAuthorize("@ss.hasPermission('promotion:kefu-conversation:update')")
public CommonResult<Boolean> updatePinned(@Valid @RequestBody KeFuConversationUpdatePinnedReqVO updateReqVO) {
conversationService.updatePinned(updateReqVO);
public CommonResult<Boolean> updateConversationPinned(@Valid @RequestBody KeFuConversationUpdatePinnedReqVO updateReqVO) {
conversationService.updateAdminPinned(updateReqVO);
return success(true);
}

View File

@ -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,7 +19,6 @@ 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
@ -37,11 +37,11 @@ public class KeFuMessageController {
}
@PutMapping("/update-read-status")
@Operation(summary = "更新客服消息已读状态")
@Operation(summary = "更新会员客服消息已读状态")
@Parameter(name = "conversationId", description = "会话编号", required = true)
@PreAuthorize("@ss.hasPermission('promotion:kefu-message:update')")
public CommonResult<Boolean> updateKefuMessageReadStatus(@RequestParam("conversationId") Long conversationId) {
messageService.updateKefuMessageReadStatus(conversationId, getLoginUserId());
messageService.updateKefuMessageReadStatus(conversationId);
return success(true);
}

View File

@ -1,7 +1,7 @@
package cn.iocoder.yudao.module.promotion.controller.admin.kefu.vo.conversation;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.*;
import lombok.Data;
import java.time.LocalDateTime;
@ -18,24 +18,22 @@ public class KeFuConversationRespVO {
@Schema(description = "最后聊天时间", requiredMode = Schema.RequiredMode.REQUIRED)
private LocalDateTime lastMessageTime;
// TODO @puhui999, 缺了空格哈
@Schema(description = "最后聊天内容", requiredMode = Schema.RequiredMode.REQUIRED,example = "嗨,您好啊")
@Schema(description = "最后聊天内容", requiredMode = Schema.RequiredMode.REQUIRED, example = "嗨,您好啊")
private String lastMessageContent;
@Schema(description = "最后发送的消息类型", requiredMode = Schema.RequiredMode.REQUIRED,example = "1")
@Schema(description = "最后发送的消息类型", requiredMode = Schema.RequiredMode.REQUIRED, example = "1")
private Integer lastMessageContentType;
@Schema(description = "管理端置顶", requiredMode = Schema.RequiredMode.REQUIRED,example = "false")
@Schema(description = "管理端置顶", requiredMode = Schema.RequiredMode.REQUIRED, example = "false")
private Boolean adminPinned;
@Schema(description = "用户是否可见", requiredMode = Schema.RequiredMode.REQUIRED,example = "true")
@Schema(description = "用户是否可见", requiredMode = Schema.RequiredMode.REQUIRED, example = "true")
private Boolean userDeleted;
@Schema(description = "管理员是否可见", requiredMode = Schema.RequiredMode.REQUIRED,example = "true")
@Schema(description = "管理员是否可见", requiredMode = Schema.RequiredMode.REQUIRED, example = "true")
private Boolean adminDeleted;
@Schema(description = "管理员未读消息数", requiredMode = Schema.RequiredMode.REQUIRED,example = "6")
@Schema(description = "管理员未读消息数", requiredMode = Schema.RequiredMode.REQUIRED, example = "6")
private Integer adminUnreadMessageCount;
@Schema(description = "创建时间", requiredMode = Schema.RequiredMode.REQUIRED)

View File

@ -6,8 +6,6 @@ import cn.iocoder.yudao.framework.common.pojo.PageParam;
@Schema(description = "管理后台 - 客服消息分页 Request VO")
@Data
// TODO @puhui999不用 @EqualsAndHashCode
@EqualsAndHashCode(callSuper = true)
@ToString(callSuper = true)
public class KeFuMessagePageReqVO extends PageParam {

View File

@ -1,37 +0,0 @@
package cn.iocoder.yudao.module.promotion.controller.app.kefu;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.security.core.annotations.PreAuthenticated;
import cn.iocoder.yudao.module.promotion.controller.app.kefu.vo.conversation.AppKeFuConversationRespVO;
import cn.iocoder.yudao.module.promotion.service.kefu.KeFuConversationService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;
@Tag(name = "用户 APP - 客户会话")
@RestController
@RequestMapping("/promotion/kefu-conversation")
@Validated
public class AppKeFuConversationController {
@Resource
private KeFuConversationService conversationService;
// TODO @puhui999接口名不对噢
@GetMapping("/get")
@Operation(summary = "获得客服会话")
@PreAuthenticated
public CommonResult<AppKeFuConversationRespVO> getDiyPage() {
// TODO @puhui999建议获取和转换分成 2 个哈干净一些
return success(BeanUtils.toBean(conversationService.getOrCreateConversation(getLoginUserId()), AppKeFuConversationRespVO.class));
}
}

View File

@ -1,5 +1,6 @@
package cn.iocoder.yudao.module.promotion.controller.app.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;
@ -35,7 +36,8 @@ public class AppKeFuMessageController {
@Operation(summary = "发送客服消息")
@PreAuthenticated
public CommonResult<Long> createKefuMessage(@Valid @RequestBody AppKeFuMessageSendReqVO sendReqVO) {
return success(kefuMessageService.sendKefuMessage(BeanUtils.toBean(sendReqVO, KeFuMessageSendReqVO.class)));
sendReqVO.setSenderId(getLoginUserId()).setSenderType(UserTypeEnum.MEMBER.getValue()); // 设置用户编号和类型
return success(kefuMessageService.sendKefuMessage(sendReqVO));
}
@PutMapping("/update-read-status")
@ -43,7 +45,7 @@ public class AppKeFuMessageController {
@Parameter(name = "conversationId", description = "会话编号", required = true)
@PreAuthenticated
public CommonResult<Boolean> updateKefuMessageReadStatus(@RequestParam("conversationId") Long conversationId) {
kefuMessageService.updateKefuMessageReadStatus(conversationId, getLoginUserId());
kefuMessageService.updateKefuMessageReadStatus(conversationId);
return success(true);
}

View File

@ -1,42 +0,0 @@
package cn.iocoder.yudao.module.promotion.controller.app.kefu.vo.conversation;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.*;
import java.time.LocalDateTime;
@Schema(description = "用户 App - 客服会话 Response VO")
@Data
public class AppKeFuConversationRespVO {
@Schema(description = "编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "24988")
private Long id;
@Schema(description = "会话所属用户", requiredMode = Schema.RequiredMode.REQUIRED, example = "8300")
private Long userId;
@Schema(description = "最后聊天时间", requiredMode = Schema.RequiredMode.REQUIRED,example = "2024-01-01 00:00:00")
private LocalDateTime lastMessageTime;
@Schema(description = "最后聊天内容", requiredMode = Schema.RequiredMode.REQUIRED,example = "嗨,您好啊")
private String lastMessageContent;
@Schema(description = "最后发送的消息类型", requiredMode = Schema.RequiredMode.REQUIRED,example = "1")
private Integer lastMessageContentType;
@Schema(description = "管理端置顶", requiredMode = Schema.RequiredMode.REQUIRED,example = "false")
private Boolean adminPinned;
@Schema(description = "用户是否可见", requiredMode = Schema.RequiredMode.REQUIRED,example = "true")
private Boolean userDeleted;
@Schema(description = "管理员是否可见", requiredMode = Schema.RequiredMode.REQUIRED,example = "true")
private Boolean adminDeleted;
@Schema(description = "管理员未读消息数", requiredMode = Schema.RequiredMode.REQUIRED,example = "6")
private Integer adminUnreadMessageCount;
@Schema(description = "创建时间", requiredMode = Schema.RequiredMode.REQUIRED,example = "2024-01-01 00:00:00")
private LocalDateTime createTime;
}

View File

@ -8,8 +8,6 @@ import lombok.ToString;
@Schema(description = "用户 App - 客服消息分页 Request VO")
@Data
// TODO @puhui999不用 @EqualsAndHashCode
@EqualsAndHashCode(callSuper = true)
@ToString(callSuper = true)
public class AppKeFuMessagePageReqVO extends PageParam {

View File

@ -12,30 +12,14 @@ public class AppKeFuMessageSendReqVO {
@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;

View File

@ -0,0 +1 @@
package cn.iocoder.yudao.module.promotion.controller.app.kefu.vo;

View File

@ -16,24 +16,22 @@ import java.util.List;
@Mapper
public interface KeFuConversationMapper extends BaseMapperX<KeFuConversationDO> {
// TODO @puhui999排序可以交给前端或者 controller数据库的计算尽量少哈
default List<KeFuConversationDO> selectListWithSort() {
default List<KeFuConversationDO> selectConversationList() {
return selectList(new LambdaQueryWrapperX<KeFuConversationDO>()
.eq(KeFuConversationDO::getAdminDeleted, Boolean.FALSE)
.orderByDesc(KeFuConversationDO::getAdminPinned) // 置顶优先
.orderByDesc(KeFuConversationDO::getCreateTime));
}
// TODO @puhui999是不是置零 update ok 然后单独搞个 +1 的方法
default void updateAdminUnreadMessageCountByConversationId(Long id, Integer count) {
LambdaUpdateWrapper<KeFuConversationDO> updateWrapper = new LambdaUpdateWrapper<>();
updateWrapper.eq(KeFuConversationDO::getId, id);
if (count != null && count > 0) { // 情况一会员发送消息时增加管理员的未读消息数
updateWrapper.setSql("admin_unread_message_count = admin_unread_message_count + 1");
} else { // 情况二管理员已读后重置
updateWrapper.set(KeFuConversationDO::getAdminUnreadMessageCount, 0);
}
update(updateWrapper);
default void updateAdminUnreadMessageCountWithZero(Long id) {
update(new LambdaUpdateWrapper<KeFuConversationDO>()
.eq(KeFuConversationDO::getId, id)
.set(KeFuConversationDO::getAdminUnreadMessageCount, 0));
}
default void updateAdminUnreadMessageCount(Long id) {
update(new LambdaUpdateWrapper<KeFuConversationDO>()
.eq(KeFuConversationDO::getId, id)
.setSql("admin_unread_message_count = admin_unread_message_count + 1"));
}
}

View File

@ -26,20 +26,15 @@ public interface KeFuMessageMapper extends BaseMapperX<KeFuMessageDO> {
.orderByDesc(KeFuMessageDO::getId));
}
default List<KeFuMessageDO> selectListByConversationIdAndReceiverIdAndReadStatus(Long conversationId,
Long receiverId,
Boolean readStatus) {
default List<KeFuMessageDO> selectListByConversationIdAndReadStatus(Long conversationId, Boolean readStatus) {
return selectList(new LambdaQueryWrapper<KeFuMessageDO>()
.eq(KeFuMessageDO::getConversationId, conversationId)
.eq(KeFuMessageDO::getReceiverId, receiverId)
.eq(KeFuMessageDO::getReadStatus, readStatus));
}
// TODO @puhui999status 拼写不对哈ps是不是搞个 ids + entity 的更新更通用点
default void updateReadStstusBatchByIds(Collection<Long> ids, Boolean readStatus) {
update(new LambdaUpdateWrapper<KeFuMessageDO>()
.in(KeFuMessageDO::getId, ids)
.set(KeFuMessageDO::getReadStatus, readStatus));
default void updateReadStatusBatchByIds(Collection<Long> ids, KeFuMessageDO keFuMessageDO) {
update(keFuMessageDO, new LambdaUpdateWrapper<KeFuMessageDO>()
.in(KeFuMessageDO::getId, ids));
}
}

View File

@ -2,11 +2,10 @@ package cn.iocoder.yudao.module.promotion.service.kefu;
import cn.iocoder.yudao.module.promotion.controller.admin.kefu.vo.conversation.KeFuConversationUpdatePinnedReqVO;
import cn.iocoder.yudao.module.promotion.dal.dataobject.kefu.KeFuConversationDO;
import cn.iocoder.yudao.module.promotion.dal.dataobject.kefu.KeFuMessageDO;
import java.time.LocalDateTime;
import java.util.List;
// TODO @puhui999可以在每个方法前面加个会员管理员区分下
/**
* 客服会话 Service 接口
*
@ -15,48 +14,43 @@ import java.util.List;
public interface KeFuConversationService {
/**
* 删除客服会话
* 管理员删除客服会话
*
* @param id 编号
*/
void deleteKefuConversation(Long id);
// TODO @puhui999是不是方法名体现出更新的是管理员的置顶哈
/**
* 客服会话置顶
* 管理员客服会话置顶
*
* @param updateReqVO 请求
*/
void updatePinned(KeFuConversationUpdatePinnedReqVO updateReqVO);
void updateAdminPinned(KeFuConversationUpdatePinnedReqVO updateReqVO);
// TODO @puhui999updateConversationLastMessage 会好点哈
/**
* 更新会话客服消息冗余信息
*
* @param id 编号
* @param lastMessageTime 最后聊天时间
* @param lastMessageContent 最后聊天内容
* @param lastMessageContentType 最后聊天内容类型
* @param kefuMessage 消息
*/
void updateConversationMessage(Long id, LocalDateTime lastMessageTime, String lastMessageContent, Integer lastMessageContentType);
void updateConversationLastMessage(KeFuMessageDO kefuMessage);
/**
* 更新管理员未读消息数
* 管理员将管理员未读消息计数更新为零
*
* @param id 编号
* @param count 数量0 则重置 1 则消息数加一
* @param id 编号
*/
void updateAdminUnreadMessageCountByConversationId(Long id, Integer count);
void updateAdminUnreadMessageCountWithZero(Long id);
/**
* 更新会话对于管理员是否可见
* 管理员更新会话对于管理员是否可见
*
* @param id 编号
* @param adminDeleted 管理员是否可见
*/
void updateConversationAdminDeleted(Long id, Boolean adminDeleted);
/**
* 获得客服会话列表
* 管理员获得客服会话列表
*
* @return 会话列表
*/

View File

@ -1,11 +1,15 @@
package cn.iocoder.yudao.module.promotion.service.kefu;
import cn.hutool.core.util.StrUtil;
import cn.iocoder.yudao.framework.common.enums.UserTypeEnum;
import cn.iocoder.yudao.module.promotion.controller.admin.kefu.vo.conversation.KeFuConversationUpdatePinnedReqVO;
import cn.iocoder.yudao.module.promotion.dal.dataobject.kefu.KeFuConversationDO;
import cn.iocoder.yudao.module.promotion.dal.dataobject.kefu.KeFuMessageDO;
import cn.iocoder.yudao.module.promotion.dal.mysql.kefu.KeFuConversationMapper;
import cn.iocoder.yudao.module.promotion.enums.kehu.KeFuMessageContentTypeEnum;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import java.time.LocalDateTime;
@ -36,19 +40,35 @@ public class KeFuConversationServiceImpl implements KeFuConversationService {
}
@Override
public void updatePinned(KeFuConversationUpdatePinnedReqVO updateReqVO) {
public void updateAdminPinned(KeFuConversationUpdatePinnedReqVO updateReqVO) {
conversationMapper.updateById(new KeFuConversationDO().setId(updateReqVO.getId()).setAdminPinned(updateReqVO.getAdminPinned()));
}
@Override
public void updateConversationMessage(Long id, LocalDateTime lastMessageTime, String lastMessageContent, Integer lastMessageContentType) {
conversationMapper.updateById(new KeFuConversationDO().setId(id).setLastMessageTime(lastMessageTime)
.setLastMessageContent(lastMessageContent).setLastMessageContentType(lastMessageContentType));
@Transactional(rollbackFor = Exception.class)
public void updateConversationLastMessage(KeFuMessageDO kefuMessage) {
// 1.1 校验会话是否存在
KeFuConversationDO conversation = validateKefuConversationExists(kefuMessage.getConversationId());
// 1.2 更新会话消息冗余
conversationMapper.updateById(new KeFuConversationDO().setId(kefuMessage.getConversationId())
.setLastMessageTime(kefuMessage.getCreateTime()).setLastMessageContent(kefuMessage.getContent())
.setLastMessageContentType(kefuMessage.getContentType()));
// 2.2 更新管理员未读消息数
if (UserTypeEnum.MEMBER.getValue().equals(kefuMessage.getSenderType())) {
conversationMapper.updateAdminUnreadMessageCount(kefuMessage.getConversationId());
}
// 2.4 会员用户发送消息时如果管理员删除过会话则进行恢复
if (UserTypeEnum.MEMBER.getValue().equals(kefuMessage.getSenderType())
&& Boolean.TRUE.equals(conversation.getAdminDeleted())) {
updateConversationAdminDeleted(kefuMessage.getConversationId(), Boolean.FALSE);
}
}
@Override
public void updateAdminUnreadMessageCountByConversationId(Long id, Integer count) {
conversationMapper.updateAdminUnreadMessageCountByConversationId(id, count);
public void updateAdminUnreadMessageCountWithZero(Long id) {
validateKefuConversationExists(id);
conversationMapper.updateAdminUnreadMessageCountWithZero(id);
}
@Override
@ -58,17 +78,16 @@ public class KeFuConversationServiceImpl implements KeFuConversationService {
@Override
public List<KeFuConversationDO> getKefuConversationList() {
return conversationMapper.selectListWithSort();
return conversationMapper.selectConversationList();
}
// TODO @puhui999貌似这个对话得用户主动创建不然管理员会看到一个空的对话
@Override
public KeFuConversationDO getOrCreateConversation(Long userId) {
KeFuConversationDO conversation = conversationMapper.selectOne(KeFuConversationDO::getUserId, userId);
// 没有历史会话则初始化一个新会话
if (conversation == null) {
conversation = new KeFuConversationDO().setUserId(userId).setLastMessageTime(LocalDateTime.now())
.setLastMessageContent("").setLastMessageContentType(KeFuMessageContentTypeEnum.TEXT.getType())
.setLastMessageContent(StrUtil.EMPTY).setLastMessageContentType(KeFuMessageContentTypeEnum.TEXT.getType())
.setAdminPinned(Boolean.FALSE).setUserDeleted(Boolean.FALSE).setAdminDeleted(Boolean.FALSE)
.setAdminUnreadMessageCount(0);
conversationMapper.insert(conversation);

View File

@ -3,10 +3,10 @@ package cn.iocoder.yudao.module.promotion.service.kefu;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.promotion.controller.admin.kefu.vo.message.KeFuMessagePageReqVO;
import cn.iocoder.yudao.module.promotion.controller.admin.kefu.vo.message.KeFuMessageSendReqVO;
import cn.iocoder.yudao.module.promotion.controller.app.kefu.vo.message.AppKeFuMessageSendReqVO;
import cn.iocoder.yudao.module.promotion.dal.dataobject.kefu.KeFuMessageDO;
import jakarta.validation.Valid;
// TODO @puhui999可以在每个方法前面加个会员管理员区分下
/**
* 客服消息 Service 接口
*
@ -15,7 +15,7 @@ import jakarta.validation.Valid;
public interface KeFuMessageService {
/**
* 发送客服消息
* 管理员发送客服消息
*
* @param sendReqVO 信息
* @return 编号
@ -23,12 +23,19 @@ public interface KeFuMessageService {
Long sendKefuMessage(@Valid KeFuMessageSendReqVO sendReqVO);
/**
* 更新消息已读状态
* 会员发送客服消息
*
* @param sendReqVO 信息
* @return 编号
*/
Long sendKefuMessage(AppKeFuMessageSendReqVO sendReqVO);
/**
* 管理员更新消息已读状态
*
* @param conversationId 会话编号
* @param receiverId 用户编号
*/
void updateKefuMessageReadStatus(Long conversationId, Long receiverId);
void updateKefuMessageReadStatus(Long conversationId);
/**
* 获得客服消息分页

View File

@ -1,6 +1,7 @@
package cn.iocoder.yudao.module.promotion.service.kefu;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.spring.SpringUtil;
import cn.iocoder.yudao.framework.common.enums.UserTypeEnum;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
@ -9,6 +10,7 @@ import cn.iocoder.yudao.module.infra.api.websocket.WebSocketSenderApi;
import cn.iocoder.yudao.module.member.api.user.MemberUserApi;
import cn.iocoder.yudao.module.promotion.controller.admin.kefu.vo.message.KeFuMessagePageReqVO;
import cn.iocoder.yudao.module.promotion.controller.admin.kefu.vo.message.KeFuMessageSendReqVO;
import cn.iocoder.yudao.module.promotion.controller.app.kefu.vo.message.AppKeFuMessageSendReqVO;
import cn.iocoder.yudao.module.promotion.dal.dataobject.kefu.KeFuConversationDO;
import cn.iocoder.yudao.module.promotion.dal.dataobject.kefu.KeFuMessageDO;
import cn.iocoder.yudao.module.promotion.dal.mysql.kefu.KeFuMessageMapper;
@ -19,11 +21,11 @@ import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import java.time.LocalDateTime;
import java.util.List;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertSet;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.getFirst;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.*;
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;
/**
* 客服消息 Service 实现类
@ -34,16 +36,10 @@ import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.
@Validated
public class KeFuMessageServiceImpl implements KeFuMessageService {
// TODO @puhui999@芋艿捉摸要不要拿到一个地方枚举
private static final String KEFU_MESSAGE_TYPE = "kefu_message_type"; // 客服消息类型
// TODO @puhui999kefuMessageMapper因为 messageMapper 可能会重叠
@Resource
private KeFuMessageMapper messageMapper;
private KeFuMessageMapper keFuMessageMapper;
@Resource
private KeFuConversationService conversationService;
@Resource
private AdminUserApi adminUserApi;
@Resource
@ -55,58 +51,60 @@ public class KeFuMessageServiceImpl implements KeFuMessageService {
@Transactional(rollbackFor = Exception.class)
public Long sendKefuMessage(KeFuMessageSendReqVO sendReqVO) {
// 1.1 校验会话是否存在
KeFuConversationDO conversation = conversationService.validateKefuConversationExists(sendReqVO.getConversationId());
conversationService.validateKefuConversationExists(sendReqVO.getConversationId());
// 1.2 校验接收人是否存在
validateReceiverExist(sendReqVO.getReceiverId(), sendReqVO.getReceiverType());
// 2.1 保存消息
KeFuMessageDO kefuMessage = BeanUtils.toBean(sendReqVO, KeFuMessageDO.class);
messageMapper.insert(kefuMessage);
// TODO @puhui999是不是 updateConversationMessage里面统一处理未读恢复直接设置 KeFuMessageDO 作为参数好了
keFuMessageMapper.insert(kefuMessage);
// 2.2 更新会话消息冗余
conversationService.updateConversationMessage(kefuMessage.getConversationId(), LocalDateTime.now(),
kefuMessage.getContent(), kefuMessage.getContentType());
// 2.3 更新管理员未读消息数
if (UserTypeEnum.ADMIN.getValue().equals(kefuMessage.getReceiverType())) {
conversationService.updateAdminUnreadMessageCountByConversationId(kefuMessage.getConversationId(), 1);
}
// 2.4 会员用户发送消息时如果管理员删除过会话则进行恢复
// TODO @puhui999建议 && 换一行
if (UserTypeEnum.MEMBER.getValue().equals(kefuMessage.getSenderType()) && Boolean.TRUE.equals(conversation.getAdminDeleted())) {
conversationService.updateConversationAdminDeleted(kefuMessage.getConversationId(), Boolean.FALSE);
}
conversationService.updateConversationLastMessage(kefuMessage);
// 3. 发送消息
getSelf().sendAsyncMessage(sendReqVO.getReceiverType(), sendReqVO.getReceiverId(), kefuMessage);
return kefuMessage.getId();
}
@Override
public Long sendKefuMessage(AppKeFuMessageSendReqVO sendReqVO) {
// 1.1 设置会话编号
KeFuMessageDO kefuMessage = BeanUtils.toBean(sendReqVO, KeFuMessageDO.class);
KeFuConversationDO conversation = conversationService.getOrCreateConversation(sendReqVO.getSenderId());
kefuMessage.setConversationId(conversation.getId());
// 1.2 保存消息
keFuMessageMapper.insert(kefuMessage);
// 2. 更新会话消息冗余
conversationService.updateConversationLastMessage(kefuMessage);
// 3. 发送消息
getSelf().sendAsyncMessageToAdmin(kefuMessage);
return kefuMessage.getId();
}
@Override
@Transactional(rollbackFor = Exception.class)
public void updateKefuMessageReadStatus(Long conversationId, Long receiverId) {
public void updateKefuMessageReadStatus(Long conversationId) {
// 1.1 校验会话是否存在
conversationService.validateKefuConversationExists(conversationId);
// 1.2 查询接收人所有的未读消息
// TODO @puhui999应该不能 receiverId 过滤哈因为多个客服一个人点了就都点了
List<KeFuMessageDO> messageList = messageMapper.selectListByConversationIdAndReceiverIdAndReadStatus(
conversationId, receiverId, Boolean.FALSE);
// 1.2 查询会话所有的未读消息 (tips: 多个客服一个人点了就都点了)
List<KeFuMessageDO> messageList = keFuMessageMapper.selectListByConversationIdAndReadStatus(conversationId, Boolean.FALSE);
// 1.3 情况一没有未读消息
if (CollUtil.isEmpty(messageList)) {
return;
}
// 2.1 情况二更新未读消息状态为已读
messageMapper.updateReadStstusBatchByIds(convertSet(messageList, KeFuMessageDO::getId), Boolean.TRUE);
// 2.2 更新管理员未读消息数
KeFuMessageDO message = getFirst(messageList);
assert message != null;
if (UserTypeEnum.ADMIN.getValue().equals(message.getReceiverType())) {
conversationService.updateAdminUnreadMessageCountByConversationId(conversationId, 0);
}
keFuMessageMapper.updateReadStatusBatchByIds(convertSet(messageList, KeFuMessageDO::getId),
new KeFuMessageDO().setReadStatus(Boolean.TRUE));
// 2.2 将管理员未读消息计数更新为零
conversationService.updateAdminUnreadMessageCountWithZero(conversationId);
// 2.3 发送消息通知发送者接收者已读 -> 发送者更新发送的消息状态
// 2.3 发送消息通知会员管理员已读 -> 会员更新发送的消息状态
// TODO @puhui999待定~
getSelf().sendAsyncMessage(message.getSenderType(), message.getSenderId(), "keFuMessageReadStatusChange");
KeFuMessageDO keFuMessage = getFirst(filterList(messageList, message -> UserTypeEnum.MEMBER.getValue().equals(message.getSenderType())));
assert keFuMessage != null; // 断言避免警告
webSocketSenderApi.sendObject(UserTypeEnum.MEMBER.getValue(), keFuMessage.getSenderId(), KEFU_MESSAGE_ADMIN_READ, StrUtil.EMPTY);
}
private void validateReceiverExist(Long receiverId, Integer receiverType) {
@ -123,9 +121,14 @@ public class KeFuMessageServiceImpl implements KeFuMessageService {
webSocketSenderApi.sendObject(userType, userId, KEFU_MESSAGE_TYPE, content);
}
@Async
public void sendAsyncMessageToAdmin(Object content) {
webSocketSenderApi.sendObject(UserTypeEnum.ADMIN.getValue(), KEFU_MESSAGE_TYPE, content);
}
@Override
public PageResult<KeFuMessageDO> getKefuMessagePage(KeFuMessagePageReqVO pageReqVO) {
return messageMapper.selectPage(pageReqVO);
return keFuMessageMapper.selectPage(pageReqVO);
}
private KeFuMessageServiceImpl getSelf() {

View File

@ -156,7 +156,7 @@ yudao:
- /admin-api/mp/open/** # 微信公众号开放平台,微信回调接口,不需要登录
websocket:
enable: true # websocket的开关
path: /infra/ws # 路径
path: /infra/ws/ # 路径 // TODO @puhui999: 小程序 socket.io 连接会多一个 / 🤣🤣🤣
sender-type: local # 消息发送的类型,可选值为 local、redis、rocketmq、kafka、rabbitmq
sender-rocketmq:
topic: ${spring.application.name}-websocket # 消息发送的 RocketMQ Topic