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

Merge pull request !993 from puhui999/develop
This commit is contained in:
芋道源码 2024-06-17 11:16:01 +00:00 committed by Gitee
commit 9c4afeb7ba
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
20 changed files with 225 additions and 248 deletions

View File

@ -1,37 +1,39 @@
DROP TABLE IF EXISTS `promotion_kefu_conversation`; DROP TABLE IF EXISTS `promotion_kefu_conversation`;
CREATE TABLE `promotion_kefu_conversation` ( CREATE TABLE `promotion_kefu_conversation` (
`id` BIGINT NOT NULL AUTO_INCREMENT COMMENT '编号', `id` bigint NOT NULL AUTO_INCREMENT COMMENT '编号',
`user_id` BIGINT NOT NULL COMMENT '会话所属用户', `user_id` bigint NOT NULL COMMENT '会话所属用户',
`last_message_time` DATETIME NOT NULL COMMENT '最后聊天时间', `last_message_time` datetime NOT NULL COMMENT '最后聊天时间',
`last_message_content` VARCHAR(500) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '最后聊天内容', `last_message_content` varchar(500) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '最后聊天内容',
`last_message_content_type` INT NOT NULL COMMENT '最后发送的消息类型', `last_message_content_type` int NOT NULL COMMENT '最后发送的消息类型',
`admin_pinned` BIT(1) NOT NULL DEFAULT b'0' COMMENT '管理端置顶', `admin_pinned` bit(1) NOT NULL DEFAULT b'0' COMMENT '管理端置顶',
`user_deleted` BIT(1) NOT NULL DEFAULT b'0' COMMENT '用户是否可见', `user_deleted` bit(1) NOT NULL DEFAULT b'0' COMMENT '用户是否可见',
`admin_deleted` BIT(1) NOT NULL DEFAULT b'0' COMMENT '管理员是否可见', `admin_deleted` bit(1) NOT NULL DEFAULT b'0' COMMENT '管理员是否可见',
`admin_unread_message_count` INT NOT NULL COMMENT '管理员未读消息数', `admin_unread_message_count` int NOT NULL COMMENT '管理员未读消息数',
`creator` VARCHAR(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT '' COMMENT '创建者', `creator` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT '' COMMENT '创建者',
`create_time` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间', `create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
`updater` VARCHAR(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT '' COMMENT '更新者', `updater` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT '' COMMENT '更新者',
`update_time` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间', `update_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
`deleted` BIT(1) NOT NULL DEFAULT b'0' COMMENT '是否删除', `deleted` bit(1) NOT NULL DEFAULT b'0' COMMENT '是否删除',
`tenant_id` bigint NOT NULL DEFAULT 0 COMMENT '租户编号',
PRIMARY KEY (`id`) USING BTREE PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci COMMENT = '客服会话' ROW_FORMAT = Dynamic; ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci ROW_FORMAT=DYNAMIC COMMENT='客服会话';
DROP TABLE IF EXISTS `promotion_kefu_message`; DROP TABLE IF EXISTS `promotion_kefu_message`;
CREATE TABLE `promotion_kefu_message` ( CREATE TABLE `promotion_kefu_message` (
`id` BIGINT NOT NULL AUTO_INCREMENT COMMENT '编号', `id` bigint NOT NULL AUTO_INCREMENT COMMENT '编号',
`conversation_id` BIGINT NOT NULL COMMENT '会话编号', `conversation_id` bigint NOT NULL COMMENT '会话编号',
`sender_id` BIGINT NOT NULL COMMENT '发送人编号', `sender_id` bigint NOT NULL COMMENT '发送人编号',
`sender_type` INT NOT NULL COMMENT '发送人类型', `sender_type` int NOT NULL COMMENT '发送人类型',
`receiver_id` BIGINT NOT NULL COMMENT '接收人编号', `receiver_id` bigint DEFAULT NULL COMMENT '接收人编号',
`receiver_type` INT NOT NULL COMMENT '接收人类型', `receiver_type` int DEFAULT NULL COMMENT '接收人类型',
`content_type` INT NOT NULL COMMENT '消息类型', `content_type` int NOT NULL COMMENT '消息类型',
`content` VARCHAR(500) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '消息', `content` varchar(500) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '消息',
`read_status` BIT(1) NOT NULL DEFAULT b'0' COMMENT '是否已读', `read_status` bit(1) NOT NULL DEFAULT b'0' COMMENT '是否已读',
`creator` VARCHAR(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT '' COMMENT '创建者', `creator` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT '' COMMENT '创建者',
`create_time` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间', `create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
`updater` VARCHAR(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT '' COMMENT '更新者', `updater` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT '' COMMENT '更新者',
`update_time` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间', `update_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
`deleted` BIT(1) NOT NULL DEFAULT b'0' COMMENT '是否删除', `deleted` bit(1) NOT NULL DEFAULT b'0' COMMENT '是否删除',
`tenant_id` bigint NOT NULL DEFAULT 0 COMMENT '租户编号',
PRIMARY KEY (`id`) USING BTREE PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci COMMENT = '客服消息' ROW_FORMAT = Dynamic; ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci ROW_FORMAT=DYNAMIC COMMENT='客服消息';

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, "图片消息"), IMAGE(2, "图片消息"),
VOICE(3, "语音消息"), VOICE(3, "语音消息"),
VIDEO(4, "视频消息"), VIDEO(4, "视频消息"),
SYSTEM(5, "系统消息"),
// ========== 商城特殊消息 ========== // ========== 商城特殊消息 ==========
PRODUCT(10, "商品消息"), PRODUCT(10, "商品消息"),
ORDER(11, "订单消息"); ORDER(11, "订单消息");

View File

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

View File

@ -1,5 +1,6 @@
package cn.iocoder.yudao.module.promotion.controller.admin.kefu; 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.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult; import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils; 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 org.springframework.web.bind.annotation.*;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success; import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;
@Tag(name = "管理后台 - 客服消息") @Tag(name = "管理后台 - 客服消息")
@RestController @RestController
@ -37,11 +37,11 @@ public class KeFuMessageController {
} }
@PutMapping("/update-read-status") @PutMapping("/update-read-status")
@Operation(summary = "更新客服消息已读状态") @Operation(summary = "更新会员客服消息已读状态")
@Parameter(name = "conversationId", description = "会话编号", required = true) @Parameter(name = "conversationId", description = "会话编号", required = true)
@PreAuthorize("@ss.hasPermission('promotion:kefu-message:update')") @PreAuthorize("@ss.hasPermission('promotion:kefu-message:update')")
public CommonResult<Boolean> updateKefuMessageReadStatus(@RequestParam("conversationId") Long conversationId) { public CommonResult<Boolean> updateKefuMessageReadStatus(@RequestParam("conversationId") Long conversationId) {
messageService.updateKefuMessageReadStatus(conversationId, getLoginUserId()); messageService.updateKefuMessageReadStatus(conversationId);
return success(true); return success(true);
} }

View File

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

View File

@ -6,8 +6,6 @@ import cn.iocoder.yudao.framework.common.pojo.PageParam;
@Schema(description = "管理后台 - 客服消息分页 Request VO") @Schema(description = "管理后台 - 客服消息分页 Request VO")
@Data @Data
// TODO @puhui999不用 @EqualsAndHashCode
@EqualsAndHashCode(callSuper = true)
@ToString(callSuper = true) @ToString(callSuper = true)
public class KeFuMessagePageReqVO extends PageParam { 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,12 +1,12 @@
package cn.iocoder.yudao.module.promotion.controller.app.kefu; 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.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult; import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils; import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.security.core.annotations.PreAuthenticated; import cn.iocoder.yudao.framework.security.core.annotations.PreAuthenticated;
import cn.iocoder.yudao.module.promotion.controller.admin.kefu.vo.message.KeFuMessagePageReqVO;
import cn.iocoder.yudao.module.promotion.controller.admin.kefu.vo.message.KeFuMessageRespVO; import cn.iocoder.yudao.module.promotion.controller.admin.kefu.vo.message.KeFuMessageRespVO;
import cn.iocoder.yudao.module.promotion.controller.admin.kefu.vo.message.KeFuMessageSendReqVO; import cn.iocoder.yudao.module.promotion.controller.app.kefu.vo.message.AppKeFuMessagePageReqVO;
import cn.iocoder.yudao.module.promotion.controller.app.kefu.vo.message.AppKeFuMessageSendReqVO; import cn.iocoder.yudao.module.promotion.controller.app.kefu.vo.message.AppKeFuMessageSendReqVO;
import cn.iocoder.yudao.module.promotion.dal.dataobject.kefu.KeFuMessageDO; import cn.iocoder.yudao.module.promotion.dal.dataobject.kefu.KeFuMessageDO;
import cn.iocoder.yudao.module.promotion.service.kefu.KeFuMessageService; import cn.iocoder.yudao.module.promotion.service.kefu.KeFuMessageService;
@ -15,14 +15,13 @@ import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag; import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource; import jakarta.annotation.Resource;
import jakarta.validation.Valid; import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated; import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success; import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId; import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;
@Tag(name = "管理后台 - 客服消息") @Tag(name = "用户 APP - 客服消息")
@RestController @RestController
@RequestMapping("/promotion/kefu-message") @RequestMapping("/promotion/kefu-message")
@Validated @Validated
@ -35,7 +34,8 @@ public class AppKeFuMessageController {
@Operation(summary = "发送客服消息") @Operation(summary = "发送客服消息")
@PreAuthenticated @PreAuthenticated
public CommonResult<Long> createKefuMessage(@Valid @RequestBody AppKeFuMessageSendReqVO sendReqVO) { 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") @PutMapping("/update-read-status")
@ -43,15 +43,15 @@ public class AppKeFuMessageController {
@Parameter(name = "conversationId", description = "会话编号", required = true) @Parameter(name = "conversationId", description = "会话编号", required = true)
@PreAuthenticated @PreAuthenticated
public CommonResult<Boolean> updateKefuMessageReadStatus(@RequestParam("conversationId") Long conversationId) { public CommonResult<Boolean> updateKefuMessageReadStatus(@RequestParam("conversationId") Long conversationId) {
kefuMessageService.updateKefuMessageReadStatus(conversationId, getLoginUserId()); kefuMessageService.updateKefuMessageReadStatus(conversationId);
return success(true); return success(true);
} }
@GetMapping("/page") @GetMapping("/page")
@Operation(summary = "获得客服消息分页") @Operation(summary = "获得客服消息分页")
@PreAuthenticated @PreAuthenticated
public CommonResult<PageResult<KeFuMessageRespVO>> getKefuMessagePage(@Valid KeFuMessagePageReqVO pageReqVO) { public CommonResult<PageResult<KeFuMessageRespVO>> getKefuMessagePage(@Valid AppKeFuMessagePageReqVO pageReqVO) {
PageResult<KeFuMessageDO> pageResult = kefuMessageService.getKefuMessagePage(pageReqVO); PageResult<KeFuMessageDO> pageResult = kefuMessageService.getKefuMessagePage(pageReqVO, getLoginUserId());
return success(BeanUtils.toBean(pageResult, KeFuMessageRespVO.class)); return success(BeanUtils.toBean(pageResult, KeFuMessageRespVO.class));
} }

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") @Schema(description = "用户 App - 客服消息分页 Request VO")
@Data @Data
// TODO @puhui999不用 @EqualsAndHashCode
@EqualsAndHashCode(callSuper = true)
@ToString(callSuper = true) @ToString(callSuper = true)
public class AppKeFuMessagePageReqVO extends PageParam { public class AppKeFuMessagePageReqVO extends PageParam {

View File

@ -12,30 +12,14 @@ public class AppKeFuMessageSendReqVO {
@Schema(description = "编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "23202") @Schema(description = "编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "23202")
private Long id; private Long id;
@Schema(description = "会话编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "12580")
@NotNull(message = "会话编号不能为空")
private Long conversationId;
@Schema(description = "发送人编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "24571") @Schema(description = "发送人编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "24571")
@NotNull(message = "发送人编号不能为空")
private Long senderId; private Long senderId;
@Schema(description = "发送人类型", requiredMode = Schema.RequiredMode.REQUIRED, example = "1") @Schema(description = "发送人类型", requiredMode = Schema.RequiredMode.REQUIRED, example = "1")
@NotNull(message = "发送人类型不能为空")
private Integer senderType; 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") @Schema(description = "消息类型", requiredMode = Schema.RequiredMode.REQUIRED, example = "1")
@NotNull(message = "消息类型不能为空") @NotNull(message = "消息类型不能为空")
private Integer contentType; private Integer contentType;
@Schema(description = "消息", requiredMode = Schema.RequiredMode.REQUIRED) @Schema(description = "消息", requiredMode = Schema.RequiredMode.REQUIRED)
@NotEmpty(message = "消息不能为空") @NotEmpty(message = "消息不能为空")
private String content; 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 @Mapper
public interface KeFuConversationMapper extends BaseMapperX<KeFuConversationDO> { public interface KeFuConversationMapper extends BaseMapperX<KeFuConversationDO> {
// TODO @puhui999排序可以交给前端或者 controller数据库的计算尽量少哈 default List<KeFuConversationDO> selectConversationList() {
default List<KeFuConversationDO> selectListWithSort() {
return selectList(new LambdaQueryWrapperX<KeFuConversationDO>() return selectList(new LambdaQueryWrapperX<KeFuConversationDO>()
.eq(KeFuConversationDO::getAdminDeleted, Boolean.FALSE) .eq(KeFuConversationDO::getAdminDeleted, Boolean.FALSE)
.orderByDesc(KeFuConversationDO::getAdminPinned) // 置顶优先
.orderByDesc(KeFuConversationDO::getCreateTime)); .orderByDesc(KeFuConversationDO::getCreateTime));
} }
// TODO @puhui999是不是置零 update ok 然后单独搞个 +1 的方法 default void updateAdminUnreadMessageCountWithZero(Long id) {
default void updateAdminUnreadMessageCountByConversationId(Long id, Integer count) { update(new LambdaUpdateWrapper<KeFuConversationDO>()
LambdaUpdateWrapper<KeFuConversationDO> updateWrapper = new LambdaUpdateWrapper<>(); .eq(KeFuConversationDO::getId, id)
updateWrapper.eq(KeFuConversationDO::getId, id); .set(KeFuConversationDO::getAdminUnreadMessageCount, 0));
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 updateAdminUnreadMessageCount(Long id) {
update(new LambdaUpdateWrapper<KeFuConversationDO>()
.eq(KeFuConversationDO::getId, id)
.setSql("admin_unread_message_count = admin_unread_message_count + 1"));
} }
} }

View File

@ -4,6 +4,7 @@ import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX; import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX; import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.promotion.controller.admin.kefu.vo.message.KeFuMessagePageReqVO; import cn.iocoder.yudao.module.promotion.controller.admin.kefu.vo.message.KeFuMessagePageReqVO;
import cn.iocoder.yudao.module.promotion.controller.app.kefu.vo.message.AppKeFuMessagePageReqVO;
import cn.iocoder.yudao.module.promotion.dal.dataobject.kefu.KeFuMessageDO; import cn.iocoder.yudao.module.promotion.dal.dataobject.kefu.KeFuMessageDO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper; import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
@ -23,23 +24,24 @@ public interface KeFuMessageMapper extends BaseMapperX<KeFuMessageDO> {
default PageResult<KeFuMessageDO> selectPage(KeFuMessagePageReqVO reqVO) { default PageResult<KeFuMessageDO> selectPage(KeFuMessagePageReqVO reqVO) {
return selectPage(reqVO, new LambdaQueryWrapperX<KeFuMessageDO>() return selectPage(reqVO, new LambdaQueryWrapperX<KeFuMessageDO>()
.eqIfPresent(KeFuMessageDO::getConversationId, reqVO.getConversationId()) .eqIfPresent(KeFuMessageDO::getConversationId, reqVO.getConversationId())
.orderByDesc(KeFuMessageDO::getId)); .orderByDesc(KeFuMessageDO::getCreateTime));
} }
default List<KeFuMessageDO> selectListByConversationIdAndReceiverIdAndReadStatus(Long conversationId, default List<KeFuMessageDO> selectListByConversationIdAndReadStatus(Long conversationId, Boolean readStatus) {
Long receiverId,
Boolean readStatus) {
return selectList(new LambdaQueryWrapper<KeFuMessageDO>() return selectList(new LambdaQueryWrapper<KeFuMessageDO>()
.eq(KeFuMessageDO::getConversationId, conversationId) .eq(KeFuMessageDO::getConversationId, conversationId)
.eq(KeFuMessageDO::getReceiverId, receiverId)
.eq(KeFuMessageDO::getReadStatus, readStatus)); .eq(KeFuMessageDO::getReadStatus, readStatus));
} }
// TODO @puhui999status 拼写不对哈ps是不是搞个 ids + entity 的更新更通用点 default void updateReadStatusBatchByIds(Collection<Long> ids, KeFuMessageDO keFuMessageDO) {
default void updateReadStstusBatchByIds(Collection<Long> ids, Boolean readStatus) { update(keFuMessageDO, new LambdaUpdateWrapper<KeFuMessageDO>()
update(new LambdaUpdateWrapper<KeFuMessageDO>() .in(KeFuMessageDO::getId, ids));
.in(KeFuMessageDO::getId, ids) }
.set(KeFuMessageDO::getReadStatus, readStatus));
default PageResult<KeFuMessageDO> selectPage(AppKeFuMessagePageReqVO pageReqVO){
return selectPage(pageReqVO, new LambdaQueryWrapperX<KeFuMessageDO>()
.eqIfPresent(KeFuMessageDO::getConversationId, pageReqVO.getConversationId())
.orderByDesc(KeFuMessageDO::getCreateTime));
} }
} }

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

View File

@ -1,11 +1,15 @@
package cn.iocoder.yudao.module.promotion.service.kefu; 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.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.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.dal.mysql.kefu.KeFuConversationMapper;
import cn.iocoder.yudao.module.promotion.enums.kehu.KeFuMessageContentTypeEnum; import cn.iocoder.yudao.module.promotion.enums.kehu.KeFuMessageContentTypeEnum;
import jakarta.annotation.Resource; import jakarta.annotation.Resource;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated; import org.springframework.validation.annotation.Validated;
import java.time.LocalDateTime; import java.time.LocalDateTime;
@ -36,19 +40,35 @@ public class KeFuConversationServiceImpl implements KeFuConversationService {
} }
@Override @Override
public void updatePinned(KeFuConversationUpdatePinnedReqVO updateReqVO) { public void updateAdminPinned(KeFuConversationUpdatePinnedReqVO updateReqVO) {
conversationMapper.updateById(new KeFuConversationDO().setId(updateReqVO.getId()).setAdminPinned(updateReqVO.getAdminPinned())); conversationMapper.updateById(new KeFuConversationDO().setId(updateReqVO.getId()).setAdminPinned(updateReqVO.getAdminPinned()));
} }
@Override @Override
public void updateConversationMessage(Long id, LocalDateTime lastMessageTime, String lastMessageContent, Integer lastMessageContentType) { @Transactional(rollbackFor = Exception.class)
conversationMapper.updateById(new KeFuConversationDO().setId(id).setLastMessageTime(lastMessageTime) public void updateConversationLastMessage(KeFuMessageDO kefuMessage) {
.setLastMessageContent(lastMessageContent).setLastMessageContentType(lastMessageContentType)); // 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 @Override
public void updateAdminUnreadMessageCountByConversationId(Long id, Integer count) { public void updateAdminUnreadMessageCountWithZero(Long id) {
conversationMapper.updateAdminUnreadMessageCountByConversationId(id, count); validateKefuConversationExists(id);
conversationMapper.updateAdminUnreadMessageCountWithZero(id);
} }
@Override @Override
@ -58,17 +78,16 @@ public class KeFuConversationServiceImpl implements KeFuConversationService {
@Override @Override
public List<KeFuConversationDO> getKefuConversationList() { public List<KeFuConversationDO> getKefuConversationList() {
return conversationMapper.selectListWithSort(); return conversationMapper.selectConversationList();
} }
// TODO @puhui999貌似这个对话得用户主动创建不然管理员会看到一个空的对话
@Override @Override
public KeFuConversationDO getOrCreateConversation(Long userId) { public KeFuConversationDO getOrCreateConversation(Long userId) {
KeFuConversationDO conversation = conversationMapper.selectOne(KeFuConversationDO::getUserId, userId); KeFuConversationDO conversation = conversationMapper.selectOne(KeFuConversationDO::getUserId, userId);
// 没有历史会话则初始化一个新会话 // 没有历史会话则初始化一个新会话
if (conversation == null) { if (conversation == null) {
conversation = new KeFuConversationDO().setUserId(userId).setLastMessageTime(LocalDateTime.now()) 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) .setAdminPinned(Boolean.FALSE).setUserDeleted(Boolean.FALSE).setAdminDeleted(Boolean.FALSE)
.setAdminUnreadMessageCount(0); .setAdminUnreadMessageCount(0);
conversationMapper.insert(conversation); conversationMapper.insert(conversation);
@ -85,4 +104,9 @@ public class KeFuConversationServiceImpl implements KeFuConversationService {
return conversation; return conversation;
} }
@Override
public KeFuConversationDO getConversationByUserId(Long userId) {
return conversationMapper.selectOne(KeFuConversationDO::getUserId, userId);
}
} }

View File

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

View File

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

View File

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