feat: 消息功能完成

This commit is contained in:
Bunny 2024-11-02 17:03:38 +08:00
parent a427c38fcb
commit a492aee703
20 changed files with 232 additions and 18 deletions

View File

@ -18,9 +18,9 @@ public class Knife4jConfig {
// 作者等信息
Contact contact = new Contact().name("Bunny").email("1319900154@qq.com").url("http://z-bunny.cn");
// 使用协议
License license = new License().name("MIT").url("http://MUT.com");
License license = new License().name("MIT").url("https://MUT.com");
// 相关信息
Info info = new Info().title("Bunny-Java-Template").description("Bunny的Java模板").version("v1.0.0").contact(contact).license(license).termsOfService("维护不易~求个start");
Info info = new Info().title("Bunny-Admin").description("权限管理模板").version("v1.0.0").contact(contact).license(license).termsOfService("MIT");
return new OpenAPI().info(info).externalDocs(new ExternalDocumentation());
}
@ -28,6 +28,6 @@ public class Knife4jConfig {
// 管理员相关分类接口
@Bean
public GroupedOpenApi groupedOpenAdminApi() {
return GroupedOpenApi.builder().group("admin管理员接口请求").pathsToMatch("/admin/**").build();
return GroupedOpenApi.builder().group("默认请求接口").pathsToMatch("/admin/**").build();
}
}

View File

@ -13,9 +13,13 @@ import lombok.NoArgsConstructor;
@Schema(name = "MessageUserDto对象", title = "用户消息查询内容", description = "用户消息查询内容")
public class MessageUserDto {
@Schema(name = "title", title = "消息标题")
private String title;
@Schema(name = "messageType", title = "消息类型")
private String messageType;
@Schema(name = "status", title = "0:未读 1:已读")
private Boolean status;
}

View File

@ -19,6 +19,7 @@ public class AssignRolesToRoutersDto {
@Schema(name = "routerId", title = "路由id")
@NotNull(message = "路由id不能为空")
@NotEmpty(message = "路由id不能为空")
private List<Long> routerIds;
@Schema(name = "roleIds", title = "角色id列表")

View File

@ -49,7 +49,6 @@ public class RouterUpdateDto {
@Schema(name = "routerRank", title = "等级")
@JsonProperty("rank")
@NotNull(message = "菜单排序不能为空")
@Max(value = 999, message = "不能超过999")
private Integer routerRank;

View File

@ -1,21 +1,26 @@
package cn.bunny.dao.vo.system.message;
import cn.bunny.dao.common.vo.BaseVo;
import com.alibaba.fastjson2.annotation.JSONField;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.*;
import java.time.LocalDateTime;
@EqualsAndHashCode(callSuper = true)
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
@Schema(name = "MessageVo对象", title = "系统消息返回内容", description = "系统消息返回内容")
public class MessageUserVo {
public class MessageUserVo extends BaseVo {
@Schema(name = "id", title = "主键")
@JsonProperty("id")
@ -41,4 +46,14 @@ public class MessageUserVo {
@Schema(name = "extra", title = "消息等级详情")
private String extra;
@Schema(name = "status", title = "消息状态")
private Boolean status;
@Schema(name = "updateTime", title = "更新时间")
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
@JsonSerialize(using = LocalDateTimeSerializer.class)
@JsonDeserialize(using = LocalDateTimeDeserializer.class)
@JsonProperty("acceptanceTime")
private LocalDateTime updateTime;
}

View File

@ -85,10 +85,24 @@ public class MessageController {
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 = "删除系统消息")
@DeleteMapping("deleteMessage")
public Mono<Result<String>> deleteMessage(@RequestBody List<Long> ids) {
messageService.deleteMessage(ids);
return Mono.just(Result.success(ResultCodeEnum.DELETE_SUCCESS));
}
@Operation(summary = "用户删除消息", description = "用户删除消息")
@DeleteMapping("/noManage/deleteUserMessageByIds")
public Mono<Result<String>> deleteUserMessageByIds(@RequestBody List<Long> ids) {
messageService.deleteUserMessageByIds(ids);
return Mono.just(Result.success(ResultCodeEnum.DELETE_SUCCESS));
}
}

View File

@ -4,6 +4,7 @@ import cn.bunny.dao.pojo.result.Result;
import cn.bunny.dao.vo.system.message.MessageReceivedWithUserVo;
import cn.bunny.services.service.MessageReceivedService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
@ -20,6 +21,7 @@ import java.util.List;
* @author Bunny
* @since 2024-10-31
*/
@Tag(name = "消息接受关系", description = "消息接受关系相关接口")
@RestController
@RequestMapping("/admin/messageReceived")
public class MessageReceivedController {

View File

@ -41,6 +41,13 @@ public class RouterRoleController {
return Mono.just(Result.success());
}
@Operation(summary = "批量为菜单添加角色", description = "批量为菜单添加角色")
@PostMapping("assignAddBatchRolesToRouter")
public Mono<Result<String>> assignAddBatchRolesToRouter(@RequestBody AssignRolesToRoutersDto dto) {
routerRoleService.assignAddBatchRolesToRouter(dto);
return Mono.just(Result.success());
}
@Operation(summary = "清除选中菜单所有角色", description = "清除选中菜单所有角色")
@DeleteMapping("clearAllRolesSelect")
public Mono<Result<String>> clearAllRolesSelect(@RequestBody List<Long> routerIds) {

View File

@ -61,7 +61,7 @@ public class UserController {
return Mono.just(Result.success(voList));
}
@Operation(summary = "更新用户信息", description = "更新用户信息需要更新Redis中的内容")
@Operation(summary = "更新用户信息", description = "更新用户信息需要更新Redis中的内容")
@PutMapping("updateAdminUser")
public Result<String> updateAdminUser(@Valid @RequestBody AdminUserUpdateDto dto) {
userService.updateAdminUser(dto);

View File

@ -55,4 +55,5 @@ public interface MessageMapper extends BaseMapper<Message> {
* @return 消息返回对象
*/
MessageVo selectMessageVoById(Long id);
}

View File

@ -39,4 +39,12 @@ public interface MessageReceivedMapper extends BaseMapper<MessageReceived> {
* @return 消息接收人用户名等信息
*/
List<MessageReceivedWithUserVo> selectUserinfoListByMessageId(Long messageId);
/**
* 根据当前用户id删除消息接受表中数据
*
* @param ids 消息列表
* @param userId 用户id
*/
void deleteBatchIdsByMessageIdsAndUserIdWithPhysics(List<Long> ids, Long userId);
}

View File

@ -38,4 +38,11 @@ public interface RouterRoleMapper extends BaseMapper<RouterRole> {
* @return 路由角色关系视图列表
*/
List<ViewRouterRole> viewRouterRolesWithAll();
/**
* 根据Id列表物理删除路由角色关系表
*
* @param ids 路由角色关系表ids
*/
void deleteBatchIdsWithPhysics(List<Long> ids);
}

View File

@ -68,4 +68,18 @@ public interface MessageService extends IService<Message> {
* @return 消息详情
*/
MessageVo getMessageDetailById(Long id);
/**
* 用户将消息标为已读
*
* @param ids 消息id列表
*/
void updateUserMarkAsRead(List<Long> ids);
/**
* 用户删除消息
*
* @param ids 消息Id列表
*/
void deleteUserMessageByIds(List<Long> ids);
}

View File

@ -37,4 +37,11 @@ public interface RouterRoleService extends IService<RouterRole> {
* @param routerIds 路由id
*/
void clearAllRolesSelect(List<Long> routerIds);
/**
* 批量为菜单添加角色
*
* @param dto 路由分配角色
*/
void assignAddBatchRolesToRouter(AssignRolesToRoutersDto dto);
}

View File

@ -8,6 +8,7 @@ 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 org.springframework.transaction.annotation.Transactional;
import java.util.List;
@ -20,6 +21,7 @@ import java.util.List;
* @since 2024-10-31
*/
@Service
@Transactional
public class MessageReceivedServiceImpl extends ServiceImpl<MessageReceivedMapper, MessageReceived> implements MessageReceivedService {
/**
@ -45,5 +47,4 @@ public class MessageReceivedServiceImpl extends ServiceImpl<MessageReceivedMappe
}
return baseMapper.selectUserinfoListByMessageId(messageId);
}
}

View File

@ -1,6 +1,7 @@
package cn.bunny.services.service.impl;
import cn.bunny.common.service.context.BaseContext;
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;
@ -9,6 +10,7 @@ import cn.bunny.dao.dto.system.message.MessageUserDto;
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.pojo.result.ResultCodeEnum;
import cn.bunny.dao.vo.system.message.MessageUserVo;
import cn.bunny.dao.vo.system.message.MessageVo;
import cn.bunny.services.factory.UserFactory;
@ -27,6 +29,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList;
import java.util.List;
/**
@ -99,6 +102,16 @@ public class MessageServiceImpl extends ServiceImpl<MessageMapper, Message> impl
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的列表
IPage<Message> page = baseMapper.selectListByPageWithMessageUserDto(pageParams, dto, messageIds);
List<MessageUserVo> voList = page.getRecords().stream().map(messageVo -> {
@ -111,6 +124,7 @@ public class MessageServiceImpl extends ServiceImpl<MessageMapper, Message> impl
vo.setCover(cover);
return vo;
}).toList();
return PageResult.<MessageUserVo>builder()
.list(voList)
.pageNo(page.getCurrent())
@ -121,15 +135,67 @@ public class MessageServiceImpl extends ServiceImpl<MessageMapper, Message> impl
/**
* 根据消息id查询消息详情
* 请求消息内容后标为已读
*
* @param id 消息id
* @return 消息详情
*/
@Override
public MessageVo getMessageDetailById(Long id) {
// 将消息设为已读
Message message = new Message();
message.setId(id);
message.setStatus(true);
updateById(message);
// 返回详情内容给前端
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);
}
/**
* 用户删除消息
*
* @param ids 消息Id列表
*/
@Override
public void deleteUserMessageByIds(List<Long> ids) {
// 判断ids是否为空
if (ids.isEmpty()) {
throw new BunnyException(ResultCodeEnum.REQUEST_IS_EMPTY);
}
// 删除消息表中数据
baseMapper.deleteBatchIdsWithPhysics(ids);
// 根据当前用户id删除消息接受表中数据
Long userId = BaseContext.getUserId();
messageReceivedMapper.deleteBatchIdsByMessageIdsAndUserIdWithPhysics(ids, userId);
}
/**
* 添加系统消息
* 判断发送消息的接收人是否为空如果为空默认是所有用户都是接受者

View File

@ -6,6 +6,7 @@ import cn.bunny.dao.entity.system.RouterRole;
import cn.bunny.dao.pojo.result.ResultCodeEnum;
import cn.bunny.services.mapper.RouterRoleMapper;
import cn.bunny.services.service.RouterRoleService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
@ -79,4 +80,42 @@ public class RouterRoleServiceImpl extends ServiceImpl<RouterRoleMapper, RouterR
}
baseMapper.deleteBatchIdsByRouterIdsWithPhysics(routerIds);
}
/**
* 批量为菜单添加角色
* 查询所有满足角色id列表的路由
* 将满足条件的路由提取出routerId列表
* 并删除所有routerIds
*
* @param dto 路由分配角色
*/
@Override
public void assignAddBatchRolesToRouter(AssignRolesToRoutersDto dto) {
// 查询所有满足角色id和路由Id相关
LambdaQueryWrapper<RouterRole> wrapper = new LambdaQueryWrapper<>();
wrapper.in(RouterRole::getRoleId, dto.getRoleIds())
.and(qw -> qw.in(RouterRole::getRouterId, dto.getRouterIds()));
List<RouterRole> routerRoleList = list(wrapper);
// 根据Id列表物理删除路由角色关系表
List<Long> ids = routerRoleList.stream().map(RouterRole::getId).toList();
if (!ids.isEmpty()) {
baseMapper.deleteBatchIdsWithPhysics(ids);
}
// 保存分配好的角色信息
List<RouterRole> roleList = new ArrayList<>();
for (Long roleId : dto.getRoleIds()) {
List<RouterRole> list = dto.getRouterIds().stream().map(routerId -> {
RouterRole routerRole = new RouterRole();
routerRole.setRouterId(routerId);
routerRole.setRoleId(roleId);
return routerRole;
}).toList();
roleList.addAll(list);
}
saveBatch(roleList);
}
}

View File

@ -74,20 +74,28 @@
<!-- 根据消息所有包含匹配当前消息Id的列表 -->
<select id="selectListByPageWithMessageUserDto" resultType="cn.bunny.dao.entity.system.Message">
select
<include refid="Base_Column_List"/>
from sys_message
message.*,
create_user.username as create_username,
update_user.username as update_username
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
<where>
<if test="messageIds != null">
id in
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">
and status = #{dto.status}
and message.status = #{dto.status}
</if>
<if test="dto.messageType != null and dto.messageType != ''">
and message_type like CONCAT('%',#{dto.messageType},'%')
and message.message_type = #{dto.messageType}
</if>
<if test="dto.title != null and dto.title != ''">
and message.title like CONCAT('%',#{dto.title},'%')
</if>
</where>
</select>
@ -100,13 +108,14 @@
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
WHERE message.id = #{id}
and message.is_deleted = 0
</select>
<!-- 物理删除系统消息 -->
<delete id="deleteBatchIdsWithPhysics">
delete
from sys_message
where id in
where is_deleted=0 and id in
<foreach collection="ids" item="id" open="(" close=")" separator=",">
#{id}
</foreach>

View File

@ -34,6 +34,16 @@
</foreach>
</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批量删除消息接受者 -->
<select id="selectUserinfoListByMessageId"
resultType="cn.bunny.dao.vo.system.message.MessageReceivedWithUserVo">

View File

@ -39,6 +39,16 @@
</foreach>
</delete>
<!-- 根据Id列表物理删除路由角色关系表 -->
<delete id="deleteBatchIdsWithPhysics">
delete
from sys_router_role
where id in
<foreach collection="ids" item="id" open="(" close=")" separator=",">
#{id}
</foreach>
</delete>
<!-- 查看所有路由的角色 -->
<select id="viewRouterRolesWithAll" resultType="cn.bunny.dao.view.ViewRouterRole">
SELECT rr.router_id,