mirror of
https://github.com/continew-org/continew-admin.git
synced 2025-09-11 16:57:12 +08:00
refactor: 优化站内信及消息管理
1.新增站内信未读消息轮询 2.优化消息管理 API,移除部分无用 API 3.优化部分代码格式
This commit is contained in:
@@ -28,10 +28,10 @@ import cn.hutool.core.bean.BeanUtil;
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import cn.hutool.core.lang.tree.Tree;
|
||||
import cn.hutool.core.lang.tree.TreeNodeConfig;
|
||||
import cn.hutool.core.map.MapUtil;
|
||||
import cn.hutool.core.util.IdUtil;
|
||||
import cn.hutool.core.util.RandomUtil;
|
||||
import cn.hutool.core.util.ReUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.hutool.json.JSONUtil;
|
||||
|
||||
import top.charles7c.cnadmin.auth.model.vo.MetaVO;
|
||||
@@ -45,6 +45,7 @@ import top.charles7c.cnadmin.common.constant.SysConsts;
|
||||
import top.charles7c.cnadmin.common.enums.DisEnableStatusEnum;
|
||||
import top.charles7c.cnadmin.common.enums.GenderEnum;
|
||||
import top.charles7c.cnadmin.common.enums.MenuTypeEnum;
|
||||
import top.charles7c.cnadmin.common.enums.MessageTypeEnum;
|
||||
import top.charles7c.cnadmin.common.model.dto.LoginUser;
|
||||
import top.charles7c.cnadmin.common.util.SecureUtils;
|
||||
import top.charles7c.cnadmin.common.util.TreeUtils;
|
||||
@@ -71,6 +72,7 @@ import me.zhyd.oauth.model.AuthUser;
|
||||
@RequiredArgsConstructor
|
||||
public class LoginServiceImpl implements LoginService {
|
||||
|
||||
private final ProjectProperties projectProperties;
|
||||
private final UserService userService;
|
||||
private final DeptService deptService;
|
||||
private final RoleService roleService;
|
||||
@@ -79,7 +81,6 @@ public class LoginServiceImpl implements LoginService {
|
||||
private final UserRoleService userRoleService;
|
||||
private final UserSocialService userSocialService;
|
||||
private final MessageService messageService;
|
||||
private final ProjectProperties projectProperties;
|
||||
|
||||
@Override
|
||||
public String accountLogin(String username, String password) {
|
||||
@@ -137,7 +138,7 @@ public class LoginServiceImpl implements LoginService {
|
||||
userSocial.setUserId(userId);
|
||||
userSocial.setSource(source);
|
||||
userSocial.setOpenId(openId);
|
||||
this.sendMsg(user);
|
||||
this.sendSystemMsg(user);
|
||||
} else {
|
||||
user = BeanUtil.toBean(userService.get(userSocial.getUserId()), UserDO.class);
|
||||
}
|
||||
@@ -214,20 +215,17 @@ public class LoginServiceImpl implements LoginService {
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送消息
|
||||
* 发送系统消息
|
||||
*
|
||||
* @param user
|
||||
* 用户信息
|
||||
*/
|
||||
private void sendMsg(UserDO user) {
|
||||
private void sendSystemMsg(UserDO user) {
|
||||
MessageRequest request = new MessageRequest();
|
||||
MessageTemplateEnum socialRegister = MessageTemplateEnum.SOCIAL_REGISTER;
|
||||
request.setTitle(socialRegister.getTitle());
|
||||
Map<String, Object> contentMap = MapUtil.newHashMap(2);
|
||||
contentMap.put("nickname", user.getNickname());
|
||||
contentMap.put("projectName", projectProperties.getName());
|
||||
request.setContent(socialRegister.getContent(), contentMap);
|
||||
request.setType(SysConsts.SYSTEM_MESSAGE_TYPE);
|
||||
request.setTitle(StrUtil.format(socialRegister.getTitle(), projectProperties.getName()));
|
||||
request.setContent(StrUtil.format(socialRegister.getContent(), user.getNickname()));
|
||||
request.setType(MessageTypeEnum.SYSTEM);
|
||||
messageService.add(request, CollUtil.toList(user.getId()));
|
||||
}
|
||||
}
|
||||
|
@@ -32,7 +32,7 @@ public enum MessageTemplateEnum {
|
||||
/**
|
||||
* 第三方登录
|
||||
*/
|
||||
SOCIAL_REGISTER("欢迎注册 {projectName}", "尊敬的 {nickname},欢迎注册使用,请及时配置您的密码。");
|
||||
SOCIAL_REGISTER("欢迎注册 {}", "尊敬的 {},欢迎注册使用,请及时配置您的密码。");
|
||||
|
||||
private final String title;
|
||||
private final String content;
|
||||
|
@@ -16,8 +16,6 @@
|
||||
|
||||
package top.charles7c.cnadmin.system.mapper;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
@@ -35,24 +33,16 @@ import top.charles7c.cnadmin.system.model.vo.MessageVO;
|
||||
* @since 2023/10/15 19:05
|
||||
*/
|
||||
public interface MessageMapper extends BaseMapper<MessageDO> {
|
||||
|
||||
/**
|
||||
* 分页查询列表
|
||||
*
|
||||
* @param queryWrapper
|
||||
* 查询条件
|
||||
* @param page
|
||||
* 分页查询条件
|
||||
* @param queryWrapper
|
||||
* 查询条件
|
||||
* @return 分页信息
|
||||
*/
|
||||
IPage<MessageVO> selectVoPage(@Param("page") IPage<Object> page,
|
||||
@Param(Constants.WRAPPER) QueryWrapper<MessageDO> queryWrapper);
|
||||
|
||||
/**
|
||||
* 查询列表
|
||||
*
|
||||
* @param queryWrapper
|
||||
* 查询条件
|
||||
* @return 列表信息
|
||||
*/
|
||||
List<MessageVO> selectVoList(@Param(Constants.WRAPPER) QueryWrapper<MessageDO> queryWrapper);
|
||||
}
|
@@ -16,13 +16,27 @@
|
||||
|
||||
package top.charles7c.cnadmin.system.mapper;
|
||||
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
|
||||
import top.charles7c.cnadmin.common.base.BaseMapper;
|
||||
import top.charles7c.cnadmin.system.model.entity.MessageUserDO;
|
||||
|
||||
/**
|
||||
* 消息和用户关联 Mapper
|
||||
* 消息和用户 Mapper
|
||||
*
|
||||
* @author BULL_BCLS
|
||||
* @since 2023/10/15 20:25
|
||||
*/
|
||||
public interface MessageUserMapper extends BaseMapper<MessageUserDO> {}
|
||||
public interface MessageUserMapper extends BaseMapper<MessageUserDO> {
|
||||
|
||||
/**
|
||||
* 根据用户 ID 和消息类型查询未读消息数量
|
||||
*
|
||||
* @param userId
|
||||
* 用户 ID
|
||||
* @param type
|
||||
* 消息类型
|
||||
* @return 未读消息信息
|
||||
*/
|
||||
Long selectUnreadCountByUserIdAndType(@Param("userId") Long userId, @Param("type") Integer type);
|
||||
}
|
@@ -16,11 +16,17 @@
|
||||
|
||||
package top.charles7c.cnadmin.system.model.entity;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.FieldFill;
|
||||
import com.baomidou.mybatisplus.annotation.TableField;
|
||||
import com.baomidou.mybatisplus.annotation.TableId;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
|
||||
import top.charles7c.cnadmin.common.base.BaseDO;
|
||||
import top.charles7c.cnadmin.common.enums.MessageTypeEnum;
|
||||
|
||||
/**
|
||||
* 消息实体
|
||||
@@ -30,17 +36,18 @@ import top.charles7c.cnadmin.common.base.BaseDO;
|
||||
*/
|
||||
@Data
|
||||
@TableName("sys_message")
|
||||
public class MessageDO extends BaseDO {
|
||||
public class MessageDO implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
/**
|
||||
* 消息ID
|
||||
* ID
|
||||
*/
|
||||
@TableId
|
||||
private Long id;
|
||||
|
||||
/**
|
||||
* 主题
|
||||
* 标题
|
||||
*/
|
||||
private String title;
|
||||
|
||||
@@ -50,7 +57,18 @@ public class MessageDO extends BaseDO {
|
||||
private String content;
|
||||
|
||||
/**
|
||||
* 类型(取值于字典 message_type)
|
||||
* 类型(1:系统消息)
|
||||
*/
|
||||
private String type;
|
||||
private MessageTypeEnum type;
|
||||
|
||||
/**
|
||||
* 创建人
|
||||
*/
|
||||
private Long createUser;
|
||||
|
||||
/**
|
||||
* 创建时间
|
||||
*/
|
||||
@TableField(fill = FieldFill.INSERT)
|
||||
private LocalDateTime createTime;
|
||||
}
|
@@ -16,14 +16,13 @@
|
||||
|
||||
package top.charles7c.cnadmin.system.model.entity;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
|
||||
import top.charles7c.cnadmin.common.base.BaseDO;
|
||||
|
||||
/**
|
||||
* 消息和用户关联实体
|
||||
*
|
||||
@@ -32,24 +31,24 @@ import top.charles7c.cnadmin.common.base.BaseDO;
|
||||
*/
|
||||
@Data
|
||||
@TableName("sys_message_user")
|
||||
public class MessageUserDO extends BaseDO {
|
||||
public class MessageUserDO implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
/**
|
||||
* 消息ID
|
||||
* 消息 ID
|
||||
*/
|
||||
private Long messageId;
|
||||
|
||||
/**
|
||||
* 用户ID
|
||||
* 用户 ID
|
||||
*/
|
||||
private Long userId;
|
||||
|
||||
/**
|
||||
* 读取状态 (0未读 1已读)
|
||||
* 是否已读
|
||||
*/
|
||||
private Boolean readStatus;
|
||||
private Boolean isRead;
|
||||
|
||||
/**
|
||||
* 读取时间
|
||||
|
@@ -41,32 +41,32 @@ public class MessageQuery implements Serializable {
|
||||
* ID
|
||||
*/
|
||||
@Schema(description = "ID", example = "1")
|
||||
@Query(type = QueryTypeEnum.EQUAL)
|
||||
@Query
|
||||
private Long id;
|
||||
|
||||
/**
|
||||
* 类型(取值于字典 message_type)
|
||||
* 标题
|
||||
*/
|
||||
@Schema(description = "类型(取值于字典 message_type)", example = "1")
|
||||
@Query(type = QueryTypeEnum.EQUAL)
|
||||
private String type;
|
||||
|
||||
/**
|
||||
* 主题
|
||||
*/
|
||||
@Schema(description = "主题", example = "欢迎 xxx")
|
||||
@Schema(description = "标题", example = "欢迎注册 xxx")
|
||||
@Query(type = QueryTypeEnum.INNER_LIKE)
|
||||
private String title;
|
||||
|
||||
/**
|
||||
* 用户ID
|
||||
* 类型
|
||||
*/
|
||||
@Schema(description = "用户ID", example = "1")
|
||||
private Long uid;
|
||||
@Schema(description = "类型(1:系统消息)", example = "1")
|
||||
@Query
|
||||
private Integer type;
|
||||
|
||||
/**
|
||||
* 是否已读
|
||||
*/
|
||||
@Schema(description = "是否已读", example = "true")
|
||||
private Boolean readStatus;
|
||||
private Boolean isRead;
|
||||
|
||||
/**
|
||||
* 用户 ID
|
||||
*/
|
||||
@Schema(hidden = true)
|
||||
private Long userId;
|
||||
}
|
@@ -16,9 +16,8 @@
|
||||
|
||||
package top.charles7c.cnadmin.system.model.request;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import javax.validation.constraints.NotBlank;
|
||||
import javax.validation.constraints.NotNull;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@@ -26,9 +25,8 @@ import io.swagger.v3.oas.annotations.media.Schema;
|
||||
|
||||
import org.hibernate.validator.constraints.Length;
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
|
||||
import top.charles7c.cnadmin.common.base.BaseRequest;
|
||||
import top.charles7c.cnadmin.common.enums.MessageTypeEnum;
|
||||
|
||||
/**
|
||||
* 创建消息信息
|
||||
@@ -36,37 +34,32 @@ import top.charles7c.cnadmin.common.base.BaseRequest;
|
||||
* @author BULL_BCLS
|
||||
* @since 2023/10/15 19:05
|
||||
*/
|
||||
@Schema(description = "创建消息信息")
|
||||
@Data
|
||||
@Schema(description = "创建消息信息")
|
||||
public class MessageRequest extends BaseRequest {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
/**
|
||||
* 主题
|
||||
* 标题
|
||||
*/
|
||||
@Schema(description = "主题", example = "欢迎 xxx")
|
||||
@NotBlank(message = "主题不能为空")
|
||||
@Length(max = 50, message = "主题长度不能超过 {max} 个字符")
|
||||
@Schema(description = "标题", example = "欢迎注册 xxx")
|
||||
@NotBlank(message = "标题不能为空")
|
||||
@Length(max = 50, message = "标题长度不能超过 {max} 个字符")
|
||||
private String title;
|
||||
|
||||
/**
|
||||
* 内容
|
||||
*/
|
||||
@Schema(description = "内容", example = "欢迎 xxx 来到 ContiNew Admin")
|
||||
@Schema(description = "内容", example = "尊敬的 xx,欢迎注册使用,请及时配置您的密码。")
|
||||
@NotBlank(message = "内容不能为空")
|
||||
@Length(max = 255, message = "内容长度不能超过 {max} 个字符")
|
||||
private String content;
|
||||
|
||||
/**
|
||||
* 类型(取值于字典 message_type)
|
||||
* 类型
|
||||
*/
|
||||
@Schema(description = "类型(取值于字典 message_type)", example = "1")
|
||||
@NotBlank(message = "类型不能为空")
|
||||
@Length(max = 30, message = "类型长度不能超过 {max} 个字符")
|
||||
private String type;
|
||||
|
||||
public void setContent(String content, Map<String, Object> contentMap) {
|
||||
this.content = StrUtil.format(content, contentMap);
|
||||
}
|
||||
@Schema(description = "类型(1:系统消息)", example = "1")
|
||||
@NotNull(message = "类型非法")
|
||||
private MessageTypeEnum type;
|
||||
}
|
@@ -16,47 +16,35 @@
|
||||
|
||||
package top.charles7c.cnadmin.system.model.vo;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.io.Serializable;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
|
||||
import top.charles7c.cnadmin.common.base.BaseVO;
|
||||
import top.charles7c.cnadmin.common.enums.MessageTypeEnum;
|
||||
|
||||
/**
|
||||
* 消息和用户关联信息
|
||||
* 各类型未读消息信息
|
||||
*
|
||||
* @author BULL_BCLS
|
||||
* @since 2023/10/15 20:25
|
||||
* @author Charles7c
|
||||
* @since 2023/11/2 23:00
|
||||
*/
|
||||
@Data
|
||||
@Schema(description = "消息和用户关联信息")
|
||||
public class MessageUserVO extends BaseVO {
|
||||
@Schema(description = "各类型未读消息信息")
|
||||
public class MessageTypeUnreadVO implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
/**
|
||||
* 消息ID
|
||||
* 类型
|
||||
*/
|
||||
@Schema(description = "消息ID", example = "1")
|
||||
private Long messageId;
|
||||
@Schema(description = "类型(1:系统消息)", example = "1")
|
||||
private MessageTypeEnum type;
|
||||
|
||||
/**
|
||||
* 用户ID
|
||||
* 数量
|
||||
*/
|
||||
@Schema(description = "用户ID", example = "1")
|
||||
private Long userId;
|
||||
|
||||
/**
|
||||
* 是否已读
|
||||
*/
|
||||
@Schema(description = "是否已读", example = "true")
|
||||
private Boolean readStatus;
|
||||
|
||||
/**
|
||||
* 读取时间
|
||||
*/
|
||||
@Schema(description = "读取时间", example = "2023-08-08 23:59:59", type = "string")
|
||||
private LocalDateTime readTime;
|
||||
@Schema(description = "数量", example = "10")
|
||||
private Long count;
|
||||
}
|
@@ -0,0 +1,52 @@
|
||||
/*
|
||||
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package top.charles7c.cnadmin.system.model.vo;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.List;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonInclude;
|
||||
|
||||
/**
|
||||
* 未读消息信息
|
||||
*
|
||||
* @author Charles7c
|
||||
* @since 2023/11/2 23:00
|
||||
*/
|
||||
@Data
|
||||
@Schema(description = "未读消息信息")
|
||||
@JsonInclude(JsonInclude.Include.NON_EMPTY)
|
||||
public class MessageUnreadVO implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
/**
|
||||
* 未读消息数量
|
||||
*/
|
||||
@Schema(description = "未读消息数量", example = "20")
|
||||
private Long total;
|
||||
|
||||
/**
|
||||
* 各类型未读消息数量
|
||||
*/
|
||||
@Schema(description = "各类型未读消息数量")
|
||||
private List<MessageTypeUnreadVO> details;
|
||||
}
|
@@ -16,13 +16,16 @@
|
||||
|
||||
package top.charles7c.cnadmin.system.model.vo;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
|
||||
import top.charles7c.cnadmin.common.base.BaseVO;
|
||||
import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||
|
||||
import top.charles7c.cnadmin.common.enums.MessageTypeEnum;
|
||||
|
||||
/**
|
||||
* 消息信息
|
||||
@@ -32,43 +35,61 @@ import top.charles7c.cnadmin.common.base.BaseVO;
|
||||
*/
|
||||
@Data
|
||||
@Schema(description = "消息信息")
|
||||
public class MessageVO extends BaseVO {
|
||||
public class MessageVO implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
/**
|
||||
* 消息ID
|
||||
* ID
|
||||
*/
|
||||
@Schema(description = "消息ID", example = "1")
|
||||
@Schema(description = "ID", example = "1")
|
||||
private Long id;
|
||||
|
||||
/**
|
||||
* 主题
|
||||
* 标题
|
||||
*/
|
||||
@Schema(description = "主题", example = "欢迎 xxx")
|
||||
@Schema(description = "标题", example = "欢迎注册 xxx")
|
||||
private String title;
|
||||
|
||||
/**
|
||||
* 内容
|
||||
*/
|
||||
@Schema(description = "内容", example = "欢迎 xxx")
|
||||
@Schema(description = "内容", example = "尊敬的 xx,欢迎注册使用,请及时配置您的密码。")
|
||||
private String content;
|
||||
|
||||
/**
|
||||
* 类型(取值于字典 message_type)
|
||||
* 类型
|
||||
*/
|
||||
@Schema(description = "类型(取值于字典 message_type)", example = "1")
|
||||
private String type;
|
||||
@Schema(description = "类型(1:系统消息)", example = "1")
|
||||
private MessageTypeEnum type;
|
||||
|
||||
/**
|
||||
* 是否已读
|
||||
*/
|
||||
@Schema(description = "是否已读", example = "true")
|
||||
private Boolean readStatus;
|
||||
private Boolean isRead;
|
||||
|
||||
/**
|
||||
* 读取时间
|
||||
*/
|
||||
@Schema(description = "读取时间", example = "2023-08-08 23:59:59", type = "string")
|
||||
private LocalDateTime readTime;
|
||||
|
||||
/**
|
||||
* 创建人
|
||||
*/
|
||||
@JsonIgnore
|
||||
private Long createUser;
|
||||
|
||||
/**
|
||||
* 创建人
|
||||
*/
|
||||
@Schema(description = "创建人", example = "超级管理员")
|
||||
private String createUserString;
|
||||
|
||||
/**
|
||||
* 创建时间
|
||||
*/
|
||||
@Schema(description = "创建时间", example = "2023-08-08 08:08:08", type = "string")
|
||||
private LocalDateTime createTime;
|
||||
}
|
@@ -18,7 +18,8 @@ package top.charles7c.cnadmin.system.service;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import top.charles7c.cnadmin.common.base.BaseService;
|
||||
import top.charles7c.cnadmin.common.model.query.PageQuery;
|
||||
import top.charles7c.cnadmin.common.model.vo.PageDataVO;
|
||||
import top.charles7c.cnadmin.system.model.query.MessageQuery;
|
||||
import top.charles7c.cnadmin.system.model.request.MessageRequest;
|
||||
import top.charles7c.cnadmin.system.model.vo.MessageVO;
|
||||
@@ -29,15 +30,34 @@ import top.charles7c.cnadmin.system.model.vo.MessageVO;
|
||||
* @author BULL_BCLS
|
||||
* @since 2023/10/15 19:05
|
||||
*/
|
||||
public interface MessageService extends BaseService<MessageVO, MessageVO, MessageQuery, MessageRequest> {
|
||||
public interface MessageService {
|
||||
|
||||
/**
|
||||
* 发送消息
|
||||
* 分页查询列表
|
||||
*
|
||||
* @param query
|
||||
* 查询条件
|
||||
* @param pageQuery
|
||||
* 分页查询条件
|
||||
* @return 分页列表信息
|
||||
*/
|
||||
PageDataVO<MessageVO> page(MessageQuery query, PageQuery pageQuery);
|
||||
|
||||
/**
|
||||
* 新增
|
||||
*
|
||||
* @param request
|
||||
* 消息
|
||||
* @param userIdList
|
||||
* 接收人
|
||||
* 接收人列表
|
||||
*/
|
||||
void add(MessageRequest request, List<Long> userIdList);
|
||||
|
||||
/**
|
||||
* 删除
|
||||
*
|
||||
* @param ids
|
||||
* ID 列表
|
||||
*/
|
||||
void delete(List<Long> ids);
|
||||
}
|
@@ -18,6 +18,8 @@ package top.charles7c.cnadmin.system.service;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import top.charles7c.cnadmin.system.model.vo.MessageUnreadVO;
|
||||
|
||||
/**
|
||||
* 消息和用户关联业务接口
|
||||
*
|
||||
@@ -27,12 +29,23 @@ import java.util.List;
|
||||
public interface MessageUserService {
|
||||
|
||||
/**
|
||||
* 发送消息
|
||||
* 根据用户 ID 查询未读消息数量
|
||||
*
|
||||
* @param userId
|
||||
* 用户 ID
|
||||
* @param isDetail
|
||||
* 是否查询详情
|
||||
* @return 未读消息信息
|
||||
*/
|
||||
MessageUnreadVO countUnreadMessageByUserId(Long userId, Boolean isDetail);
|
||||
|
||||
/**
|
||||
* 新增
|
||||
*
|
||||
* @param messageId
|
||||
* 消息ID
|
||||
* 消息 ID
|
||||
* @param userIdList
|
||||
* 接收人
|
||||
* 用户 ID 列表
|
||||
*/
|
||||
void add(Long messageId, List<Long> userIdList);
|
||||
|
||||
@@ -45,10 +58,10 @@ public interface MessageUserService {
|
||||
void readMessage(List<Long> ids);
|
||||
|
||||
/**
|
||||
* 删除消息
|
||||
* 根据消息 ID 删除
|
||||
*
|
||||
* @param ids
|
||||
* 消息ID
|
||||
* @param messageIds
|
||||
* 消息 ID 列表
|
||||
*/
|
||||
void delete(List<Long> ids);
|
||||
void deleteByMessageIds(List<Long> messageIds);
|
||||
}
|
@@ -16,7 +16,6 @@
|
||||
|
||||
package top.charles7c.cnadmin.system.service.impl;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
@@ -27,13 +26,14 @@ import org.springframework.transaction.annotation.Transactional;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||
|
||||
import cn.hutool.core.bean.BeanUtil;
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import cn.hutool.extra.spring.SpringUtil;
|
||||
|
||||
import top.charles7c.cnadmin.common.base.BaseServiceImpl;
|
||||
import top.charles7c.cnadmin.common.model.query.PageQuery;
|
||||
import top.charles7c.cnadmin.common.model.query.SortQuery;
|
||||
import top.charles7c.cnadmin.common.model.vo.PageDataVO;
|
||||
import top.charles7c.cnadmin.common.util.helper.LoginHelper;
|
||||
import top.charles7c.cnadmin.common.service.CommonUserService;
|
||||
import top.charles7c.cnadmin.common.util.ExceptionUtils;
|
||||
import top.charles7c.cnadmin.common.util.helper.QueryHelper;
|
||||
import top.charles7c.cnadmin.common.util.validate.CheckUtils;
|
||||
import top.charles7c.cnadmin.system.mapper.MessageMapper;
|
||||
@@ -52,57 +52,48 @@ import top.charles7c.cnadmin.system.service.MessageUserService;
|
||||
*/
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class MessageServiceImpl
|
||||
extends BaseServiceImpl<MessageMapper, MessageDO, MessageVO, MessageVO, MessageQuery, MessageRequest>
|
||||
implements MessageService {
|
||||
public class MessageServiceImpl implements MessageService {
|
||||
|
||||
private final MessageMapper baseMapper;
|
||||
private final MessageUserService messageUserService;
|
||||
|
||||
@Override
|
||||
public PageDataVO<MessageVO> page(MessageQuery query, PageQuery pageQuery) {
|
||||
QueryWrapper<MessageDO> queryWrapper = QueryHelper.build(query);
|
||||
queryWrapper.apply(null != query.getUid(), "msgUser.user_id={0}", query.getUid())
|
||||
.apply(null != query.getReadStatus(), "msgUser.read_status={0}", query.getReadStatus());
|
||||
queryWrapper.apply(null != query.getUserId(), "t2.user_id={0}", query.getUserId())
|
||||
.apply(null != query.getIsRead(), "t2.is_read={0}", query.getIsRead());
|
||||
IPage<MessageVO> page = baseMapper.selectVoPage(pageQuery.toPage(), queryWrapper);
|
||||
page.getRecords().forEach(this::fill);
|
||||
return PageDataVO.build(page);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<MessageVO> list(MessageQuery query, SortQuery sortQuery) {
|
||||
QueryWrapper<MessageDO> queryWrapper = QueryHelper.build(query);
|
||||
queryWrapper.apply("msgUser.user_id={0}", LoginHelper.getUserId()).apply(null != query.getReadStatus(),
|
||||
"msgUser.read_status={0}", query.getReadStatus());
|
||||
// 设置排序
|
||||
this.sort(queryWrapper, sortQuery);
|
||||
return baseMapper.selectVoList(queryWrapper);
|
||||
}
|
||||
|
||||
@Override
|
||||
public MessageVO get(Long id) {
|
||||
MessageQuery messageQuery = new MessageQuery();
|
||||
messageQuery.setId(id);
|
||||
PageDataVO<MessageVO> page = this.page(messageQuery, new PageQuery());
|
||||
List<MessageVO> messageVOList = page.getList();
|
||||
if (CollUtil.isEmpty(messageVOList)) {
|
||||
return new MessageVO();
|
||||
}
|
||||
MessageVO messageVO = messageVOList.get(0);
|
||||
messageUserService.readMessage(Collections.singletonList(messageVO.getId()));
|
||||
return messageVO;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void add(MessageRequest request, List<Long> userIdList) {
|
||||
CheckUtils.throwIf(() -> CollUtil.isEmpty(userIdList), "消息接收人不能为空");
|
||||
Long messageId = super.add(request);
|
||||
messageUserService.add(messageId, userIdList);
|
||||
MessageDO message = BeanUtil.copyProperties(request, MessageDO.class);
|
||||
baseMapper.insert(message);
|
||||
messageUserService.add(message.getId(), userIdList);
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public void delete(List<Long> ids) {
|
||||
super.delete(ids);
|
||||
messageUserService.delete(ids);
|
||||
baseMapper.deleteBatchIds(ids);
|
||||
messageUserService.deleteByMessageIds(ids);
|
||||
}
|
||||
|
||||
/**
|
||||
* 填充数据
|
||||
*
|
||||
* @param message
|
||||
* 待填充信息
|
||||
*/
|
||||
private void fill(MessageVO message) {
|
||||
Long createUser = message.getCreateUser();
|
||||
if (null == createUser) {
|
||||
return;
|
||||
}
|
||||
CommonUserService userService = SpringUtil.getBean(CommonUserService.class);
|
||||
message.setCreateUserString(ExceptionUtils.exToNull(() -> userService.getNicknameById(createUser)));
|
||||
}
|
||||
}
|
@@ -17,6 +17,7 @@
|
||||
package top.charles7c.cnadmin.system.service.impl;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@@ -24,13 +25,14 @@ import lombok.RequiredArgsConstructor;
|
||||
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
||||
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
|
||||
import top.charles7c.cnadmin.common.enums.MessageTypeEnum;
|
||||
import top.charles7c.cnadmin.common.util.validate.CheckUtils;
|
||||
import top.charles7c.cnadmin.system.mapper.MessageUserMapper;
|
||||
import top.charles7c.cnadmin.system.model.entity.MessageUserDO;
|
||||
import top.charles7c.cnadmin.system.model.vo.MessageTypeUnreadVO;
|
||||
import top.charles7c.cnadmin.system.model.vo.MessageUnreadVO;
|
||||
import top.charles7c.cnadmin.system.service.MessageUserService;
|
||||
|
||||
/**
|
||||
@@ -43,32 +45,52 @@ import top.charles7c.cnadmin.system.service.MessageUserService;
|
||||
@RequiredArgsConstructor
|
||||
public class MessageUserServiceImpl implements MessageUserService {
|
||||
|
||||
private final MessageUserMapper messageUserMapper;
|
||||
private final MessageUserMapper baseMapper;
|
||||
|
||||
@Override
|
||||
public MessageUnreadVO countUnreadMessageByUserId(Long userId, Boolean isDetail) {
|
||||
MessageUnreadVO result = new MessageUnreadVO();
|
||||
Long total = 0L;
|
||||
if (Boolean.TRUE.equals(isDetail)) {
|
||||
List<MessageTypeUnreadVO> detailList = new ArrayList<>();
|
||||
for (MessageTypeEnum messageType : MessageTypeEnum.values()) {
|
||||
MessageTypeUnreadVO vo = new MessageTypeUnreadVO();
|
||||
vo.setType(messageType);
|
||||
Long count = baseMapper.selectUnreadCountByUserIdAndType(userId, messageType.getValue());
|
||||
vo.setCount(count);
|
||||
detailList.add(vo);
|
||||
total += count;
|
||||
}
|
||||
result.setDetails(detailList);
|
||||
} else {
|
||||
total = baseMapper.selectUnreadCountByUserIdAndType(userId, null);
|
||||
}
|
||||
result.setTotal(total);
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void add(Long messageId, List<Long> userIdList) {
|
||||
CheckUtils.throwIf(() -> CollUtil.isEmpty(userIdList), "消息接收人不能为空");
|
||||
List<MessageUserDO> messageUserDOList = userIdList.stream().map(userId -> {
|
||||
MessageUserDO messageUserDO = new MessageUserDO();
|
||||
messageUserDO.setUserId(userId);
|
||||
messageUserDO.setMessageId(messageId);
|
||||
messageUserDO.setReadStatus(false);
|
||||
return messageUserDO;
|
||||
CheckUtils.throwIfEmpty(userIdList, "消息接收人不能为空");
|
||||
List<MessageUserDO> messageUserList = userIdList.stream().map(userId -> {
|
||||
MessageUserDO messageUser = new MessageUserDO();
|
||||
messageUser.setUserId(userId);
|
||||
messageUser.setMessageId(messageId);
|
||||
messageUser.setIsRead(false);
|
||||
return messageUser;
|
||||
}).collect(Collectors.toList());
|
||||
messageUserMapper.insertBatch(messageUserDOList);
|
||||
baseMapper.insertBatch(messageUserList);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void readMessage(List<Long> ids) {
|
||||
messageUserMapper.lambdaUpdate().set(MessageUserDO::getReadStatus, true)
|
||||
.set(MessageUserDO::getReadTime, LocalDateTime.now()).eq(MessageUserDO::getReadStatus, false)
|
||||
baseMapper.lambdaUpdate().set(MessageUserDO::getIsRead, true)
|
||||
.set(MessageUserDO::getReadTime, LocalDateTime.now()).eq(MessageUserDO::getIsRead, false)
|
||||
.in(CollUtil.isNotEmpty(ids), MessageUserDO::getMessageId, ids).update();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void delete(List<Long> ids) {
|
||||
if (CollUtil.isNotEmpty(ids)) {
|
||||
messageUserMapper.delete(Wrappers.<MessageUserDO>lambdaQuery().in(MessageUserDO::getMessageId, ids));
|
||||
}
|
||||
public void deleteByMessageIds(List<Long> messageIds) {
|
||||
baseMapper.lambdaUpdate().in(MessageUserDO::getMessageId, messageIds).remove();
|
||||
}
|
||||
}
|
@@ -2,31 +2,13 @@
|
||||
"http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
|
||||
<mapper namespace="top.charles7c.cnadmin.system.mapper.MessageMapper">
|
||||
<select id="selectVoPage" resultType="top.charles7c.cnadmin.system.model.vo.MessageVO">
|
||||
SELECT msg.id,
|
||||
msg.type,
|
||||
msg.title,
|
||||
msg.content,
|
||||
msg.create_user,
|
||||
msg.create_time,
|
||||
msgUser.read_status,
|
||||
msgUser.read_time,
|
||||
msgUser.user_id
|
||||
FROM `sys_message` msg
|
||||
LEFT JOIN sys_message_user msgUser ON msg.id = msgUser.message_id
|
||||
SELECT
|
||||
t1.*,
|
||||
t2.`user_id`,
|
||||
t2.`is_read`,
|
||||
t2.`read_time`
|
||||
FROM `sys_message` AS t1
|
||||
LEFT JOIN `sys_message_user` AS t2 ON t2.`message_id` = t1.`id`
|
||||
${ew.getCustomSqlSegment}
|
||||
</select>
|
||||
<select id="selectVoList" resultType="top.charles7c.cnadmin.system.model.vo.MessageVO">
|
||||
SELECT msg.id,
|
||||
msg.type,
|
||||
msg.title,
|
||||
msg.content,
|
||||
msg.create_user,
|
||||
msg.create_time,
|
||||
msgUser.read_status,
|
||||
msgUser.read_time,
|
||||
msgUser.user_id
|
||||
FROM `sys_message` msg
|
||||
LEFT JOIN sys_message_user msgUser ON msg.id = msgUser.message_id
|
||||
${ew.getCustomSqlSegment}
|
||||
</select>
|
||||
</mapper>
|
||||
|
@@ -0,0 +1,14 @@
|
||||
<?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="top.charles7c.cnadmin.system.mapper.MessageUserMapper">
|
||||
<select id="selectUnreadCountByUserIdAndType" resultType="Long">
|
||||
SELECT
|
||||
COUNT(t1.`message_id`)
|
||||
FROM `sys_message_user` AS t1
|
||||
LEFT JOIN `sys_message` AS t2 ON t2.`id` = t1.`message_id`
|
||||
WHERE t1.`user_id` = #{userId} AND t1.`is_read` = 0
|
||||
<if test="type != null">
|
||||
AND t2.`type` = #{type}
|
||||
</if>
|
||||
</select>
|
||||
</mapper>
|
Reference in New Issue
Block a user