Authored by LUOXC

refactor

... ... @@ -15,6 +15,7 @@ import com.yohoufo.dal.product.ProductMapper;
import com.yohoufo.dal.product.StoragePriceMapper;
import com.yohoufo.dal.product.model.Product;
import com.yohoufo.dal.product.model.StoragePrice;
import com.yohoufo.order.utils.MailSender;
import lombok.Builder;
import lombok.Getter;
import lombok.Setter;
... ... @@ -92,7 +93,7 @@ public class HkAccountSettlement {
.reduce(BigDecimal.ZERO, BigDecimal::add);
log.info("{} settle, sum income is {}", uid, sumIncome);
// 满足打款条件,通知财务打款
double limitAmount = configReader.getDouble("ufo.order.seller.hkAccountSettlementAmountLimit",5000);
double limitAmount = configReader.getDouble("ufo.order.seller.hkAccountSettlementAmountLimit", 5000);
if (sumIncome.doubleValue() >= limitAmount) {
try {
log.info("{} settle, send email", uid, sumIncome);
... ... @@ -217,80 +218,15 @@ public class HkAccountSettlement {
public void sendMail(List<TradeBillResult> tradeBillResults) throws MessagingException {
//环境
Properties props = new Properties(); // 参数配置
props.setProperty("mail.transport.protocol", "smtp"); // 使用的协议(JavaMail规范要求)
props.setProperty("mail.smtp.host", "smtp.126.com"); // 发件人的邮箱的 SMTP 服务器地址
props.setProperty("mail.smtp.auth", "true");
// 2. 根据配置创建会话对象, 用于和邮件服务器交互
Session session = Session.getDefaultInstance(props);
//邮件
MimeMessage msg = new MimeMessage(session);
//设置主题
msg.setSubject("UFO香港卖家结算");
//发件人,注意中文的处理
msg.setFrom(new InternetAddress("UFO System<lxc1317@126.com>"));
//设置邮件回复人
String toMail = configReader.getString("ufo.order.seller.hkAccountSettlementEmailTo","xiuchun.luo@yoho.cn");
msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(toMail));
//整封邮件的MINE消息体
MimeMultipart msgMultipart = new MimeMultipart("mixed");//混合的组合关系
//设置邮件的MINE消息体
msg.setContent(msgMultipart);
//附件1
MimeBodyPart attch1 = new MimeBodyPart();
//正文内容
MimeBodyPart content = new MimeBodyPart();
//把内容,附件1,附件2加入到 MINE消息体中
msgMultipart.addBodyPart(attch1);
msgMultipart.addBodyPart(content);
//把文件,添加到附件1中
//数据源
DataSource dataSource = buildDataSource(tradeBillResults);
//数据处理器
DataHandler dataHandler = new DataHandler(dataSource);
//设置第一个附件的数据
attch1.setDataHandler(dataHandler);
//设置第一个附件的文件名
try {
attch1.setFileName(MimeUtility.encodeText("卖家结算清单.csv"));
} catch (UnsupportedEncodingException e) {
attch1.setFileName("list.csv");
MailSender.newMailSender()
.subject("UFO香港卖家结算")
.to(configReader.getString("ufo.order.seller.hkAccountSettlementEmailTo", "xiuchun.luo@yoho.cn"))
.body("ok", "text/html;charset=utf-8")
.attachment("卖家结算清单.csv", buildDate(tradeBillResults), "application/x-csv;charset=utf-8")
.send();
}
//正文(图片和文字部分)
MimeMultipart bodyMultipart = new MimeMultipart("related");
//设置内容为正文
content.setContent(bodyMultipart);
//html代码部分
MimeBodyPart htmlPart = new MimeBodyPart();
//正文添加图片和html代码
bodyMultipart.addBodyPart(htmlPart);
//html代码
htmlPart.setContent("ok", "text/html;charset=utf-8");
//生成文件邮件
msg.saveChanges();
Transport transport = session.getTransport();
transport.connect("lxc1317@126.com", "luoxiuchun");
transport.sendMessage(msg, msg.getAllRecipients());
}
private ByteArrayDataSource buildDataSource(List<TradeBillResult> tradeBillResults) {
private String buildDate(List<TradeBillResult> tradeBillResults) {
StringBuffer text = new StringBuffer();
text.append("用户UID,订单编号,商品货号,商品名称,商品金额,税费,平台服务费,打款金额,金额类型");
tradeBillResults.forEach(tradeBillResult -> text.append("\n")
... ... @@ -304,11 +240,7 @@ public class HkAccountSettlement {
.append(tradeBillResult.getPayAmount()).append(",")
.append(tradeBillResult.getPayType())
);
try {
return new ByteArrayDataSource(text.toString(), "application/x-csv;charset=utf-8");
} catch (IOException e) {
throw new IllegalStateException(e);
}
return text.toString();
}
}
... ...
package com.yohoufo.order.utils;
import com.google.common.collect.Lists;
import lombok.Builder;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.*;
import javax.mail.util.ByteArrayDataSource;
import java.io.IOException;
import java.util.List;
import java.util.Properties;
public class MailSender {
private Properties properties;
private String user;
private String password;
String subject;
String from;
String to;
Part body;
List<Part> attachments;
public static MailSender newMailSender() {
return newMailSender("smtp.126.com", "lxc1317@126.com", "luoxiuchun").from("UFO System<lxc1317@126.com>");
}
public static MailSender newMailSender(String host, String user, String password) {
Properties properties = new Properties(); // 参数配置
properties.setProperty("mail.transport.protocol", "smtp"); // 使用的协议(JavaMail规范要求)
properties.setProperty("mail.smtp.host", host); // 发件人的邮箱的 SMTP 服务器地址
properties.setProperty("mail.smtp.auth", "true");
MailSender mailSender = new MailSender();
mailSender.properties = properties;
mailSender.user = user;
mailSender.password = password;
return mailSender;
}
private MailSender() {
this.attachments = Lists.newArrayList();
}
public MailSender subject(String subject) {
this.subject = subject;
return this;
}
public MailSender from(String from) {
this.from = from;
return this;
}
public MailSender to(String to) {
this.to = to;
return this;
}
public MailSender body(String content, String contentType) {
this.body = Part.builder().content(content).contentType(contentType).build();
return this;
}
public MailSender attachment(String filename, String content, String contentType) {
attachments.add(Part.builder().filename(filename).content(content).contentType(contentType).build());
return this;
}
public void send() throws MessagingException {
// 2. 根据配置创建会话对象, 用于和邮件服务器交互
Session session = Session.getDefaultInstance(properties);
//邮件
MimeMessage msg = new MimeMessage(session);
//设置主题
msg.setSubject(subject);
//发件人,注意中文的处理
msg.setFrom(new InternetAddress(from));
//设置邮件回复人
msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
//整封邮件的MINE消息体
MimeMultipart msgMultipart = new MimeMultipart("mixed");
//设置邮件的MINE消息体
msg.setContent(msgMultipart);
//正文内容
MimeBodyPart content = new MimeBodyPart();
msgMultipart.addBodyPart(content);
attachments.forEach(attachment -> {
try {
MimeBodyPart attachmentBodyPart = new MimeBodyPart();
msgMultipart.addBodyPart(attachmentBodyPart);
DataSource dataSource = new ByteArrayDataSource(attachment.content, attachment.contentType);
DataHandler dataHandler = new DataHandler(dataSource);
attachmentBodyPart.setDataHandler(dataHandler);
attachmentBodyPart.setFileName(MimeUtility.encodeText(attachment.filename));
} catch (MessagingException | IOException e) {
}
});
//正文(图片和文字部分)
MimeMultipart bodyMultipart = new MimeMultipart("related");
//设置内容为正文
content.setContent(bodyMultipart);
//html代码部分
MimeBodyPart htmlPart = new MimeBodyPart();
//正文添加图片和html代码
bodyMultipart.addBodyPart(htmlPart);
//html代码
htmlPart.setContent(body.content, body.contentType);
//生成文件邮件
msg.saveChanges();
Transport transport = session.getTransport();
transport.connect(user, password);
transport.sendMessage(msg, msg.getAllRecipients());
transport.close();
}
@Builder
private class Part {
String filename;
String content;
String contentType;
}
}
... ...