Commit 434c39b50419bf40234696e3507e2ae6a983ec08

Authored by 廖磊
2 parents b20ee5e7 69db0030

Merge branch 'pudong' of http://222.66.0.204:8090/panzhaov5/bsth_control

into pudong
src/main/java/com/bsth/email/MailAuthenticator.java 0 → 100644
  1 +package com.bsth.email;
  2 +
  3 +import javax.mail.Authenticator;
  4 +import javax.mail.PasswordAuthentication;
  5 +
  6 +/**
  7 + * 服务器邮箱登录验证
  8 + */
  9 +public class MailAuthenticator extends Authenticator{
  10 + /**
  11 + * 用户名(登录邮箱)
  12 + */
  13 + private String username;
  14 + /**
  15 + * 密码
  16 + */
  17 + private String password;
  18 +
  19 + /**
  20 + * 初始化邮箱和密码
  21 + *
  22 + * @param username
  23 + * 邮箱
  24 + * @param password
  25 + * 密码
  26 + */
  27 + public MailAuthenticator(String username, String password) {
  28 + this.username = username;
  29 + this.password = password;
  30 + }
  31 +
  32 + String getPassword() {
  33 + return password;
  34 + }
  35 +
  36 + @Override
  37 + protected PasswordAuthentication getPasswordAuthentication() {
  38 + return new PasswordAuthentication(username, password);
  39 + }
  40 +
  41 + String getUsername() {
  42 + return username;
  43 + }
  44 +
  45 + public void setPassword(String password) {
  46 + this.password = password;
  47 + }
  48 +
  49 + public void setUsername(String username) {
  50 + this.username = username;
  51 + }
  52 +}
... ...
src/main/java/com/bsth/email/SendEmailController.java 0 → 100644
  1 +package com.bsth.email;
  2 +
  3 +import com.bsth.email.entity.EmailBean;
  4 +import com.bsth.util.Tools;
  5 +import org.springframework.stereotype.Component;
  6 +
  7 +import java.util.List;
  8 +
  9 +@Component
  10 +public class SendEmailController {
  11 +
  12 + /*
  13 + * recipients
  14 + * 收件人集合
  15 + * mail
  16 + * 邮件
  17 + */
  18 + public int sendMail(List<String> recipients,EmailBean mail){
  19 + Tools t = new Tools("mailbox.properties");
  20 + SimpleMailSender sms = new SimpleMailSender(t.getValue("username"),t.getValue("password"));
  21 + try {
  22 + for (String recipient : recipients) {
  23 + sms.send(recipient, mail.getSubject(),mail.getContent());
  24 + }
  25 + } catch (Exception e) {
  26 + e.printStackTrace();
  27 + return -1;
  28 + }
  29 + return 1;
  30 + }
  31 +
  32 + /*
  33 + * recipient
  34 + * 收件人
  35 + * mail
  36 + * 邮件
  37 + */
  38 + public int sendMail(String recipient,EmailBean mail){
  39 + Tools t = new Tools("mailbox.properties");
  40 + SimpleMailSender sms = new SimpleMailSender(t.getValue("username"),t.getValue("password"));
  41 + try {
  42 + sms.send(recipient, mail.getSubject(),mail.getContent());
  43 + } catch (Exception e) {
  44 + e.printStackTrace();
  45 + return -1;
  46 + }
  47 + return 1;
  48 + }
  49 +}
... ...
src/main/java/com/bsth/email/SimpleMailSender.java 0 → 100644
  1 +package com.bsth.email;
  2 +
  3 +import com.bsth.email.entity.EmailBean;
  4 +
  5 +import java.util.List;
  6 +import java.util.Properties;
  7 +
  8 +import javax.activation.CommandMap;
  9 +import javax.activation.MailcapCommandMap;
  10 +import javax.mail.MessagingException;
  11 +import javax.mail.Session;
  12 +import javax.mail.Transport;
  13 +import javax.mail.internet.AddressException;
  14 +import javax.mail.internet.InternetAddress;
  15 +import javax.mail.internet.MimeMessage;
  16 +import javax.mail.internet.MimeMessage.RecipientType;
  17 +
  18 +
  19 +public class SimpleMailSender {
  20 + /**
  21 + * 发送邮件的props文件
  22 + */
  23 + private final transient Properties props = System.getProperties();
  24 + /**
  25 + * 邮件服务器登录验证
  26 + */
  27 + private transient MailAuthenticator authenticator;
  28 +
  29 + /**
  30 + * 邮箱session
  31 + */
  32 + private transient Session session;
  33 +
  34 + /**
  35 + * 初始化邮件发送器
  36 + *
  37 + * @param smtpHostName
  38 + * SMTP邮件服务器地址
  39 + * @param username
  40 + * 发送邮件的用户名(地址)
  41 + * @param password
  42 + * 发送邮件的密码
  43 + */
  44 + public SimpleMailSender(final String smtpHostName, final String username,
  45 + final String password) {
  46 + init(username, password, smtpHostName);
  47 + }
  48 +
  49 + /**
  50 + * 初始化邮件发送器
  51 + *
  52 + * @param username
  53 + * 发送邮件的用户名(地址),并以此解析SMTP服务器地址
  54 + * @param password
  55 + * 发送邮件的密码
  56 + */
  57 + public SimpleMailSender(final String username, final String password) {
  58 + //通过邮箱地址解析出smtp服务器,对大多数邮箱都管用
  59 + final String smtpHostName = "smtp." + username.split("@")[1];
  60 + init(username, password, smtpHostName);
  61 +
  62 + }
  63 +
  64 + /**
  65 + * 初始化
  66 + *
  67 + * @param username
  68 + * 发送邮件的用户名(地址)
  69 + * @param password
  70 + * 密码
  71 + * @param smtpHostName
  72 + * SMTP主机地址
  73 + */
  74 + private void init(String username, String password, String smtpHostName) {
  75 + // 初始化props
  76 + props.put("mail.smtp.auth", "true");
  77 + props.put("mail.smtp.host", smtpHostName);
  78 + // 验证
  79 + authenticator = new MailAuthenticator(username, password);
  80 + // 创建session
  81 + session = Session.getInstance(props, authenticator);
  82 + }
  83 +
  84 + /**
  85 + * 发送邮件
  86 + *
  87 + * @param recipient
  88 + * 收件人邮箱地址
  89 + * @param subject
  90 + * 邮件主题
  91 + * @param content
  92 + * 邮件内容
  93 + * @throws AddressException
  94 + * @throws MessagingException
  95 + */
  96 + public void send(String recipient, String subject, Object content)
  97 + throws AddressException, MessagingException {
  98 + // 创建mime类型邮件
  99 + final MimeMessage message = new MimeMessage(session);
  100 + // 设置发信人
  101 + message.setFrom(new InternetAddress(authenticator.getUsername()));
  102 + // 设置收件人
  103 + message.setRecipient(RecipientType.TO, new InternetAddress(recipient));
  104 + // 设置主题
  105 + message.setSubject(subject);
  106 + // 设置邮件内容
  107 + message.setContent(content.toString(), "text/html;charset=utf-8");
  108 +
  109 + MailcapCommandMap mc = (MailcapCommandMap) CommandMap.getDefaultCommandMap();
  110 + mc.addMailcap("text/html;; x-java-content-handler=com.sun.mail.handlers.text_html");
  111 + mc.addMailcap("text/xml;; x-java-content-handler=com.sun.mail.handlers.text_xml");
  112 + mc.addMailcap("text/plain;; x-java-content-handler=com.sun.mail.handlers.text_plain");
  113 + mc.addMailcap("multipart/*;; x-java-content-handler=com.sun.mail.handlers.multipart_mixed");
  114 + mc.addMailcap("message/rfc822;; x-java-content-handler=com.sun.mail.handlers.message_rfc822");
  115 + CommandMap.setDefaultCommandMap(mc);
  116 + // 发送
  117 + Transport.send(message);
  118 + }
  119 +
  120 + /**
  121 + * 群发邮件
  122 + *
  123 + * @param recipients
  124 + * 收件人们
  125 + * @param subject
  126 + * 主题
  127 + * @param content
  128 + * 内容
  129 + * @throws AddressException
  130 + * @throws MessagingException
  131 + */
  132 + public void send(List<String> recipients, String subject, Object content)
  133 + throws AddressException, MessagingException {
  134 + // 创建mime类型邮件
  135 + final MimeMessage message = new MimeMessage(session);
  136 + // 设置发信人
  137 + message.setFrom(new InternetAddress(authenticator.getUsername()));
  138 + // 设置收件人们
  139 + final int num = recipients.size();
  140 + InternetAddress[] addresses = new InternetAddress[num];
  141 + for (int i = 0; i < num; i++) {
  142 + addresses[i] = new InternetAddress(recipients.get(i));
  143 + }
  144 + message.setRecipients(RecipientType.TO, addresses);
  145 + // 设置主题
  146 + message.setSubject(subject);
  147 + // 设置邮件内容
  148 + message.setContent(content.toString(), "text/html;charset=utf-8");
  149 + // 发送
  150 + Transport.send(message);
  151 + }
  152 +
  153 + /**
  154 + * 发送邮件
  155 + *
  156 + * @param recipient
  157 + * 收件人邮箱地址
  158 + * @param mail
  159 + * 邮件对象
  160 + * @throws AddressException
  161 + * @throws MessagingException
  162 + */
  163 + public void send(String recipient, EmailBean mail)
  164 + throws AddressException, MessagingException {
  165 + send(recipient, mail.getSubject(), mail.getContent());
  166 + }
  167 +
  168 + /**
  169 + * 群发邮件
  170 + *
  171 + * @param recipients
  172 + * 收件人们
  173 + * @param mail
  174 + * 邮件对象
  175 + * @throws AddressException
  176 + * @throws MessagingException
  177 + */
  178 + public void send(List<String> recipients, EmailBean mail)
  179 + throws AddressException, MessagingException {
  180 + send(recipients, mail.getSubject(), mail.getContent());
  181 + }
  182 +}
... ...
src/main/java/com/bsth/email/entity/EmailBean.java 0 → 100644
  1 +package com.bsth.email.entity;
  2 +
  3 +/**
  4 + * Created by zlz on 2017-12-29.
  5 + */
  6 +public class EmailBean {
  7 + private String subject;// 邮件标题
  8 + private String content;//邮件内容
  9 + public String getSubject() {
  10 + return subject;
  11 + }
  12 + public void setSubject(String subject) {
  13 + this.subject = subject;
  14 + }
  15 + public String getContent() {
  16 + return content;
  17 + }
  18 + public void setContent(String content) {
  19 + this.content = content;
  20 + }
  21 +}
... ...
src/main/java/com/bsth/service/impl/TrafficManageServiceImpl.java
1 1 package com.bsth.service.impl;
2 2  
3 3 import com.bsth.data.BasicData;
  4 +import com.bsth.email.SendEmailController;
  5 +import com.bsth.email.entity.EmailBean;
4 6 import com.bsth.entity.*;
5 7 import com.bsth.entity.realcontrol.ChildTaskPlan;
6 8 import com.bsth.entity.realcontrol.ScheduleRealInfo;
... ... @@ -109,6 +111,9 @@ public class TrafficManageServiceImpl implements TrafficManageService{
109 111 @Autowired
110 112 private YgcBasicDataService ygcBasicDataService;
111 113  
  114 + // 发送邮件
  115 + @Autowired
  116 + private SendEmailController sendEmailController;
112 117  
113 118 // 运管处上传接口
114 119 private com.bsth.webService.trafficManage.up.org.tempuri.WebServiceSoap webServiceSoapUp;
... ... @@ -138,6 +143,10 @@ public class TrafficManageServiceImpl implements TrafficManageService{
138 143 private final String userNameUp = "user";
139 144 // 密码
140 145 private final String passwordUp = "user";
  146 + // 接收邮件人
  147 + private final String emailSendToAddress = "175912183@qq.com";
  148 + // 记录路单上线的成功、失败线路数
  149 + private Integer countSuccess,countFailure;
141 150  
142 151 private synchronized com.bsth.webService.trafficManage.up.org.tempuri.WebServiceSoap getWebServiceSoapUp(){
143 152 try {
... ... @@ -396,20 +405,27 @@ public class TrafficManageServiceImpl implements TrafficManageService{
396 405 */
397 406 private String uploadLD(String theDate){
398 407 String result = "failure";
  408 + countSuccess = 0 ;countFailure = 0;
399 409 Line line;
400 410 // 取昨天 的日期
401 411 String date = theDate == null ?sdfnyr.format(DateUtils.addDays(new Date(), -1)) : theDate;
402 412 StringBuffer sf = new StringBuffer();
403   - String str;
  413 + StringBuffer logSuccess = new StringBuffer("成功:");
  414 + StringBuffer logFailure = new StringBuffer("失败:");
  415 + HashMap logXlbmSuccessMap = new HashMap();
  416 + HashMap logXlbmFailureMap = new HashMap();
  417 + HashMap logXlbmMap = new HashMap();
  418 + Results results = null;
  419 + String str = "",xlbm;
404 420 try {
405 421 int counter = 0; // 计数器
406 422 int per = 10; // 每几条线路上传一次路单
407 423  
408 424 List<ScheduleRealInfo> list = scheduleRealInfoRepository.setLD(date);
409 425 List<Map<String,Object>> listGroup = scheduleRealInfoRepository.setLDGroup(date);
410   - Map<String,Object> map = new HashMap<String,Object>();
  426 + Map<String,Object> map = new HashMap();
411 427 HashMap<String,String> paramMap;
412   - HashMap<String,String> otherMap = new HashMap<String, String>();
  428 + HashMap<String,String> otherMap = new HashMap();
413 429 for(Map<String,Object> schRealInfo:listGroup){
414 430 if(schRealInfo != null){
415 431 //根据车辆自编号查询车牌号
... ... @@ -425,9 +441,14 @@ public class TrafficManageServiceImpl implements TrafficManageService{
425 441 sf.append("<DLDS>");
426 442 }
427 443 counter ++;
  444 + xlbm = BasicData.lineCode2ShangHaiCodeMap.get(schRealInfo.get("xlBm")+"");
  445 + // 保存一次路单的线路编码,用于发送邮箱
  446 + if(logXlbmMap.get(xlbm) == null){
  447 + logXlbmMap.put(xlbm,xlbm);
  448 + }
428 449 sf.append("<DLD>");
429 450 sf.append("<RQ>"+date+"</RQ>");
430   - sf.append("<XLBM>"+BasicData.lineCode2ShangHaiCodeMap.get(schRealInfo.get("xlBm")+"")+"</XLBM>");
  451 + sf.append("<XLBM>"+xlbm+"</XLBM>");
431 452 sf.append("<LPBH>"+schRealInfo.get("lpName")+"</LPBH>");
432 453 sf.append("<CPH>"+car.getCarPlate()+"</CPH>");
433 454 sf.append("<UPDT>"+sdfnyrsfm.format(new Date())+"</UPDT>");
... ... @@ -485,39 +506,85 @@ public class TrafficManageServiceImpl implements TrafficManageService{
485 506 counter = 0;
486 507 sf.append("</DLDS>");
487 508 str = sf.toString().replace("'","");// 去掉'号
488   - Results results = ssop.setLD(userNameOther, passwordOther, StringEscapeUtils.unescapeHtml(str));
489   - if(results.isSuccess()){
490   - result = "success";
491   - }else{
492   - result = "failure";
493   - }
494   - logger.info("setLD:"+str);
495   - logger.info("setLD:"+result);
  509 + results = ssop.setLD(userNameOther, passwordOther, StringEscapeUtils.unescapeHtml(str));
  510 + // 记录日志
  511 + result = logRecord(results,logXlbmMap,logXlbmSuccessMap,logXlbmFailureMap,logSuccess,logFailure,str);
496 512 }
497 513 }
498 514 // 每per条线路上传后剩下的数据再上传
499 515 if(counter > 0){
500 516 sf.append("</DLDS>");
501 517 str = sf.toString().replace("'","");// 去掉'号
502   - Results results = ssop.setLD(userNameOther, passwordOther, StringEscapeUtils.unescapeHtml(str));
503   - if(results.isSuccess()){
504   - result = "success";
505   - }else{
506   - result = "failure";
507   - }
508   - logger.info("setLD:"+str);
509   - logger.info("setLD:"+result);
  518 + results = ssop.setLD(userNameOther, passwordOther, StringEscapeUtils.unescapeHtml(str));
510 519 }
  520 + // 记录日志
  521 + result = logRecord(results,logXlbmMap,logXlbmSuccessMap,logXlbmFailureMap,logSuccess,logFailure,str);
511 522 } catch (Exception e) {
512 523 logger.error("setLD:",e);
  524 + logFailure.append(e).append("<br/>");
  525 + for (StackTraceElement traceElement : e.getStackTrace()){
  526 + logFailure.append("\r\t").append(traceElement);
  527 + }
513 528 e.printStackTrace();
514 529 }finally{
515   -
  530 + //发送邮件
  531 + EmailBean mail = new EmailBean();
  532 + mail.setSubject("路单日志数据"+date);
  533 + mail.setContent(logSuccess+"<br/>成功数:"+countSuccess+"<br/>" +logFailure+"<br/>失败数:"+countFailure);
  534 + sendEmailController.sendMail(emailSendToAddress, mail);
516 535 }
517 536 return result;
518 537 }
519 538  
520 539 /**
  540 + * 记录日志
  541 + * @param results
  542 + * @param logXlbmMap
  543 + * @param logXlbmSuccessMap
  544 + * @param logXlbmFailureMap
  545 + * @param logSuccess
  546 + * @param logFailure
  547 + * @param str
  548 + */
  549 + private String logRecord(Results results,HashMap logXlbmMap,HashMap logXlbmSuccessMap,HashMap logXlbmFailureMap,StringBuffer logSuccess,
  550 + StringBuffer logFailure,String str){
  551 + String result = "failure";
  552 + // 记录日志
  553 + if(results != null){
  554 + if(results.isSuccess()){// 上传成功
  555 + // 把上线成功的线路编码放入 logXlbmSuccessMap,并记录logSuccess
  556 + countSuccess += fillMailXlbmMap(logXlbmMap,logXlbmSuccessMap,logSuccess);
  557 + result = "success";
  558 + }else{// 上传失败
  559 + // 把上线失败的线路编码放入 logXlbmFailureMap,并记录logFailure
  560 + countFailure += fillMailXlbmMap(logXlbmMap,logXlbmFailureMap,logFailure);
  561 + result = "failure";
  562 + }
  563 + logger.info("setLD:"+str);
  564 + logger.info("setLD:"+result);
  565 + results = null;
  566 + logXlbmMap = new HashMap();
  567 + }
  568 + return result;
  569 + }
  570 + /**
  571 + * 填充线路编码到相应的map
  572 + * @param fromMap
  573 + * @param toMap
  574 + */
  575 + private int fillMailXlbmMap(HashMap fromMap,HashMap toMap,StringBuffer logStr){
  576 + int tmpCount = 0;
  577 + for (Object key : fromMap.keySet()) {
  578 + if(toMap.get(key) == null){
  579 + toMap.put(key,fromMap.get(key));
  580 + logStr.append(key).append(",");
  581 + tmpCount ++;
  582 + }
  583 + }
  584 + fromMap = new HashMap();
  585 + return tmpCount;
  586 + }
  587 + /**
521 588 * 上传路单 xml来自文件
522 589 * @return 上传成功标识
523 590 */
... ...
src/main/resources/mailbox.properties 0 → 100644
  1 +username=pdda_zhmm2014@163.COM
  2 +password=ZHMM2014_PDDA
  3 +address=116.236.208.198:9090
  4 +qq.com=http://mail.qq.com
  5 +gmail.com=http://mail.google.com
  6 +sina.com=http://mail.sina.com.cn
  7 +163.com=http://mail.163.com
  8 +126.com=http://mail.126.com
  9 +yeah.net=http://www.yeah.net/
  10 +sohu.com=http://mail.sohu.com/
  11 +tom.com=http://mail.tom.com/
  12 +sogou.com=http://mail.sogou.com/
  13 +139.com=http://mail.10086.cn/
  14 +hotmail.com=http://www.hotmail.com
  15 +live.com=http://login.live.com/
  16 +live.cn=http://login.live.cn/
  17 +live.com.cn=http://login.live.com.cn
  18 +189.com=http://webmail16.189.cn/webmail/
  19 +yahoo.com.cn=http://mail.cn.yahoo.com/
  20 +yahoo.cn=http://mail.cn.yahoo.com/
  21 +eyou.com=http://www.eyou.com/
  22 +21cn.com=http://mail.21cn.com/
  23 +188.com=http://www.188.com/
  24 +foxmail.coom=http\://www.foxmail.com
0 25 \ No newline at end of file
... ...