feat: 新增系统管理/消息管理(列表、查看详情、标记已读、全部已读、删除)

This commit is contained in:
Bull-BCLS
2023-10-30 12:15:37 +08:00
parent 4d70bc84db
commit 9217166e9d
30 changed files with 1633 additions and 67 deletions

View File

@@ -28,6 +28,7 @@ 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;
@@ -38,6 +39,7 @@ import top.charles7c.cnadmin.auth.model.vo.RouteVO;
import top.charles7c.cnadmin.auth.service.LoginService;
import top.charles7c.cnadmin.auth.service.PermissionService;
import top.charles7c.cnadmin.common.annotation.TreeField;
import top.charles7c.cnadmin.common.config.properties.ProjectProperties;
import top.charles7c.cnadmin.common.constant.RegexConsts;
import top.charles7c.cnadmin.common.constant.SysConsts;
import top.charles7c.cnadmin.common.enums.DisEnableStatusEnum;
@@ -48,9 +50,11 @@ import top.charles7c.cnadmin.common.util.SecureUtils;
import top.charles7c.cnadmin.common.util.TreeUtils;
import top.charles7c.cnadmin.common.util.helper.LoginHelper;
import top.charles7c.cnadmin.common.util.validate.CheckUtils;
import top.charles7c.cnadmin.system.enums.MessageTemplateEnum;
import top.charles7c.cnadmin.system.model.entity.RoleDO;
import top.charles7c.cnadmin.system.model.entity.UserDO;
import top.charles7c.cnadmin.system.model.entity.UserSocialDO;
import top.charles7c.cnadmin.system.model.request.MessageRequest;
import top.charles7c.cnadmin.system.model.vo.DeptDetailVO;
import top.charles7c.cnadmin.system.model.vo.MenuVO;
import top.charles7c.cnadmin.system.service.*;
@@ -74,6 +78,8 @@ public class LoginServiceImpl implements LoginService {
private final PermissionService permissionService;
private final UserRoleService userRoleService;
private final UserSocialService userSocialService;
private final MessageService messageService;
private final ProjectProperties projectProperties;
@Override
public String accountLogin(String username, String password) {
@@ -131,6 +137,7 @@ public class LoginServiceImpl implements LoginService {
userSocial.setUserId(userId);
userSocial.setSource(source);
userSocial.setOpenId(openId);
this.sendMsg(user);
} else {
user = BeanUtil.toBean(userService.get(userSocial.getUserId()), UserDO.class);
}
@@ -180,7 +187,7 @@ public class LoginServiceImpl implements LoginService {
/**
* 登录并缓存用户信息
*
*
* @param user
* 用户信息
* @return 令牌
@@ -205,4 +212,22 @@ public class LoginServiceImpl implements LoginService {
DeptDetailVO deptDetailVO = deptService.get(user.getDeptId());
CheckUtils.throwIfEqual(DisEnableStatusEnum.DISABLE, deptDetailVO.getStatus(), "此账号所属部门已被禁用,如有疑问,请联系管理员");
}
/**
* 发送消息
*
* @param user
* 用户信息
*/
private void sendMsg(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);
messageService.add(request, CollUtil.toList(user.getId()));
}
}

View File

@@ -0,0 +1,39 @@
/*
* 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.enums;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
/**
* 消息模板枚举
*
* @author BULL_BCLS
* @since 2023/10/15 19:51
*/
@Getter
@RequiredArgsConstructor
public enum MessageTemplateEnum {
/**
* 第三方登录
*/
SOCIAL_REGISTER("欢迎注册 {projectName}", "尊敬的 {nickname},欢迎注册使用,请及时配置您的密码。");
private final String title;
private final String content;
}

View File

@@ -0,0 +1,58 @@
/*
* 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.mapper;
import java.util.List;
import org.apache.ibatis.annotations.Param;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import top.charles7c.cnadmin.common.base.BaseMapper;
import top.charles7c.cnadmin.system.model.entity.MessageDO;
import top.charles7c.cnadmin.system.model.vo.MessageVO;
/**
* 消息 Mapper
*
* @author BULL_BCLS
* @since 2023/10/15 19:05
*/
public interface MessageMapper extends BaseMapper<MessageDO> {
/**
* 分页查询列表
*
* @param queryWrapper
* 查询条件
* @param page
* 分页查询条件
* @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);
}

View File

@@ -0,0 +1,28 @@
/*
* 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.mapper;
import top.charles7c.cnadmin.common.base.BaseMapper;
import top.charles7c.cnadmin.system.model.entity.MessageUserDO;
/**
* 消息和用户关联 Mapper
*
* @author BULL_BCLS
* @since 2023/10/15 20:25
*/
public interface MessageUserMapper extends BaseMapper<MessageUserDO> {}

View File

@@ -0,0 +1,56 @@
/*
* 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.entity;
import lombok.Data;
import com.baomidou.mybatisplus.annotation.TableName;
import top.charles7c.cnadmin.common.base.BaseDO;
/**
* 消息实体
*
* @author BULL_BCLS
* @since 2023/10/15 19:05
*/
@Data
@TableName("sys_message")
public class MessageDO extends BaseDO {
private static final long serialVersionUID = 1L;
/**
* 消息ID
*/
private Long id;
/**
* 主题
*/
private String title;
/**
* 内容
*/
private String content;
/**
* 类型(取值于字典 message_type
*/
private String type;
}

View File

@@ -0,0 +1,58 @@
/*
* 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.entity;
import java.time.LocalDateTime;
import lombok.Data;
import com.baomidou.mybatisplus.annotation.TableName;
import top.charles7c.cnadmin.common.base.BaseDO;
/**
* 消息和用户关联实体
*
* @author BULL_BCLS
* @since 2023/10/15 20:25
*/
@Data
@TableName("sys_message_user")
public class MessageUserDO extends BaseDO {
private static final long serialVersionUID = 1L;
/**
* 消息ID
*/
private Long messageId;
/**
* 用户ID
*/
private Long userId;
/**
* 读取状态 0未读 1已读
*/
private Boolean readStatus;
/**
* 读取时间
*/
private LocalDateTime readTime;
}

View File

@@ -0,0 +1,72 @@
/*
* 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.query;
import java.io.Serializable;
import lombok.Data;
import io.swagger.v3.oas.annotations.media.Schema;
import top.charles7c.cnadmin.common.annotation.Query;
import top.charles7c.cnadmin.common.enums.QueryTypeEnum;
/**
* 消息查询条件
*
* @author BULL_BCLS
* @since 2023/10/15 19:05
*/
@Data
@Schema(description = "消息查询条件")
public class MessageQuery implements Serializable {
private static final long serialVersionUID = 1L;
/**
* ID
*/
@Schema(description = "ID", example = "1")
@Query(type = QueryTypeEnum.EQUAL)
private Long id;
/**
* 类型(取值于字典 message_type
*/
@Schema(description = "类型(取值于字典 message_type", example = "1")
@Query(type = QueryTypeEnum.EQUAL)
private String type;
/**
* 主题
*/
@Schema(description = "主题", example = "欢迎 xxx")
@Query(type = QueryTypeEnum.INNER_LIKE)
private String title;
/**
* 用户ID
*/
@Schema(description = "用户ID", example = "1")
private Long uid;
/**
* 是否已读
*/
@Schema(description = "是否已读", example = "true")
private Boolean readStatus;
}

View File

@@ -0,0 +1,72 @@
/*
* 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.request;
import java.util.Map;
import javax.validation.constraints.NotBlank;
import lombok.Data;
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;
/**
* 创建消息信息
*
* @author BULL_BCLS
* @since 2023/10/15 19:05
*/
@Schema(description = "创建消息信息")
@Data
public class MessageRequest extends BaseRequest {
private static final long serialVersionUID = 1L;
/**
* 主题
*/
@Schema(description = "主题", example = "欢迎 xxx")
@NotBlank(message = "主题不能为空")
@Length(max = 50, message = "主题长度不能超过 {max} 个字符")
private String title;
/**
* 内容
*/
@Schema(description = "内容", example = "欢迎 xxx 来到 ContiNew Admin")
@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);
}
}

View File

@@ -0,0 +1,62 @@
/*
* 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.time.LocalDateTime;
import lombok.Data;
import io.swagger.v3.oas.annotations.media.Schema;
import top.charles7c.cnadmin.common.base.BaseVO;
/**
* 消息和用户关联信息
*
* @author BULL_BCLS
* @since 2023/10/15 20:25
*/
@Data
@Schema(description = "消息和用户关联信息")
public class MessageUserVO extends BaseVO {
private static final long serialVersionUID = 1L;
/**
* 消息ID
*/
@Schema(description = "消息ID", example = "1")
private Long messageId;
/**
* 用户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;
}

View File

@@ -0,0 +1,74 @@
/*
* 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.time.LocalDateTime;
import lombok.Data;
import io.swagger.v3.oas.annotations.media.Schema;
import top.charles7c.cnadmin.common.base.BaseVO;
/**
* 消息信息
*
* @author BULL_BCLS
* @since 2023/10/15 19:05
*/
@Data
@Schema(description = "消息信息")
public class MessageVO extends BaseVO {
private static final long serialVersionUID = 1L;
/**
* 消息ID
*/
@Schema(description = "消息ID", example = "1")
private Long id;
/**
* 主题
*/
@Schema(description = "主题", example = "欢迎 xxx")
private String title;
/**
* 内容
*/
@Schema(description = "内容", example = "欢迎 xxx")
private String content;
/**
* 类型(取值于字典 message_type
*/
@Schema(description = "类型(取值于字典 message_type", example = "1")
private String type;
/**
* 是否已读
*/
@Schema(description = "是否已读", example = "true")
private Boolean readStatus;
/**
* 读取时间
*/
@Schema(description = "读取时间", example = "2023-08-08 23:59:59", type = "string")
private LocalDateTime readTime;
}

View File

@@ -0,0 +1,43 @@
/*
* 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.service;
import java.util.List;
import top.charles7c.cnadmin.common.base.BaseService;
import top.charles7c.cnadmin.system.model.query.MessageQuery;
import top.charles7c.cnadmin.system.model.request.MessageRequest;
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> {
/**
* 发送消息
*
* @param request
* 消息
* @param userIdList
* 接收人
*/
void add(MessageRequest request, List<Long> userIdList);
}

View File

@@ -0,0 +1,54 @@
/*
* 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.service;
import java.util.List;
/**
* 消息和用户关联业务接口
*
* @author BULL_BCLS
* @since 2023/10/15 19:05
*/
public interface MessageUserService {
/**
* 发送消息
*
* @param messageId
* 消息ID
* @param userIdList
* 接收人
*/
void add(Long messageId, List<Long> userIdList);
/**
* 将消息标记已读
*
* @param ids
* 消息ID为空则将所有消息标记已读
*/
void readMessage(List<Long> ids);
/**
* 删除消息
*
* @param ids
* 消息ID
*/
void delete(List<Long> ids);
}

View File

@@ -0,0 +1,108 @@
/*
* 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.service.impl;
import java.util.Collections;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
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.collection.CollUtil;
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.util.helper.QueryHelper;
import top.charles7c.cnadmin.common.util.validate.CheckUtils;
import top.charles7c.cnadmin.system.mapper.MessageMapper;
import top.charles7c.cnadmin.system.model.entity.MessageDO;
import top.charles7c.cnadmin.system.model.query.MessageQuery;
import top.charles7c.cnadmin.system.model.request.MessageRequest;
import top.charles7c.cnadmin.system.model.vo.MessageVO;
import top.charles7c.cnadmin.system.service.MessageService;
import top.charles7c.cnadmin.system.service.MessageUserService;
/**
* 消息业务实现
*
* @author BULL_BCLS
* @since 2023/10/15 19:05
*/
@Service
@RequiredArgsConstructor
public class MessageServiceImpl
extends BaseServiceImpl<MessageMapper, MessageDO, MessageVO, MessageVO, MessageQuery, MessageRequest>
implements MessageService {
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());
queryWrapper.apply(null != query.getReadStatus(), "msgUser.read_status={0}", query.getReadStatus());
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());
queryWrapper.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);
}
@Transactional(rollbackFor = Exception.class)
@Override
public void delete(List<Long> ids) {
super.delete(ids);
messageUserService.delete(ids);
}
}

View File

@@ -0,0 +1,74 @@
/*
* 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.service.impl;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;
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.util.validate.CheckUtils;
import top.charles7c.cnadmin.system.mapper.MessageUserMapper;
import top.charles7c.cnadmin.system.model.entity.MessageUserDO;
import top.charles7c.cnadmin.system.service.MessageUserService;
/**
* 消息和用户关联业务实现
*
* @author BULL_BCLS
* @since 2023/10/15 19:05
*/
@Service
@RequiredArgsConstructor
public class MessageUserServiceImpl implements MessageUserService {
private final MessageUserMapper messageUserMapper;
@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;
}).collect(Collectors.toList());
messageUserMapper.insertBatch(messageUserDOList);
}
@Override
public void readMessage(List<Long> ids) {
messageUserMapper.lambdaUpdate().set(MessageUserDO::getReadStatus, true)
.set(MessageUserDO::getReadTime, LocalDateTime.now()).eq(MessageUserDO::getReadStatus, 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));
}
}
}

View File

@@ -0,0 +1,32 @@
<?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.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
${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>