【message】系统消息后台接口

pull/3/head
hansson 2021-01-08 14:25:11 +08:00
parent cc65953d31
commit c48f973e6b
9 changed files with 345 additions and 16 deletions

View File

@ -43,7 +43,7 @@ public class GunsLogAutoConfiguration {
* @date 2020/12/28 22:09
*/
@Bean
@ConditionalOnMissingBean(SysLogServiceImpl.class)
@ConditionalOnMissingBean(SysLogService.class)
@ConditionalOnProperty(prefix = SYS_LOG_PREFIX, name = "type", havingValue = "db")
public SysLogService sysLogService() {
return new SysLogServiceImpl();

View File

@ -37,6 +37,14 @@ public interface MessageApi {
*/
void updateReadFlag(MessageParam messageParam);
/**
*
*
* @author liuhanqing
* @date 2021/1/2 22:15
*/
void allMessageReadFlag();
/**
*
*
@ -74,14 +82,14 @@ public interface MessageApi {
MessageResponse messageDetail(MessageParam messageParam);
/**
*
*
*
* @param messageParam
* @return
* @author liuhanqing
* @date 2021/1/2 21:21
*/
PageResult<MessageResponse> queryMessagePage(MessageParam messageParam);
PageResult<MessageResponse> queryPage(MessageParam messageParam);
/**
*
@ -91,6 +99,26 @@ public interface MessageApi {
* @author liuhanqing
* @date 2021/1/2 21:21
*/
List<MessageResponse> queryMessageList(MessageParam messageParam);
List<MessageResponse> queryList(MessageParam messageParam);
/**
*
*
* @param messageParam
* @return
* @author liuhanqing
* @date 2021/1/2 21:21
*/
PageResult<MessageResponse> queryPageCurrentUser(MessageParam messageParam);
/**
*
*
* @param messageParam
* @return
* @author liuhanqing
* @date 2021/1/2 21:21
*/
List<MessageResponse> queryListCurrentUser(MessageParam messageParam);
}

View File

@ -0,0 +1,50 @@
package cn.stylefeng.roses.kernel.message.api.enums;
import lombok.Getter;
/**
*
*
* @author liuhanqing
* @date 2021/1/8 13:26
*/
@Getter
public enum MessageProrityLevelEnum {
/**
*
*/
HIGH("high", "高"),
/**
*
*/
MIDDLE("middle", "中"),
/**
*
*/
LOW("low", "低");
private String code;
private String name;
MessageProrityLevelEnum(String code, String name) {
this.code = code;
this.name = name;
}
public static String getName(String code) {
if (code == null) {
return null;
}
for (MessageProrityLevelEnum flagEnum : MessageProrityLevelEnum.values()) {
if (flagEnum.getCode().equals(code)) {
return flagEnum.name;
}
}
return null;
}
}

View File

@ -21,13 +21,25 @@ public enum MessageReadFlagEnum {
*/
READ(1, "已读");
private final Integer code;
private Integer code;
private final String name;
private String name;
MessageReadFlagEnum(Integer code, String name) {
this.code = code;
this.name = name;
}
public static String getName(Integer code) {
if (code == null) {
return null;
}
for (MessageReadFlagEnum flagEnum : MessageReadFlagEnum.values()) {
if (flagEnum.getCode().equals(code)) {
return flagEnum.name;
}
}
return null;
}
}

View File

@ -20,7 +20,7 @@ public class MessageParam extends BaseRequest {
/**
* id
*/
@NotNull(message = "messageId不能为空", groups = {edit.class, delete.class, detail.class})
@NotNull(message = "messageId不能为空", groups = {edit.class, delete.class, detail.class, updateStatus.class})
private Long messageId;
/**
@ -71,6 +71,7 @@ public class MessageParam extends BaseRequest {
/**
* 0-1-
*/
@NotNull(message = "阅读状态不能为空", groups = {updateStatus.class})
private Integer readFlag;
}

View File

@ -1,8 +1,16 @@
package cn.stylefeng.roses.kernel.message.modular.manage.controller;
import cn.stylefeng.roses.kernel.message.api.MessageApi;
import cn.stylefeng.roses.kernel.message.api.pojo.MessageParam;
import cn.stylefeng.roses.kernel.message.api.pojo.MessageSendParam;
import cn.stylefeng.roses.kernel.resource.api.annotation.ApiResource;
import cn.stylefeng.roses.kernel.resource.api.annotation.GetResource;
import cn.stylefeng.roses.kernel.resource.api.annotation.PostResource;
import cn.stylefeng.roses.kernel.rule.pojo.response.ResponseData;
import cn.stylefeng.roses.kernel.rule.pojo.response.SuccessResponseData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
/**
@ -21,5 +29,75 @@ public class SysMessageController {
@Autowired
private MessageApi messageApi;
/**
*
*
* @author liuhanqing
* @date 2021/1/8 13:50
*/
@PostResource(name = "发送系统消息", path = "/sysMessage/sendMessage")
public ResponseData sendMessage(@RequestBody @Validated(MessageSendParam.add.class) MessageSendParam messageSendParam) {
messageApi.sendMessage(messageSendParam);
return new SuccessResponseData();
}
/**
*
*
* @author liuhanqing
* @date 2021/1/8 13:50
*/
@GetResource(name = "系统消息全部修改已读", path = "/sysMessage/allMessageReadFlag")
public ResponseData allMessageReadFlag() {
messageApi.allMessageReadFlag();
return new SuccessResponseData();
}
/**
*
*
* @author liuhanqing
* @date 2021/1/8 13:50
*/
@PostResource(name = "删除系统消息", path = "/sysMessage/delete")
public ResponseData delete(@RequestBody @Validated(MessageParam.delete.class) MessageParam messageParam) {
messageApi.deleteByMessageId(messageParam.getMessageId());
return new SuccessResponseData();
}
/**
*
*
* @author liuhanqing
* @date 2021/1/8 13:50
*/
@GetResource(name = "查看系统消息", path = "/sysMessage/detail")
public ResponseData detail(@Validated(MessageParam.detail.class) MessageParam messageParam) {
return new SuccessResponseData(messageApi.messageDetail(messageParam));
}
/**
*
*
* @author liuhanqing
* @date 2021/1/8 13:50
*/
@GetResource(name = "分页查询系统消息列表", path = "/sysMessage/page")
public ResponseData page(MessageParam messageParam) {
return new SuccessResponseData(messageApi.queryPageCurrentUser(messageParam));
}
/**
*
*
* @author liuhanqing
* @date 2021/1/8 13:50
*/
@GetResource(name = "系统消息列表", path = "/sysMessage/list")
public ResponseData list(MessageParam messageParam) {
return new SuccessResponseData(messageApi.queryListCurrentUser(messageParam));
}
}

View File

@ -2,6 +2,7 @@ package cn.stylefeng.roses.kernel.message.db;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.roses.kernel.auth.api.context.LoginContext;
import cn.stylefeng.roses.kernel.auth.api.pojo.login.LoginUser;
import cn.stylefeng.roses.kernel.db.api.pojo.page.PageResult;
@ -14,11 +15,14 @@ import cn.stylefeng.roses.kernel.message.api.pojo.MessageResponse;
import cn.stylefeng.roses.kernel.message.api.pojo.MessageSendParam;
import cn.stylefeng.roses.kernel.message.db.entity.SysMessage;
import cn.stylefeng.roses.kernel.message.db.service.SysMessageService;
import cn.stylefeng.roses.kernel.rule.enums.YesOrNotEnum;
import cn.stylefeng.roses.kernel.system.UserServiceApi;
import cn.stylefeng.roses.kernel.system.exception.SystemModularException;
import cn.stylefeng.roses.kernel.system.pojo.user.request.SysUserRequest;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.*;
@ -41,12 +45,13 @@ public class MessageDbServiceImpl implements MessageApi {
private SysMessageService sysMessageService;
@Override
@Transactional(rollbackFor = Exception.class)
public void sendMessage(MessageSendParam messageSendParam) {
String receiveUserIds = messageSendParam.getReceiveUserIds();
// 获取当前登录人
LoginUser loginUser = LoginContext.me().getLoginUser();
List<SysMessage> sendMsgList = new ArrayList<>();
List<Long> userIds = new ArrayList<>();
List<Long> userIds;
// 发送所有人判断
if (MessageConstants.RECEIVE_ALL_USER_FLAG.equals(receiveUserIds)) {
// 查询所有用户
@ -69,7 +74,7 @@ public class MessageDbServiceImpl implements MessageApi {
sysMessage.setMessageSendTime(new Date());
userIdSet.forEach(userId -> {
// 判断用户是否存在
if(userServiceApi.userExist(userId)){
if (userServiceApi.userExist(userId)) {
sysMessage.setReceiveUserId(userId);
sendMsgList.add(sysMessage);
}
@ -79,37 +84,110 @@ public class MessageDbServiceImpl implements MessageApi {
}
@Override
@Transactional(rollbackFor = Exception.class)
public void updateReadFlag(MessageParam messageParam) {
Long messageId = messageParam.getMessageId();
SysMessage sysMessage = sysMessageService.getById(messageId);
Optional.ofNullable(sysMessage).ifPresent(msg -> {
msg.setReadFlag(messageParam.getReadFlag());
sysMessageService.updateById(msg);
});
}
@Override
public void allMessageReadFlag() {
// 获取当前登录人
LoginUser loginUser = LoginContext.me().getLoginUser();
Long userId = loginUser.getUserId();
LambdaUpdateWrapper<SysMessage> updateWrapper = new LambdaUpdateWrapper<>();
updateWrapper.set(SysMessage::getReadFlag, MessageReadFlagEnum.READ.getCode())
.eq(SysMessage::getReadFlag, MessageReadFlagEnum.UNREAD.getCode())
.eq(SysMessage::getReceiveUserId, userId)
.set(SysMessage::getDelFlag, YesOrNotEnum.N.getCode());
sysMessageService.update(updateWrapper);
}
@Override
@Transactional(rollbackFor = Exception.class)
public void batchReadFlagByMessageIds(String messageIds, MessageReadFlagEnum flagEnum) {
LambdaUpdateWrapper<SysMessage> updateWrapper = new LambdaUpdateWrapper<>();
updateWrapper.inSql(SysMessage::getMessageId, messageIds).set(SysMessage::getReadFlag, flagEnum.getCode());
sysMessageService.update(updateWrapper);
}
@Override
@Transactional(rollbackFor = Exception.class)
public void deleteByMessageId(Long messageId) {
LambdaUpdateWrapper<SysMessage> updateWrapper = new LambdaUpdateWrapper<>();
// 修改为逻辑删除
updateWrapper.eq(SysMessage::getMessageId, messageId)
.set(SysMessage::getDelFlag, YesOrNotEnum.Y.getCode());
sysMessageService.update(updateWrapper);
// sysMessageService.remove(updateWrapper);
}
@Override
@Transactional(rollbackFor = Exception.class)
public void batchDeleteByMessageIds(String messageIds) {
LambdaUpdateWrapper<SysMessage> updateWrapper = new LambdaUpdateWrapper<>();
updateWrapper.inSql(SysMessage::getMessageId, messageIds)
.set(SysMessage::getDelFlag, YesOrNotEnum.Y.getCode());
sysMessageService.update(updateWrapper);
}
@Override
public MessageResponse messageDetail(MessageParam messageParam) {
return null;
SysMessage sysMessage = sysMessageService.getById(messageParam.getMessageId());
// 判断消息为未读状态更新为已读
Optional.ofNullable(sysMessage).ifPresent(msg -> {
if(MessageReadFlagEnum.UNREAD.getCode().equals(sysMessage.getReadFlag())){
msg.setReadFlag(MessageReadFlagEnum.READ.getCode());
sysMessageService.updateById(msg);
}
});
MessageResponse messageResponse = new MessageResponse();
BeanUtil.copyProperties(sysMessage, messageResponse);
return messageResponse;
}
@Override
public PageResult<MessageResponse> queryMessagePage(MessageParam messageParam) {
return null;
public PageResult<MessageResponse> queryPage(MessageParam messageParam) {
PageResult<SysMessage> pageResult = sysMessageService.page(messageParam);
PageResult<MessageResponse> result = new PageResult<>();
BeanUtil.copyProperties(pageResult, result);
return result;
}
@Override
public List<MessageResponse> queryMessageList(MessageParam messageParam) {
return null;
public List<MessageResponse> queryList(MessageParam messageParam) {
List<SysMessage> messageList = sysMessageService.list(messageParam);
List<MessageResponse> resultList = new ArrayList<>();
BeanUtil.copyProperties(messageList, resultList);
return resultList;
}
@Override
public PageResult<MessageResponse> queryPageCurrentUser(MessageParam messageParam) {
if (ObjectUtil.isEmpty(messageParam)) {
messageParam = new MessageParam();
}
// 获取当前登录人
LoginUser loginUser = LoginContext.me().getLoginUser();
messageParam.setReceiveUserId(loginUser.getUserId());
return this.queryPage(messageParam);
}
@Override
public List<MessageResponse> queryListCurrentUser(MessageParam messageParam) {
if (ObjectUtil.isEmpty(messageParam)) {
messageParam = new MessageParam();
}
// 获取当前登录人
LoginUser loginUser = LoginContext.me().getLoginUser();
messageParam.setReceiveUserId(loginUser.getUserId());
return this.queryList(messageParam);
}
}

View File

@ -1,8 +1,12 @@
package cn.stylefeng.roses.kernel.message.db.service;
import cn.stylefeng.roses.kernel.db.api.pojo.page.PageResult;
import cn.stylefeng.roses.kernel.message.api.pojo.MessageParam;
import cn.stylefeng.roses.kernel.message.db.entity.SysMessage;
import com.baomidou.mybatisplus.extension.service.IService;
import java.util.List;
/**
* service
*
@ -10,5 +14,21 @@ import com.baomidou.mybatisplus.extension.service.IService;
* @date 2020/12/31 20:09
*/
public interface SysMessageService extends IService<SysMessage> {
/**
*
*
* @param messageParam
* @author liuhanqing
* @date 2021/1/2 15:21
*/
PageResult<SysMessage> page(MessageParam messageParam);
/**
*
*
* @param messageParam
* @author liuhanqing
* @date 2021/1/8 15:21
*/
List<SysMessage> list(MessageParam messageParam);
}

View File

@ -1,11 +1,21 @@
package cn.stylefeng.roses.kernel.message.db.service.impl;
import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.roses.kernel.db.api.factory.PageFactory;
import cn.stylefeng.roses.kernel.db.api.factory.PageResultFactory;
import cn.stylefeng.roses.kernel.db.api.pojo.page.PageResult;
import cn.stylefeng.roses.kernel.message.api.pojo.MessageParam;
import cn.stylefeng.roses.kernel.message.db.entity.SysMessage;
import cn.stylefeng.roses.kernel.message.db.mapper.SysMessageMapper;
import cn.stylefeng.roses.kernel.message.db.service.SysMessageService;
import cn.stylefeng.roses.kernel.rule.enums.YesOrNotEnum;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* service
*
@ -15,4 +25,56 @@ import org.springframework.stereotype.Service;
@Service
public class SysMessageServiceImpl extends ServiceImpl<SysMessageMapper, SysMessage> implements SysMessageService {
@Override
public PageResult<SysMessage> page(MessageParam messageParam) {
LambdaQueryWrapper<SysMessage> wrapper = createWrapper(messageParam);
Page<SysMessage> page = this.page(PageFactory.defaultPage(), wrapper);
return PageResultFactory.createPageResult(page);
}
@Override
public List<SysMessage> list(MessageParam messageParam) {
LambdaQueryWrapper<SysMessage> wrapper = createWrapper(messageParam);
return this.list(wrapper);
}
/**
* wrapper
*
* @author liuhanqing
* @date 2021/1/8 14:16
*/
private LambdaQueryWrapper<SysMessage> createWrapper(MessageParam messageParam) {
LambdaQueryWrapper<SysMessage> queryWrapper = new LambdaQueryWrapper<>();
if (ObjectUtil.isNotNull(messageParam)) {
// 拼接消息标题
if (ObjectUtil.isNotEmpty(messageParam.getMessageTitle())) {
queryWrapper.like(SysMessage::getMessageTitle, messageParam.getMessageTitle());
}
// 拼接接收人id查询条件
if (ObjectUtil.isNotEmpty(messageParam.getReceiveUserId())) {
queryWrapper.eq(SysMessage::getReceiveUserId, messageParam.getReceiveUserId());
}
// 拼接消息类型
if (ObjectUtil.isNotEmpty(messageParam.getMessageType())) {
queryWrapper.eq(SysMessage::getMessageType, messageParam.getMessageType());
}
// 拼接阅读状态
if (ObjectUtil.isNotEmpty(messageParam.getReadFlag())) {
queryWrapper.eq(SysMessage::getReadFlag, messageParam.getReadFlag());
}
}
// 查询未删除的
queryWrapper.ne(SysMessage::getDelFlag, YesOrNotEnum.Y.getCode());
// 按发送事件倒序
queryWrapper.orderByDesc(SysMessage::getMessageSendTime);
return queryWrapper;
}
}