update 登录用户消息获取为空修改,查找聊天室和随机匹配客服功能合并。
This commit is contained in:
parent
e077f5f296
commit
b95eb1c1b2
|
@ -2,6 +2,7 @@ package com.m2pool.chat.controller;
|
|||
|
||||
import com.m2pool.chat.service.ChatMessageService;
|
||||
import com.m2pool.common.core.web.Result.AjaxResult;
|
||||
import com.m2pool.common.security.annotation.RequiresLogin;
|
||||
import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import io.swagger.annotations.ApiParam;
|
||||
|
@ -22,6 +23,7 @@ public class ChatMessageController {
|
|||
*/
|
||||
@GetMapping("/find/history/message")
|
||||
@ApiOperation(value = "查询7天前的历史记录", notes = "根据ID查询7天前的历史记录")
|
||||
@RequiresLogin
|
||||
public AjaxResult findHistoryMessage(
|
||||
@ApiParam(value = "游标:最后一条消息id", required = true, example = "1")
|
||||
@RequestParam(defaultValue = "1") Long id,
|
||||
|
@ -35,6 +37,7 @@ public class ChatMessageController {
|
|||
|
||||
@GetMapping("/find/recently/message")
|
||||
@ApiOperation(value = "查询七天内的记录", notes = "根据ID查询七天内的记录")
|
||||
@RequiresLogin
|
||||
public AjaxResult findRecentlyMessage(
|
||||
@ApiParam(value = "游标:最后一条消息id", required = true, example = "1")
|
||||
@RequestParam(defaultValue = "1") Long id,
|
||||
|
@ -48,6 +51,7 @@ public class ChatMessageController {
|
|||
|
||||
@GetMapping("/delete/message")
|
||||
@ApiOperation(value = "消息撤回或删除")
|
||||
@RequiresLogin
|
||||
public AjaxResult deleteMessage(
|
||||
@ApiParam(value = "消息id", required = true, example = "1")
|
||||
@RequestParam(defaultValue = "1") Long id,
|
||||
|
@ -59,6 +63,7 @@ public class ChatMessageController {
|
|||
|
||||
@PostMapping("/read/message")
|
||||
@ApiOperation(value = "聊天室消息改已读")
|
||||
@RequiresLogin
|
||||
public AjaxResult readMessage(
|
||||
@ApiParam(value = "聊天室id", required = true, example = "1")
|
||||
@RequestParam Long roomId) {
|
||||
|
|
|
@ -4,9 +4,9 @@ import com.m2pool.chat.service.ChatRoomService;
|
|||
import com.m2pool.chat.vo.CharRoomVo;
|
||||
import com.m2pool.common.core.web.Result.AjaxResult;
|
||||
import com.m2pool.common.core.web.page.TableDataInfo;
|
||||
import com.m2pool.common.security.annotation.RequiresLogin;
|
||||
import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import io.swagger.annotations.ApiParam;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
|
@ -21,20 +21,22 @@ public class ChatRoomController {
|
|||
|
||||
@GetMapping("/find/room/list")
|
||||
@ApiOperation(value = "查询客服人员的聊天室列表")
|
||||
@RequiresLogin
|
||||
public TableDataInfo findRoomList() {
|
||||
return chatRoomService.findRoomList();
|
||||
}
|
||||
|
||||
@GetMapping("/find/room/by/userid")
|
||||
@ApiOperation(value = "根据当前用户邮箱查询聊天室")
|
||||
public AjaxResult findRoomByUserid(@ApiParam(value = "当前用户聊天对象的userId", example = "1")
|
||||
@RequestParam(required = false) String email) {
|
||||
return chatRoomService.findRoomByUserid(email);
|
||||
@RequiresLogin
|
||||
public AjaxResult findRoomByUserid() {
|
||||
return chatRoomService.findRoomByUserid();
|
||||
}
|
||||
|
||||
|
||||
@PostMapping("/update/room")
|
||||
@ApiOperation(value = "修改room相关信息")
|
||||
@RequiresLogin
|
||||
public AjaxResult updateRoom(@RequestBody CharRoomVo charRoomVo) {
|
||||
return chatRoomService.updateRoom(charRoomVo);
|
||||
}
|
||||
|
|
|
@ -2,7 +2,9 @@ package com.m2pool.chat.dto;
|
|||
|
||||
import io.swagger.annotations.ApiModel;
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
|
@ -15,6 +17,8 @@ import java.time.LocalDateTime;
|
|||
|
||||
@ApiModel(description = "聊天消息返回对象")
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
public class ChatMessageDto {
|
||||
|
||||
@ApiModelProperty(value = "消息ID", example = "1")
|
||||
|
|
|
@ -1,6 +1,9 @@
|
|||
package com.m2pool.chat.dto;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
|
@ -10,7 +13,10 @@ import java.time.LocalDateTime;
|
|||
* @Author yyb
|
||||
* @Date 2025/4/14 14:28
|
||||
*/
|
||||
@Builder
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
public class ChatRoomDto {
|
||||
|
||||
/**
|
||||
|
|
|
@ -1,6 +1,9 @@
|
|||
package com.m2pool.chat.dto;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
/**
|
||||
* @ClassName WebsocketMessageDto
|
||||
|
@ -8,11 +11,14 @@ import lombok.Data;
|
|||
* @Author yyb
|
||||
* @Date 2025/4/10 16:27
|
||||
*/
|
||||
@Builder
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
public class WebsocketMessageDto {
|
||||
|
||||
/**
|
||||
* 消息id
|
||||
* 消息id : 目前没用,不做消息撤回功能了
|
||||
*/
|
||||
private Long id;
|
||||
|
||||
|
@ -26,7 +32,7 @@ public class WebsocketMessageDto {
|
|||
*/
|
||||
private String content;
|
||||
/**
|
||||
* 是否是登录用户
|
||||
* 发送者类型 0 游客 1 登录用户 2 客服人员
|
||||
*/
|
||||
private boolean isLoginUser;
|
||||
private Integer sendUserType;
|
||||
}
|
||||
|
|
|
@ -1,12 +1,16 @@
|
|||
package com.m2pool.chat.entity;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
@Builder
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
public class ChatMessage {
|
||||
private Long id;
|
||||
private Integer type;
|
||||
|
|
|
@ -1,10 +1,14 @@
|
|||
package com.m2pool.chat.entity;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
public class ChatMessageHistory {
|
||||
private Long id;
|
||||
private Integer type;
|
||||
|
|
|
@ -1,19 +1,23 @@
|
|||
package com.m2pool.chat.entity;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
@Builder
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class ChatRoom {
|
||||
private Long id;
|
||||
private String userOneEmail;
|
||||
private String userTwoEmail;
|
||||
private LocalDateTime lastUserSendTime;
|
||||
private LocalDateTime lastCustomerSendTime;
|
||||
private Boolean flag;
|
||||
private Integer flag;
|
||||
private LocalDateTime createTime;
|
||||
private LocalDateTime updateTime;
|
||||
private Boolean del;
|
||||
|
|
|
@ -25,10 +25,9 @@ public interface ChatRoomMapper extends BaseMapper<ChatRoom> {
|
|||
/**
|
||||
* 查找当前用户与对应用户是否已存在创建的聊天室
|
||||
* @param userEmail 用户邮箱
|
||||
* @param email 客服邮箱
|
||||
* @return
|
||||
*/
|
||||
Long findRoomByUserEmail(@Param("userEmail") String userEmail,@Param("email") String email);
|
||||
ChatRoomDto findRoomByUserEmail(@Param("userEmail") String userEmail);
|
||||
|
||||
|
||||
|
||||
|
|
|
@ -18,7 +18,7 @@ public interface ChatRoomService extends IService<ChatRoom> {
|
|||
* 根据当前用户邮箱查询聊天室
|
||||
* @return
|
||||
*/
|
||||
AjaxResult findRoomByUserid(String email);
|
||||
AjaxResult findRoomByUserid();
|
||||
|
||||
/**
|
||||
* 修改聊天室信息
|
||||
|
|
|
@ -13,6 +13,9 @@ import com.m2pool.common.core.utils.PageUtils;
|
|||
import com.m2pool.common.core.web.Result.AjaxResult;
|
||||
import com.m2pool.common.core.web.page.TableDataInfo;
|
||||
import com.m2pool.common.security.utils.SecurityUtils;
|
||||
import com.m2pool.system.api.RemoteUserService;
|
||||
import com.m2pool.system.api.entity.SysUser;
|
||||
import io.jsonwebtoken.lang.Collections;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
@ -20,6 +23,7 @@ import org.springframework.transaction.annotation.Transactional;
|
|||
import javax.annotation.Resource;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Random;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Service
|
||||
|
@ -31,11 +35,16 @@ public class ChatRoomServiceImpl extends ServiceImpl<ChatRoomMapper, ChatRoom> i
|
|||
@Resource
|
||||
private ChatMessageMapper chatMessageMapper;
|
||||
|
||||
@Resource
|
||||
private RemoteUserService remoteUserService;
|
||||
|
||||
@Override
|
||||
public TableDataInfo findRoomList() {
|
||||
PageUtils.clearPage();
|
||||
PageUtils.startPage();
|
||||
String userEmail = SecurityUtils.getUsername();
|
||||
//1.查找当前客服对应的聊天室
|
||||
List<ChatRoomDto> roomList = chatRoomMapper.findRoomList(SecurityUtils.getUsername());
|
||||
List<ChatRoomDto> roomList = chatRoomMapper.findRoomList(userEmail);
|
||||
List<String> userEmails = roomList.stream().map(ChatRoomDto::getUserEmail).collect(Collectors.toList());
|
||||
//2.查询未读数量
|
||||
if (!userEmails.isEmpty()){
|
||||
|
@ -64,19 +73,25 @@ public class ChatRoomServiceImpl extends ServiceImpl<ChatRoomMapper, ChatRoom> i
|
|||
|
||||
@Override
|
||||
@Transactional
|
||||
public AjaxResult findRoomByUserid(String email) {
|
||||
public AjaxResult findRoomByUserid() {
|
||||
//1.查询当前用户与对应用户是否已存在创建的聊天室
|
||||
String userEmail = SecurityUtils.getUsername();
|
||||
Long roomId = chatRoomMapper.findRoomByUserEmail(userEmail, email);
|
||||
if(roomId != null){
|
||||
return AjaxResult.success(roomId);
|
||||
ChatRoomDto roomByUserEmail = chatRoomMapper.findRoomByUserEmail(userEmail);
|
||||
if(roomByUserEmail != null){
|
||||
return AjaxResult.success(roomByUserEmail);
|
||||
}
|
||||
ChatRoom build = ChatRoom.builder().userOneEmail(userEmail).userTwoEmail(email).build();
|
||||
|
||||
//2.不存在创建一个聊天室
|
||||
List<SysUser> data = remoteUserService.getCSList().getData();
|
||||
if(Collections.isEmpty(data)){
|
||||
return AjaxResult.error("客服人数不足");
|
||||
}
|
||||
Random random = new Random();
|
||||
SysUser sysUser = data.get(random.nextInt(data.size()));
|
||||
ChatRoom build = ChatRoom.builder().userOneEmail(userEmail).userTwoEmail(sysUser.getEmail()).build();
|
||||
int insert = chatRoomMapper.insert(build);
|
||||
if (insert > 0){
|
||||
roomId = build.getId();
|
||||
return AjaxResult.success(roomId);
|
||||
return AjaxResult.success(ChatRoomDto.builder().id(build.getId()).userEmail(build.getUserTwoEmail()).build());
|
||||
}
|
||||
return AjaxResult.error("聊天室不存在,并且创建聊天室失败");
|
||||
}
|
||||
|
|
|
@ -44,15 +44,13 @@ public class StompServiceImpl implements StompService {
|
|||
|
||||
@Override
|
||||
public AjaxResult sendMessageToUser(UserMessageVo userMessageVo) {
|
||||
WebsocketMessageDto websocketMessageDto = new WebsocketMessageDto();
|
||||
websocketMessageDto.setType(userMessageVo.getType());
|
||||
websocketMessageDto.setContent(userMessageVo.getContent());
|
||||
// 检查目标用户是否在线
|
||||
if (!checkOnline(userMessageVo)) {
|
||||
return AjaxResult.error("目标用户不在线");
|
||||
}
|
||||
//当前用户发送其他人, 发送给指定用户. 否则默认发送给当前发送者
|
||||
messagingTemplate.convertAndSendToUser(userMessageVo.getEmail(), Destination.QUEUE + "/" + userMessageVo.getEmail(), userMessageVo.getContent());
|
||||
messagingTemplate.convertAndSendToUser(userMessageVo.getEmail(), Destination.QUEUE + "/" + userMessageVo.getEmail(),
|
||||
WebsocketMessageDto.builder().type(userMessageVo.getType()).content(userMessageVo.getContent()).sendUserType(userMessageVo.getSendUserType()).build());
|
||||
// 接收者和发送者 都不是游客才能存储消息 和修改聊天室信息。后续可改为消息中间件解耦形式。
|
||||
if (!TOURIST.equals(userMessageVo.getReceiveUserType()) && !TOURIST.equals(userMessageVo.getSendUserType())){
|
||||
transactionTemplate.execute(new TransactionCallbackWithoutResult() {
|
||||
|
@ -60,8 +58,7 @@ public class StompServiceImpl implements StompService {
|
|||
protected void doInTransactionWithoutResult(TransactionStatus status) {
|
||||
try {
|
||||
// 插入消息并获取 ID
|
||||
Long id = insertMessage(userMessageVo);
|
||||
websocketMessageDto.setId(id);
|
||||
insertMessage(userMessageVo);
|
||||
// 更新房间信息
|
||||
updateRoom(userMessageVo);
|
||||
} catch (Exception e) {
|
||||
|
@ -72,7 +69,8 @@ public class StompServiceImpl implements StompService {
|
|||
}
|
||||
});
|
||||
}
|
||||
return AjaxResult.success(websocketMessageDto);
|
||||
//可以用void
|
||||
return AjaxResult.success("成功");
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -11,5 +11,5 @@ import lombok.Data;
|
|||
@Data
|
||||
public class CharRoomVo {
|
||||
private Long id;
|
||||
private Boolean flag;
|
||||
private Integer flag;
|
||||
}
|
||||
|
|
|
@ -2,15 +2,6 @@
|
|||
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||
<mapper namespace="com.m2pool.chat.mapper.ChatRoomMapper">
|
||||
|
||||
<select id="findRoomByUserEmail" resultType="java.lang.Long">
|
||||
SELECT
|
||||
id
|
||||
FROM
|
||||
chat_room
|
||||
WHERE
|
||||
user_one_email = #{userEmail} AND user_two_email = #{email} OR
|
||||
(user_one_email = #{email} AND user_two_email = #{userEmail})
|
||||
</select>
|
||||
<select id="findRoomList" resultType="com.m2pool.chat.dto.ChatRoomDto">
|
||||
SELECT
|
||||
id,
|
||||
|
@ -28,4 +19,12 @@
|
|||
last_user_send_time DESC,
|
||||
last_customer_send_time DESC
|
||||
</select>
|
||||
<select id="findRoomByUserEmail" resultType="com.m2pool.chat.dto.ChatRoomDto">
|
||||
SELECT
|
||||
id,user_two_email AS userEmail
|
||||
FROM
|
||||
chat_room
|
||||
WHERE
|
||||
user_one_email = #{userEmail}
|
||||
</select>
|
||||
</mapper>
|
Loading…
Reference in New Issue