Commit 85d08bda authored by wujipeng's avatar wujipeng
Browse files

项目初始化

parents
HELP.md
target/
!.mvn/wrapper/maven-wrapper.jar
!**/src/main/**/target/
!**/src/test/**/target/
### STS ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
### IntelliJ IDEA ###
.idea
*.iws
*.iml
*.ipr
### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
build/
!**/src/main/**/build/
!**/src/test/**/build/
### VS Code ###
.vscode/
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.ruzhoukj</groupId>
<artifactId>sms</artifactId>
<version>0.1</version>
<description>sms</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.2.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--lombok工具-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.8</version>
</dependency>
<!--数据库连接-->
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
<version>6.4.0.jre8</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!--mybatis-plus-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.0</version>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.8.4</version>
</dependency>
</dependencies>
<repositories>
<repository>
<id>central</id>
<url>https://repo.maven.apache.org/maven2</url>
</repository>
<!-- 可以添加其他可用的仓库 -->
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
package com.ruzhoukj.sms;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;
/**
* @author 邬吉朋
*/
@SpringBootApplication
@EnableScheduling
public class SmsApplication {
public static void main(String[] args) {
SpringApplication.run(SmsApplication.class, args);
}
}
package com.ruzhoukj.sms.dao;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.ruzhoukj.sms.entity.SSLEntity;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import java.util.Date;
import java.util.List;
/**
* @author wujipeng
* @version 1.0
* @create 2024-08-16 13:43
*/
@Mapper
public interface SSLDao extends BaseMapper<SSLEntity> {
/**
* 查询本月要过期的项目
*
* @param startDate 月初
* @param endDate 月末
* @return List<SSLEntity>
*/
List<SSLEntity> listSsl(@Param("startDate") Date startDate, @Param("endDate") Date endDate);
/**
* 查询七天内要过期的项目
*
* @return List<SSLEntity>
*/
List<SSLEntity> listSslExigency();
}
package com.ruzhoukj.sms.dao;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.ruzhoukj.sms.entity.ScheduleEntity;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import java.util.Date;
/**
* @author wujipeng
* @version 1.0
* @create 2024-08-16 13:44
*/
@Mapper
public interface ScheduleDao extends BaseMapper<ScheduleEntity> {
/**
* 查询本月运维人员
*
* @return ScheduleEntity
*/
ScheduleEntity getUser(@Param("startDate") Date startDate, @Param("endDate") Date endDate);
}
package com.ruzhoukj.sms.entity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;
import java.time.LocalDate;
import java.util.Date;
/**
* @author wujipeng
* @version 1.0
* @create 2024-08-16 13:42
*/
@Data
@TableName(value = "ssl")
public class SSLEntity {
@TableId(type = IdType.AUTO)
private Integer id;
private String name;
private LocalDate time;
}
package com.ruzhoukj.sms.entity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;
import java.util.Date;
/**
* @author wujipeng
* @version 1.0
* @create 2024-08-16 13:40
*/
@Data
@TableName(value = "schedule")
public class ScheduleEntity {
@TableId(type = IdType.AUTO)
private Integer id;
private String name;
private String email;
@JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")
private Date time;
}
package com.ruzhoukj.sms.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.ruzhoukj.sms.entity.SSLEntity;
import java.util.Date;
import java.util.List;
/**
* @author wujipeng
* @version 1.0
* @create 2024-08-16 13:47
*/
public interface SSLService extends IService<SSLEntity> {
/**
* 查询本月要过期的项目
*
* @param startDate 月初
* @param endDate 月末
* @return List<SSLEntity>
*/
List<SSLEntity> listSsl(Date startDate, Date endDate);
/**
* 查询七天内要过期的项目
*
* @return List<SSLEntity>
*/
List<SSLEntity> listSslExigency();
}
package com.ruzhoukj.sms.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.ruzhoukj.sms.entity.ScheduleEntity;
import java.util.Date;
/**
* @author wujipeng
* @version 1.0
* @create 2024-08-16 13:48
*/
public interface ScheduleService extends IService<ScheduleEntity> {
/**
* 查询本月运维人员
*
* @return ScheduleEntity
*/
ScheduleEntity getUser(Date startDate, Date endDate);
}
package com.ruzhoukj.sms.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruzhoukj.sms.dao.SSLDao;
import com.ruzhoukj.sms.entity.SSLEntity;
import com.ruzhoukj.sms.service.SSLService;
import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.List;
/**
* @author wujipeng
* @version 1.0
* @create 2024-08-16 13:47
*/
@Service
public class SSLServiceImpl extends ServiceImpl<SSLDao, SSLEntity> implements SSLService {
@Override
public List<SSLEntity> listSsl(Date startDate, Date endDate) {
return baseMapper.listSsl(startDate, endDate);
}
@Override
public List<SSLEntity> listSslExigency() {
return baseMapper.listSslExigency();
}
}
package com.ruzhoukj.sms.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruzhoukj.sms.dao.ScheduleDao;
import com.ruzhoukj.sms.entity.ScheduleEntity;
import com.ruzhoukj.sms.service.ScheduleService;
import org.springframework.stereotype.Service;
import java.util.Date;
/**
* @author wujipeng
* @version 1.0
* @create 2024-08-16 13:49
*/
@Service
public class ScheduleServiceImpl extends ServiceImpl<ScheduleDao, ScheduleEntity> implements ScheduleService {
@Override
public ScheduleEntity getUser(Date startDate, Date endDate) {
return baseMapper.getUser(startDate, endDate);
}
}
package com.ruzhoukj.sms.task;
import cn.hutool.core.bean.BeanUtil;
import com.ruzhoukj.sms.entity.SSLEntity;
import com.ruzhoukj.sms.entity.ScheduleEntity;
import com.ruzhoukj.sms.service.SSLService;
import com.ruzhoukj.sms.service.ScheduleService;
import com.ruzhoukj.sms.util.DateUtils;
import com.ruzhoukj.sms.util.EmailUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
/**
* @author wujipeng
* @version 1.0
* @create 2024-08-16 13:45
*/
@Component
@Slf4j
public class OpsTask {
@Resource
private ScheduleService scheduleService;
@Resource
private SSLService sslService;
//每个月一号执行一次,本月要到期的证书
@Scheduled(cron = "0 10 1 * * ?")
public void slow() {
//查询有没有本月要到期的ssl证书
//获取本月开始时间和结束时间
Date[] dates = DateUtils.getStartAndEndDateOfCurrentMonth();
List<SSLEntity> sslEntities = sslService.listSsl(dates[0], dates[1]);
//如果有就发送邮件到指定人邮箱
if (BeanUtil.isNotEmpty(sslEntities) && !sslEntities.isEmpty()) {
//查询本月维护人的信息
ScheduleEntity scheduleEntity = scheduleService.getUser(dates[0], dates[1]);
if (BeanUtil.isNotEmpty(scheduleEntity)) {
//发送邮件
EmailUtil.sendOperationsEmail(scheduleEntity.getName(), scheduleEntity.getEmail(), sslEntities);
}
}
}
//每天执行一次,七天内要到期的证书
@Scheduled(cron = "0 10 * * * ?")
public void fast() {
//查询有没有本月要到期的ssl证书
//获取本月开始时间和结束时间
Date[] dates = DateUtils.getStartAndEndDateOfCurrentMonth();
List<SSLEntity> sslEntities = sslService.listSslExigency();
//如果有就发送邮件到指定人邮箱
if (BeanUtil.isNotEmpty(sslEntities) && !sslEntities.isEmpty()) {
//查询本月维护人的信息
ScheduleEntity scheduleEntity = scheduleService.getUser(dates[0], dates[1]);
if (BeanUtil.isNotEmpty(scheduleEntity)) {
//发送邮件
EmailUtil.sendOperationsExigencyEmail(scheduleEntity.getName(), scheduleEntity.getEmail(), sslEntities);
}
}
}
}
package com.ruzhoukj.sms.task;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class SendEmailExample {
public static void main(String[] args) {
final String username = "2404263178@qq.com";
final String password = "eryzvsveckiudiee";
// QQ Mail SMTP server information
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", "smtp.qq.com");
props.put("mail.smtp.port", "587");
Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
@Override
protected javax.mail.PasswordAuthentication getPasswordAuthentication() {
return new javax.mail.PasswordAuthentication(username, password);
}
});
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(username));
// 添加多个收件人
String[] toAddresses = {"2404263178@qq.com","zhuxiang@ruzhoukj.com"};
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(String.join(", ", toAddresses)));
message.setSubject("ssl证书即将到期");
message.setText("This is the message body");
Transport.send(message);
System.out.println("Email sent successfully to multiple recipients.");
} catch (MessagingException e) {
throw new RuntimeException(e);
}
}
}
package com.ruzhoukj.sms.util;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
/**
* @author 邬吉朋
*/
public class DateUtils {
/**
* 获取当前月份的开始和结束日期。
*
* @return 一个包含两个元素的数组,第一个元素是当前月份的开始日期,
* 第二个元素是当前月份的结束日期。
*/
public static Date[] getStartAndEndDateOfCurrentMonth() {
// 获取当前日期
LocalDate today = LocalDate.now();
// 获取本月第一天和最后一天
LocalDate firstDate = today.withDayOfMonth(1);
LocalDate lastDate = today.withDayOfMonth(today.lengthOfMonth());
// 转换为LocalDateTime并设置时间
LocalDateTime firstDateTime = firstDate.atStartOfDay(); // 本月第一天的开始时间
LocalDateTime lastDateTime = lastDate.atTime(23, 59, 59); // 本月最后一天的结束时间
// 转换为Date
Date startDate = Date.from(firstDateTime.atZone(ZoneId.systemDefault()).toInstant());
Date endDate = Date.from(lastDateTime.atZone(ZoneId.systemDefault()).toInstant());
return new Date[]{startDate, endDate};
}
public static void main(String[] args) {
Date[] dates = getStartAndEndDateOfCurrentMonth();
System.out.println("Start date of this month: " + dates[0]);
System.out.println("End date of this month: " + dates[1]);
}
}
package com.ruzhoukj.sms.util;
import com.ruzhoukj.sms.entity.SSLEntity;
import lombok.extern.slf4j.Slf4j;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.List;
import java.util.Properties;
import java.util.StringJoiner;
/**
* @author wujipeng
* @version 1.0
* @create 2024-08-16 13:56
*/
@Slf4j
public class EmailUtil {
public static void sendOperationsEmail(String name, String email, List<SSLEntity> list) {
final String username = "2404263178@qq.com";
final String password = "eryzvsveckiudiee";
// QQ Mail SMTP server information
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", "smtp.qq.com");
props.put("mail.smtp.port", "587");
Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
@Override
protected javax.mail.PasswordAuthentication getPasswordAuthentication() {
return new javax.mail.PasswordAuthentication(username, password);
}
});
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(username));
// 添加多个收件人
String[] toAddresses = {email};
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(String.join(", ", toAddresses)));
StringJoiner stringJoiner = new StringJoiner("\n");
stringJoiner.add(name + "您好,以下是本月运维提醒");
for (SSLEntity sslEntity : list) {
String msg = sslEntity.getName() + "即将到期," + "到期时间:" + sslEntity.getTime();
stringJoiner.add(msg);
}
stringJoiner.add("请及时处理");
message.setSubject("本月运维提醒");
message.setText(stringJoiner.toString());
Transport.send(message);
log.info("Email sent successfully");
} catch (MessagingException e) {
throw new RuntimeException(e);
}
}
public static void sendOperationsExigencyEmail(String name, String email, List<SSLEntity> list) {
final String username = "2404263178@qq.com";
final String password = "eryzvsveckiudiee";
// QQ Mail SMTP server information
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", "smtp.qq.com");
props.put("mail.smtp.port", "587");
Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
@Override
protected javax.mail.PasswordAuthentication getPasswordAuthentication() {
return new javax.mail.PasswordAuthentication(username, password);
}
});
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(username));
// 添加多个收件人
String[] toAddresses = {email};
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(String.join(", ", toAddresses)));
StringJoiner stringJoiner = new StringJoiner("\n");
stringJoiner.add(name + "您好,以下项目即将在七天内到期");
for (SSLEntity sslEntity : list) {
String msg = sslEntity.getName() + "到期时间:" + sslEntity.getTime();
stringJoiner.add(msg);
}
stringJoiner.add("请及时处理");
message.setSubject("紧急运维提醒");
message.setText(stringJoiner.toString());
Transport.send(message);
log.info("Email sent successfully");
} catch (MessagingException e) {
throw new RuntimeException(e);
}
}
}
server:
port: 8123
spring:
datasource:
username: root
password: ruzhoukj@2022
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://192.168.10.3:3307/ops?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8&nullCatalogMeansCurrent=true
<?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.ruzhoukj.sms.dao.SSLDao">
<select id="listSsl" resultType="com.ruzhoukj.sms.entity.SSLEntity">
select
id, name, time
from
`ssl`
where
date_format(time,'%Y%M%d')
between
date_format(#{startDate},'%Y%M%d')
and
date_format(#{endDate},'%Y%M%d');
</select>
<select id="listSslExigency" resultType="com.ruzhoukj.sms.entity.SSLEntity">
select
id, name, time
from
`ssl`
where
date_format(time,'%Y%M%d')
between
date_format(now(),'%Y%M%d')
and
date_format(date_add(now() ,interval 7 day),'%Y%M%d');
</select>
</mapper>
<?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.ruzhoukj.sms.dao.ScheduleDao">
<select id="getUser" resultType="com.ruzhoukj.sms.entity.ScheduleEntity">
select
id,
name,
email,
time
from
schedule
where
date_format(time,'%Y%M%d')
between
date_format(#{startDate},'%Y%M%d')
and
date_format(#{endDate},'%Y%M%d');
</select>
</mapper>
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment