feat: 消息相关修复

This commit is contained in:
Bunny 2024-11-03 18:56:38 +08:00
parent a492aee703
commit 02321fbcb2
16 changed files with 310 additions and 286 deletions

View File

@ -28,10 +28,9 @@ public class MessageAddDto {
@Schema(name = "sendUserId", title = "发送人用户ID") @Schema(name = "sendUserId", title = "发送人用户ID")
private Long sendUserId; private Long sendUserId;
@Schema(name = "messageType", title = "消息类型") @Schema(name = "messageTypeId", title = "消息类型")
@NotBlank(message = "消息类型 不能为空")
@NotNull(message = "消息类型 不能为空") @NotNull(message = "消息类型 不能为空")
private String messageType; private Long messageTypeId;
@Schema(name = "cover", title = "封面") @Schema(name = "cover", title = "封面")
private String cover; private String cover;

View File

@ -26,17 +26,19 @@ public class MessageUpdateDto {
@NotBlank(message = "消息标题 不能为空") @NotBlank(message = "消息标题 不能为空")
private String title; private String title;
@Schema(name = "receivedUserIds", title = "接收人用户ID") @Schema(name = "receivedUserId", title = "接收人用户ID")
private Long receivedUserId;
@Schema(name = "receivedUserIds", title = "接收人用户ID列表")
private List<Long> receivedUserIds; private List<Long> receivedUserIds;
@Schema(name = "sendUserId", title = "发送人用户ID") @Schema(name = "sendUserId", title = "发送人用户ID")
@NotNull(message = "发送人用户ID 不能为空") @NotNull(message = "发送人用户ID 不能为空")
private Long sendUserId; private Long sendUserId;
@Schema(name = "messageType", title = "消息类型") @Schema(name = "messageTypeId", title = "消息类型")
@NotBlank(message = "消息类型 不能为空")
@NotNull(message = "消息类型 不能为空") @NotNull(message = "消息类型 不能为空")
private String messageType; private Long messageTypeId;
@Schema(name = "cover", title = "封面") @Schema(name = "cover", title = "封面")
private String cover; private String cover;

View File

@ -43,9 +43,6 @@ public class Message extends BaseEntity {
@Schema(name = "editorType", title = "编辑器类型") @Schema(name = "editorType", title = "编辑器类型")
private String editorType; private String editorType;
@Schema(name = "status", title = "0:未读 1:已读")
private Boolean status;
@Schema(name = "level", title = "消息等级") @Schema(name = "level", title = "消息等级")
private String level; private String level;

View File

@ -28,4 +28,7 @@ public class MessageReceived extends BaseEntity {
@Schema(name = "messageId", title = "消息ID") @Schema(name = "messageId", title = "消息ID")
private Long messageId; private Long messageId;
@Schema(name = "status", title = "0:未读 1:已读")
private Boolean status;
} }

View File

@ -22,16 +22,20 @@ import java.time.LocalDateTime;
@Schema(name = "MessageVo对象", title = "系统消息返回内容", description = "系统消息返回内容") @Schema(name = "MessageVo对象", title = "系统消息返回内容", description = "系统消息返回内容")
public class MessageUserVo extends BaseVo { public class MessageUserVo extends BaseVo {
@Schema(name = "id", title = "主键") @Schema(name = "id", title = "消息messageId")
@JsonProperty("id")
@JsonFormat(shape = JsonFormat.Shape.STRING) @JsonFormat(shape = JsonFormat.Shape.STRING)
@JSONField(serializeUsing = ToStringSerializer.class) @JSONField(serializeUsing = ToStringSerializer.class)
private Long id; private Long id;
@Schema(name = "messageReceivedId", title = "用户消息表id")
@JsonFormat(shape = JsonFormat.Shape.STRING)
@JSONField(serializeUsing = ToStringSerializer.class)
private Long messageReceivedId;
@Schema(name = "title", title = "消息标题") @Schema(name = "title", title = "消息标题")
private String title; private String title;
@Schema(name = "messageType", title = "sys:系统消息,user用户消息") @Schema(name = "messageType", title = "消息类型")
private String messageType; private String messageType;
@Schema(name = "cover", title = "封面") @Schema(name = "cover", title = "封面")

View File

@ -3,12 +3,11 @@ package cn.bunny.dao.vo.system.message;
import cn.bunny.dao.common.vo.BaseUserVo; import cn.bunny.dao.common.vo.BaseUserVo;
import com.alibaba.fastjson2.annotation.JSONField; import com.alibaba.fastjson2.annotation.JSONField;
import com.fasterxml.jackson.annotation.JsonFormat; import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer; import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import io.swagger.v3.oas.annotations.media.Schema; import io.swagger.v3.oas.annotations.media.Schema;
import lombok.*; import lombok.*;
import java.util.List;
@EqualsAndHashCode(callSuper = true) @EqualsAndHashCode(callSuper = true)
@Data @Data
@AllArgsConstructor @AllArgsConstructor
@ -17,6 +16,12 @@ import java.util.List;
@Schema(name = "MessageVo对象", title = "系统消息返回内容", description = "系统消息返回内容") @Schema(name = "MessageVo对象", title = "系统消息返回内容", description = "系统消息返回内容")
public class MessageVo extends BaseUserVo { public class MessageVo extends BaseUserVo {
@Schema(name = "messageReceivedId", title = "用户消息表id")
@JsonProperty("id")
@JsonFormat(shape = JsonFormat.Shape.STRING)
@JSONField(serializeUsing = ToStringSerializer.class)
private Long messageReceivedId;
@Schema(name = "title", title = "消息标题") @Schema(name = "title", title = "消息标题")
private String title; private String title;
@ -28,10 +33,16 @@ public class MessageVo extends BaseUserVo {
@Schema(name = "sendNickname", title = "发送人昵称") @Schema(name = "sendNickname", title = "发送人昵称")
private String sendNickname; private String sendNickname;
@Schema(name = "receivedUserIds", title = "接收人用户ID") @Schema(name = "receivedUserId", title = "接收人用户ID")
private List<String> receivedUserIds; private String receivedUserId;
@Schema(name = "messageType", title = "sys:系统消息,user用户消息") @Schema(name = "receivedUserNickname", title = "接收人用户昵称")
private String receivedUserNickname;
@Schema(name = "messageTypeId", title = "消息类型id")
private String messageTypeId;
@Schema(name = "messageType", title = "消息类型")
private String messageType; private String messageType;
@Schema(name = "cover", title = "封面") @Schema(name = "cover", title = "封面")

View File

@ -1,8 +1,6 @@
package cn.bunny.services.controller; package cn.bunny.services.controller;
import cn.bunny.dao.dto.system.message.MessageAddDto;
import cn.bunny.dao.dto.system.message.MessageDto; import cn.bunny.dao.dto.system.message.MessageDto;
import cn.bunny.dao.dto.system.message.MessageUpdateDto;
import cn.bunny.dao.dto.system.message.MessageUserDto; import cn.bunny.dao.dto.system.message.MessageUserDto;
import cn.bunny.dao.entity.system.Message; import cn.bunny.dao.entity.system.Message;
import cn.bunny.dao.pojo.result.PageResult; import cn.bunny.dao.pojo.result.PageResult;
@ -15,7 +13,6 @@ import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter; 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.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Mono; import reactor.core.publisher.Mono;
@ -52,7 +49,7 @@ public class MessageController {
} }
@Operation(summary = "分页查询用户消息", description = "分页查询用户消息") @Operation(summary = "分页查询用户消息", description = "分页查询用户消息")
@GetMapping("/noManage/getUserMessageList/{page}/{limit}") @GetMapping("noManage/getUserMessageList/{page}/{limit}")
public Mono<Result<PageResult<MessageUserVo>>> getUserMessageList( public Mono<Result<PageResult<MessageUserVo>>> getUserMessageList(
@Parameter(name = "page", description = "当前页", required = true) @Parameter(name = "page", description = "当前页", required = true)
@PathVariable("page") Integer page, @PathVariable("page") Integer page,
@ -65,33 +62,12 @@ public class MessageController {
} }
@Operation(summary = "根据消息id查询消息详情", description = "根据消息id查询消息详情") @Operation(summary = "根据消息id查询消息详情", description = "根据消息id查询消息详情")
@GetMapping("/noManage/getMessageDetailById") @GetMapping("noManage/getMessageDetailById")
public Mono<Result<MessageVo>> getMessageDetailById(Long id) { public Mono<Result<MessageVo>> getMessageDetailById(Long id) {
MessageVo vo = messageService.getMessageDetailById(id); MessageVo vo = messageService.getMessageDetailById(id);
return Mono.just(Result.success(vo)); return Mono.just(Result.success(vo));
} }
@Operation(summary = "添加系统消息", description = "添加系统消息")
@PostMapping("addMessage")
public Mono<Result<String>> addMessage(@Valid @RequestBody MessageAddDto dto) {
messageService.addMessage(dto);
return Mono.just(Result.success(ResultCodeEnum.ADD_SUCCESS));
}
@Operation(summary = "更新系统消息", description = "更新系统消息")
@PutMapping("updateMessage")
public Mono<Result<String>> updateMessage(@Valid @RequestBody MessageUpdateDto dto) {
messageService.updateMessage(dto);
return Mono.just(Result.success(ResultCodeEnum.UPDATE_SUCCESS));
}
@Operation(summary = "用户将消息标为已读", description = "用户将消息标为已读")
@PutMapping("/noManage/updateUserMarkAsRead")
public Mono<Result<String>> updateUserMarkAsRead(@Valid @RequestBody List<Long> ids) {
messageService.updateUserMarkAsRead(ids);
return Mono.just(Result.success(ResultCodeEnum.UPDATE_SUCCESS));
}
@Operation(summary = "删除系统消息", description = "删除系统消息") @Operation(summary = "删除系统消息", description = "删除系统消息")
@DeleteMapping("deleteMessage") @DeleteMapping("deleteMessage")
public Mono<Result<String>> deleteMessage(@RequestBody List<Long> ids) { public Mono<Result<String>> deleteMessage(@RequestBody List<Long> ids) {
@ -100,7 +76,7 @@ public class MessageController {
} }
@Operation(summary = "用户删除消息", description = "用户删除消息") @Operation(summary = "用户删除消息", description = "用户删除消息")
@DeleteMapping("/noManage/deleteUserMessageByIds") @DeleteMapping("noManage/deleteUserMessageByIds")
public Mono<Result<String>> deleteUserMessageByIds(@RequestBody List<Long> ids) { public Mono<Result<String>> deleteUserMessageByIds(@RequestBody List<Long> ids) {
messageService.deleteUserMessageByIds(ids); messageService.deleteUserMessageByIds(ids);
return Mono.just(Result.success(ResultCodeEnum.DELETE_SUCCESS)); return Mono.just(Result.success(ResultCodeEnum.DELETE_SUCCESS));

View File

@ -1,14 +1,16 @@
package cn.bunny.services.controller; package cn.bunny.services.controller;
import cn.bunny.dao.dto.system.message.MessageAddDto;
import cn.bunny.dao.dto.system.message.MessageUpdateDto;
import cn.bunny.dao.pojo.result.Result; import cn.bunny.dao.pojo.result.Result;
import cn.bunny.dao.pojo.result.ResultCodeEnum;
import cn.bunny.dao.vo.system.message.MessageReceivedWithUserVo; import cn.bunny.dao.vo.system.message.MessageReceivedWithUserVo;
import cn.bunny.services.service.MessageReceivedService; import cn.bunny.services.service.MessageReceivedService;
import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag; import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Mono; import reactor.core.publisher.Mono;
import java.util.List; import java.util.List;
@ -35,4 +37,25 @@ public class MessageReceivedController {
List<MessageReceivedWithUserVo> voList = messageReceivedService.getReceivedUserinfoByMessageId(messageId); List<MessageReceivedWithUserVo> voList = messageReceivedService.getReceivedUserinfoByMessageId(messageId);
return Mono.just(Result.success(voList)); return Mono.just(Result.success(voList));
} }
@Operation(summary = "添加系统消息", description = "添加系统消息")
@PostMapping("addMessage")
public Mono<Result<String>> addMessage(@Valid @RequestBody MessageAddDto dto) {
messageReceivedService.addMessage(dto);
return Mono.just(Result.success(ResultCodeEnum.ADD_SUCCESS));
}
@Operation(summary = "更新系统消息", description = "更新系统消息")
@PutMapping("updateMessage")
public Mono<Result<String>> updateMessage(@Valid @RequestBody MessageUpdateDto dto) {
messageReceivedService.updateMessage(dto);
return Mono.just(Result.success(ResultCodeEnum.UPDATE_SUCCESS));
}
@Operation(summary = "用户将消息标为已读", description = "用户将消息标为已读")
@PutMapping("noManage/updateUserMarkAsRead")
public Mono<Result<String>> updateUserMarkAsRead(@Valid @RequestBody List<Long> ids) {
messageReceivedService.updateUserMarkAsRead(ids);
return Mono.just(Result.success(ResultCodeEnum.UPDATE_SUCCESS));
}
} }

View File

@ -46,7 +46,7 @@ public interface MessageMapper extends BaseMapper<Message> {
* @param dto 系统消息查询表单 * @param dto 系统消息查询表单
* @return 系统消息分页结果 * @return 系统消息分页结果
*/ */
IPage<Message> selectListByPageWithMessageUserDto(@Param("page") Page<Message> pageParams, @Param("dto") MessageUserDto dto, @Param("messageIds") List<Long> messageIds); IPage<MessageVo> selectListByPageWithMessageUserDto(@Param("page") Page<Message> pageParams, @Param("dto") MessageUserDto dto, @Param("userId") Long userId);
/** /**
* 根据消息id查询消息详情 * 根据消息id查询消息详情

View File

@ -19,32 +19,24 @@ import java.util.List;
public interface MessageReceivedMapper extends BaseMapper<MessageReceived> { public interface MessageReceivedMapper extends BaseMapper<MessageReceived> {
/** /**
* 根据发送者id批量删除消息接受者 * 根据id批量删除
* *
* @param userIds 发送用户ID * @param userIds 发送用户ID
*/ */
void deleteBatchIdsWithPhysics(List<Long> userIds); void deleteBatchIdsWithPhysics(List<Long> userIds);
/** /**
* 根据消息Id物理删除接受者消息 * 根据消息Id物理删除
* *
* @param ids 消息id * @param ids 消息id
*/ */
void deleteBatchIdsByMessageIdsWithPhysics(List<Long> ids); void deleteBatchIdsByMessageIdsWithPhysics(List<Long> ids);
/** /**
* 根据发送者id批量删除消息接受者 * 根据发送者id批量删除
* *
* @param messageId 消息id * @param messageId 消息id
* @return 消息接收人用户名等信息 * @return 消息接收人用户名等信息
*/ */
List<MessageReceivedWithUserVo> selectUserinfoListByMessageId(Long messageId); List<MessageReceivedWithUserVo> selectUserinfoListByMessageId(Long messageId);
/**
* 根据当前用户id删除消息接受表中数据
*
* @param ids 消息列表
* @param userId 用户id
*/
void deleteBatchIdsByMessageIdsAndUserIdWithPhysics(List<Long> ids, Long userId);
} }

View File

@ -1,8 +1,11 @@
package cn.bunny.services.service; package cn.bunny.services.service;
import cn.bunny.dao.dto.system.message.MessageAddDto;
import cn.bunny.dao.dto.system.message.MessageUpdateDto;
import cn.bunny.dao.entity.system.MessageReceived; import cn.bunny.dao.entity.system.MessageReceived;
import cn.bunny.dao.vo.system.message.MessageReceivedWithUserVo; import cn.bunny.dao.vo.system.message.MessageReceivedWithUserVo;
import com.baomidou.mybatisplus.extension.service.IService; import com.baomidou.mybatisplus.extension.service.IService;
import jakarta.validation.Valid;
import java.util.List; import java.util.List;
@ -30,4 +33,25 @@ public interface MessageReceivedService extends IService<MessageReceived> {
* @return 消息接收人用户名等信息 * @return 消息接收人用户名等信息
*/ */
List<MessageReceivedWithUserVo> getReceivedUserinfoByMessageId(Long messageId); List<MessageReceivedWithUserVo> getReceivedUserinfoByMessageId(Long messageId);
/**
* * 添加系统消息
*
* @param dto 添加表单
*/
void addMessage(@Valid MessageAddDto dto);
/**
* 用户将消息标为已读
*
* @param ids 消息id列表
*/
void updateUserMarkAsRead(List<Long> ids);
/**
* * 更新系统消息
*
* @param dto 更新表单
*/
void updateMessage(@Valid MessageUpdateDto dto);
} }

View File

@ -1,8 +1,6 @@
package cn.bunny.services.service; package cn.bunny.services.service;
import cn.bunny.dao.dto.system.message.MessageAddDto;
import cn.bunny.dao.dto.system.message.MessageDto; import cn.bunny.dao.dto.system.message.MessageDto;
import cn.bunny.dao.dto.system.message.MessageUpdateDto;
import cn.bunny.dao.dto.system.message.MessageUserDto; import cn.bunny.dao.dto.system.message.MessageUserDto;
import cn.bunny.dao.entity.system.Message; import cn.bunny.dao.entity.system.Message;
import cn.bunny.dao.pojo.result.PageResult; import cn.bunny.dao.pojo.result.PageResult;
@ -10,7 +8,6 @@ import cn.bunny.dao.vo.system.message.MessageUserVo;
import cn.bunny.dao.vo.system.message.MessageVo; import cn.bunny.dao.vo.system.message.MessageVo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService; import com.baomidou.mybatisplus.extension.service.IService;
import jakarta.validation.Valid;
import java.util.List; import java.util.List;
@ -31,20 +28,6 @@ public interface MessageService extends IService<Message> {
*/ */
PageResult<MessageVo> getMessageList(Page<Message> pageParams, MessageDto dto); PageResult<MessageVo> getMessageList(Page<Message> pageParams, MessageDto dto);
/**
* * 添加系统消息
*
* @param dto 添加表单
*/
void addMessage(@Valid MessageAddDto dto);
/**
* * 更新系统消息
*
* @param dto 更新表单
*/
void updateMessage(@Valid MessageUpdateDto dto);
/** /**
* * 删除|批量删除系统消息类型 * * 删除|批量删除系统消息类型
* *
@ -69,13 +52,6 @@ public interface MessageService extends IService<Message> {
*/ */
MessageVo getMessageDetailById(Long id); MessageVo getMessageDetailById(Long id);
/**
* 用户将消息标为已读
*
* @param ids 消息id列表
*/
void updateUserMarkAsRead(List<Long> ids);
/** /**
* 用户删除消息 * 用户删除消息
* *

View File

@ -1,12 +1,23 @@
package cn.bunny.services.service.impl; package cn.bunny.services.service.impl;
import cn.bunny.common.service.context.BaseContext;
import cn.bunny.common.service.exception.BunnyException; import cn.bunny.common.service.exception.BunnyException;
import cn.bunny.dao.common.entity.BaseEntity;
import cn.bunny.dao.dto.system.message.MessageAddDto;
import cn.bunny.dao.dto.system.message.MessageUpdateDto;
import cn.bunny.dao.entity.system.Message;
import cn.bunny.dao.entity.system.MessageReceived; import cn.bunny.dao.entity.system.MessageReceived;
import cn.bunny.dao.pojo.result.ResultCodeEnum; import cn.bunny.dao.pojo.result.ResultCodeEnum;
import cn.bunny.dao.vo.system.message.MessageReceivedWithUserVo; import cn.bunny.dao.vo.system.message.MessageReceivedWithUserVo;
import cn.bunny.services.factory.UserFactory;
import cn.bunny.services.mapper.MessageMapper;
import cn.bunny.services.mapper.MessageReceivedMapper; import cn.bunny.services.mapper.MessageReceivedMapper;
import cn.bunny.services.mapper.UserMapper;
import cn.bunny.services.service.MessageReceivedService; import cn.bunny.services.service.MessageReceivedService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.validation.Valid;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
@ -24,15 +35,13 @@ import java.util.List;
@Transactional @Transactional
public class MessageReceivedServiceImpl extends ServiceImpl<MessageReceivedMapper, MessageReceived> implements MessageReceivedService { public class MessageReceivedServiceImpl extends ServiceImpl<MessageReceivedMapper, MessageReceived> implements MessageReceivedService {
/** @Autowired
* 根据发送者id批量删除消息接受者 private UserMapper userMapper;
*
* @param sendUserIds 发送用户ID @Autowired
*/ private MessageMapper messageMapper;
@Override @Autowired
public void deleteBatchIdsWithPhysics(List<Long> sendUserIds) { private UserFactory userFactory;
baseMapper.deleteBatchIdsWithPhysics(sendUserIds);
}
/** /**
* 根据消息id获取接收人信息 * 根据消息id获取接收人信息
@ -47,4 +56,128 @@ public class MessageReceivedServiceImpl extends ServiceImpl<MessageReceivedMappe
} }
return baseMapper.selectUserinfoListByMessageId(messageId); return baseMapper.selectUserinfoListByMessageId(messageId);
} }
/**
* 添加系统消息
* 判断发送消息的接收人是否为空如果为空默认是所有用户都是接受者
* 之后要将消息的接受者要保存在消息接收表中在这之前是没有消息id的
* 先要保存消息内容之后获取到保存消息的id
* 将消息的id和接收者的id构建成map插入到消息接收表中
*
* @param dto 系统消息添加
*/
@Override
public void addMessage(@Valid MessageAddDto dto) {
// 如果发送人为空设置当前登录的人的ID
if (dto.getSendUserId() == null) dto.setSendUserId(BaseContext.getUserId());
// 设置封面返回内容
String cover = dto.getCover();
dto.setCover(userFactory.checkGetUserAvatar(cover));
// 先保存消息数据之后拿到保存消息的id
Message message = new Message();
BeanUtils.copyProperties(dto, message);
message.setMessageType(dto.getMessageTypeId().toString());
messageMapper.insert(message);
// 如果接收人为空默认接收全部人
List<Long> receivedUserIds = dto.getReceivedUserIds();
if (receivedUserIds.isEmpty()) {
receivedUserIds = userMapper.selectList(null).stream().map(BaseEntity::getId).toList();
}
// 从之前保存的消息中获取消息id保存到消息接收表中
List<MessageReceived> receivedList = receivedUserIds.stream().map(id -> {
MessageReceived messageReceived = new MessageReceived();
messageReceived.setMessageId(message.getId());
messageReceived.setReceivedUserId(id);
messageReceived.setStatus(false);
return messageReceived;
}).toList();
saveBatch(receivedList);
}
/**
* 更新系统消息
*
* @param dto 系统消息更新
*/
@Override
public void updateMessage(@Valid MessageUpdateDto dto) {
// 如果发送人为空设置当前登录的人的ID
Long sendUserId = dto.getSendUserId();
if (sendUserId == null) dto.setSendUserId(BaseContext.getUserId());
// 如果接收人为空默认接收全部人
List<Long> receivedUserIds = dto.getReceivedUserIds();
if (receivedUserIds.isEmpty()) {
receivedUserIds = userMapper.selectList(null).stream().map(BaseEntity::getId).toList();
}
// 设置封面返回内容
String cover = dto.getCover();
dto.setCover(userFactory.checkGetUserAvatar(cover));
// 更新内容
Message message = new Message();
BeanUtils.copyProperties(dto, message);
message.setMessageType(dto.getMessageTypeId().toString());
messageMapper.updateById(message);
// 保存消息和用户之间关联数据
List<MessageReceived> receivedList = receivedUserIds.stream().map(id -> {
MessageReceived messageReceived = new MessageReceived();
messageReceived.setMessageId(dto.getId());
messageReceived.setReceivedUserId(id);
// 当更新的时当前消息用户表是否已读
if (id.equals(dto.getReceivedUserId())) {
messageReceived.setStatus(dto.getStatus());
}
return messageReceived;
}).toList();
// 删除这个消息id下所有用户消息关系内容
baseMapper.deleteBatchIdsByMessageIdsWithPhysics(List.of(dto.getId()));
// 插入接收者和消息表
saveBatch(receivedList);
}
/**
* 用户将消息标为已读
* 将消息表中满足id条件全部标为已读
*
* @param ids 消息id列表
*/
@Override
public void updateUserMarkAsRead(List<Long> ids) {
// 判断ids是否为空
if (ids.isEmpty()) {
throw new BunnyException(ResultCodeEnum.REQUEST_IS_EMPTY);
}
// 更新表中消息状态
List<MessageReceived> messageList = ids.stream().map(id -> {
MessageReceived messageReceived = new MessageReceived();
messageReceived.setId(id);
messageReceived.setStatus(true);
return messageReceived;
}).toList();
updateBatchById(messageList);
}
/**
* 根据发送者id批量删除消息接受者
*
* @param sendUserIds 发送用户ID
*/
@Override
public void deleteBatchIdsWithPhysics(List<Long> sendUserIds) {
baseMapper.deleteBatchIdsWithPhysics(sendUserIds);
}
} }

View File

@ -2,10 +2,7 @@ package cn.bunny.services.service.impl;
import cn.bunny.common.service.context.BaseContext; import cn.bunny.common.service.context.BaseContext;
import cn.bunny.common.service.exception.BunnyException; import cn.bunny.common.service.exception.BunnyException;
import cn.bunny.dao.common.entity.BaseEntity;
import cn.bunny.dao.dto.system.message.MessageAddDto;
import cn.bunny.dao.dto.system.message.MessageDto; import cn.bunny.dao.dto.system.message.MessageDto;
import cn.bunny.dao.dto.system.message.MessageUpdateDto;
import cn.bunny.dao.dto.system.message.MessageUserDto; import cn.bunny.dao.dto.system.message.MessageUserDto;
import cn.bunny.dao.entity.system.Message; import cn.bunny.dao.entity.system.Message;
import cn.bunny.dao.entity.system.MessageReceived; import cn.bunny.dao.entity.system.MessageReceived;
@ -16,20 +13,17 @@ import cn.bunny.dao.vo.system.message.MessageVo;
import cn.bunny.services.factory.UserFactory; import cn.bunny.services.factory.UserFactory;
import cn.bunny.services.mapper.MessageMapper; import cn.bunny.services.mapper.MessageMapper;
import cn.bunny.services.mapper.MessageReceivedMapper; import cn.bunny.services.mapper.MessageReceivedMapper;
import cn.bunny.services.mapper.UserMapper;
import cn.bunny.services.service.MessageReceivedService;
import cn.bunny.services.service.MessageService; import cn.bunny.services.service.MessageService;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.validation.Valid;
import org.springframework.beans.BeanUtils; import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList;
import java.util.List; import java.util.List;
/** /**
@ -44,15 +38,9 @@ import java.util.List;
@Transactional @Transactional
public class MessageServiceImpl extends ServiceImpl<MessageMapper, Message> implements MessageService { public class MessageServiceImpl extends ServiceImpl<MessageMapper, Message> implements MessageService {
@Autowired
private UserMapper userMapper;
@Autowired @Autowired
private UserFactory userFactory; private UserFactory userFactory;
@Autowired
private MessageReceivedService messageReceivedService;
@Autowired @Autowired
private MessageReceivedMapper messageReceivedMapper; private MessageReceivedMapper messageReceivedMapper;
@ -77,43 +65,25 @@ public class MessageServiceImpl extends ServiceImpl<MessageMapper, Message> impl
vo.setCover(cover); vo.setCover(cover);
return vo; return vo;
}).toList(); }).toList();
return PageResult.<MessageVo>builder() return PageResult.<MessageVo>builder().list(voList).pageNo(page.getCurrent())
.list(voList) .pageSize(page.getSize()).total(page.getTotal())
.pageNo(page.getCurrent())
.pageSize(page.getSize())
.total(page.getTotal())
.build(); .build();
} }
/** /**
* 分页查询用户消息 * 分页查询用户消息
* 用户在主页查询内容为是否已读和消息类型 * 查询用户消息关系表找到当前用户所有的消息
* 为了方便用户之后进入消息详情页面查询按照消息类型查询消息 * 拿到用户消息关系表中数据只要MessageId
* 也可以查询消息是否已读 * 根据MessageId分页查询消息表
* 节省带宽的情况下传给前端消息详情等内容
* *
* @param pageParams 系统消息返回列表 * @param pageParams 系统消息返回列表
* @param dto 用户消息查询内容" * @param dto 用户消息查询内容
* @return 分页结果 * @return 分页结果
*/ */
@Override @Override
public PageResult<MessageUserVo> getUserMessageList(Page<Message> pageParams, MessageUserDto dto) { public PageResult<MessageUserVo> getUserMessageList(Page<Message> pageParams, MessageUserDto dto) {
// 查询当前用户需要接收的消息id
List<MessageReceived> messageReceivedList = messageReceivedMapper.selectList(Wrappers.<MessageReceived>lambdaQuery().eq(MessageReceived::getReceivedUserId, BaseContext.getUserId()));
List<Long> messageIds = messageReceivedList.stream().map(MessageReceived::getMessageId).toList();
// 消息为空直接返回
if (messageIds.isEmpty()) {
return PageResult.<MessageUserVo>builder()
.list(new ArrayList<>())
.pageNo(pageParams.getCurrent())
.pageSize(pageParams.getSize())
.total(pageParams.getTotal())
.build();
}
// 根据消息所有包含匹配当前消息Id的列表 // 根据消息所有包含匹配当前消息Id的列表
IPage<Message> page = baseMapper.selectListByPageWithMessageUserDto(pageParams, dto, messageIds); IPage<MessageVo> page = baseMapper.selectListByPageWithMessageUserDto(pageParams, dto, BaseContext.getUserId());
List<MessageUserVo> voList = page.getRecords().stream().map(messageVo -> { List<MessageUserVo> voList = page.getRecords().stream().map(messageVo -> {
MessageUserVo vo = new MessageUserVo(); MessageUserVo vo = new MessageUserVo();
BeanUtils.copyProperties(messageVo, vo); BeanUtils.copyProperties(messageVo, vo);
@ -125,11 +95,8 @@ public class MessageServiceImpl extends ServiceImpl<MessageMapper, Message> impl
return vo; return vo;
}).toList(); }).toList();
return PageResult.<MessageUserVo>builder() return PageResult.<MessageUserVo>builder().list(voList).pageNo(page.getCurrent())
.list(voList) .pageSize(page.getSize()).total(page.getTotal())
.pageNo(page.getCurrent())
.pageSize(page.getSize())
.total(page.getTotal())
.build(); .build();
} }
@ -143,39 +110,21 @@ public class MessageServiceImpl extends ServiceImpl<MessageMapper, Message> impl
@Override @Override
public MessageVo getMessageDetailById(Long id) { public MessageVo getMessageDetailById(Long id) {
// 将消息设为已读 // 将消息设为已读
Message message = new Message(); MessageReceived messageReceived = new MessageReceived();
message.setId(id); messageReceived.setReceivedUserId(BaseContext.getUserId());
message.setStatus(true); messageReceived.setMessageId(id);
updateById(message); messageReceived.setStatus(true);
// 更新满足条件的消息用户表
LambdaUpdateWrapper<MessageReceived> wrapper = Wrappers.<MessageReceived>lambdaUpdate()
.eq(MessageReceived::getMessageId, id)
.eq(MessageReceived::getReceivedUserId, BaseContext.getUserId());
messageReceivedMapper.update(messageReceived, wrapper);
// 返回详情内容给前端 // 返回详情内容给前端
return baseMapper.selectMessageVoById(id); return baseMapper.selectMessageVoById(id);
} }
/**
* 用户将消息标为已读
* 将消息表中满足id条件全部标为已读
*
* @param ids 消息id列表
*/
@Override
public void updateUserMarkAsRead(List<Long> ids) {
// 判断ids是否为空
if (ids.isEmpty()) {
throw new BunnyException(ResultCodeEnum.REQUEST_IS_EMPTY);
}
// 更新表中消息状态
List<Message> messageList = ids.stream().map(id -> {
Message message = new Message();
message.setId(id);
message.setStatus(true);
return message;
}).toList();
updateBatchById(messageList);
}
/** /**
* 用户删除消息 * 用户删除消息
* *
@ -188,85 +137,8 @@ public class MessageServiceImpl extends ServiceImpl<MessageMapper, Message> impl
throw new BunnyException(ResultCodeEnum.REQUEST_IS_EMPTY); throw new BunnyException(ResultCodeEnum.REQUEST_IS_EMPTY);
} }
// 删除消息表中数据
baseMapper.deleteBatchIdsWithPhysics(ids);
// 根据当前用户id删除消息接受表中数据 // 根据当前用户id删除消息接受表中数据
Long userId = BaseContext.getUserId(); messageReceivedMapper.deleteBatchIdsWithPhysics(ids);
messageReceivedMapper.deleteBatchIdsByMessageIdsAndUserIdWithPhysics(ids, userId);
}
/**
* 添加系统消息
* 判断发送消息的接收人是否为空如果为空默认是所有用户都是接受者
* 之后要将消息的接受者要保存在消息接收表中在这之前是没有消息id的
* 先要保存消息内容之后获取到保存消息的id
* 将消息的id和接收者的id构建成map插入到消息接收表中
*
* @param dto 系统消息添加
*/
@Override
public void addMessage(@Valid MessageAddDto dto) {
// 如果发送人为空设置当前登录的人的ID
if (dto.getSendUserId() == null) dto.setSendUserId(BaseContext.getUserId());
// 先保存消息数据之后拿到保存消息的id
Message message = new Message();
BeanUtils.copyProperties(dto, message);
save(message);
// 如果接收人为空默认接收全部人
List<Long> receivedUserIds = dto.getReceivedUserIds();
if (receivedUserIds.isEmpty()) {
receivedUserIds = userMapper.selectList(null).stream().map(BaseEntity::getId).toList();
}
// 从之前保存的消息中获取消息id保存到消息接收表中
List<MessageReceived> receivedList = receivedUserIds.stream().map(id -> {
MessageReceived messageReceived = new MessageReceived();
messageReceived.setMessageId(message.getId());
messageReceived.setReceivedUserId(id);
return messageReceived;
}).toList();
messageReceivedService.saveBatch(receivedList);
}
/**
* 更新系统消息
*
* @param dto 系统消息更新
*/
@Override
public void updateMessage(@Valid MessageUpdateDto dto) {
// 如果发送人为空设置当前登录的人的ID
Long sendUserId = dto.getSendUserId();
if (sendUserId == null) dto.setSendUserId(BaseContext.getUserId());
// 如果接收人为空默认接收全部人
List<Long> receivedUserIds = dto.getReceivedUserIds();
if (receivedUserIds.isEmpty()) {
receivedUserIds = userMapper.selectList(null).stream().map(BaseEntity::getId).toList();
}
// 更新内容
Message message = new Message();
BeanUtils.copyProperties(dto, message);
updateById(message);
// 保存消息和用户之间关联数据
List<MessageReceived> receivedList = receivedUserIds.stream().map(id -> {
MessageReceived messageReceived = new MessageReceived();
messageReceived.setMessageId(dto.getId());
messageReceived.setReceivedUserId(id);
return messageReceived;
}).toList();
// 删除这个消息接受下所有发送者
messageReceivedMapper.deleteBatchIdsByMessageIdsWithPhysics(List.of(dto.getId()));
// 插入接收者和消息表
messageReceivedService.saveBatch(receivedList);
} }
/** /**

View File

@ -17,28 +17,49 @@
<id column="cover" property="cover"/> <id column="cover" property="cover"/>
<id column="summary" property="summary"/> <id column="summary" property="summary"/>
<id column="editor_type" property="editorType"/> <id column="editor_type" property="editorType"/>
<id column="status" property="status"/>
<id column="level" property="level"/> <id column="level" property="level"/>
<id column="extra" property="extra"/> <id column="extra" property="extra"/>
</resultMap> </resultMap>
<!-- 通用查询结果列 --> <!-- 通用查询结果列 -->
<sql id="Base_Column_List"> <sql id="Base_Column_List">
id id, create_time, update_time, create_user, update_user, is_deleted, title, send_user_id, message_type, content, cover, summary, editor_type, level,extra
, create_time, update_time, create_user, update_user, is_deleted, title, send_user_id, message_type, content, cover, summary, editor_type, status,level,extra
</sql> </sql>
<!-- 分页查询系统消息内容 --> <!-- 分页查询系统消息内容 -->
<select id="selectListByPage" resultType="cn.bunny.dao.vo.system.message.MessageVo"> <select id="selectListByPage" resultType="cn.bunny.dao.vo.system.message.MessageVo">
select SELECT
message.*, message.id,
create_user.username as create_username, message.create_time,
update_user.username as update_username, message.update_time,
send_user.nickname as send_nickname message.create_user,
from sys_message message message.update_user,
left join sys_user create_user on create_user.id = message.create_user message.is_deleted,
left join sys_user update_user on update_user.id = message.update_user message.title,
left join sys_user send_user on send_user.id = message.send_user_id message.send_user_id,
message.content,
message.cover,
message.summary,
message.editor_type,
message.level,
message.extra,
message_received.status AS status,
message_received.received_user_id AS received_user_id,
message_received.id AS message_received_id,
message_type.message_name AS message_type,
message_type.id AS message_type_id,
received_user.nickname received_user_nickname,
create_user.username AS create_username,
update_user.username AS update_username,
send_user.nickname AS send_nickname
FROM
sys_message message
LEFT JOIN sys_user create_user ON create_user.id = message.create_user
LEFT JOIN sys_user update_user ON update_user.id = message.update_user
LEFT JOIN sys_user send_user ON send_user.id = message.send_user_id
LEFT JOIN sys_message_received message_received ON message_received.message_id = message.id
LEFT JOIN sys_user received_user ON message_received.received_user_id = received_user.id
LEFT JOIN sys_message_type message_type ON message_type.id = message.message_type
<where> <where>
message.is_deleted = 0 message.is_deleted = 0
<if test="dto.title != null and dto.title != ''"> <if test="dto.title != null and dto.title != ''">
@ -48,7 +69,7 @@
and send_user.nickname like CONCAT('%',#{dto.sendNickname},'%') and send_user.nickname like CONCAT('%',#{dto.sendNickname},'%')
</if> </if>
<if test="dto.messageType != null and dto.messageType != ''"> <if test="dto.messageType != null and dto.messageType != ''">
and message.message_type like CONCAT('%',#{dto.messageType},'%') and message_type.message_type like CONCAT('%',#{dto.messageType},'%')
</if> </if>
<if test="dto.content != null and dto.content != ''"> <if test="dto.content != null and dto.content != ''">
and message.content like CONCAT('%',#{dto.content},'%') and message.content like CONCAT('%',#{dto.content},'%')
@ -56,9 +77,6 @@
<if test="dto.editorType != null and dto.editorType != ''"> <if test="dto.editorType != null and dto.editorType != ''">
and message.editor_type = #{dto.editorType} and message.editor_type = #{dto.editorType}
</if> </if>
<if test="dto.editorType != null and dto.editorType != ''">
and message.editor_type = #{dto.editorType}
</if>
<if test="dto.level != null and dto.level != ''"> <if test="dto.level != null and dto.level != ''">
and message.level = #{dto.level} and message.level = #{dto.level}
</if> </if>
@ -66,33 +84,37 @@
and message.extra like CONCAT('%',#{dto.extra},'%') and message.extra like CONCAT('%',#{dto.extra},'%')
</if> </if>
<if test="dto.status != null"> <if test="dto.status != null">
and message.status = #{dto.status} and message_received.status = #{dto.status}
</if> </if>
</where> </where>
</select> </select>
<!-- 根据消息所有包含匹配当前消息Id的列表 --> <!-- 根据消息所有包含匹配当前消息Id的列表 -->
<select id="selectListByPageWithMessageUserDto" resultType="cn.bunny.dao.entity.system.Message"> <select id="selectListByPageWithMessageUserDto" resultType="cn.bunny.dao.vo.system.message.MessageVo">
select SELECT
message.*, message.id,
create_user.username as create_username, message.title,
update_user.username as update_username message.cover,
from sys_message message message.summary,
left join sys_user create_user on create_user.id = message.create_user message.level,
left join sys_user update_user on update_user.id = message.update_user message.extra,
message.update_time,
message_received.id AS message_received_id,
message_received.status AS status,
message_type.message_type AS message_type
FROM sys_message message
LEFT JOIN sys_user create_user ON create_user.id = message.create_user
LEFT JOIN sys_user update_user ON update_user.id = message.update_user
LEFT JOIN sys_message_type message_type ON message_type.id = message.message_type
INNER JOIN sys_message_received message_received ON message_received.received_user_id = #{userId}
AND message.id = message_received.message_id
<where> <where>
message.is_deleted = 0 message.is_deleted = 0
<if test="messageIds != null ">
and message.id in
<foreach collection="messageIds" item="id" open="(" close=")" separator=",">
#{id}
</foreach>
</if>
<if test="dto.status != null"> <if test="dto.status != null">
and message.status = #{dto.status} and message_received.status = #{dto.status}
</if> </if>
<if test="dto.messageType != null and dto.messageType != ''"> <if test="dto.messageType != null and dto.messageType != ''">
and message.message_type = #{dto.messageType} and message_type.message_type = #{dto.messageType}
</if> </if>
<if test="dto.title != null and dto.title != ''"> <if test="dto.title != null and dto.title != ''">
and message.title like CONCAT('%',#{dto.title},'%') and message.title like CONCAT('%',#{dto.title},'%')

View File

@ -18,7 +18,7 @@
<delete id="deleteBatchIdsWithPhysics"> <delete id="deleteBatchIdsWithPhysics">
delete delete
from sys_message_received from sys_message_received
where received_user_id in where id in
<foreach collection="userIds" item="id" open="(" close=")" separator=","> <foreach collection="userIds" item="id" open="(" close=")" separator=",">
#{id} #{id}
</foreach> </foreach>
@ -34,16 +34,6 @@
</foreach> </foreach>
</delete> </delete>
<!-- 根据当前用户id删除消息接受表中数据 -->
<delete id="deleteBatchIdsByMessageIdsAndUserIdWithPhysics">
delete
from sys_message_received where
received_user_id = #{userId} and received_user_id in
<foreach collection="ids" item="id" open="(" close=")" separator=",">
#{id}
</foreach>
</delete>
<!-- 根据发送者id批量删除消息接受者 --> <!-- 根据发送者id批量删除消息接受者 -->
<select id="selectUserinfoListByMessageId" <select id="selectUserinfoListByMessageId"
resultType="cn.bunny.dao.vo.system.message.MessageReceivedWithUserVo"> resultType="cn.bunny.dao.vo.system.message.MessageReceivedWithUserVo">