!1001 MALL-KEFU: 完善管理端接口

Merge pull request !1001 from puhui999/develop
This commit is contained in:
芋道源码 2024-07-02 12:53:40 +00:00 committed by Gitee
commit 80a1f50cc8
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
12 changed files with 98 additions and 65 deletions

View File

@ -2,6 +2,8 @@ package cn.iocoder.yudao.module.promotion.controller.admin.kefu;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.member.api.user.MemberUserApi;
import cn.iocoder.yudao.module.member.api.user.dto.MemberUserRespDTO;
import cn.iocoder.yudao.module.promotion.controller.admin.kefu.vo.conversation.KeFuConversationRespVO;
import cn.iocoder.yudao.module.promotion.controller.admin.kefu.vo.conversation.KeFuConversationUpdatePinnedReqVO;
import cn.iocoder.yudao.module.promotion.service.kefu.KeFuConversationService;
@ -15,8 +17,11 @@ import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Map;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertSet;
import static cn.iocoder.yudao.framework.common.util.collection.MapUtils.findAndThen;
@Tag(name = "管理后台 - 客服会话")
@RestController
@ -26,6 +31,8 @@ public class KeFuConversationController {
@Resource
private KeFuConversationService conversationService;
@Resource
private MemberUserApi memberUserApi;
@PostMapping("/update-conversation-pinned")
@Operation(summary = "置顶客服会话")
@ -39,7 +46,7 @@ public class KeFuConversationController {
@Operation(summary = "删除客服会话")
@Parameter(name = "id", description = "编号", required = true)
@PreAuthorize("@ss.hasPermission('promotion:kefu-conversation:delete')")
public CommonResult<Boolean> deleteKefuConversation(@RequestParam("id") Long id) {
public CommonResult<Boolean> deleteConversation(@RequestParam("id") Long id) {
conversationService.deleteKefuConversation(id);
return success(true);
}
@ -47,8 +54,18 @@ public class KeFuConversationController {
@GetMapping("/list")
@Operation(summary = "获得客服会话列表")
@PreAuthorize("@ss.hasPermission('promotion:kefu-conversation:query')")
public CommonResult<List<KeFuConversationRespVO>> getKefuConversationPage() {
return success(BeanUtils.toBean(conversationService.getKefuConversationList(), KeFuConversationRespVO.class));
public CommonResult<List<KeFuConversationRespVO>> getConversationList() {
// 查询会话列表
List<KeFuConversationRespVO> respList = BeanUtils.toBean(conversationService.getKefuConversationList(),
KeFuConversationRespVO.class);
// 拼接数据
Map<Long, MemberUserRespDTO> userMap = memberUserApi.getUserMap(convertSet(respList, KeFuConversationRespVO::getUserId));
respList.forEach(item->{
findAndThen(userMap, item.getUserId(), memberUser-> item.setUserAvatar(memberUser.getAvatar())
.setUserNickname(memberUser.getNickname()));
});
return success(respList);
}
}

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;
@ -8,6 +9,8 @@ import cn.iocoder.yudao.module.promotion.controller.admin.kefu.vo.message.KeFuMe
import cn.iocoder.yudao.module.promotion.controller.admin.kefu.vo.message.KeFuMessageSendReqVO;
import cn.iocoder.yudao.module.promotion.dal.dataobject.kefu.KeFuMessageDO;
import cn.iocoder.yudao.module.promotion.service.kefu.KeFuMessageService;
import cn.iocoder.yudao.module.system.api.user.AdminUserApi;
import cn.iocoder.yudao.module.system.api.user.dto.AdminUserRespDTO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
@ -17,7 +20,13 @@ import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import java.util.Map;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertSet;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.filterList;
import static cn.iocoder.yudao.framework.common.util.collection.MapUtils.findAndThen;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;
@Tag(name = "管理后台 - 客服消息")
@RestController
@ -27,11 +36,14 @@ public class KeFuMessageController {
@Resource
private KeFuMessageService messageService;
@Resource
private AdminUserApi adminUserApi;
@PostMapping("/send")
@Operation(summary = "发送客服消息")
@PreAuthorize("@ss.hasPermission('promotion:kefu-message:send')")
public CommonResult<Long> createKefuMessage(@Valid @RequestBody KeFuMessageSendReqVO sendReqVO) {
public CommonResult<Long> sendKeFuMessage(@Valid @RequestBody KeFuMessageSendReqVO sendReqVO) {
sendReqVO.setSenderId(getLoginUserId()).setSenderType(UserTypeEnum.ADMIN.getValue()); // 设置用户编号和类型
return success(messageService.sendKefuMessage(sendReqVO));
}
@ -39,18 +51,26 @@ public class KeFuMessageController {
@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);
public CommonResult<Boolean> updateKeFuMessageReadStatus(@RequestParam("conversationId") Long conversationId) {
messageService.updateKeFuMessageReadStatus(conversationId, getLoginUserId(), UserTypeEnum.ADMIN.getValue());
return success(true);
}
// TODO @puhui999这个应该是某个会话上翻下翻不是传统的分页哈
@GetMapping("/page")
@Operation(summary = "获得客服消息分页")
@PreAuthorize("@ss.hasPermission('promotion:kefu-message:query')")
public CommonResult<PageResult<KeFuMessageRespVO>> getKefuMessagePage(@Valid KeFuMessagePageReqVO pageReqVO) {
PageResult<KeFuMessageDO> pageResult = messageService.getKefuMessagePage(pageReqVO);
return success(BeanUtils.toBean(pageResult, KeFuMessageRespVO.class));
public CommonResult<PageResult<KeFuMessageRespVO>> getKeFuMessagePage(@Valid KeFuMessagePageReqVO pageReqVO) {
// 获得数据
PageResult<KeFuMessageDO> pageResult = messageService.getKeFuMessagePage(pageReqVO);
// 拼接数据
PageResult<KeFuMessageRespVO> result = BeanUtils.toBean(pageResult, KeFuMessageRespVO.class);
Map<Long, AdminUserRespDTO> userMap = adminUserApi.getUserMap(convertSet(filterList(result.getList(),
item -> UserTypeEnum.ADMIN.getValue().equals(item.getSenderType())), KeFuMessageRespVO::getSenderId));
result.getList().forEach(item->{
findAndThen(userMap, item.getSenderId(), adminUser -> item.setSenderAvatar(adminUser.getAvatar()));
});
return success(result);
}
}

View File

@ -14,6 +14,10 @@ public class KeFuConversationRespVO {
@Schema(description = "会话所属用户", requiredMode = Schema.RequiredMode.REQUIRED, example = "8300")
private Long userId;
@Schema(description = "会话所属用户头像", requiredMode = Schema.RequiredMode.REQUIRED, example = "https://yudao.com/images/avatar.jpg")
private String userAvatar;
@Schema(description = "会话所属用户昵称", requiredMode = Schema.RequiredMode.REQUIRED, example = "芋道")
private String userNickname;
@Schema(description = "最后聊天时间", requiredMode = Schema.RequiredMode.REQUIRED)
private LocalDateTime lastMessageTime;

View File

@ -18,6 +18,8 @@ public class KeFuMessageRespVO {
@Schema(description = "发送人编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "24571")
private Long senderId;
@Schema(description = "发送人头像", requiredMode = Schema.RequiredMode.REQUIRED, example = "https://yudao.com/images/avatar.jpg")
private String senderAvatar;
@Schema(description = "发送人类型", requiredMode = Schema.RequiredMode.REQUIRED, example = "1")
private Integer senderType;

View File

@ -9,31 +9,10 @@ import lombok.Data;
@Data
public class KeFuMessageSendReqVO {
// TODO @puhui999貌似字段多了1id 不用2senderIdsenderType 不用3receiverIdreceiverType 也不用原因可以想下哈
@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;
@ -42,4 +21,11 @@ public class KeFuMessageSendReqVO {
@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;
}

View File

@ -43,8 +43,7 @@ public class AppKeFuMessageController {
@Parameter(name = "conversationId", description = "会话编号", required = true)
@PreAuthenticated
public CommonResult<Boolean> updateKefuMessageReadStatus(@RequestParam("conversationId") Long conversationId) {
// TODO @puhui999需要传递 userId万一用户模拟一个 conversationId
kefuMessageService.updateKefuMessageReadStatus(conversationId);
kefuMessageService.updateKeFuMessageReadStatus(conversationId, getLoginUserId(), UserTypeEnum.MEMBER.getValue());
return success(true);
}
@ -52,7 +51,7 @@ public class AppKeFuMessageController {
@Operation(summary = "获得客服消息分页")
@PreAuthenticated
public CommonResult<PageResult<KeFuMessageRespVO>> getKefuMessagePage(@Valid AppKeFuMessagePageReqVO pageReqVO) {
PageResult<KeFuMessageDO> pageResult = kefuMessageService.getKefuMessagePage(pageReqVO, getLoginUserId());
PageResult<KeFuMessageDO> pageResult = kefuMessageService.getKeFuMessagePage(pageReqVO, getLoginUserId());
return success(BeanUtils.toBean(pageResult, KeFuMessageRespVO.class));
}

View File

@ -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;

View File

@ -22,18 +22,14 @@ public interface KeFuConversationMapper extends BaseMapperX<KeFuConversationDO>
.orderByDesc(KeFuConversationDO::getCreateTime));
}
// TODO @puhui999这个不用单独搞个方法哈Service 直接 new 一个对象然后调用 update 方法
default void updateAdminUnreadMessageCountWithZero(Long id) {
update(new LambdaUpdateWrapper<KeFuConversationDO>()
.eq(KeFuConversationDO::getId, id)
.set(KeFuConversationDO::getAdminUnreadMessageCount, 0));
}
// TODO @puhui999改成 updateAdminUnreadMessageCountIncrement 增加
default void updateAdminUnreadMessageCount(Long id) {
default void updateAdminUnreadMessageCountIncrement(Long id) {
update(new LambdaUpdateWrapper<KeFuConversationDO>()
.eq(KeFuConversationDO::getId, id)
.setSql("admin_unread_message_count = admin_unread_message_count + 1"));
}
default KeFuConversationDO selectByUserId(Long userId){
return selectOne(KeFuConversationDO::getUserId, userId);
}
}

View File

@ -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 @puhui999service 不写 dao 的逻辑哈
return conversationMapper.selectOne(KeFuConversationDO::getUserId, userId);
return conversationMapper.selectByUserId(userId);
}
}

View File

@ -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);
/**
* 获得客服消息分页
@ -44,7 +46,7 @@ public interface KeFuMessageService {
* @param pageReqVO 分页查询
* @return 客服消息分页
*/
PageResult<KeFuMessageDO> getKefuMessagePage(KeFuMessagePageReqVO pageReqVO);
PageResult<KeFuMessageDO> getKeFuMessagePage(KeFuMessagePageReqVO pageReqVO);
/**
* 会员获得客服消息分页
@ -53,6 +55,6 @@ public interface KeFuMessageService {
* @param userId 用户编号
* @return 客服消息分页
*/
PageResult<KeFuMessageDO> getKefuMessagePage(AppKeFuMessagePageReqVO pageReqVO, Long userId);
PageResult<KeFuMessageDO> getKeFuMessagePage(AppKeFuMessagePageReqVO pageReqVO, Long userId);
}

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.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<KeFuMessageDO> messageList = keFuMessageMapper.selectListByConversationIdAndReadStatus(conversationId, Boolean.FALSE);
// 1.3 情况一没有未读消息
@ -129,12 +136,12 @@ public class KeFuMessageServiceImpl implements KeFuMessageService {
}
@Override
public PageResult<KeFuMessageDO> getKefuMessagePage(KeFuMessagePageReqVO pageReqVO) {
public PageResult<KeFuMessageDO> getKeFuMessagePage(KeFuMessagePageReqVO pageReqVO) {
return keFuMessageMapper.selectPage(pageReqVO);
}
@Override
public PageResult<KeFuMessageDO> getKefuMessagePage(AppKeFuMessagePageReqVO pageReqVO, Long userId) {
public PageResult<KeFuMessageDO> getKeFuMessagePage(AppKeFuMessagePageReqVO pageReqVO, Long userId) {
// 1. 获得客服会话
KeFuConversationDO conversation = conversationService.getConversationByUserId(userId);
if (conversation == null) {

View File

@ -40,5 +40,9 @@ public class AdminUserRespDTO {
* 手机号码
*/
private String mobile;
/**
* 用户头像
*/
private String avatar;
}