Authored by LUOXC

结算发邮件

@@ -2,6 +2,7 @@ package com.yohoufo.order.service.transfer; @@ -2,6 +2,7 @@ package com.yohoufo.order.service.transfer;
2 2
3 import com.google.common.collect.Lists; 3 import com.google.common.collect.Lists;
4 import com.google.common.collect.Sets; 4 import com.google.common.collect.Sets;
  5 +import com.yoho.core.config.ConfigReader;
5 import com.yoho.core.redis.cluster.annotation.Redis; 6 import com.yoho.core.redis.cluster.annotation.Redis;
6 import com.yoho.core.redis.cluster.operations.nosync.YHValueOperations; 7 import com.yoho.core.redis.cluster.operations.nosync.YHValueOperations;
7 import com.yoho.core.redis.cluster.operations.serializer.RedisKeyBuilder; 8 import com.yoho.core.redis.cluster.operations.serializer.RedisKeyBuilder;
@@ -21,9 +22,20 @@ import lombok.extern.slf4j.Slf4j; @@ -21,9 +22,20 @@ import lombok.extern.slf4j.Slf4j;
21 import org.springframework.beans.factory.annotation.Autowired; 22 import org.springframework.beans.factory.annotation.Autowired;
22 import org.springframework.stereotype.Service; 23 import org.springframework.stereotype.Service;
23 24
  25 +import javax.activation.DataHandler;
  26 +import javax.activation.DataSource;
  27 +import javax.mail.Message;
  28 +import javax.mail.MessagingException;
  29 +import javax.mail.Session;
  30 +import javax.mail.Transport;
  31 +import javax.mail.internet.*;
  32 +import javax.mail.util.ByteArrayDataSource;
  33 +import java.io.IOException;
  34 +import java.io.UnsupportedEncodingException;
24 import java.math.BigDecimal; 35 import java.math.BigDecimal;
25 import java.util.List; 36 import java.util.List;
26 import java.util.Optional; 37 import java.util.Optional;
  38 +import java.util.Properties;
27 import java.util.Set; 39 import java.util.Set;
28 import java.util.concurrent.TimeUnit; 40 import java.util.concurrent.TimeUnit;
29 import java.util.stream.Collectors; 41 import java.util.stream.Collectors;
@@ -47,6 +59,9 @@ public class HkAccountSettlement { @@ -47,6 +59,9 @@ public class HkAccountSettlement {
47 @Autowired 59 @Autowired
48 private ProductMapper productMapper; 60 private ProductMapper productMapper;
49 61
  62 + @Autowired
  63 + private ConfigReader configReader;
  64 +
50 @Redis("gwNoSyncRedis") 65 @Redis("gwNoSyncRedis")
51 private YHValueOperations valueOperations; 66 private YHValueOperations valueOperations;
52 67
@@ -77,7 +92,8 @@ public class HkAccountSettlement { @@ -77,7 +92,8 @@ public class HkAccountSettlement {
77 .reduce(BigDecimal.ZERO, BigDecimal::add); 92 .reduce(BigDecimal.ZERO, BigDecimal::add);
78 log.info("{} settle, sum income is {}", uid, sumIncome); 93 log.info("{} settle, sum income is {}", uid, sumIncome);
79 // 满足打款条件,通知财务打款 94 // 满足打款条件,通知财务打款
80 - if (sumIncome.compareTo(BigDecimal.valueOf(100)) > 0) { 95 + double limitAmount = configReader.getDouble("ufo.order.seller.hkAccountSettlementAmountLimit",5000);
  96 + if (sumIncome.doubleValue() >= limitAmount) {
81 try { 97 try {
82 log.info("{} settle, send email", uid, sumIncome); 98 log.info("{} settle, send email", uid, sumIncome);
83 List<TradeBillResult> tradeBillResults = getTradeBillResults(uid, tradeBills); 99 List<TradeBillResult> tradeBillResults = getTradeBillResults(uid, tradeBills);
@@ -88,6 +104,7 @@ public class HkAccountSettlement { @@ -88,6 +104,7 @@ public class HkAccountSettlement {
88 log.info("{} settle, difference is {}", uid, difference); 104 log.info("{} settle, difference is {}", uid, difference);
89 // 通知财务打款 105 // 通知财务打款
90 tradeBillResults.forEach(tradeBillResult -> log.info("{} settle, item {}", uid, tradeBillResult)); 106 tradeBillResults.forEach(tradeBillResult -> log.info("{} settle, item {}", uid, tradeBillResult));
  107 + sendMail(tradeBillResults);
91 } catch (Exception e) { 108 } catch (Exception e) {
92 // 通知失败 109 // 通知失败
93 log.info("{} settle, send email fail", uid, sumIncome); 110 log.info("{} settle, send email fail", uid, sumIncome);
@@ -198,4 +215,100 @@ public class HkAccountSettlement { @@ -198,4 +215,100 @@ public class HkAccountSettlement {
198 } 215 }
199 } 216 }
200 217
  218 +
  219 + public void sendMail(List<TradeBillResult> tradeBillResults) throws MessagingException {
  220 +
  221 + //环境
  222 + Properties props = new Properties(); // 参数配置
  223 + props.setProperty("mail.transport.protocol", "smtp"); // 使用的协议(JavaMail规范要求)
  224 + props.setProperty("mail.smtp.host", "smtp.126.com"); // 发件人的邮箱的 SMTP 服务器地址
  225 + props.setProperty("mail.smtp.auth", "true");
  226 +
  227 + // 2. 根据配置创建会话对象, 用于和邮件服务器交互
  228 + Session session = Session.getDefaultInstance(props);
  229 +
  230 + //邮件
  231 + MimeMessage msg = new MimeMessage(session);
  232 + //设置主题
  233 + msg.setSubject("UFO香港卖家结算");
  234 + //发件人,注意中文的处理
  235 + msg.setFrom(new InternetAddress("UFO System<lxc1317@126.com>"));
  236 + //设置邮件回复人
  237 +
  238 + String toMail = configReader.getString("ufo.order.seller.hkAccountSettlementEmailTo","xiuchun.luo@yoho.cn");
  239 + msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(toMail));
  240 +
  241 + //整封邮件的MINE消息体
  242 + MimeMultipart msgMultipart = new MimeMultipart("mixed");//混合的组合关系
  243 + //设置邮件的MINE消息体
  244 + msg.setContent(msgMultipart);
  245 +
  246 + //附件1
  247 + MimeBodyPart attch1 = new MimeBodyPart();
  248 + //正文内容
  249 + MimeBodyPart content = new MimeBodyPart();
  250 +
  251 + //把内容,附件1,附件2加入到 MINE消息体中
  252 + msgMultipart.addBodyPart(attch1);
  253 + msgMultipart.addBodyPart(content);
  254 +
  255 + //把文件,添加到附件1中
  256 + //数据源
  257 + DataSource dataSource = buildDataSource(tradeBillResults);
  258 + //数据处理器
  259 + DataHandler dataHandler = new DataHandler(dataSource);
  260 + //设置第一个附件的数据
  261 + attch1.setDataHandler(dataHandler);
  262 + //设置第一个附件的文件名
  263 +
  264 + try {
  265 + attch1.setFileName(MimeUtility.encodeText("卖家结算清单.csv"));
  266 + } catch (UnsupportedEncodingException e) {
  267 + attch1.setFileName("list.csv");
  268 + }
  269 +
  270 + //正文(图片和文字部分)
  271 + MimeMultipart bodyMultipart = new MimeMultipart("related");
  272 + //设置内容为正文
  273 + content.setContent(bodyMultipart);
  274 +
  275 + //html代码部分
  276 + MimeBodyPart htmlPart = new MimeBodyPart();
  277 +
  278 + //正文添加图片和html代码
  279 + bodyMultipart.addBodyPart(htmlPart);
  280 +
  281 + //html代码
  282 + htmlPart.setContent("ok", "text/html;charset=utf-8");
  283 +
  284 + //生成文件邮件
  285 + msg.saveChanges();
  286 +
  287 +
  288 + Transport transport = session.getTransport();
  289 + transport.connect("lxc1317@126.com", "luoxiuchun");
  290 + transport.sendMessage(msg, msg.getAllRecipients());
  291 + }
  292 +
  293 + private ByteArrayDataSource buildDataSource(List<TradeBillResult> tradeBillResults) {
  294 + StringBuffer text = new StringBuffer();
  295 + text.append("用户UID,订单编号,商品货号,商品名称,商品金额,税费,平台服务费,打款金额,金额类型");
  296 + tradeBillResults.forEach(tradeBillResult -> text.append("\n")
  297 + .append(tradeBillResult.getUid()).append(",")
  298 + .append(tradeBillResult.getOrderCode()).append(",")
  299 + .append(tradeBillResult.getProductNo()).append(",")
  300 + .append(tradeBillResult.getProductName()).append(",")
  301 + .append("").append(",")
  302 + .append(tradeBillResult.getTariffAmount()).append(",")
  303 + .append(tradeBillResult.getPlatformServiceAmount()).append(",")
  304 + .append(tradeBillResult.getPayAmount()).append(",")
  305 + .append(tradeBillResult.getPayType())
  306 + );
  307 + try {
  308 + return new ByteArrayDataSource(text.toString(), "application/x-csv;charset=utf-8");
  309 + } catch (IOException e) {
  310 + throw new IllegalStateException(e);
  311 + }
  312 + }
  313 +
201 } 314 }
@@ -33,3 +33,6 @@ ufo.product.addSizeSortId=40 @@ -33,3 +33,6 @@ ufo.product.addSizeSortId=40
33 33
34 34
35 ufo.order.buyer.allow_buy_self_sell_goods=false 35 ufo.order.buyer.allow_buy_self_sell_goods=false
  36 +
  37 +ufo.order.seller.hkAccountSettlementAmountLimit=100
  38 +ufo.order.seller.hkAccountSettlementEmailTo=xiuchun.luo@yoho.cn