Authored by LUOXC

结算发邮件

... ... @@ -2,6 +2,7 @@ package com.yohoufo.order.service.transfer;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import com.yoho.core.config.ConfigReader;
import com.yoho.core.redis.cluster.annotation.Redis;
import com.yoho.core.redis.cluster.operations.nosync.YHValueOperations;
import com.yoho.core.redis.cluster.operations.serializer.RedisKeyBuilder;
... ... @@ -21,9 +22,20 @@ import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
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.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.util.List;
import java.util.Optional;
import java.util.Properties;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
... ... @@ -47,6 +59,9 @@ public class HkAccountSettlement {
@Autowired
private ProductMapper productMapper;
@Autowired
private ConfigReader configReader;
@Redis("gwNoSyncRedis")
private YHValueOperations valueOperations;
... ... @@ -77,7 +92,8 @@ public class HkAccountSettlement {
.reduce(BigDecimal.ZERO, BigDecimal::add);
log.info("{} settle, sum income is {}", uid, sumIncome);
// 满足打款条件,通知财务打款
if (sumIncome.compareTo(BigDecimal.valueOf(100)) > 0) {
double limitAmount = configReader.getDouble("ufo.order.seller.hkAccountSettlementAmountLimit",5000);
if (sumIncome.doubleValue() >= limitAmount) {
try {
log.info("{} settle, send email", uid, sumIncome);
List<TradeBillResult> tradeBillResults = getTradeBillResults(uid, tradeBills);
... ... @@ -88,6 +104,7 @@ public class HkAccountSettlement {
log.info("{} settle, difference is {}", uid, difference);
// 通知财务打款
tradeBillResults.forEach(tradeBillResult -> log.info("{} settle, item {}", uid, tradeBillResult));
sendMail(tradeBillResults);
} catch (Exception e) {
// 通知失败
log.info("{} settle, send email fail", uid, sumIncome);
... ... @@ -198,4 +215,100 @@ 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");
}
//正文(图片和文字部分)
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) {
StringBuffer text = new StringBuffer();
text.append("用户UID,订单编号,商品货号,商品名称,商品金额,税费,平台服务费,打款金额,金额类型");
tradeBillResults.forEach(tradeBillResult -> text.append("\n")
.append(tradeBillResult.getUid()).append(",")
.append(tradeBillResult.getOrderCode()).append(",")
.append(tradeBillResult.getProductNo()).append(",")
.append(tradeBillResult.getProductName()).append(",")
.append("").append(",")
.append(tradeBillResult.getTariffAmount()).append(",")
.append(tradeBillResult.getPlatformServiceAmount()).append(",")
.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);
}
}
}
... ...
... ... @@ -33,3 +33,6 @@ ufo.product.addSizeSortId=40
ufo.order.buyer.allow_buy_self_sell_goods=false
ufo.order.seller.hkAccountSettlementAmountLimit=100
ufo.order.seller.hkAccountSettlementEmailTo=xiuchun.luo@yoho.cn
... ...