package com.yssh.service; import com.yssh.mapper.QxshMapper; 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.annotation.Resource; import javax.mail.*; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; import java.io.File; import java.text.SimpleDateFormat; import java.util.*; @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 String names; @Value("${email.title}") private String title; @Value("${email.enable}") private Boolean enable; @Value("${email.debug}") private Boolean debug; @Resource private QxshMapper qxshMapper; private static boolean isBusy = false; 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(); // 126—smtp.126.com,163—smtp.163.com,qq-smtp.qq.com" props.put("mail.smtp.host", smtpHost); props.put("mail.smtp.port", smtpPort); props.put("mail.smtp.auth", smtpAuth); props.put("mail.smtp.starttls.enale", smtpTls); Session session = Session.getInstance(props, new Authenticator() { @Override protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(userName, password); } }); session.setDebug(debug); return session; } public Boolean send(String title, String text) { try { if (!enable) { return null; } 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 Integer calcData() { try { if (isBusy) { return null; } isBusy = true; List list = new ArrayList<>(); calcCsvData(list); calcDbData(list); int size = list.size(); if (size > 0) { String text = String.join("、", list.toArray(new String[list.size()])) + ",近" + hours + "小时内存在数据缺失。"; send(title, text); } isBusy = false; return size; } catch (Exception ex) { logger.error(ex.getMessage(), ex); isBusy = false; return null; } } private void calcCsvData(List list) { String[] strs = names.split(","); int count = countCsv(bigPath, hours); if (0 == count) { list.add(strs[0]); } count = countCsv(filePath, hours); if (0 == count) { list.add(strs[1]); } count = countCsv(vocPath, hours); if (0 == count) { list.add(strs[2]); } } private void calcDbData(List list) { Calendar 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()); String[] strs = names.split(","); int count = qxshMapper.countGcsjByTime(iStart, iEnd); if (0 == count) { list.add(strs[3]); } count = qxshMapper.countQxshByTime(iStart, iEnd); if (0 == count) { list.add(strs[4]); } count = qxshMapper.countSuYuan46ByTime(sStart, sEnd); if (0 == count) { list.add(strs[5]); } count = qxshMapper.countSuYuan70ByTime(sStart, sEnd); if (0 == count) { list.add(strs[6]); } count = qxshMapper.countSuYuanFastByTime(sStart, sEnd); if (0 == count) { list.add(strs[7]); } } 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; } }