提交 831ae4e2 作者: han

消息管理

上级 46502006
流水线 #7597 已通过 于阶段
in 2 分 25 秒
package com.mmc.iuav.user.model.vo;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.io.Serializable;
import java.util.Date;
/**
* @author han
*/
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class SystemMessageVO implements Serializable {
private static final long serialVersionUID = 973044768156590099L;
@ApiModelProperty(value = "消息主题")
private String subject;
@ApiModelProperty(value = "消息内容")
private String content;
}
package com.mmc.iuav.user.controller;
import com.mmc.iuav.response.ResultBody;
import com.mmc.iuav.user.model.vo.SystemMessageVO;
import com.mmc.iuav.user.service.MessageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
@Api(tags = "消息管理")
@RequestMapping("/message/")
@RestController
public class MessageController extends BaseController{
@Resource
private MessageService messageService;
@ApiOperation(value = "后台发布消息——新增系统消息")
@ApiResponses({@ApiResponse(code = 200, message = "OK", response = ResultBody.class)})
@PostMapping("/insertSystemMessage")
public ResultBody insertSystemMessage(@RequestBody SystemMessageVO messageVO){
return messageService.insertSystemMessage(messageVO);
}
@ApiOperation(value = "后台——根据ID查询系统消息")
@ApiResponses({@ApiResponse(code = 200, message = "OK", response = ResultBody.class)})
@GetMapping("/selectSystemMessageById")
public ResultBody selectSystemMessageById(@RequestParam Integer id){
return messageService.selectSystemMessageById(id);
}
@ApiOperation(value = "后台——系统消息列表")
@ApiResponses({@ApiResponse(code = 200, message = "OK", response = ResultBody.class)})
@GetMapping("/selectAllSystemMessage")
public ResultBody selectAllSystemMessage(){
return messageService.selectAllSystemMessage();
}
@ApiOperation(value = "后台——删除消息")
@ApiResponses({@ApiResponse(code = 200, message = "OK", response = ResultBody.class)})
@PostMapping("/removeMessage")
public ResultBody removeMessage(@RequestParam Integer id){
return messageService.removeMessage(id);
}
@ApiOperation(value = "用户——系统消息列表")
@ApiResponses({@ApiResponse(code = 200, message = "OK", response = ResultBody.class)})
@GetMapping("/systemMessageListByUser")
public ResultBody selectAllSystemMessageByUser(HttpServletRequest request){
Integer userId = this.getUserLoginInfoFromRedis(request).getUserAccountId();
return messageService.selectAllSystemMessageByReceive(userId);
}
@ApiOperation(value = "用户查看——系统消息详情")
@ApiResponses({@ApiResponse(code = 200, message = "OK", response = ResultBody.class)})
@GetMapping("/showSystemMessageByUser")
public ResultBody showSystemMessageByUser(HttpServletRequest request, @RequestParam Integer messageId){
Integer userId = this.getUserLoginInfoFromRedis(request).getUserAccountId();
return messageService.showSystemMessageByUser(userId,messageId);
}
@ApiOperation(value = "用户——删除接收的消息")
@ApiResponses({@ApiResponse(code = 200, message = "OK", response = ResultBody.class)})
@PostMapping("/removeMessageByUser")
public ResultBody removeMessageByUser(@RequestParam Integer id){
return messageService.removeMessageByUser(id);
}
}
package com.mmc.iuav.user.dao;
import com.mmc.iuav.user.entity.SystemMessageDO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import java.util.List;
/**
* @author han
* @createDate 2023-12-14 10:46:45
*/
@Mapper
public interface MessageDao {
/**
* 添加系统消息
* @param messageDO
* @return
*/
int insertSystemMessage(SystemMessageDO messageDO);
/**
* 根据ID查询系统消息
* @param id
* @return
*/
SystemMessageDO selectSystemMessageById(Integer id);
/**
* 查询所有的系统消息
* @return
*/
List<SystemMessageDO> selectAllSystemMessage();
/**
* 删除消息
* @param id
* @return
*/
int removeMessage(Integer id);
/**
* 查询用户是否读取过
*/
int selectReceiverMessage(@Param("userId") Integer userId,@Param("messageId") Integer messageId);
/**
* 添加用户读取过得信息
* @param userId
* @param messageId
* @return
*/
int insertReceiverMessage(@Param("userId") Integer userId,@Param("messageId") Integer messageId);
/**
* 删除读取过的消息
* @param id
* @return
*/
int removeReceiverMessage(Integer id);
/**
* 查询读取过但是已经被用户删除的信息
* @param userId
* @param messageId
* @return
*/
int selectReceiverMessageByDel(@Param("userId") Integer userId,@Param("messageId") Integer messageId);
}
package com.mmc.iuav.user.entity;
import com.alibaba.fastjson2.annotation.JSONField;
import com.mmc.iuav.user.model.vo.RoleInfoVO;
import com.mmc.iuav.user.model.vo.SystemMessageVO;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.io.Serializable;
import java.util.Date;
/**
* @author han
*/
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class SystemMessageDO implements Serializable {
private static final long serialVersionUID = 973044768156590099L;
/**
* 消息ID
*/
private Integer id;
/**
* 消息主题
*/
private String subject;
/**
* 消息内容
*/
private String content;
/**
* 是否读过 false为未读 true为已读
*/
private boolean isRead;
/**
* 创建时间
*/
@JSONField(format = "yyyy-MM-dd HH:mm:ss")
private Date createTime;
public SystemMessageDO(SystemMessageVO message) {
this.subject = message.getSubject();
this.content = message.getContent();
}
}
package com.mmc.iuav.user.service;
import com.mmc.iuav.response.ResultBody;
import com.mmc.iuav.user.entity.SystemMessageDO;
import com.mmc.iuav.user.model.vo.SystemMessageVO;
import java.util.List;
public interface MessageService {
/**
* 添加系统消息
* @param messageVO
* @return
*/
ResultBody insertSystemMessage(SystemMessageVO messageVO);
/**
* 根据ID查询系统消息
* @param id
* @return
*/
ResultBody selectSystemMessageById(Integer id);
/**
* 后台发布——系统消息列表
* 查询所有的系统消息
* @return
*/
ResultBody selectAllSystemMessage();
/**
* 删除消息
* @param id
* @return
*/
ResultBody removeMessage(Integer id);
/**
* 用户接收——系统消息列表
* 查询所有的系统消息
* @return
*/
ResultBody selectAllSystemMessageByReceive(Integer userId);
/**
* 用户查看——系统消息详情
* @return
*/
ResultBody showSystemMessageByUser(Integer userId, Integer messageId);
/**
* 用户——删除用户接收的消息
* @param id
* @return
*/
ResultBody removeMessageByUser(Integer id);
}
package com.mmc.iuav.user.service.impl;
import com.mmc.iuav.response.ResultBody;
import com.mmc.iuav.user.dao.MessageDao;
import com.mmc.iuav.user.entity.SystemMessageDO;
import com.mmc.iuav.user.model.vo.SystemMessageVO;
import com.mmc.iuav.user.service.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
@Service
public class MessageServiceImpl implements MessageService {
@Autowired
private MessageDao messageDao;
@Override
public ResultBody insertSystemMessage(SystemMessageVO messageVO) {
SystemMessageDO systemMessageDO = new SystemMessageDO(messageVO);
if(messageDao.insertSystemMessage(systemMessageDO) != 0){
return ResultBody.success("添加成功");
}
return ResultBody.error("添加失败");
}
@Override
public ResultBody selectSystemMessageById(Integer id) {
return ResultBody.success(messageDao.selectSystemMessageById(id));
}
@Override
public ResultBody selectAllSystemMessage() {
return ResultBody.success(messageDao.selectAllSystemMessage());
}
@Override
public ResultBody removeMessage(Integer id) {
if(messageDao.removeMessage(id) != 0){
return ResultBody.success("删除成功");
}
return ResultBody.error("删除失败");
}
@Override
public ResultBody selectAllSystemMessageByReceive(Integer userId) {
if(userId == null){
ResultBody.error("未登录");
}
List<SystemMessageDO> messagesDO = new ArrayList<>();
List<SystemMessageDO> messages = messageDao.selectAllSystemMessage();
for (SystemMessageDO message : messages) {
if(messageDao.selectReceiverMessage(userId, message.getId()) != 0){
// 已读
message.setRead(true);
}
// 读过但是已经删除
if(messageDao.selectReceiverMessageByDel(userId, message.getId()) != 0){
continue;
}
messagesDO.add(message);
}
return ResultBody.success(messagesDO);
}
@Override
public ResultBody showSystemMessageByUser(Integer userId, Integer messageId) {
if(userId == null){
ResultBody.error("未登录");
}
SystemMessageDO message = messageDao.selectSystemMessageById(messageId);
if(message == null){
return ResultBody.success(message);
}
//没有读过,查看之后标为已读
if(messageDao.selectReceiverMessage(userId, messageId) == 0){
messageDao.insertReceiverMessage(userId,messageId);
}
message.setRead(true);
// 读过但是已经删除
if(messageDao.selectReceiverMessageByDel(userId, message.getId()) != 0){
message = null;
}
return ResultBody.success(message);
}
@Override
public ResultBody removeMessageByUser(Integer id) {
if(messageDao.removeReceiverMessage(id) != 0){
return ResultBody.success("删除成功");
}
return ResultBody.error("删除失败");
}
}
<?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="com.mmc.iuav.user.dao.MessageDao">
<select id="selectSystemMessageById" resultType="com.mmc.iuav.user.entity.SystemMessageDO">
select
id, subject, content, type, create_time
from message
where id = #{id} and is_deleted = 0
</select>
<select id="selectAllSystemMessage" resultType="com.mmc.iuav.user.entity.SystemMessageDO">
select
id, subject, content, type, create_time
from message
where type = 0 and is_deleted = 0
</select>
<select id="selectReceiverMessage" resultType="java.lang.Integer">
select count(*)
from receiver_message
where user_id = #{userId} and message_id = #{messageId}
</select>
<select id="selectReceiverMessageByDel" resultType="java.lang.Integer">
select count(*)
from receiver_message
where user_id = #{userId} and message_id = #{messageId} and is_deleted = 1
</select>
<insert id="insertSystemMessage">
insert into message
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="subject != null">subject,</if>
<if test="content != null">content,</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="subject != null">#{subject,jdbcType=VARCHAR},</if>
<if test="content != null">#{content,jdbcType=VARCHAR},</if>
</trim>
</insert>
<insert id="insertReceiverMessage">
insert into receiver_message
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="userId != null">user_id,</if>
<if test="messageId != null">message_id,</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="userId != null">#{userId},</if>
<if test="messageId != null">#{messageId},</if>
</trim>
</insert>
<update id="removeMessage">
update message
set is_deleted = 1, deleted_time = NOW()
where id = #{id,jdbcType=INTEGER}
</update>
<update id="removeReceiverMessage">
update receiver_message
set is_deleted = 1, deleted_time = NOW()
where id = #{id,jdbcType=INTEGER}
</update>
</mapper>
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论