Commit c2ebc6a5 authored by 赵建伟's avatar 赵建伟

refact codes

parent efbc1e75
...@@ -79,6 +79,7 @@ ...@@ -79,6 +79,7 @@
</component> </component>
<component name="RunAnythingCache"> <component name="RunAnythingCache">
<option name="myCommands"> <option name="myCommands">
<command value="mvn clean" />
<command value="mvn clean install -DskipTests" /> <command value="mvn clean install -DskipTests" />
</option> </option>
</component> </component>
...@@ -214,25 +215,25 @@ ...@@ -214,25 +215,25 @@
<screen x="0" y="22" width="1920" height="1129" /> <screen x="0" y="22" width="1920" height="1129" />
</state> </state>
<state x="773" y="303" key="#com.intellij.ide.util.MemberChooser/0.22.1920.1129/-981.-1200.1920.1200/939.-1080.1920.1080@0.22.1920.1129" timestamp="1593835959317" /> <state x="773" y="303" key="#com.intellij.ide.util.MemberChooser/0.22.1920.1129/-981.-1200.1920.1200/939.-1080.1920.1080@0.22.1920.1129" timestamp="1593835959317" />
<state width="1878" height="313" key="GridCell.Tab.0.bottom" timestamp="1594109306575"> <state width="1878" height="313" key="GridCell.Tab.0.bottom" timestamp="1594452233729">
<screen x="0" y="22" width="1920" height="1129" /> <screen x="0" y="22" width="1920" height="1129" />
</state> </state>
<state width="1878" height="313" key="GridCell.Tab.0.bottom/0.22.1920.1129/-981.-1200.1920.1200/939.-1080.1920.1080@0.22.1920.1129" timestamp="1594109306575" /> <state width="1878" height="313" key="GridCell.Tab.0.bottom/0.22.1920.1129/-981.-1200.1920.1200/939.-1080.1920.1080@0.22.1920.1129" timestamp="1594452233729" />
<state width="1878" height="243" key="GridCell.Tab.0.bottom/1752.1200.1920.1151/0.22.1920.1178/1920.120.1920.1080@1752.1200.1920.1151" timestamp="1590742520783" /> <state width="1878" height="243" key="GridCell.Tab.0.bottom/1752.1200.1920.1151/0.22.1920.1178/1920.120.1920.1080@1752.1200.1920.1151" timestamp="1590742520783" />
<state width="1878" height="313" key="GridCell.Tab.0.center" timestamp="1594109306573"> <state width="1878" height="313" key="GridCell.Tab.0.center" timestamp="1594452233727">
<screen x="0" y="22" width="1920" height="1129" /> <screen x="0" y="22" width="1920" height="1129" />
</state> </state>
<state width="1878" height="313" key="GridCell.Tab.0.center/0.22.1920.1129/-981.-1200.1920.1200/939.-1080.1920.1080@0.22.1920.1129" timestamp="1594109306573" /> <state width="1878" height="313" key="GridCell.Tab.0.center/0.22.1920.1129/-981.-1200.1920.1200/939.-1080.1920.1080@0.22.1920.1129" timestamp="1594452233727" />
<state width="1878" height="243" key="GridCell.Tab.0.center/1752.1200.1920.1151/0.22.1920.1178/1920.120.1920.1080@1752.1200.1920.1151" timestamp="1590742520781" /> <state width="1878" height="243" key="GridCell.Tab.0.center/1752.1200.1920.1151/0.22.1920.1178/1920.120.1920.1080@1752.1200.1920.1151" timestamp="1590742520781" />
<state width="1878" height="313" key="GridCell.Tab.0.left" timestamp="1594109306572"> <state width="1878" height="313" key="GridCell.Tab.0.left" timestamp="1594452233726">
<screen x="0" y="22" width="1920" height="1129" /> <screen x="0" y="22" width="1920" height="1129" />
</state> </state>
<state width="1878" height="313" key="GridCell.Tab.0.left/0.22.1920.1129/-981.-1200.1920.1200/939.-1080.1920.1080@0.22.1920.1129" timestamp="1594109306572" /> <state width="1878" height="313" key="GridCell.Tab.0.left/0.22.1920.1129/-981.-1200.1920.1200/939.-1080.1920.1080@0.22.1920.1129" timestamp="1594452233726" />
<state width="1878" height="243" key="GridCell.Tab.0.left/1752.1200.1920.1151/0.22.1920.1178/1920.120.1920.1080@1752.1200.1920.1151" timestamp="1590742520781" /> <state width="1878" height="243" key="GridCell.Tab.0.left/1752.1200.1920.1151/0.22.1920.1178/1920.120.1920.1080@1752.1200.1920.1151" timestamp="1590742520781" />
<state width="1878" height="313" key="GridCell.Tab.0.right" timestamp="1594109306574"> <state width="1878" height="313" key="GridCell.Tab.0.right" timestamp="1594452233728">
<screen x="0" y="22" width="1920" height="1129" /> <screen x="0" y="22" width="1920" height="1129" />
</state> </state>
<state width="1878" height="313" key="GridCell.Tab.0.right/0.22.1920.1129/-981.-1200.1920.1200/939.-1080.1920.1080@0.22.1920.1129" timestamp="1594109306574" /> <state width="1878" height="313" key="GridCell.Tab.0.right/0.22.1920.1129/-981.-1200.1920.1200/939.-1080.1920.1080@0.22.1920.1129" timestamp="1594452233728" />
<state width="1878" height="243" key="GridCell.Tab.0.right/1752.1200.1920.1151/0.22.1920.1178/1920.120.1920.1080@1752.1200.1920.1151" timestamp="1590742520782" /> <state width="1878" height="243" key="GridCell.Tab.0.right/1752.1200.1920.1151/0.22.1920.1178/1920.120.1920.1080@1752.1200.1920.1151" timestamp="1590742520782" />
<state width="1878" height="386" key="GridCell.Tab.1.bottom" timestamp="1591671347527"> <state width="1878" height="386" key="GridCell.Tab.1.bottom" timestamp="1591671347527">
<screen x="0" y="22" width="1920" height="1129" /> <screen x="0" y="22" width="1920" height="1129" />
...@@ -250,9 +251,9 @@ ...@@ -250,9 +251,9 @@
<screen x="0" y="22" width="1920" height="1129" /> <screen x="0" y="22" width="1920" height="1129" />
</state> </state>
<state width="1878" height="386" key="GridCell.Tab.1.right/0.22.1920.1129/-981.-1200.1920.1200/939.-1080.1920.1080@0.22.1920.1129" timestamp="1591671347526" /> <state width="1878" height="386" key="GridCell.Tab.1.right/0.22.1920.1129/-981.-1200.1920.1200/939.-1080.1920.1080@0.22.1920.1129" timestamp="1591671347526" />
<state x="625" y="273" width="670" height="675" key="run.anything.popup" timestamp="1594109288006"> <state x="625" y="273" width="670" height="675" key="run.anything.popup" timestamp="1594452219810">
<screen x="0" y="22" width="1920" height="1129" /> <screen x="0" y="22" width="1920" height="1129" />
</state> </state>
<state x="625" y="273" width="670" height="675" key="run.anything.popup/0.22.1920.1129/-981.-1200.1920.1200/939.-1080.1920.1080@0.22.1920.1129" timestamp="1594109288006" /> <state x="625" y="273" width="670" height="675" key="run.anything.popup/0.22.1920.1129/-981.-1200.1920.1200/939.-1080.1920.1080@0.22.1920.1129" timestamp="1594452219810" />
</component> </component>
</project> </project>
\ No newline at end of file
...@@ -17,18 +17,18 @@ public class BiReportEndpoint { ...@@ -17,18 +17,18 @@ public class BiReportEndpoint {
return biReportService.execSql(name); return biReportService.execSql(name);
} }
@ResponseBody
@RequestMapping(value = "/genExcel")
public int genExcel(@RequestParam String name){
return biReportService.genExcel(name);
}
@ResponseBody @ResponseBody
@RequestMapping(value = "/execSqlByHive") @RequestMapping(value = "/execSqlByHive")
public int execSqlByHive(@RequestParam String name){ public int execSqlByHive(@RequestParam String name){
return biReportService.execSql(name); return biReportService.execSql(name);
} }
@ResponseBody
@RequestMapping(value = "/genExcel")
public int genExcel(@RequestParam String name){
return biReportService.genExcel(name);
}
@ResponseBody @ResponseBody
@RequestMapping(value = "/sendEmail/{to}/{cc}/{name}",method = RequestMethod.GET) @RequestMapping(value = "/sendEmail/{to}/{cc}/{name}",method = RequestMethod.GET)
public int sendEmail(@PathVariable("to") String to,@PathVariable("cc") String cc,@PathVariable("name") String name){ public int sendEmail(@PathVariable("to") String to,@PathVariable("cc") String cc,@PathVariable("name") String name){
...@@ -37,7 +37,7 @@ public class BiReportEndpoint { ...@@ -37,7 +37,7 @@ public class BiReportEndpoint {
@ResponseBody @ResponseBody
@RequestMapping(value = "/email/{name}/{to}/{cc}",method = RequestMethod.GET) @RequestMapping(value = "/email/{name}/{to}/{cc}",method = RequestMethod.GET)
public int email(@PathVariable("name") String name,@PathVariable("to") String to,@PathVariable("cc") String cc){ public void email(@PathVariable("name") String name,@PathVariable("to") String to,@PathVariable("cc") String cc) throws Exception{
return biReportService.email(name,to,cc); biReportService.email(name,to,cc);
} }
} }
...@@ -4,6 +4,6 @@ public interface BiReportService { ...@@ -4,6 +4,6 @@ public interface BiReportService {
int execSql(String name); int execSql(String name);
int genExcel(String name); int genExcel(String name);
int execSqlByHive(String name); int execSqlByHive(String name);
int email(String name, String to, String cc); void email(String name, String to, String cc) throws Exception;
int sendEmail(String to,String cc,String name); int sendEmail(String to,String cc,String name);
} }
package com.gmei.data.gateway.server.service; package com.gmei.data.gateway.server.service;
public interface EmailService { public interface EmailService {
int sendAttachmentsMail(String to,String cc,String name); void sendAttachmentsMail(String to,String cc,String name) throws Exception;
int sendFormatedMail(String to,String cc,String name,String content); int sendFormatedMail(String to,String cc,String name,String content);
} }
...@@ -35,18 +35,16 @@ public class BiReportServiceImpl implements BiReportService { ...@@ -35,18 +35,16 @@ public class BiReportServiceImpl implements BiReportService {
} }
@Override @Override
public int email(String name, String to, String cc) { public void email(String name, String to, String cc) throws Exception{
int rs = Constants.SUCCESS;
ExcelGenDto resultDto = sparksqlService.getResult(name); ExcelGenDto resultDto = sparksqlService.getResult(name);
if(null == resultDto){ if(null == resultDto){
rs = Constants.FAIL; throw new Exception("获取查询结果失败,请核实!");
}else{ }else{
int genExcelRs = excelService.genExcel(resultDto,name); int genExcelRs = excelService.genExcel(resultDto,name);
if(Constants.SUCCESS == genExcelRs){ if(Constants.SUCCESS == genExcelRs){
rs = emailService.sendAttachmentsMail(to,cc,name); emailService.sendAttachmentsMail(to,cc,name);
} }
} }
return rs;
} }
@Override @Override
...@@ -68,7 +66,11 @@ public class BiReportServiceImpl implements BiReportService { ...@@ -68,7 +66,11 @@ public class BiReportServiceImpl implements BiReportService {
}else{ }else{
int genExcelRs = excelService.genExcel(resultDto,name); int genExcelRs = excelService.genExcel(resultDto,name);
if(Constants.SUCCESS == genExcelRs){ if(Constants.SUCCESS == genExcelRs){
rs = emailService.sendAttachmentsMail(to,cc,name); try{
emailService.sendAttachmentsMail(to,cc,name);
}catch (Exception e){
rs = Constants.FAIL;
}
} }
} }
return rs; return rs;
......
...@@ -4,6 +4,7 @@ import com.gmei.data.gateway.server.common.Constants; ...@@ -4,6 +4,7 @@ import com.gmei.data.gateway.server.common.Constants;
import com.gmei.data.gateway.server.service.EmailService; import com.gmei.data.gateway.server.service.EmailService;
import com.gmei.data.gateway.server.utils.DateUtils; import com.gmei.data.gateway.server.utils.DateUtils;
import com.gmei.data.gateway.server.utils.ProperUtils; import com.gmei.data.gateway.server.utils.ProperUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
...@@ -13,8 +14,16 @@ import org.springframework.mail.javamail.JavaMailSender; ...@@ -13,8 +14,16 @@ import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper; import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import javax.mail.Address;
import javax.mail.MessagingException;
import javax.mail.SendFailedException;
import javax.mail.internet.MimeMessage; import javax.mail.internet.MimeMessage;
import java.io.File; import java.io.File;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
@Component @Component
public class EmailServiceImpl implements EmailService { public class EmailServiceImpl implements EmailService {
...@@ -28,40 +37,25 @@ public class EmailServiceImpl implements EmailService { ...@@ -28,40 +37,25 @@ public class EmailServiceImpl implements EmailService {
private String filePath; private String filePath;
@Value("${proper.file.path}") @Value("${proper.file.path}")
private String properFilePath; private String properFilePath;
@Value("${proper.base.path}")
private String properBasePath;
@Autowired @Autowired
private JavaMailSender mailSender; private JavaMailSender mailSender;
private static final Logger logger = LoggerFactory.getLogger(EmailServiceImpl.class); private static final Logger logger = LoggerFactory.getLogger(EmailServiceImpl.class);
@Override private void sendAttachMail(String[] toUsers,String[] ccUsers,String name) throws MessagingException {
public int sendAttachmentsMail(String to,String cc,String name){
int rs = Constants.SUCCESS;
String subject = ""; String subject = "";
String properPath = properBasePath + "pm/" + name + "/en-cn.properties"; String value = ProperUtils.getValue(properFilePath, name);
logger.info("邮件主题属性文件目录为:{}",properPath);
String value = ProperUtils.getValue(properPath , name);
//String value = ProperUtils.getValue(properFilePath, name);
if(null != value){ if(null != value){
subject = value; subject = value;
}else{ }else{
subject = name; subject = name;
} }
MimeMessage message = mailSender.createMimeMessage(); MimeMessage message = mailSender.createMimeMessage();
try {
//MimeMessageHelper helper = new MimeMessageHelper(message, true);
MimeMessageHelper helper = new MimeMessageHelper(message, true,"UTF-8"); MimeMessageHelper helper = new MimeMessageHelper(message, true,"UTF-8");
helper.setFrom(from); helper.setFrom(from);
String[] toUsers = to.split(",");
helper.setTo(toUsers); helper.setTo(toUsers);
helper.setSubject(subject); helper.setSubject(subject);
helper.setText(content, true); helper.setText(content, true);
if (cc != null && !"".equals(cc.trim())) {
String[] ccUsers = cc.split(",");
helper.setCc(ccUsers); helper.setCc(ccUsers);
}
if (bcc != null && !"".equals(bcc.trim())) { if (bcc != null && !"".equals(bcc.trim())) {
String[] bccUsers = bcc.split(","); String[] bccUsers = bcc.split(",");
helper.setBcc(bccUsers); helper.setBcc(bccUsers);
...@@ -72,12 +66,38 @@ public class EmailServiceImpl implements EmailService { ...@@ -72,12 +66,38 @@ public class EmailServiceImpl implements EmailService {
helper.addAttachment(fileName, file); helper.addAttachment(fileName, file);
//helper.addAttachment(MimeUtility.encodeWord(file.getFilename(),"utf-8","B"), file); //helper.addAttachment(MimeUtility.encodeWord(file.getFilename(),"utf-8","B"), file);
mailSender.send(message); mailSender.send(message);
}
@Override
public void sendAttachmentsMail(String to,String cc,String name) throws Exception{
String[] toUsers = to.split(",");
String[] ccUsers = cc.split(",");
try {
sendAttachMail(toUsers,ccUsers,name);
logger.info("带附件的邮件已经发送。"); logger.info("带附件的邮件已经发送。");
} catch (Exception e) { } catch (SendFailedException sendFailedException) {
logger.error("发送带附件的邮件时发生异常!", e); Set<String> failMails = new HashSet<String>();
rs = Constants.FAIL; for(Address address: sendFailedException.getInvalidAddresses()){
failMails.add(address.toString().trim());
}
List<String> validToMails = new ArrayList<String>();
for(String toMail : toUsers){
if(StringUtils.isBlank(toMail)||failMails.contains(toMail.trim())){
logger.warn("剔除无效邮箱地址:{}",toMail);
continue;
}
validToMails.add(toMail);
}
List<String> validCcMails = new ArrayList<String>();
for(String ccMail : ccUsers){
if(StringUtils.isBlank(ccMail)||failMails.contains(ccMail.trim())){
logger.warn("剔除无效邮箱地址:{}",ccMail);
continue;
}
validCcMails.add(ccMail);
}
sendAttachMail((String[])validToMails.toArray(),(String[])validCcMails.toArray(),name);
} }
return rs;
} }
@Override @Override
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment