diff --git a/sql/mysql/mall-promotion-kefu.sql b/sql/mysql/mall-promotion-kefu.sql new file mode 100644 index 000000000..e0b478f57 --- /dev/null +++ b/sql/mysql/mall-promotion-kefu.sql @@ -0,0 +1,37 @@ +DROP TABLE IF EXISTS `promotion_kefu_conversation`; +CREATE TABLE `promotion_kefu_conversation` ( + `id` BIGINT NOT NULL AUTO_INCREMENT COMMENT '编号', + `user_id` BIGINT 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_type` INT NOT NULL COMMENT '最后发送的消息类型', + `admin_pinned` 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_unread_message_count` INT NOT NULL COMMENT '管理员未读消息数', + `creator` VARCHAR(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT '' COMMENT '创建者', + `create_time` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间', + `updater` VARCHAR(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT '' COMMENT '更新者', + `update_time` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间', + `deleted` BIT(1) NOT NULL DEFAULT b'0' COMMENT '是否删除', + PRIMARY KEY (`id`) USING BTREE +) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci COMMENT = '客服会话' ROW_FORMAT = Dynamic; + +DROP TABLE IF EXISTS `promotion_kefu_message`; +CREATE TABLE `promotion_kefu_message` ( + `id` BIGINT NOT NULL AUTO_INCREMENT COMMENT '编号', + `conversation_id` BIGINT NOT NULL COMMENT '会话编号', + `sender_id` BIGINT NOT NULL COMMENT '发送人编号', + `sender_type` INT NOT NULL COMMENT '发送人类型', + `receiver_id` BIGINT NOT NULL COMMENT '接收人编号', + `receiver_type` INT NOT NULL COMMENT '接收人类型', + `content_type` INT 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 '是否已读', + `creator` VARCHAR(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT '' COMMENT '创建者', + `create_time` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间', + `updater` VARCHAR(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT '' COMMENT '更新者', + `update_time` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间', + `deleted` BIT(1) NOT NULL DEFAULT b'0' COMMENT '是否删除', + PRIMARY KEY (`id`) USING BTREE +) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci COMMENT = '客服消息' ROW_FORMAT = Dynamic; diff --git a/yudao-module-mall/yudao-module-promotion-api/src/main/java/cn/iocoder/yudao/module/promotion/enums/ErrorCodeConstants.java b/yudao-module-mall/yudao-module-promotion-api/src/main/java/cn/iocoder/yudao/module/promotion/enums/ErrorCodeConstants.java index 3b19d616a..8cebd6e13 100644 --- a/yudao-module-mall/yudao-module-promotion-api/src/main/java/cn/iocoder/yudao/module/promotion/enums/ErrorCodeConstants.java +++ b/yudao-module-mall/yudao-module-promotion-api/src/main/java/cn/iocoder/yudao/module/promotion/enums/ErrorCodeConstants.java @@ -125,4 +125,10 @@ public interface ErrorCodeConstants { ErrorCode DIY_PAGE_NOT_EXISTS = new ErrorCode(1_013_018_000, "装修页面不存在"); ErrorCode DIY_PAGE_NAME_USED = new ErrorCode(1_013_018_001, "装修页面名称({})已经被使用"); + // ========== 客服会话 1-013-019-000 ========== + ErrorCode KEFU_CONVERSATION_NOT_EXISTS = new ErrorCode(1_013_019_000, "客服会话不存在"); + + // ========== 客服消息 1-013-020-000 ========== + ErrorCode KEFU_MESSAGE_NOT_EXISTS = new ErrorCode(1_013_020_000, "客服消息不存在"); + } diff --git a/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/controller/admin/kefu/KeFuConversationController.java b/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/controller/admin/kefu/KeFuConversationController.java new file mode 100644 index 000000000..099cc264c --- /dev/null +++ b/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/controller/admin/kefu/KeFuConversationController.java @@ -0,0 +1,54 @@ +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.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; +import io.swagger.v3.oas.annotations.Operation; +import io.swagger.v3.oas.annotations.Parameter; +import io.swagger.v3.oas.annotations.tags.Tag; +import jakarta.annotation.Resource; +import jakarta.validation.Valid; +import org.springframework.security.access.prepost.PreAuthorize; +import org.springframework.validation.annotation.Validated; +import org.springframework.web.bind.annotation.*; + +import java.util.List; + +import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success; + +@Tag(name = "管理后台 - 客服会话") +@RestController +@RequestMapping("/promotion/kefu-conversation") +@Validated +public class KeFuConversationController { + + @Resource + private KeFuConversationService conversationService; + + @PostMapping("/update-pinned") + @Operation(summary = "置顶客服会话") + @PreAuthorize("@ss.hasPermission('promotion:kefu-conversation:update')") + public CommonResult updatePinned(@Valid @RequestBody KeFuConversationUpdatePinnedReqVO updateReqVO) { + conversationService.updatePinned(updateReqVO); + return success(true); + } + + @DeleteMapping("/delete") + @Operation(summary = "删除客服会话") + @Parameter(name = "id", description = "编号", required = true) + @PreAuthorize("@ss.hasPermission('promotion:kefu-conversation:delete')") + public CommonResult deleteKefuConversation(@RequestParam("id") Long id) { + conversationService.deleteKefuConversation(id); + return success(true); + } + + @GetMapping("/list") + @Operation(summary = "获得客服会话列表") + @PreAuthorize("@ss.hasPermission('promotion:kefu-conversation:query')") + public CommonResult> getKefuConversationPage() { + return success(BeanUtils.toBean(conversationService.getKefuConversationList(), KeFuConversationRespVO.class)); + } + +} \ No newline at end of file diff --git a/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/controller/admin/kefu/KeFuMessageController.java b/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/controller/admin/kefu/KeFuMessageController.java new file mode 100644 index 000000000..b8adada3a --- /dev/null +++ b/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/controller/admin/kefu/KeFuMessageController.java @@ -0,0 +1,58 @@ +package cn.iocoder.yudao.module.promotion.controller.admin.kefu; + +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.KeFuMessageSendReqVO; +import org.springframework.web.bind.annotation.*; +import jakarta.annotation.Resource; +import org.springframework.validation.annotation.Validated; +import org.springframework.security.access.prepost.PreAuthorize; +import io.swagger.v3.oas.annotations.tags.Tag; +import io.swagger.v3.oas.annotations.Parameter; +import io.swagger.v3.oas.annotations.Operation; + +import jakarta.validation.*; + +import cn.iocoder.yudao.framework.common.pojo.PageResult; +import cn.iocoder.yudao.framework.common.pojo.CommonResult; +import cn.iocoder.yudao.framework.common.util.object.BeanUtils; +import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success; +import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId; + +import cn.iocoder.yudao.module.promotion.dal.dataobject.kefu.KeFuMessageDO; +import cn.iocoder.yudao.module.promotion.service.kefu.KeFuMessageService; + +@Tag(name = "管理后台 - 客服消息") +@RestController +@RequestMapping("/promotion/kefu-message") +@Validated +public class KeFuMessageController { + + @Resource + private KeFuMessageService messageService; + + @PostMapping("/send") + @Operation(summary = "发送客服消息") + @PreAuthorize("@ss.hasPermission('promotion:kefu-message:send')") + public CommonResult createKefuMessage(@Valid @RequestBody KeFuMessageSendReqVO sendReqVO) { + return success(messageService.sendKefuMessage(sendReqVO)); + } + + @PutMapping("/update-read-status") + @Operation(summary = "更新客服消息已读状态") + @Parameter(name = "conversationId", description = "会话编号", required = true) + @PreAuthorize("@ss.hasPermission('promotion:kefu-message:update')") + public CommonResult updateKefuMessageReadStatus(@RequestParam("conversationId") Long conversationId) { + messageService.updateKefuMessageReadStatus(conversationId, getLoginUserId()); + return success(true); + } + + @GetMapping("/page") + @Operation(summary = "获得客服消息分页") + @PreAuthorize("@ss.hasPermission('promotion:kefu-message:query')") + public CommonResult> getKefuMessagePage(@Valid KeFuMessagePageReqVO pageReqVO) { + PageResult pageResult = messageService.getKefuMessagePage(pageReqVO); + return success(BeanUtils.toBean(pageResult, KeFuMessageRespVO.class)); + } + +} \ No newline at end of file diff --git a/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/controller/admin/kefu/vo/conversation/KeFuConversationRespVO.java b/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/controller/admin/kefu/vo/conversation/KeFuConversationRespVO.java new file mode 100644 index 000000000..b3748e147 --- /dev/null +++ b/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/controller/admin/kefu/vo/conversation/KeFuConversationRespVO.java @@ -0,0 +1,42 @@ +package cn.iocoder.yudao.module.promotion.controller.admin.kefu.vo.conversation; + +import io.swagger.v3.oas.annotations.media.Schema; +import lombok.*; + +import java.time.LocalDateTime; + +@Schema(description = "管理后台 - 客服会话 Response VO") +@Data +public class KeFuConversationRespVO { + + @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; + +} \ No newline at end of file diff --git a/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/controller/admin/kefu/vo/conversation/KeFuConversationUpdatePinnedReqVO.java b/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/controller/admin/kefu/vo/conversation/KeFuConversationUpdatePinnedReqVO.java new file mode 100644 index 000000000..235f78227 --- /dev/null +++ b/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/controller/admin/kefu/vo/conversation/KeFuConversationUpdatePinnedReqVO.java @@ -0,0 +1,19 @@ +package cn.iocoder.yudao.module.promotion.controller.admin.kefu.vo.conversation; + +import io.swagger.v3.oas.annotations.media.Schema; +import jakarta.validation.constraints.NotNull; +import lombok.Data; + +@Schema(description = "管理后台 - 客服会话置顶 Request VO") +@Data +public class KeFuConversationUpdatePinnedReqVO { + + @Schema(description = "编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "23202") + @NotNull(message = "会话编号,不能为空") + private Long id; + + @Schema(description = "管理端置顶", requiredMode = Schema.RequiredMode.REQUIRED, example = "false") + @NotNull(message = "管理端置顶,不能为空") + private Boolean adminPinned; + +} \ No newline at end of file diff --git a/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/controller/admin/kefu/vo/message/KeFuMessagePageReqVO.java b/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/controller/admin/kefu/vo/message/KeFuMessagePageReqVO.java new file mode 100644 index 000000000..ec5e7261c --- /dev/null +++ b/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/controller/admin/kefu/vo/message/KeFuMessagePageReqVO.java @@ -0,0 +1,16 @@ +package cn.iocoder.yudao.module.promotion.controller.admin.kefu.vo.message; + +import lombok.*; +import io.swagger.v3.oas.annotations.media.Schema; +import cn.iocoder.yudao.framework.common.pojo.PageParam; + +@Schema(description = "管理后台 - 客服消息分页 Request VO") +@Data +@EqualsAndHashCode(callSuper = true) +@ToString(callSuper = true) +public class KeFuMessagePageReqVO extends PageParam { + + @Schema(description = "会话编号", example = "12580") + private Long conversationId; + +} \ No newline at end of file diff --git a/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/controller/admin/kefu/vo/message/KeFuMessageRespVO.java b/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/controller/admin/kefu/vo/message/KeFuMessageRespVO.java new file mode 100644 index 000000000..41f9c52f6 --- /dev/null +++ b/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/controller/admin/kefu/vo/message/KeFuMessageRespVO.java @@ -0,0 +1,43 @@ +package cn.iocoder.yudao.module.promotion.controller.admin.kefu.vo.message; + +import io.swagger.v3.oas.annotations.media.Schema; +import lombok.*; + +import java.time.LocalDateTime; +import com.alibaba.excel.annotation.*; + +@Schema(description = "管理后台 - 客服消息 Response VO") +@Data +public class KeFuMessageRespVO { + + @Schema(description = "编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "23202") + private Long id; + + @Schema(description = "会话编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "12580") + private Long conversationId; + + @Schema(description = "发送人编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "24571") + private Long senderId; + + @Schema(description = "发送人类型", requiredMode = Schema.RequiredMode.REQUIRED, example = "1") + private Integer senderType; + + @Schema(description = "接收人编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "29124") + private Long receiverId; + + @Schema(description = "接收人类型", requiredMode = Schema.RequiredMode.REQUIRED, example = "2") + private Integer receiverType; + + @Schema(description = "消息类型", requiredMode = Schema.RequiredMode.REQUIRED, example = "1") + private Integer contentType; + + @Schema(description = "消息", requiredMode = Schema.RequiredMode.REQUIRED) + private String content; + + @Schema(description = "是否已读", requiredMode = Schema.RequiredMode.REQUIRED, example = "1") + private Boolean readStatus; + + @Schema(description = "创建时间", requiredMode = Schema.RequiredMode.REQUIRED) + private LocalDateTime createTime; + +} \ No newline at end of file diff --git a/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/controller/admin/kefu/vo/message/KeFuMessageSendReqVO.java b/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/controller/admin/kefu/vo/message/KeFuMessageSendReqVO.java new file mode 100644 index 000000000..5ac8f04c4 --- /dev/null +++ b/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/controller/admin/kefu/vo/message/KeFuMessageSendReqVO.java @@ -0,0 +1,43 @@ +package cn.iocoder.yudao.module.promotion.controller.admin.kefu.vo.message; + +import io.swagger.v3.oas.annotations.media.Schema; +import jakarta.validation.constraints.NotEmpty; +import jakarta.validation.constraints.NotNull; +import lombok.Data; + +@Schema(description = "管理后台 - 发送客服消息 Request VO") +@Data +public class KeFuMessageSendReqVO { + + @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; + +} \ No newline at end of file diff --git a/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/controller/app/kefu/AppKeFuConversationController.java b/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/controller/app/kefu/AppKeFuConversationController.java new file mode 100644 index 000000000..15c0e2892 --- /dev/null +++ b/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/controller/app/kefu/AppKeFuConversationController.java @@ -0,0 +1,35 @@ +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; + + @GetMapping("/get") + @Operation(summary = "获得客服会话") + @PreAuthenticated + public CommonResult getDiyPage() { + return success(BeanUtils.toBean(conversationService.getOrCreateConversation(getLoginUserId()), AppKeFuConversationRespVO.class)); + } + +} \ No newline at end of file diff --git a/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/controller/app/kefu/AppKeFuMessageController.java b/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/controller/app/kefu/AppKeFuMessageController.java new file mode 100644 index 000000000..1af72dba7 --- /dev/null +++ b/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/controller/app/kefu/AppKeFuMessageController.java @@ -0,0 +1,58 @@ +package cn.iocoder.yudao.module.promotion.controller.app.kefu; + +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; +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.KeFuMessageSendReqVO; +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.service.kefu.KeFuMessageService; +import io.swagger.v3.oas.annotations.Operation; +import io.swagger.v3.oas.annotations.Parameter; +import io.swagger.v3.oas.annotations.tags.Tag; +import jakarta.annotation.Resource; +import jakarta.validation.Valid; +import org.springframework.security.access.prepost.PreAuthorize; +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 +@RequestMapping("/promotion/kefu-message") +@Validated +public class AppKeFuMessageController { + + @Resource + private KeFuMessageService kefuMessageService; + + @PostMapping("/send") + @Operation(summary = "发送客服消息") + @PreAuthenticated + public CommonResult createKefuMessage(@Valid @RequestBody AppKeFuMessageSendReqVO sendReqVO) { + return success(kefuMessageService.sendKefuMessage(BeanUtils.toBean(sendReqVO, KeFuMessageSendReqVO.class))); + } + + @PutMapping("/update-read-status") + @Operation(summary = "更新客服消息已读状态") + @Parameter(name = "conversationId", description = "会话编号", required = true) + @PreAuthenticated + public CommonResult updateKefuMessageReadStatus(@RequestParam("conversationId") Long conversationId) { + kefuMessageService.updateKefuMessageReadStatus(conversationId, getLoginUserId()); + return success(true); + } + + @GetMapping("/page") + @Operation(summary = "获得客服消息分页") + @PreAuthenticated + public CommonResult> getKefuMessagePage(@Valid KeFuMessagePageReqVO pageReqVO) { + PageResult pageResult = kefuMessageService.getKefuMessagePage(pageReqVO); + return success(BeanUtils.toBean(pageResult, KeFuMessageRespVO.class)); + } + +} \ No newline at end of file diff --git a/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/controller/app/kefu/vo/conversation/AppKeFuConversationRespVO.java b/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/controller/app/kefu/vo/conversation/AppKeFuConversationRespVO.java new file mode 100644 index 000000000..39a295dc1 --- /dev/null +++ b/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/controller/app/kefu/vo/conversation/AppKeFuConversationRespVO.java @@ -0,0 +1,42 @@ +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; + +} \ No newline at end of file diff --git a/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/controller/app/kefu/vo/message/AppKeFuMessagePageReqVO.java b/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/controller/app/kefu/vo/message/AppKeFuMessagePageReqVO.java new file mode 100644 index 000000000..a332d34ad --- /dev/null +++ b/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/controller/app/kefu/vo/message/AppKeFuMessagePageReqVO.java @@ -0,0 +1,18 @@ +package cn.iocoder.yudao.module.promotion.controller.app.kefu.vo.message; + +import cn.iocoder.yudao.framework.common.pojo.PageParam; +import io.swagger.v3.oas.annotations.media.Schema; +import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.ToString; + +@Schema(description = "用户 App - 客服消息分页 Request VO") +@Data +@EqualsAndHashCode(callSuper = true) +@ToString(callSuper = true) +public class AppKeFuMessagePageReqVO extends PageParam { + + @Schema(description = "会话编号", example = "12580") + private Long conversationId; + +} \ No newline at end of file diff --git a/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/controller/app/kefu/vo/message/AppKeFuMessageRespVO.java b/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/controller/app/kefu/vo/message/AppKeFuMessageRespVO.java new file mode 100644 index 000000000..fb7331afc --- /dev/null +++ b/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/controller/app/kefu/vo/message/AppKeFuMessageRespVO.java @@ -0,0 +1,42 @@ +package cn.iocoder.yudao.module.promotion.controller.app.kefu.vo.message; + +import io.swagger.v3.oas.annotations.media.Schema; +import lombok.Data; + +import java.time.LocalDateTime; + +@Schema(description = "用户 App - 客服消息 Response VO") +@Data +public class AppKeFuMessageRespVO { + + @Schema(description = "编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "23202") + private Long id; + + @Schema(description = "会话编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "12580") + private Long conversationId; + + @Schema(description = "发送人编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "24571") + private Long senderId; + + @Schema(description = "发送人类型", requiredMode = Schema.RequiredMode.REQUIRED, example = "1") + private Integer senderType; + + @Schema(description = "接收人编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "29124") + private Long receiverId; + + @Schema(description = "接收人类型", requiredMode = Schema.RequiredMode.REQUIRED, example = "2") + private Integer receiverType; + + @Schema(description = "消息类型", requiredMode = Schema.RequiredMode.REQUIRED, example = "1") + private Integer contentType; + + @Schema(description = "消息", requiredMode = Schema.RequiredMode.REQUIRED) + private String content; + + @Schema(description = "是否已读", requiredMode = Schema.RequiredMode.REQUIRED, example = "1") + private Boolean readStatus; + + @Schema(description = "创建时间", requiredMode = Schema.RequiredMode.REQUIRED) + private LocalDateTime createTime; + +} \ No newline at end of file diff --git a/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/controller/app/kefu/vo/message/AppKeFuMessageSendReqVO.java b/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/controller/app/kefu/vo/message/AppKeFuMessageSendReqVO.java new file mode 100644 index 000000000..f2de632da --- /dev/null +++ b/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/controller/app/kefu/vo/message/AppKeFuMessageSendReqVO.java @@ -0,0 +1,43 @@ +package cn.iocoder.yudao.module.promotion.controller.app.kefu.vo.message; + +import io.swagger.v3.oas.annotations.media.Schema; +import jakarta.validation.constraints.NotEmpty; +import jakarta.validation.constraints.NotNull; +import lombok.Data; + +@Schema(description = "用户 App - 发送客服消息 Request VO") +@Data +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; + +} \ No newline at end of file diff --git a/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/dal/dataobject/kefu/KeFuConversationDO.java b/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/dal/dataobject/kefu/KeFuConversationDO.java index 04432eebe..e9a73284f 100644 --- a/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/dal/dataobject/kefu/KeFuConversationDO.java +++ b/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/dal/dataobject/kefu/KeFuConversationDO.java @@ -61,15 +61,15 @@ public class KeFuConversationDO extends BaseDO { /** * 用户是否可见 * - * true - 可见,默认值 - * false - 不可见,用户删除时设置为 false + * false - 可见,默认值 + * true - 不可见,用户删除时设置为 true */ private Boolean userDeleted; /** * 管理员是否可见 * - * true - 可见,默认值 - * false - 不可见,管理员删除时设置为 false + * false - 可见,默认值 + * true - 不可见,管理员删除时设置为 true */ private Boolean adminDeleted; diff --git a/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/dal/mysql/kefu/KeFuConversationMapper.java b/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/dal/mysql/kefu/KeFuConversationMapper.java new file mode 100644 index 000000000..eae9b7401 --- /dev/null +++ b/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/dal/mysql/kefu/KeFuConversationMapper.java @@ -0,0 +1,38 @@ +package cn.iocoder.yudao.module.promotion.dal.mysql.kefu; + +import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX; +import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX; +import cn.iocoder.yudao.module.promotion.dal.dataobject.kefu.KeFuConversationDO; +import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper; +import org.apache.ibatis.annotations.Mapper; + +import java.util.List; + +/** + * 客服会话 Mapper + * + * @author HUIHUI + */ +@Mapper +public interface KeFuConversationMapper extends BaseMapperX { + + default List selectListWithSort() { + return selectList(new LambdaQueryWrapperX() + .eq(KeFuConversationDO::getAdminDeleted, Boolean.FALSE) + .orderByDesc(KeFuConversationDO::getAdminPinned) // 置顶优先 + .orderByDesc(KeFuConversationDO::getCreateTime)); + } + + default void updateAdminUnreadMessageCountByConversationId(Long id, Integer count) { + LambdaUpdateWrapper 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); + } + +} \ No newline at end of file diff --git a/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/dal/mysql/kefu/KeFuMessageMapper.java b/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/dal/mysql/kefu/KeFuMessageMapper.java new file mode 100644 index 000000000..5ec0a6cd9 --- /dev/null +++ b/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/dal/mysql/kefu/KeFuMessageMapper.java @@ -0,0 +1,42 @@ +package cn.iocoder.yudao.module.promotion.dal.mysql.kefu; + +import cn.iocoder.yudao.framework.common.pojo.PageResult; +import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX; +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.dal.dataobject.kefu.KeFuMessageDO; +import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; +import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper; +import org.apache.ibatis.annotations.Mapper; + +import java.util.Collection; +import java.util.List; + +/** + * 客服消息 Mapper + * + * @author HUIHUI + */ +@Mapper +public interface KeFuMessageMapper extends BaseMapperX { + + default PageResult selectPage(KeFuMessagePageReqVO reqVO) { + return selectPage(reqVO, new LambdaQueryWrapperX() + .eqIfPresent(KeFuMessageDO::getConversationId, reqVO.getConversationId()) + .orderByDesc(KeFuMessageDO::getId)); + } + + default List selectListByConversationIdAndReceiverIdAndReadStatus(Long conversationId, Long receiverId, Boolean readStatus) { + return selectList(new LambdaQueryWrapper() + .eq(KeFuMessageDO::getConversationId, conversationId) + .eq(KeFuMessageDO::getReceiverId, receiverId) + .eq(KeFuMessageDO::getReadStatus, readStatus)); + } + + default void updateReadStstusBatchByIds(Collection ids, Boolean readStatus) { + update(new LambdaUpdateWrapper() + .in(KeFuMessageDO::getId, ids) + .set(KeFuMessageDO::getReadStatus, readStatus)); + } + +} \ No newline at end of file diff --git a/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/service/kefu/KeFuConversationService.java b/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/service/kefu/KeFuConversationService.java new file mode 100644 index 000000000..828626310 --- /dev/null +++ b/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/service/kefu/KeFuConversationService.java @@ -0,0 +1,78 @@ +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 java.time.LocalDateTime; +import java.util.List; + +/** + * 客服会话 Service 接口 + * + * @author HUIHUI + */ +public interface KeFuConversationService { + + /** + * 删除客服会话 + * + * @param id 编号 + */ + void deleteKefuConversation(Long id); + + /** + * 客服会话置顶 + * + * @param updateReqVO 请求 + */ + void updatePinned(KeFuConversationUpdatePinnedReqVO updateReqVO); + + /** + * 更新会话客服消息冗余信息 + * + * @param id 编号 + * @param lastMessageTime 最后聊天时间 + * @param lastMessageContent 最后聊天内容 + * @param lastMessageContentType 最后聊天内容类型 + */ + void updateConversationMessage(Long id, LocalDateTime lastMessageTime, String lastMessageContent, Integer lastMessageContentType); + + /** + * 更新管理员未读消息数 + * + * @param id 编号 + * @param count 数量:0 则重置 1 则消息数加一 + */ + void updateAdminUnreadMessageCountByConversationId(Long id, Integer count); + + /** + * 更新会话对于管理员是否可见 + * + * @param adminDeleted 管理员是否可见 + */ + void updateConversationAdminDeleted(Long id, Boolean adminDeleted); + + /** + * 获得客服会话列表 + * + * @return 会话列表 + */ + List getKefuConversationList(); + + /** + * 获得或创建会话 + * + * @param userId 用户编号 + * @return 客服会话 + */ + KeFuConversationDO getOrCreateConversation(Long userId); + + /** + * 校验客服会话是否存在 + * + * @param id 编号 + * @return 客服会话 + */ + KeFuConversationDO validateKefuConversationExists(Long id); + +} \ No newline at end of file diff --git a/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/service/kefu/KeFuConversationServiceImpl.java b/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/service/kefu/KeFuConversationServiceImpl.java new file mode 100644 index 000000000..416e613ff --- /dev/null +++ b/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/service/kefu/KeFuConversationServiceImpl.java @@ -0,0 +1,88 @@ +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.mysql.kefu.KeFuConversationMapper; +import cn.iocoder.yudao.module.promotion.enums.kehu.KeFuMessageContentTypeEnum; +import jakarta.annotation.Resource; +import org.springframework.stereotype.Service; +import org.springframework.validation.annotation.Validated; + +import java.time.LocalDateTime; +import java.util.List; + +import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; +import static cn.iocoder.yudao.module.promotion.enums.ErrorCodeConstants.KEFU_CONVERSATION_NOT_EXISTS; + +/** + * 客服会话 Service 实现类 + * + * @author HUIHUI + */ +@Service +@Validated +public class KeFuConversationServiceImpl implements KeFuConversationService { + + @Resource + private KeFuConversationMapper conversationMapper; + + @Override + public void deleteKefuConversation(Long id) { + // 校验存在 + validateKefuConversationExists(id); + + // 只有管理员端可以删除会话,也不真的删,只是管理员端看不到啦 + conversationMapper.updateById(new KeFuConversationDO().setId(id).setAdminDeleted(Boolean.TRUE)); + } + + @Override + public void updatePinned(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)); + } + + @Override + public void updateAdminUnreadMessageCountByConversationId(Long id, Integer count) { + conversationMapper.updateAdminUnreadMessageCountByConversationId(id, count); + } + + @Override + public void updateConversationAdminDeleted(Long id, Boolean adminDeleted) { + conversationMapper.updateById(new KeFuConversationDO().setId(id).setAdminDeleted(adminDeleted)); + } + + @Override + public List getKefuConversationList() { + return conversationMapper.selectListWithSort(); + } + + @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()) + .setAdminPinned(Boolean.FALSE).setUserDeleted(Boolean.FALSE).setAdminDeleted(Boolean.FALSE) + .setAdminUnreadMessageCount(0); + conversationMapper.insert(conversation); + } + return conversation; + } + + @Override + public KeFuConversationDO validateKefuConversationExists(Long id) { + KeFuConversationDO conversationDO = conversationMapper.selectById(id); + if (conversationDO == null) { + throw exception(KEFU_CONVERSATION_NOT_EXISTS); + } + + return conversationDO; + } + +} \ No newline at end of file diff --git a/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/service/kefu/KeFuMessageService.java b/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/service/kefu/KeFuMessageService.java new file mode 100644 index 000000000..10d5aca32 --- /dev/null +++ b/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/service/kefu/KeFuMessageService.java @@ -0,0 +1,40 @@ +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.dal.dataobject.kefu.KeFuMessageDO; +import jakarta.validation.Valid; + +/** + * 客服消息 Service 接口 + * + * @author HUIHUI + */ +public interface KeFuMessageService { + + /** + * 发送客服消息 + * + * @param sendReqVO 信息 + * @return 编号 + */ + Long sendKefuMessage(@Valid KeFuMessageSendReqVO sendReqVO); + + /** + * 更新消息已读状态 + * + * @param conversationId 会话编号 + * @param receiverId 用户编号 + */ + void updateKefuMessageReadStatus(Long conversationId, Long receiverId); + + /** + * 获得客服消息分页 + * + * @param pageReqVO 分页查询 + * @return 客服消息分页 + */ + PageResult getKefuMessagePage(KeFuMessagePageReqVO pageReqVO); + +} \ No newline at end of file diff --git a/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/service/kefu/KeFuMessageServiceImpl.java b/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/service/kefu/KeFuMessageServiceImpl.java new file mode 100644 index 000000000..ff6224407 --- /dev/null +++ b/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/service/kefu/KeFuMessageServiceImpl.java @@ -0,0 +1,128 @@ +package cn.iocoder.yudao.module.promotion.service.kefu; + +import cn.hutool.core.collection.CollUtil; +import cn.hutool.extra.spring.SpringUtil; +import cn.iocoder.yudao.framework.common.enums.UserTypeEnum; +import cn.iocoder.yudao.framework.common.pojo.PageResult; +import cn.iocoder.yudao.framework.common.util.object.BeanUtils; +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.dal.dataobject.kefu.KeFuConversationDO; +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.system.api.user.AdminUserApi; +import jakarta.annotation.Resource; +import org.springframework.scheduling.annotation.Async; +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; + +/** + * 客服消息 Service 实现类 + * + * @author HUIHUI + */ +@Service +@Validated +public class KeFuMessageServiceImpl implements KeFuMessageService { + + private static final String KEFU_MESSAGE_TYPE = "kefu_message_type"; // 客服消息类型 + + @Resource + private KeFuMessageMapper messageMapper; + @Resource + private KeFuConversationService conversationService; + @Resource + private AdminUserApi adminUserApi; + @Resource + private MemberUserApi memberUserApi; + @Resource + private WebSocketSenderApi webSocketSenderApi; + + @Override + @Transactional(rollbackFor = Exception.class) + public Long sendKefuMessage(KeFuMessageSendReqVO sendReqVO) { + // 1.1 校验会话是否存在 + KeFuConversationDO conversation = conversationService.validateKefuConversationExists(sendReqVO.getConversationId()); + // 1.2 校验接收人是否存在 + validateReceiverExist(sendReqVO.getReceiverId(), sendReqVO.getReceiverType()); + + // 2.1 保存消息 + KeFuMessageDO kefuMessage = BeanUtils.toBean(sendReqVO, KeFuMessageDO.class); + messageMapper.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 会员用户发送消息时,如果管理员删除过会话则进行恢复 + if (UserTypeEnum.MEMBER.getValue().equals(kefuMessage.getSenderType()) && Boolean.TRUE.equals(conversation.getAdminDeleted())) { + conversationService.updateConversationAdminDeleted(kefuMessage.getConversationId(), Boolean.FALSE); + } + + // 3. 发送消息 + getSelf().sendAsyncMessage(sendReqVO.getReceiverType(), sendReqVO.getReceiverId(), kefuMessage); + + // 返回 + return kefuMessage.getId(); + } + + @Override + @Transactional(rollbackFor = Exception.class) + public void updateKefuMessageReadStatus(Long conversationId, Long receiverId) { + // 1.1 校验会话是否存在 + conversationService.validateKefuConversationExists(conversationId); + // 1.2 查询接收人所有的未读消息 + List messageList = messageMapper.selectListByConversationIdAndReceiverIdAndReadStatus( + conversationId, receiverId, 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); + } + // 2.3 发送消息通知发送者,接收者已读 -> 发送者更新发送的消息状态 + getSelf().sendAsyncMessage(message.getSenderType(), message.getSenderId(), "keFuMessageReadStatusChange"); + } + + private void validateReceiverExist(Long receiverId, Integer receiverType) { + if (UserTypeEnum.ADMIN.getValue().equals(receiverType)) { + adminUserApi.validateUser(receiverId); + } + if (UserTypeEnum.MEMBER.getValue().equals(receiverType)) { + memberUserApi.validateUser(receiverId); + } + } + + @Async + public void sendAsyncMessage(Integer userType, Long userId, Object content) { + webSocketSenderApi.sendObject(userType, userId, KEFU_MESSAGE_TYPE, content); + } + + @Override + public PageResult getKefuMessagePage(KeFuMessagePageReqVO pageReqVO) { + return messageMapper.selectPage(pageReqVO); + } + + private KeFuMessageServiceImpl getSelf() { + return SpringUtil.getBean(getClass()); + } + +} \ No newline at end of file diff --git a/yudao-module-member/yudao-module-member-api/src/main/java/cn/iocoder/yudao/module/member/api/user/MemberUserApi.java b/yudao-module-member/yudao-module-member-api/src/main/java/cn/iocoder/yudao/module/member/api/user/MemberUserApi.java index c9fb80100..da74aaa92 100644 --- a/yudao-module-member/yudao-module-member-api/src/main/java/cn/iocoder/yudao/module/member/api/user/MemberUserApi.java +++ b/yudao-module-member/yudao-module-member-api/src/main/java/cn/iocoder/yudao/module/member/api/user/MemberUserApi.java @@ -57,4 +57,12 @@ public interface MemberUserApi { * @return 用户信息 */ MemberUserRespDTO getUserByMobile(String mobile); + + /** + * 校验用户是否存在 + * + * @param id 用户编号 + */ + void validateUser(Long id); + } diff --git a/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/api/user/MemberUserApiImpl.java b/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/api/user/MemberUserApiImpl.java index 659c39b57..960930ddc 100644 --- a/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/api/user/MemberUserApiImpl.java +++ b/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/api/user/MemberUserApiImpl.java @@ -11,6 +11,9 @@ import jakarta.annotation.Resource; import java.util.Collection; import java.util.List; +import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; +import static cn.iocoder.yudao.module.member.enums.ErrorCodeConstants.USER_MOBILE_NOT_EXISTS; + /** * 会员用户的 API 实现类 * @@ -44,4 +47,12 @@ public class MemberUserApiImpl implements MemberUserApi { return MemberUserConvert.INSTANCE.convert2(userService.getUserByMobile(mobile)); } + @Override + public void validateUser(Long id) { + MemberUserDO user = userService.getUser(id); + if (user == null) { + throw exception(USER_MOBILE_NOT_EXISTS); + } + } + }