燕山石化溯源三维电子沙盘-【后端】-服务
1
13693261870
2024-03-22 ee948f88d17424f02cfac5d96823ed52e4079d15
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package com.yssh.service;
 
import com.yssh.utils.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
 
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.*;
 
/**
 * 邮件服务类
 *
 * @author www
 * @date 2024-03-21
 */
@Service
public class EmailService {
    @Value("${email.userName}")
    private String userName;
 
    @Value("${email.password}")
    private String password;
 
    @Value("${email.smtpHost}")
    private String smtpHost;
 
    @Value("${email.smtpPort}")
    private String smtpPort;
 
    @Value("${email.smtpAuth}")
    private String smtpAuth;
 
    @Value("${email.smtpTls}")
    private String smtpTls;
 
    @Value("${email.from}")
    private String from;
 
    @Value("${email.to}")
    private String to;
 
    @Value("${email.cc}")
    private String cc;
 
    @Value("${csv.bigPath}")
    private String bigPath;
 
    @Value("${csv.filePath}")
    private String filePath;
 
    @Value("${csv.vocPath}")
    private String vocPath;
 
    @Value("${email.hours}")
    private Integer hours;
 
    @Value("${email.names}")
    private Integer names;
 
    private final static SimpleDateFormat YMDH = new SimpleDateFormat("yyyyMMddHH");
 
    private final static SimpleDateFormat Y_M_D_H = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
 
    protected final Logger logger = LoggerFactory.getLogger(this.getClass());
 
    public Session createSession() {
        // 创建一个配置文件,并保存
        Properties props = new Properties();
 
        // SMTP服务器连接信息:126—smtp.126.com,163—smtp.163.com,qq-smtp.qq.com"
        props.put("mail.smtp.host", smtpHost); // SMTP主机名
        props.put("mail.smtp.port", smtpPort); // 主机端口号:126—25,163—645,qq-587
        props.put("mail.smtp.auth", smtpAuth); // 是否需要用户认证
        props.put("mail.smtp.starttls.enale", smtpTls); // 启用TlS加密
 
        Session session = Session.getInstance(props, new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(userName, password);
            }
        });
 
        // 控制台打印调试信息
        session.setDebug(true);
 
        return session;
    }
 
    public Boolean send(String title, String text) {
        try {
            // 创建Session会话
            Session session = createSession();
 
            // 创建邮件对象
            MimeMessage message = new MimeMessage(session);
            message.setSubject(title);
            message.setText(text);
            message.setFrom(new InternetAddress(from));
            message.setRecipient(MimeMessage.RecipientType.TO, new InternetAddress(to));
            //message.setRecipients(Message.RecipientType.CC, new InternetAddress[] {new  InternetAddress("抄送人邮箱")});
            if (!StringUtils.isEmpty(cc)) {
                String[] strs = cc.split(",");
                InternetAddress[] ias = new InternetAddress[strs.length];
                for (int i = 0, c = strs.length; i < c; i++) {
                    ias[i] = new InternetAddress(strs[i]);
                }
                message.setRecipients(Message.RecipientType.CC, ias);
            }
 
            // 发送
            Transport.send(message);
 
            return true;
        } catch (Exception ex) {
            logger.error(ex.getMessage(), ex);
            return false;
        }
    }
 
    public int calcData() {
        List<String> list = new ArrayList<>();
 
        int count = countCsv(bigPath, hours);
        if (0 == count) {
            list.add("大CSV近 " + hours + " 小时存在文件缺失,");
        }
        count = countCsv(filePath, hours);
        if (0 == count) {
            list.add("中CSV近 " + hours + " 小时存在文件缺失,");
        }
        count = countCsv(vocPath, hours);
        if (0 == count) {
            list.add("小CSV近 " + hours + " 小时存在文件缺失,");
        }
 
        Calendar calendar = getCalendar(1);
        calendar = getCalendar(0);
        Integer iEnd = Integer.parseInt(YMDH.format(calendar.getTime()));
        String sEnd = Y_M_D_H.format(calendar.getTime());
        calendar.add(Calendar.HOUR, 1 - hours);
        Integer iStart = Integer.parseInt(YMDH.format(calendar.getTime()));
        String sStart = Y_M_D_H.format(calendar.getTime());
 
        int size = list.size();
 
        return size;
    }
 
    public int countCsv(String path, Integer hours) {
        try {
            int count = 0;
            Calendar calendar = getCalendar(1);
            for (int i = 0; i < hours; i++) {
                calendar.add(Calendar.HOUR, -1);
                String filePath = path + File.separator + YMDH.format(calendar.getTime()) + ".csv";
                if (new File(filePath).exists()) {
                    count++;
                }
            }
 
            return count;
        } catch (Exception ex) {
            logger.error(ex.getMessage(), ex);
            return 0;
        }
    }
 
    public Calendar getCalendar(int start) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.add(Calendar.HOUR, start);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
 
        return calendar;
    }
}