feat(公告消息模块): 公告消息的增删改查
This commit is contained in:
parent
be99aedaba
commit
2c2e3f8092
@ -0,0 +1,10 @@
|
||||
package com.zsc.edu.gateway.exception;
|
||||
|
||||
/**
|
||||
* @author zhuang
|
||||
*/
|
||||
public class EmptyIdsException extends RuntimeException {
|
||||
public EmptyIdsException(String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
@ -0,0 +1,13 @@
|
||||
package com.zsc.edu.gateway.exception;
|
||||
|
||||
/**
|
||||
* @author zhuang
|
||||
*/
|
||||
public class PublishFailedException extends RuntimeException {
|
||||
|
||||
public PublishFailedException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
}
|
||||
|
17
src/main/java/com/zsc/edu/gateway/framework/AppConfig.java
Normal file
17
src/main/java/com/zsc/edu/gateway/framework/AppConfig.java
Normal file
@ -0,0 +1,17 @@
|
||||
package com.zsc.edu.gateway.framework;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.client.RestClient;
|
||||
|
||||
/**
|
||||
* @author zhuang
|
||||
*/
|
||||
@Configuration
|
||||
public class AppConfig {
|
||||
|
||||
@Bean
|
||||
public RestClient restClient() {
|
||||
return RestClient.builder().build();
|
||||
}
|
||||
}
|
@ -0,0 +1,15 @@
|
||||
package com.zsc.edu.gateway.framework.message.email;
|
||||
|
||||
import lombok.Data;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* @author harry_yao
|
||||
*/
|
||||
@Data
|
||||
@ConfigurationProperties("spring.mail")
|
||||
@Configuration
|
||||
public class EmailProperties {
|
||||
public String username;
|
||||
}
|
@ -0,0 +1,108 @@
|
||||
package com.zsc.edu.gateway.framework.message.email;
|
||||
|
||||
|
||||
import com.zsc.edu.gateway.modules.attachment.entity.Attachment;
|
||||
import com.zsc.edu.gateway.modules.attachment.service.AttachmentService;
|
||||
import com.zsc.edu.gateway.modules.notice.entity.Message;
|
||||
import freemarker.template.Configuration;
|
||||
import freemarker.template.Template;
|
||||
import freemarker.template.TemplateException;
|
||||
import jakarta.annotation.Resource;
|
||||
import lombok.AllArgsConstructor;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.mail.javamail.JavaMailSender;
|
||||
import org.springframework.mail.javamail.MimeMessageHelper;
|
||||
import org.springframework.scheduling.annotation.Async;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import jakarta.mail.MessagingException;
|
||||
import jakarta.mail.internet.AddressException;
|
||||
import jakarta.mail.internet.InternetAddress;
|
||||
import jakarta.mail.internet.MimeMessage;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.StringWriter;
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
|
||||
|
||||
/**
|
||||
* @author pegnzheng
|
||||
*/
|
||||
@AllArgsConstructor
|
||||
@Component
|
||||
public class EmailSender {
|
||||
|
||||
private final static String TEMPLATE = "message.ftl";
|
||||
|
||||
private final EmailProperties config;
|
||||
private final Configuration freemarkerConfig;
|
||||
@Resource
|
||||
private final JavaMailSender sender;
|
||||
private final AttachmentService attachmentService;
|
||||
|
||||
@Async
|
||||
public void send(String email, Message message) {
|
||||
if (StringUtils.hasText(email)) {
|
||||
return;
|
||||
}
|
||||
InternetAddress to;
|
||||
try {
|
||||
to = new InternetAddress(email);
|
||||
to.validate();
|
||||
} catch (AddressException e) {
|
||||
return;
|
||||
}
|
||||
send(new InternetAddress[]{to}, message);
|
||||
}
|
||||
|
||||
@Async
|
||||
public void send(Set<String> emails, Message message) {
|
||||
InternetAddress[] to = emails.stream().filter(Objects::nonNull).map(email ->
|
||||
{
|
||||
try {
|
||||
return new InternetAddress(email);
|
||||
} catch (AddressException e) {
|
||||
return null;
|
||||
}
|
||||
}).filter(Objects::nonNull).filter(internetAddress -> {
|
||||
try {
|
||||
internetAddress.validate();
|
||||
} catch (AddressException e) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}).toArray(InternetAddress[]::new);
|
||||
if (to.length == 0) {
|
||||
return;
|
||||
}
|
||||
send(to, message);
|
||||
}
|
||||
|
||||
private void send(InternetAddress[] to, Message message) {
|
||||
try {
|
||||
MimeMessage mimeMessage = sender.createMimeMessage();
|
||||
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
|
||||
helper.setTo(to);
|
||||
helper.setFrom(config.username);
|
||||
helper.setSubject(message.getTitle());
|
||||
if (message.html) {
|
||||
StringWriter sw = new StringWriter();
|
||||
Template tp = freemarkerConfig.getTemplate(TEMPLATE, "UTF-8");
|
||||
tp.process(message, sw);
|
||||
helper.setText(sw.toString(), true);
|
||||
} else {
|
||||
helper.setText(message.content);
|
||||
}
|
||||
if (Objects.nonNull(message.attachments)) {
|
||||
for (Attachment attachment : message.attachments) {
|
||||
helper.addAttachment(attachment.fileName, attachmentService.loadAsResource(attachment.id), attachment.mimeType);
|
||||
}
|
||||
}
|
||||
sender.send(mimeMessage);
|
||||
} catch (MessagingException | IOException | TemplateException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,17 @@
|
||||
package com.zsc.edu.gateway.framework.message.sms;
|
||||
|
||||
import lombok.Data;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* @author harry_yao
|
||||
*/
|
||||
@Data
|
||||
@ConfigurationProperties("sms")
|
||||
@Configuration
|
||||
public class SmsProperties {
|
||||
public String apiKey = "4d8de516324886549d0ba3ddc4bf6f47";
|
||||
public String singleSendUrl = "https://sms.yunpian.com/v2/sms/single_send.json";
|
||||
public String batchSendUrl = "https://sms.yunpian.com/v2/sms/batch_send.json";
|
||||
}
|
@ -0,0 +1,47 @@
|
||||
package com.zsc.edu.gateway.framework.message.sms;
|
||||
|
||||
|
||||
import jakarta.annotation.Resource;
|
||||
import lombok.AllArgsConstructor;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.scheduling.annotation.Async;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.util.LinkedMultiValueMap;
|
||||
import org.springframework.util.MultiValueMap;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.springframework.web.client.RestClient;
|
||||
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* @author pegnzheng
|
||||
*/
|
||||
@AllArgsConstructor
|
||||
@Component
|
||||
public class SmsSender {
|
||||
|
||||
private final SmsProperties properties;
|
||||
@Resource
|
||||
private final RestClient restClient;
|
||||
|
||||
@Async
|
||||
public void send(String mobile, String text) {
|
||||
MultiValueMap<String, Object> params = new LinkedMultiValueMap<>();
|
||||
params.add("apikey", properties.apiKey);
|
||||
params.add("mobile", mobile);
|
||||
params.add("text", text);
|
||||
restClient.post().uri(properties.singleSendUrl).body(params);
|
||||
}
|
||||
|
||||
@Async
|
||||
public void send(Set<String> mobiles, String text) {
|
||||
MultiValueMap<String, Object> params = new LinkedMultiValueMap<>();
|
||||
params.add("apikey", properties.apiKey);
|
||||
params.add("mobile", mobiles.stream().filter(StringUtils::hasText).collect(Collectors.joining(",")));
|
||||
params.add("text", text);
|
||||
restClient.post().uri(properties.batchSendUrl).body(params);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,24 @@
|
||||
package com.zsc.edu.gateway.modules.notice.dto;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author zhuang
|
||||
*/
|
||||
@Data
|
||||
public class PageDto<T> {
|
||||
/**
|
||||
* 总条数
|
||||
*/
|
||||
private Long total;
|
||||
/**
|
||||
* 总页数
|
||||
*/
|
||||
private Integer pages;
|
||||
/**
|
||||
* 集合
|
||||
*/
|
||||
private List<T> list;
|
||||
}
|
@ -0,0 +1,22 @@
|
||||
package com.zsc.edu.gateway.modules.notice.entity;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.Setter;
|
||||
|
||||
/**
|
||||
* @author zhuang
|
||||
*/
|
||||
@Setter
|
||||
@Getter
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
@TableName("sys_message_attachment")
|
||||
public class MessageAttachment {
|
||||
|
||||
private Long messageId;
|
||||
|
||||
private String attachmentId;
|
||||
}
|
@ -0,0 +1,44 @@
|
||||
package com.zsc.edu.gateway.modules.notice.vo;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.FieldFill;
|
||||
import com.baomidou.mybatisplus.annotation.TableField;
|
||||
import com.zsc.edu.gateway.modules.notice.entity.Bulletin;
|
||||
import lombok.Data;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
/**
|
||||
* @author zhuang
|
||||
*/
|
||||
@Data
|
||||
public class BulletinVo {
|
||||
private Long id;
|
||||
private String title;
|
||||
private Bulletin.State state = Bulletin.State.edit;
|
||||
private Boolean top;
|
||||
private Long editUserId;
|
||||
private String editUsername;
|
||||
private LocalDateTime editTime;
|
||||
private Long publishUserId;
|
||||
private String publishUsername;
|
||||
private LocalDateTime publishTime;
|
||||
private Long closeUserId;
|
||||
private String closeUsername;
|
||||
private LocalDateTime closeTime;
|
||||
private String content;
|
||||
private Boolean isRead;
|
||||
private String fileName;
|
||||
private String mimeType;
|
||||
private LocalDateTime uploadTime;
|
||||
private String url;
|
||||
private String remark;
|
||||
@TableField(value = "create_time", fill = FieldFill.INSERT)
|
||||
private LocalDateTime createTime;
|
||||
@TableField(value = "create_by", fill = FieldFill.INSERT)
|
||||
private String createBy;
|
||||
@TableField(value = "update_time", fill = FieldFill.INSERT_UPDATE)
|
||||
private LocalDateTime updateTime;
|
||||
@TableField(value = "update_by", fill = FieldFill.INSERT_UPDATE)
|
||||
private String updateBy;
|
||||
|
||||
}
|
@ -0,0 +1,51 @@
|
||||
package com.zsc.edu.gateway.modules.notice.vo;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.FieldFill;
|
||||
import com.baomidou.mybatisplus.annotation.TableField;
|
||||
import com.baomidou.mybatisplus.annotation.TableId;
|
||||
import com.zsc.edu.gateway.modules.attachment.entity.Attachment;
|
||||
import com.zsc.edu.gateway.modules.notice.entity.Message;
|
||||
import com.zsc.edu.gateway.modules.notice.entity.MessageType;
|
||||
import com.zsc.edu.gateway.modules.system.entity.User;
|
||||
import lombok.Data;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author zhuang
|
||||
*/
|
||||
@Data
|
||||
public class UserMessageVo {
|
||||
private Long id;
|
||||
public Long userId;
|
||||
public User user;
|
||||
public Long messageId;
|
||||
public Message message;
|
||||
public Boolean isRead;
|
||||
public LocalDateTime readTime;
|
||||
|
||||
public String username;
|
||||
public String name;
|
||||
public String avatar;
|
||||
public String address;
|
||||
|
||||
public MessageType type = MessageType.other;
|
||||
public Boolean system;
|
||||
public Boolean email;
|
||||
public Boolean sms;
|
||||
public Boolean html;
|
||||
public String title;
|
||||
public String content;
|
||||
private String remark;
|
||||
@TableField(value = "create_time", fill = FieldFill.INSERT)
|
||||
private LocalDateTime createTime;
|
||||
@TableField(value = "create_by", fill = FieldFill.INSERT)
|
||||
private String createBy;
|
||||
@TableField(value = "update_time", fill = FieldFill.INSERT_UPDATE)
|
||||
private LocalDateTime updateTime;
|
||||
@TableField(value = "update_by", fill = FieldFill.INSERT_UPDATE)
|
||||
private String updateBy;
|
||||
|
||||
public List<Attachment> attachments;
|
||||
}
|
13
src/main/resources/mappers/noticeMappers/MessageMapper.xml
Normal file
13
src/main/resources/mappers/noticeMappers/MessageMapper.xml
Normal file
@ -0,0 +1,13 @@
|
||||
<?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.zsc.edu.gateway.modules.notice.repo.MessageRepository">
|
||||
<select id="selectById" resultType="com.zsc.edu.gateway.modules.notice.entity.Message">
|
||||
select *
|
||||
from sys_message sm
|
||||
left join sys_message_attachment sma on sm.id = sma.message_id
|
||||
left join attachment a on sma.attachment_id = a.id
|
||||
where sm.id = #{messageId}
|
||||
</select>
|
||||
</mapper>
|
@ -0,0 +1,87 @@
|
||||
<?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.zsc.edu.gateway.modules.notice.repo.UserMessageRepository">
|
||||
<resultMap id="userMessageMap" type="com.zsc.edu.gateway.modules.notice.vo.UserMessageVo">
|
||||
<id column="id" jdbcType="BIGINT" property="id"/>
|
||||
<result column="is_read" jdbcType="BOOLEAN" property="isRead"/>
|
||||
<result column="read_time" jdbcType="TIMESTAMP" property="readTime"/>
|
||||
<result column="username" jdbcType="VARCHAR" property="username"/>
|
||||
<result column="address" jdbcType="VARCHAR" property="address"/>
|
||||
<result column="avatar" jdbcType="VARCHAR" property="avatar"/>
|
||||
<result column="name" jdbcType="VARCHAR" property="name"/>
|
||||
<result column="email" jdbcType="VARCHAR" property="email"/>
|
||||
<result column="type" jdbcType="INTEGER" property="type"/>
|
||||
<result column="system" jdbcType="BOOLEAN" property="system"/>
|
||||
<result column="email" jdbcType="BOOLEAN" property="email"/>
|
||||
<result column="sms" jdbcType="BOOLEAN" property="sms"/>
|
||||
<result column="html" jdbcType="BOOLEAN" property="html"/>
|
||||
<result column="title" jdbcType="VARCHAR" property="title"/>
|
||||
<result column="content" jdbcType="VARCHAR" property="content"/>
|
||||
<result column="create_by" jdbcType="VARCHAR" property="createBy"/>
|
||||
<result column="update_by" jdbcType="VARCHAR" property="updateBy"/>
|
||||
<result column="create_time" jdbcType="DATE" property="createTime"/>
|
||||
<result column="update_time" jdbcType="DATE" property="updateTime"/>
|
||||
<result column="remark" jdbcType="VARCHAR" property="remark"/>
|
||||
<collection property="attachments" ofType="com.zsc.edu.gateway.modules.attachment.entity.Attachment">
|
||||
<id column="id" jdbcType="VARCHAR" property="id"/>
|
||||
<result column="file_name" jdbcType="VARCHAR" property="fileName"/>
|
||||
<result column="mime_type" jdbcType="VARCHAR" property="mimeType"/>
|
||||
<result column="upload_time" jdbcType="TIMESTAMP" property="uploadTime"/>
|
||||
<result column="url" jdbcType="VARCHAR" property="url"/>
|
||||
</collection>
|
||||
</resultMap>
|
||||
<select id="selectByMessageIdAndUserId" resultType="com.zsc.edu.gateway.modules.notice.vo.UserMessageVo"
|
||||
resultMap="userMessageMap">
|
||||
select sum.id as user_message_id,sum.user_id as user_message_user_id,sum.message_id as
|
||||
user_message_message_id,sum.is_read as user_message_is_read,sum.read_time as user_message_read_time,su.id as
|
||||
user_id,su.username as user_username,su.address as user_address,su.avatar as user_avatar,su.name as
|
||||
user_name,su.phone as user_phone,sm.*,sm.email as message_email,a.*
|
||||
from sys_user_message sum
|
||||
left join sys_user su on sum.id=su.id
|
||||
left join sys_message sm on sm.id=sum.id
|
||||
left join sys_message_attachment sma on sm.id=sma.message_id
|
||||
left join attachment a on sma.attachment_id=a.id
|
||||
<where>
|
||||
sm.id=#{messageId}
|
||||
and su.id=#{userId}
|
||||
</where>
|
||||
</select>
|
||||
|
||||
<select id="query" resultType="com.zsc.edu.gateway.modules.notice.vo.UserMessageVo" resultMap="userMessageMap">
|
||||
select sum.id as user_message_id,sum.user_id as user_message_user_id,sum.message_id as
|
||||
user_message_message_id,sum.is_read as user_message_is_read,sum.read_time as user_message_read_time,su.id as
|
||||
user_id,su.username as user_username,su.address as user_address,su.avatar as user_avatar,su.name as
|
||||
user_name,su.email as user_email,su.phone as user_phone,sm.*,sm.email as message_email,a.*
|
||||
from sys_user_message sum
|
||||
left join sys_user su on sum.id=su.id
|
||||
left join sys_message sm on sm.id=sum.id
|
||||
left join sys_message_attachment sma on sm.id=sma.message_id
|
||||
left join attachment a on sma.attachment_id=a.id
|
||||
where 1=1
|
||||
<if test="query.userId != null">
|
||||
AND sum.user_id = #{query.userId}
|
||||
</if>
|
||||
<if test="query.title != null and query.title != ''">
|
||||
AND sm.title LIKE CONCAT('%', #{query.title}, '%')
|
||||
</if>
|
||||
<if test="query.type != null">
|
||||
AND sm.type = #{query.type}
|
||||
</if>
|
||||
<if test="query.name != null and query.name != ''">
|
||||
AND su.username LIKE CONCAT('%', #{query.name}, '%')
|
||||
</if>
|
||||
<if test="query.system != null">
|
||||
AND sm.system = #{query.system}
|
||||
</if>
|
||||
<if test="query.isRead != null">
|
||||
AND sum.is_read = #{query.isRead}
|
||||
</if>
|
||||
<if test="query.createAtBegin != null and query.createAtEnd != null">
|
||||
AND sm.create_time BETWEEN #{query.createAtBegin} AND #{query.createAtEnd}
|
||||
</if>
|
||||
ORDER BY
|
||||
sm.create_time DESC
|
||||
</select>
|
||||
</mapper>
|
Loading…
Reference in New Issue
Block a user