update bug 修改

This commit is contained in:
yyb 2025-04-23 12:24:50 +08:00
parent 798bec4812
commit c0fea6a787
21 changed files with 125 additions and 62 deletions

View File

@ -8,6 +8,7 @@ import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.oas.annotations.EnableOpenApi;
import springfox.documentation.service.*;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
@ -28,6 +29,7 @@ import java.util.function.Predicate;
@EnableSwagger2
@Configuration
@EnableAutoConfiguration
@EnableOpenApi
@ConditionalOnProperty(name = "swagger.enabled", matchIfMissing = true)
public class SwaggerAutoConfiguration {

View File

@ -1,6 +1,7 @@
package com.m2pool.chat.controller;
import com.m2pool.chat.service.ChatMessageService;
import com.m2pool.chat.vo.MessagePageVo;
import com.m2pool.common.core.web.Result.AjaxResult;
import com.m2pool.common.security.annotation.RequiresLogin;
import io.swagger.annotations.Api;
@ -21,32 +22,18 @@ public class ChatMessageController {
* 查询7天前的历史记录
* @return
*/
@GetMapping("/find/history/message")
@PostMapping("/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,
@ApiParam(value = "分页页码默认为10条/页", example = "10")
@RequestParam(required = false,defaultValue = "10")Integer pageNum,
@ApiParam(value = "聊天室id", required = true, example = "1")
@RequestParam Long roomId
) {
return chatMessageService.findHistoryMessage(id,pageNum,roomId);
public AjaxResult findHistoryMessage(@RequestBody MessagePageVo pageVo) {
return chatMessageService.findHistoryMessage(pageVo.getId(), pageVo.getPageNum(), pageVo.getRoomId());
}
@GetMapping("/find/recently/message")
@PostMapping("/find/recently/message")
@ApiOperation(value = "查询七天内的记录", notes = "根据ID查询七天内的记录")
@RequiresLogin
public AjaxResult findRecentlyMessage(
@ApiParam(value = "游标最后一条消息id", required = true, example = "1")
@RequestParam(defaultValue = "1") Long id,
@ApiParam(value = "分页页码默认为10条/页", example = "10")
@RequestParam(required = false,defaultValue = "10")Integer pageNum,
@ApiParam(value = "聊天室id", required = true, example = "1")
@RequestParam Long roomId
) {
return chatMessageService.findRecentlyMessage(id,pageNum,roomId);
public AjaxResult findRecentlyMessage(@RequestBody MessagePageVo pageVo) {
return chatMessageService.findRecentlyMessage(pageVo.getId(), pageVo.getPageNum(), pageVo.getRoomId());
}
@GetMapping("/delete/message")
@ -64,10 +51,8 @@ public class ChatMessageController {
@PostMapping("/read/message")
@ApiOperation(value = "聊天室消息改已读")
@RequiresLogin
public AjaxResult readMessage(
@ApiParam(value = "聊天室id", required = true, example = "1")
@RequestParam Long roomId) {
return chatMessageService.readMessage(roomId);
public AjaxResult readMessage(@RequestBody MessagePageVo pageVo) {
return chatMessageService.readMessage(pageVo.getRoomId());
}

View File

@ -2,6 +2,7 @@ package com.m2pool.chat.controller;
import com.m2pool.chat.service.ChatRoomService;
import com.m2pool.chat.vo.CharRoomVo;
import com.m2pool.chat.vo.MessagePageVo;
import com.m2pool.common.core.web.Result.AjaxResult;
import com.m2pool.common.core.web.page.TableDataInfo;
import com.m2pool.common.security.annotation.RequiresLogin;
@ -19,11 +20,11 @@ public class ChatRoomController {
private ChatRoomService chatRoomService;
@GetMapping("/find/room/list")
@PostMapping("/find/room/list")
@ApiOperation(value = "查询客服人员的聊天室列表")
@RequiresLogin
public TableDataInfo findRoomList() {
return chatRoomService.findRoomList();
public TableDataInfo findRoomList(@RequestBody MessagePageVo messagePageVo) {
return chatRoomService.findRoomList(messagePageVo.getSendDateTime());
}
@GetMapping("/find/room/by/userid")

View File

@ -1,5 +1,6 @@
package com.m2pool.chat.controller;
import com.m2pool.chat.entity.StompPrincipal;
import com.m2pool.chat.service.StompService;
import com.m2pool.chat.vo.UserMessageVo;
import com.m2pool.common.core.web.Result.AjaxResult;
@ -32,7 +33,7 @@ public class StompController extends BaseController {
*/
@MessageMapping("/send/message")
@SendToUser("/queue")
public AjaxResult sendMessageToUser(@Payload UserMessageVo userMessageVo) {
return stompService.sendMessageToUser(userMessageVo);
public AjaxResult sendMessageToUser(StompPrincipal principal, @Payload UserMessageVo userMessageVo) {
return stompService.sendMessageToUser(principal,userMessageVo);
}
}

View File

@ -5,6 +5,8 @@ import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.Date;
/**
* @ClassName WebsocketMessageDto
* @Description 消息返回对象
@ -35,4 +37,21 @@ public class WebsocketMessageDto {
* 发送者类型 0 游客 1 登录用户 2 客服人员
*/
private Integer sendUserType;
/**
* 发送者邮箱
*/
private String sendEmail;
/**
* 发送时间
*/
private Date sendTime;
/**
* 聊天室id
*/
private Long roomId;
}

View File

@ -1,5 +1,7 @@
package com.m2pool.chat.entity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
@ -12,6 +14,7 @@ import java.time.LocalDateTime;
@AllArgsConstructor
@NoArgsConstructor
public class ChatMessage {
@TableId(value = "id", type = IdType.AUTO)
private Long id;
private Integer type;
private Integer read;

View File

@ -1,5 +1,7 @@
package com.m2pool.chat.entity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@ -10,6 +12,7 @@ import java.time.LocalDateTime;
@AllArgsConstructor
@NoArgsConstructor
public class ChatMessageHistory {
@TableId(value = "id", type = IdType.AUTO)
private Long id;
private Integer type;
private String sendEmail;

View File

@ -1,5 +1,7 @@
package com.m2pool.chat.entity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
@ -12,6 +14,7 @@ import java.time.LocalDateTime;
@NoArgsConstructor
@AllArgsConstructor
public class ChatRoom {
@TableId(value = "id", type = IdType.AUTO)
private Long id;
private String userOneEmail;
private String userTwoEmail;

View File

@ -17,9 +17,11 @@ public class StompPrincipal implements Principal {
this.name = name;
this.type = type;
}
@Override
public String getName() {
return name;
}
}

View File

@ -7,6 +7,7 @@ import com.m2pool.chat.entity.ChatRoom;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import java.time.LocalDateTime;
import java.util.List;
@Mapper
@ -18,7 +19,7 @@ public interface ChatRoomMapper extends BaseMapper<ChatRoom> {
* @param userEmail 客服邮箱
* @return
*/
List<ChatRoomDto> findRoomList(@Param("userEmail") String userEmail);
List<ChatRoomDto> findRoomList(@Param("userEmail") String userEmail, @Param("sendDateTime") LocalDateTime sendDateTime);

View File

@ -6,13 +6,16 @@ import com.m2pool.chat.vo.CharRoomVo;
import com.m2pool.common.core.web.Result.AjaxResult;
import com.m2pool.common.core.web.page.TableDataInfo;
import java.time.LocalDateTime;
public interface ChatRoomService extends IService<ChatRoom> {
/**
* 查询客服的聊天室列表
* @param sendDateTime 最后一个发送者发送时间
* @return
*/
TableDataInfo findRoomList();
TableDataInfo findRoomList(LocalDateTime sendDateTime);
/**
* 根据当前用户邮箱查询聊天室

View File

@ -1,5 +1,6 @@
package com.m2pool.chat.service;
import com.m2pool.chat.entity.StompPrincipal;
import com.m2pool.chat.vo.UserMessageVo;
import com.m2pool.common.core.web.Result.AjaxResult;
@ -10,6 +11,6 @@ public interface StompService {
* @param userMessageVo
* @return
*/
AjaxResult sendMessageToUser(UserMessageVo userMessageVo);
AjaxResult sendMessageToUser(StompPrincipal principal, UserMessageVo userMessageVo);
}

View File

@ -55,7 +55,6 @@ public class ChatMessageServiceImpl implements ChatMessageService {
public AjaxResult readMessage(Long roomId) {
ChatRoom chatRoom = chatRoomMapper.selectById(roomId);
String username = SecurityUtils.getUsername();
if (chatRoom != null){
String updateUserEmail = "";
if( username.equals(chatRoom.getUserOneEmail())){

View File

@ -21,6 +21,7 @@ import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.Random;
@ -39,12 +40,11 @@ public class ChatRoomServiceImpl extends ServiceImpl<ChatRoomMapper, ChatRoom> i
private RemoteUserService remoteUserService;
@Override
public TableDataInfo findRoomList() {
PageUtils.clearPage();
PageUtils.startPage();
public TableDataInfo findRoomList(LocalDateTime sendDateTime) {
String userEmail = SecurityUtils.getUsername();
PageUtils.startPage();
//1.查找当前客服对应的聊天室
List<ChatRoomDto> roomList = chatRoomMapper.findRoomList(userEmail);
List<ChatRoomDto> roomList = chatRoomMapper.findRoomList(userEmail,sendDateTime);
List<String> userEmails = roomList.stream().map(ChatRoomDto::getUserEmail).collect(Collectors.toList());
//2.查询未读数量
if (!userEmails.isEmpty()){
@ -55,6 +55,7 @@ public class ChatRoomServiceImpl extends ServiceImpl<ChatRoomMapper, ChatRoom> i
}
}
PageUtils.clearPage();
return getDataTable(roomList);
}

View File

@ -4,12 +4,12 @@ import com.m2pool.chat.constant.Destination;
import com.m2pool.chat.dto.WebsocketMessageDto;
import com.m2pool.chat.entity.ChatMessage;
import com.m2pool.chat.entity.ChatRoom;
import com.m2pool.chat.entity.StompPrincipal;
import com.m2pool.chat.mapper.ChatMessageMapper;
import com.m2pool.chat.mapper.ChatRoomMapper;
import com.m2pool.chat.service.StompService;
import com.m2pool.chat.vo.UserMessageVo;
import com.m2pool.common.core.web.Result.AjaxResult;
import com.m2pool.common.security.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.messaging.simp.user.SimpUserRegistry;
@ -20,6 +20,7 @@ import org.springframework.transaction.support.TransactionTemplate;
import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Date;
import static com.m2pool.chat.constant.UserType.CUSTOMER;
import static com.m2pool.chat.constant.UserType.TOURIST;
@ -43,24 +44,35 @@ public class StompServiceImpl implements StompService {
private TransactionTemplate transactionTemplate;
@Override
public AjaxResult sendMessageToUser(UserMessageVo userMessageVo) {
public AjaxResult sendMessageToUser(StompPrincipal principal, UserMessageVo userMessageVo) {
// 检查目标用户是否在线
if (!checkOnline(userMessageVo)) {
return AjaxResult.error("目标用户不在线");
}
WebsocketMessageDto build = WebsocketMessageDto.builder()
.type(userMessageVo.getType())
.content(userMessageVo.getContent())
.sendEmail(principal.getName())
.sendUserType(principal.getType())
.sendTime(new Date())
.build();
if(!TOURIST.equals(userMessageVo.getReceiveUserType())){
build.setRoomId(userMessageVo.getRoomId());
}
//当前用户发送其他人, 发送给指定用户. 否则默认发送给当前发送者
messagingTemplate.convertAndSendToUser(userMessageVo.getEmail(), Destination.QUEUE + "/" + userMessageVo.getEmail(),
WebsocketMessageDto.builder().type(userMessageVo.getType()).content(userMessageVo.getContent()).sendUserType(userMessageVo.getSendUserType()).build());
messagingTemplate.convertAndSendToUser(userMessageVo.getEmail(), Destination.QUEUE + "/" + userMessageVo.getEmail(),build);
// 接收者和发送者 都不是游客才能存储消息 和修改聊天室信息后续可改为消息中间件解耦形式
if (!TOURIST.equals(userMessageVo.getReceiveUserType()) && !TOURIST.equals(userMessageVo.getSendUserType())){
if (!TOURIST.equals(userMessageVo.getReceiveUserType()) && !TOURIST.equals(principal.getType())){
transactionTemplate.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) {
try {
// 插入消息并获取 ID
insertMessage(userMessageVo);
insertMessage(principal,userMessageVo);
// 更新房间信息
updateRoom(userMessageVo);
updateRoom(userMessageVo.getRoomId(),principal.getType());
} catch (Exception e) {
// 回滚事务
status.setRollbackOnly();
@ -87,9 +99,9 @@ public class StompServiceImpl implements StompService {
* @param userMessageVo
* @return
*/
private Long insertMessage(UserMessageVo userMessageVo){
private Long insertMessage(StompPrincipal principal,UserMessageVo userMessageVo){
ChatMessage message = ChatMessage.builder()
.sendEmail(SecurityUtils.getUsername())
.sendEmail(principal.getName())
.content(userMessageVo.getContent())
.type(userMessageVo.getType())
.roomId(userMessageVo.getRoomId())
@ -100,15 +112,16 @@ public class StompServiceImpl implements StompService {
/**
* 修改聊天信息
* @param userMessageVo
* @param roomId
* @param sendUserType
*/
private void updateRoom(UserMessageVo userMessageVo){
ChatRoom room = ChatRoom.builder().id(userMessageVo.getRoomId()).build();
if (CUSTOMER.equals(userMessageVo.getSendUserType())) {
private void updateRoom(Long roomId,Integer sendUserType){
ChatRoom room = ChatRoom.builder().id(roomId).build();
if (CUSTOMER.equals(sendUserType)) {
room.setLastCustomerSendTime(LocalDateTime.now());
} else {
room.setLastUserSendTime(LocalDateTime.now());
}
chatRoomMapper.insert(room);
chatRoomMapper.updateById(room);
}
}

View File

@ -0,0 +1,28 @@
package com.m2pool.chat.vo;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;
import java.time.LocalDateTime;
/**
* @ClassName MessagePageVo
* @Description TODO
* @Author yyb
* @Date 2025/4/22 17:09
*/
@Data
public class MessagePageVo {
private Long id;
private Integer pageNum;
private Long roomId;
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private LocalDateTime sendDateTime;
public MessagePageVo() {
this.id = 1L;
this.pageNum = 20;
}
}

View File

@ -31,15 +31,11 @@ public class UserMessageVo {
*/
private Integer receiveUserType;
/**
* 发送者类型 0 游客 1 登录用户 2 客服人员
*/
private Integer sendUserType;
/**
* 聊天室id
*/
private Long roomId;
}

View File

@ -6,6 +6,9 @@ server:
mime-types: application/json
spring:
mvc:
pathmatch:
matching-strategy: ant-path-matcher
application:
# 应用名称
name: m2pool-chat

View File

@ -14,7 +14,7 @@
create_time as createTime
FROM chat_message_history
WHERE id <![CDATA[ <= ]]> #{id} AND room_id = #{roomId}
ORDER BY create_time DESC
ORDER BY id DESC
LIMIT #{pageNum}
</select>
</mapper>

View File

@ -11,7 +11,7 @@
create_time as createTime
FROM chat_message
WHERE id <![CDATA[ <= ]]> #{id} AND room_id = #{roomId}
ORDER BY create_time DESC
ORDER BY id DESC
LIMIT #{pageNum}
</select>
<select id="findUnReadNums" resultType="java.util.Map">

View File

@ -12,12 +12,11 @@
FROM
chat_room
WHERE
user_two_email = #{userEmail}
user_two_email = #{userEmail} AND last_user_send_time <![CDATA[ <= ]]> #{sendDateTime}
AND del = FALSE
ORDER BY
flag DESC,
last_user_send_time DESC,
last_customer_send_time DESC
last_user_send_time DESC
</select>
<select id="findRoomByUserEmail" resultType="com.m2pool.chat.dto.ChatRoomDto">
SELECT