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

refact codes

parent efbc1e75
......@@ -79,6 +79,7 @@
</component>
<component name="RunAnythingCache">
<option name="myCommands">
<command value="mvn clean" />
<command value="mvn clean install -DskipTests" />
</option>
</component>
......@@ -214,25 +215,25 @@
<screen x="0" y="22" width="1920" height="1129" />
</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 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" />
</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="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" />
</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="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" />
</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="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" />
</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="386" key="GridCell.Tab.1.bottom" timestamp="1591671347527">
<screen x="0" y="22" width="1920" height="1129" />
......@@ -250,9 +251,9 @@
<screen x="0" y="22" width="1920" height="1129" />
</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 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" />
</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>
</project>
\ No newline at end of file
......@@ -17,18 +17,18 @@ public class BiReportEndpoint {
return biReportService.execSql(name);
}
@ResponseBody
@RequestMapping(value = "/genExcel")
public int genExcel(@RequestParam String name){
return biReportService.genExcel(name);
}
@ResponseBody
@RequestMapping(value = "/execSqlByHive")
public int execSqlByHive(@RequestParam String name){
return biReportService.execSql(name);
}
@ResponseBody
@RequestMapping(value = "/genExcel")
public int genExcel(@RequestParam String name){
return biReportService.genExcel(name);
}
@ResponseBody
@RequestMapping(value = "/sendEmail/{to}/{cc}/{name}",method = RequestMethod.GET)
public int sendEmail(@PathVariable("to") String to,@PathVariable("cc") String cc,@PathVariable("name") String name){
......@@ -37,7 +37,7 @@ public class BiReportEndpoint {
@ResponseBody
@RequestMapping(value = "/email/{name}/{to}/{cc}",method = RequestMethod.GET)
public int email(@PathVariable("name") String name,@PathVariable("to") String to,@PathVariable("cc") String cc){
return biReportService.email(name,to,cc);
public void email(@PathVariable("name") String name,@PathVariable("to") String to,@PathVariable("cc") String cc) throws Exception{
biReportService.email(name,to,cc);
}
}
......@@ -4,6 +4,6 @@ public interface BiReportService {
int execSql(String name);
int genExcel(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);
}
package com.gmei.data.gateway.server.service;
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);
}
......@@ -35,18 +35,16 @@ public class BiReportServiceImpl implements BiReportService {
}
@Override
public int email(String name, String to, String cc) {
int rs = Constants.SUCCESS;
public void email(String name, String to, String cc) throws Exception{
ExcelGenDto resultDto = sparksqlService.getResult(name);
if(null == resultDto){
rs = Constants.FAIL;
throw new Exception("获取查询结果失败,请核实!");
}else{
int genExcelRs = excelService.genExcel(resultDto,name);
if(Constants.SUCCESS == genExcelRs){
rs = emailService.sendAttachmentsMail(to,cc,name);
emailService.sendAttachmentsMail(to,cc,name);
}
}
return rs;
}
@Override
......@@ -68,7 +66,11 @@ public class BiReportServiceImpl implements BiReportService {
}else{
int genExcelRs = excelService.genExcel(resultDto,name);
if(Constants.SUCCESS == genExcelRs){
rs = emailService.sendAttachmentsMail(to,cc,name);
try{
emailService.sendAttachmentsMail(to,cc,name);
}catch (Exception e){
rs = Constants.FAIL;
}
}
}
return rs;
......
......@@ -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.utils.DateUtils;
import com.gmei.data.gateway.server.utils.ProperUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
......@@ -13,8 +14,16 @@ import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;
import javax.mail.Address;
import javax.mail.MessagingException;
import javax.mail.SendFailedException;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
@Component
public class EmailServiceImpl implements EmailService {
......@@ -28,40 +37,25 @@ public class EmailServiceImpl implements EmailService {
private String filePath;
@Value("${proper.file.path}")
private String properFilePath;
@Value("${proper.base.path}")
private String properBasePath;
@Autowired
private JavaMailSender mailSender;
private static final Logger logger = LoggerFactory.getLogger(EmailServiceImpl.class);
@Override
public int sendAttachmentsMail(String to,String cc,String name){
int rs = Constants.SUCCESS;
private void sendAttachMail(String[] toUsers,String[] ccUsers,String name) throws MessagingException {
String subject = "";
String properPath = properBasePath + "pm/" + name + "/en-cn.properties";
logger.info("邮件主题属性文件目录为:{}",properPath);
String value = ProperUtils.getValue(properPath , name);
//String value = ProperUtils.getValue(properFilePath, name);
String value = ProperUtils.getValue(properFilePath, name);
if(null != value){
subject = value;
}else{
subject = name;
}
MimeMessage message = mailSender.createMimeMessage();
try {
//MimeMessageHelper helper = new MimeMessageHelper(message, true);
MimeMessageHelper helper = new MimeMessageHelper(message, true,"UTF-8");
helper.setFrom(from);
String[] toUsers = to.split(",");
helper.setTo(toUsers);
helper.setSubject(subject);
helper.setText(content, true);
if (cc != null && !"".equals(cc.trim())) {
String[] ccUsers = cc.split(",");
helper.setCc(ccUsers);
}
if (bcc != null && !"".equals(bcc.trim())) {
String[] bccUsers = bcc.split(",");
helper.setBcc(bccUsers);
......@@ -72,12 +66,38 @@ public class EmailServiceImpl implements EmailService {
helper.addAttachment(fileName, file);
//helper.addAttachment(MimeUtility.encodeWord(file.getFilename(),"utf-8","B"), file);
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("带附件的邮件已经发送。");
} catch (Exception e) {
logger.error("发送带附件的邮件时发生异常!", e);
rs = Constants.FAIL;
} catch (SendFailedException sendFailedException) {
Set<String> failMails = new HashSet<String>();
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
......
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