feat: 消息页面待完善

This commit is contained in:
Bunny 2024-11-01 00:09:11 +08:00
parent 4ebc7c3a69
commit 9a7303c596
18 changed files with 389 additions and 22 deletions

View File

@ -14,13 +14,13 @@ public class AdminCodeGenerator {
// 作者名称
public static final String author = "Bunny";
// 公共路径
public static final String outputDir = "D:\\MyFolder\\auth-admin\\auth-server-java\\service";
// public static final String outputDir = "D:\\Project\\web\\PC\\auth\\auth-server-java\\service";
// public static final String outputDir = "D:\\MyFolder\\auth-admin\\auth-server-java\\service";
public static final String outputDir = "D:\\Project\\web\\PC\\auth\\auth-server-java\\service";
// 实体类名称
public static final String entity = "Bunny";
public static void main(String[] args) {
Generation("sys_message", "sys_message_type");
Generation("sys_message_received");
}
/**

View File

@ -54,4 +54,12 @@ public class MessageAddDto {
@Schema(name = "status", title = "0:未读 1:已读")
private Boolean status = false;
@Schema(name = "level", title = "消息等级")
private String level;
@Schema(name = "extra", title = "消息等级详情")
private String extra;
}

View File

@ -31,4 +31,10 @@ public class MessageDto {
@Schema(name = "status", title = "0:未读 1:已读")
private Boolean status;
@Schema(name = "level", title = "消息等级")
private String level;
@Schema(name = "extra", title = "消息等级详情")
private String extra;
}

View File

@ -59,5 +59,11 @@ public class MessageUpdateDto {
@Schema(name = "status", title = "0:未读 1:已读")
private Boolean status;
@Schema(name = "level", title = "消息等级")
private String level;
@Schema(name = "extra", title = "消息等级详情")
private String extra;
}

View File

@ -25,9 +25,6 @@ public class Message extends BaseEntity {
@Schema(name = "title", title = "消息标题")
private String title;
@Schema(name = "receivedUserIds", title = "接收人用户ID")
private String receivedUserIds;
@Schema(name = "sendUserId", title = "发送人用户ID")
private Long sendUserId;
@ -39,7 +36,7 @@ public class Message extends BaseEntity {
@Schema(name = "summary", title = "消息简介")
private String summary;
@Schema(name = "content", title = "消息内容")
private String content;
@ -49,6 +46,12 @@ public class Message extends BaseEntity {
@Schema(name = "status", title = "0:未读 1:已读")
private Boolean status;
@Schema(name = "level", title = "消息等级")
private String level;
@Schema(name = "extra", title = "消息等级详情")
private String extra;
}

View File

@ -0,0 +1,31 @@
package cn.bunny.dao.entity.system;
import cn.bunny.dao.common.entity.BaseEntity;
import com.baomidou.mybatisplus.annotation.TableName;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;
/**
* <p>
*
* </p>
*
* @author Bunny
* @since 2024-10-31
*/
@Getter
@Setter
@Accessors(chain = true)
@TableName("sys_message_received")
@Schema(name = "MessageReceived对象", title = "系统消息接受用户", description = "系统消息接受用户")
public class MessageReceived extends BaseEntity {
@Schema(name = "receivedUserId", title = "接受者id")
private Long receivedUserId;
@Schema(name = "messageId", title = "消息ID")
private Long messageId;
}

View File

@ -0,0 +1,34 @@
package cn.bunny.dao.vo.system.message;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
@Schema(name = "MessageVo对象", title = "系统消息返回内容", description = "系统消息返回内容")
public class MessageUserVo {
@Schema(name = "title", title = "消息标题")
private String title;
@Schema(name = "messageType", title = "sys:系统消息,user用户消息")
private String messageType;
@Schema(name = "cover", title = "封面")
private String cover;
@Schema(name = "summary", title = "消息简介")
private String summary;
@Schema(name = "level", title = "消息等级")
private String level;
@Schema(name = "extra", title = "消息等级详情")
private String extra;
}

View File

@ -7,6 +7,8 @@ import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.*;
import java.util.List;
@EqualsAndHashCode(callSuper = true)
@Data
@AllArgsConstructor
@ -27,7 +29,7 @@ public class MessageVo extends BaseUserVo {
private String sendNickname;
@Schema(name = "receivedUserIds", title = "接收人用户ID")
private String receivedUserIds;
private List<String> receivedUserIds;
@Schema(name = "messageType", title = "sys:系统消息,user用户消息")
private String messageType;
@ -47,4 +49,10 @@ public class MessageVo extends BaseUserVo {
@Schema(name = "status", title = "0:未读 1:已读")
private Boolean status;
@Schema(name = "level", title = "消息等级")
private String level;
@Schema(name = "extra", title = "消息等级详情")
private String extra;
}

View File

@ -7,6 +7,7 @@ import cn.bunny.dao.entity.system.Message;
import cn.bunny.dao.pojo.result.PageResult;
import cn.bunny.dao.pojo.result.Result;
import cn.bunny.dao.pojo.result.ResultCodeEnum;
import cn.bunny.dao.vo.system.message.MessageUserVo;
import cn.bunny.dao.vo.system.message.MessageVo;
import cn.bunny.services.service.MessageService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
@ -36,7 +37,7 @@ public class MessageController {
@Autowired
private MessageService messageService;
@Operation(summary = "分页查询系统消息", description = "分页查询系统消息")
@Operation(summary = "分页查询消息", description = "分页查询消息")
@GetMapping("getMessageList/{page}/{limit}")
public Mono<Result<PageResult<MessageVo>>> getMessageList(
@Parameter(name = "page", description = "当前页", required = true)
@ -49,6 +50,18 @@ public class MessageController {
return Mono.just(Result.success(pageResult));
}
@Operation(summary = "分页查询用户消息", description = "分页查询用户消息")
@GetMapping("getUserMessageList/{page}/{limit}")
public Mono<Result<PageResult<MessageUserVo>>> getUserMessageList(
@Parameter(name = "page", description = "当前页", required = true)
@PathVariable("page") Integer page,
@Parameter(name = "limit", description = "每页记录数", required = true)
@PathVariable("limit") Integer limit) {
Page<Message> pageParams = new Page<>(page, limit);
PageResult<MessageUserVo> pageResult = messageService.getUserMessageList(pageParams);
return Mono.just(Result.success(pageResult));
}
@Operation(summary = "添加系统消息", description = "添加系统消息")
@PostMapping("addMessage")
public Mono<Result<String>> addMessage(@Valid @RequestBody MessageAddDto dto) {

View File

@ -0,0 +1,18 @@
package cn.bunny.services.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* <p>
* 前端控制器
* </p>
*
* @author Bunny
* @since 2024-10-31
*/
@RestController
@RequestMapping("/messageReceived")
public class MessageReceivedController {
}

View File

@ -2,6 +2,7 @@ package cn.bunny.services.mapper;
import cn.bunny.dao.dto.system.message.MessageDto;
import cn.bunny.dao.entity.system.Message;
import cn.bunny.dao.vo.system.message.MessageUserVo;
import cn.bunny.dao.vo.system.message.MessageVo;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
@ -37,4 +38,12 @@ public interface MessageMapper extends BaseMapper<Message> {
* @param ids 删除 id 列表
*/
void deleteBatchIdsWithPhysics(List<Long> ids);
/**
* 分页查询用户消息
*
* @param pageParams 系统消息返回列表
* @return 分页结果
*/
IPage<MessageUserVo> selectUserMessageList(@Param("page") Page<Message> pageParams, Long userId);
}

View File

@ -0,0 +1,33 @@
package cn.bunny.services.mapper;
import cn.bunny.dao.entity.system.MessageReceived;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
/**
* <p>
* Mapper 接口
* </p>
*
* @author Bunny
* @since 2024-10-31
*/
@Mapper
public interface MessageReceivedMapper extends BaseMapper<MessageReceived> {
/**
* 根据发送者id批量删除消息接受者
*
* @param userIds 发送用户ID
*/
void deleteBatchIdsWithPhysics(List<Long> userIds);
/**
* 根据消息Id物理删除接受者消息
*
* @param ids 消息id
*/
void deleteBatchIdsByMessageIdsWithPhysics(List<Long> ids);
}

View File

@ -0,0 +1,24 @@
package cn.bunny.services.service;
import cn.bunny.dao.entity.system.MessageReceived;
import com.baomidou.mybatisplus.extension.service.IService;
import java.util.List;
/**
* <p>
* 服务类
* </p>
*
* @author Bunny
* @since 2024-10-31
*/
public interface MessageReceivedService extends IService<MessageReceived> {
/**
* 根据发送者id批量删除消息接受者
*
* @param sendUserIds 发送用户ID
*/
void deleteBatchIdsWithPhysics(List<Long> sendUserIds);
}

View File

@ -5,6 +5,7 @@ import cn.bunny.dao.dto.system.message.MessageDto;
import cn.bunny.dao.dto.system.message.MessageUpdateDto;
import cn.bunny.dao.entity.system.Message;
import cn.bunny.dao.pojo.result.PageResult;
import cn.bunny.dao.vo.system.message.MessageUserVo;
import cn.bunny.dao.vo.system.message.MessageVo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
@ -49,4 +50,12 @@ public interface MessageService extends IService<Message> {
* @param ids 删除id列表
*/
void deleteMessage(List<Long> ids);
/**
* 分页查询用户消息
*
* @param pageParams 系统消息返回列表
* @return 分页结果
*/
PageResult<MessageUserVo> getUserMessageList(Page<Message> pageParams);
}

View File

@ -0,0 +1,31 @@
package cn.bunny.services.service.impl;
import cn.bunny.dao.entity.system.MessageReceived;
import cn.bunny.services.mapper.MessageReceivedMapper;
import cn.bunny.services.service.MessageReceivedService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* <p>
* 服务实现类
* </p>
*
* @author Bunny
* @since 2024-10-31
*/
@Service
public class MessageReceivedServiceImpl extends ServiceImpl<MessageReceivedMapper, MessageReceived> implements MessageReceivedService {
/**
* 根据发送者id批量删除消息接受者
*
* @param sendUserIds 发送用户ID
*/
@Override
public void deleteBatchIdsWithPhysics(List<Long> sendUserIds) {
baseMapper.deleteBatchIdsWithPhysics(sendUserIds);
}
}

View File

@ -6,20 +6,25 @@ import cn.bunny.dao.dto.system.message.MessageAddDto;
import cn.bunny.dao.dto.system.message.MessageDto;
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.pojo.result.PageResult;
import cn.bunny.dao.vo.system.message.MessageUserVo;
import cn.bunny.dao.vo.system.message.MessageVo;
import cn.bunny.services.factory.UserFactory;
import cn.bunny.services.mapper.MessageMapper;
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 com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.validation.Valid;
import jodd.util.StringUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
@ -32,6 +37,7 @@ import java.util.List;
* @since 2024-10-30 15:19:56
*/
@Service
@Transactional
public class MessageServiceImpl extends ServiceImpl<MessageMapper, Message> implements MessageService {
@Autowired
@ -40,6 +46,12 @@ public class MessageServiceImpl extends ServiceImpl<MessageMapper, Message> impl
@Autowired
private UserFactory userFactory;
@Autowired
private MessageReceivedService messageReceivedService;
@Autowired
private MessageReceivedMapper messageReceivedMapper;
/**
* * 系统消息 服务实现类
*
@ -80,20 +92,28 @@ public class MessageServiceImpl extends ServiceImpl<MessageMapper, Message> impl
Long sendUserId = dto.getSendUserId();
if (sendUserId == null) dto.setSendUserId(BaseContext.getUserId());
// 接受人id列表
List<Long> receivedUserIds = dto.getReceivedUserIds();
// 如果接收人为空默认接收全部人
if (dto.getReceivedUserIds().isEmpty()) {
if (receivedUserIds.isEmpty()) {
List<Long> userIds = userMapper.selectList(null).stream().map(BaseEntity::getId).toList();
dto.setReceivedUserIds(userIds);
}
// 保存数据
// 保存消息数据
Message message = new Message();
BeanUtils.copyProperties(dto, message);
// 将发送用户逗号分隔
String receivedUserIds = StringUtil.join(dto.getReceivedUserIds(), ",");
message.setReceivedUserIds(receivedUserIds);
save(message);
// 保存消息和用户之间关联数据
List<MessageReceived> receivedList = receivedUserIds.stream().map(id -> {
MessageReceived messageReceived = new MessageReceived();
messageReceived.setMessageId(dto.getSendUserId());
messageReceived.setReceivedUserId(id);
return messageReceived;
}).toList();
messageReceivedService.saveBatch(receivedList);
}
/**
@ -103,14 +123,37 @@ public class MessageServiceImpl extends ServiceImpl<MessageMapper, Message> impl
*/
@Override
public void updateMessage(@Valid MessageUpdateDto dto) {
// 如果发送人为空设置当前登录的人的ID
Long sendUserId = dto.getSendUserId();
if (sendUserId == null) dto.setSendUserId(BaseContext.getUserId());
// 接受人id列表
List<Long> receivedUserIds = dto.getReceivedUserIds();
// 如果接收人为空默认接收全部人
if (receivedUserIds.isEmpty()) {
List<Long> userIds = userMapper.selectList(null).stream().map(BaseEntity::getId).toList();
dto.setReceivedUserIds(userIds);
}
// 更新内容
Message message = new Message();
BeanUtils.copyProperties(dto, message);
// 将发送用户逗号分隔
String receivedUserIds = StringUtil.join(dto.getReceivedUserIds(), ",");
message.setReceivedUserIds(receivedUserIds);
updateById(message);
// 删除这个消息接受下所有发送者
if (sendUserId != null) {
messageReceivedMapper.deleteBatchIdsWithPhysics(List.of(sendUserId));
}
// 保存消息和用户之间关联数据
List<MessageReceived> receivedList = receivedUserIds.stream().map(id -> {
MessageReceived messageReceived = new MessageReceived();
messageReceived.setMessageId(dto.getSendUserId());
messageReceived.setReceivedUserId(id);
return messageReceived;
}).toList();
messageReceivedService.saveBatch(receivedList);
}
/**
@ -121,5 +164,40 @@ public class MessageServiceImpl extends ServiceImpl<MessageMapper, Message> impl
@Override
public void deleteMessage(List<Long> ids) {
baseMapper.deleteBatchIdsWithPhysics(ids);
// 根据消息Id物理删除接受者消息
messageReceivedMapper.deleteBatchIdsByMessageIdsWithPhysics(ids);
}
/**
* 分页查询用户消息
*
* @param pageParams 系统消息返回列表
* @return 分页结果
*/
@Override
public PageResult<MessageUserVo> getUserMessageList(Page<Message> pageParams) {
// 查询当前用户接收的消息的接收者关系表
List<MessageReceived> messageReceivedList = messageReceivedMapper.selectList(Wrappers.<MessageReceived>lambdaQuery().eq(MessageReceived::getReceivedUserId, BaseContext.getUserId()));
List<Long> messageIds = messageReceivedList.stream().map(MessageReceived::getMessageId).toList();
// 根据消息所有包含匹配当前消息Id的列表
Page<Message> page = page(pageParams, Wrappers.<Message>lambdaQuery().in(Message::getId, messageIds));
List<MessageUserVo> voList = page.getRecords().stream().map(messageVo -> {
MessageUserVo vo = new MessageUserVo();
BeanUtils.copyProperties(messageVo, vo);
// 设置封面返回内容
String cover = vo.getCover();
cover = userFactory.checkGetUserAvatar(cover);
vo.setCover(cover);
return vo;
}).toList();
return PageResult.<MessageUserVo>builder()
.list(voList)
.pageNo(page.getCurrent())
.pageSize(page.getSize())
.total(page.getTotal())
.build();
}
}

View File

@ -19,11 +19,13 @@
<id column="summary" property="summary"/>
<id column="editor_type" property="editorType"/>
<id column="status" property="status"/>
<id column="level" property="level"/>
<id column="extra" property="extra"/>
</resultMap>
<!-- 通用查询结果列 -->
<sql id="Base_Column_List">
id, create_time, update_time, create_user, update_user, is_deleted, title, received_user_ids, send_user_id, send_nick_name, message_type, content, editor_type, status
id, create_time, update_time, create_user, update_user, is_deleted, title, received_user_ids, send_user_id, send_nick_name, message_type, content, editor_type, status, level, extra
</sql>
<!-- 分页查询系统消息内容 -->
@ -32,11 +34,13 @@
message.*,
create_user.username as create_username,
update_user.username as update_username,
send_user.nickname as send_nickname
send_user.nickname as send_nickname,
message_recived.received_user_id as message_recivedids
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_recived message_recived on message_recived.message_id = message.id
<where>
message.is_deleted = 0
<if test="dto.title != null and dto.title != ''">
@ -54,12 +58,27 @@
<if test="dto.editorType != null and dto.editorType != ''">
and message.editor_type = #{dto.editorType}
</if>
<if test="dto.editorType != null and dto.editorType != ''">
and message.editor_type = #{dto.editorType}
</if>
<if test="dto.level != null and dto.level != ''">
and message.level = #{dto.level}
</if>
<if test="dto.extra != null and dto.extra != ''">
and message.extra = #{dto.extra}
</if>
<if test="dto.status != null">
and message.status = #{dto.status}
</if>
</where>
</select>
<!-- 分页查询用户消息 -->
<select id="selectUserMessageList" resultType="cn.bunny.dao.vo.system.message.MessageUserVo">
</select>
<!-- 物理删除系统消息 -->
<delete id="deleteBatchIdsWithPhysics">
delete

View File

@ -0,0 +1,37 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.bunny.services.mapper.MessageReceivedMapper">
<!-- 通用查询映射结果 -->
<resultMap id="BaseResultMap" type="cn.bunny.dao.entity.system.MessageReceived">
<id column="id" property="id"/>
<result column="received_user_id" property="receivedUserId"/>
<result column="message_id" property="messageId"/>
</resultMap>
<!-- 通用查询结果列 -->
<sql id="Base_Column_List">
id, received_user_id, message_id
</sql>
<!-- 根据发送者id批量删除 -->
<delete id="deleteBatchIdsWithPhysics">
delete
from sys_message_recived
where received_user_id in
<foreach collection="userIds" item="id" open="(" close=")" separator=",">
#{id}
</foreach>
</delete>
<!-- 根据消息Id物理删除-->
<delete id="deleteBatchIdsByMessageIdsWithPhysics">
delete
from sys_message_recived
where message_id in
<foreach collection="ids" item="id" open="(" close=")" separator=",">
#{id}
</foreach>
</delete>
</mapper>