@ -1,22 +1,31 @@
package au.com.royalpay.payment.manage.system.core.impl ;
import au.com.royalpay.payment.manage.mappers.system.ClientMapper ;
import au.com.royalpay.payment.manage.mappers.system.MailSendMapper ;
import au.com.royalpay.payment.manage.system.core.MailGunService ;
import au.com.royalpay.payment.tools.connections.attachment.core.AttachmentClient ;
import au.com.royalpay.payment.tools.mail.MailGunClient ;
import au.com.royalpay.payment.tools.mail.SendMail ;
import com.alibaba.fastjson.JSONObject ;
import org.apache.commons.lang3.StringUtils ;
import org.jsoup.Jsoup ;
import org.jsoup.nodes.Document ;
import org.jsoup.nodes.Element ;
import org.jsoup.select.Elements ;
import org.slf4j.Logger ;
import org.slf4j.LoggerFactory ;
import org.springframework.beans.factory.annotation.Value ;
import org.springframework.stereotype.Service ;
import java.io.IOException ;
import java.net.URISyntaxException ;
import java.net.URLDecoder ;
import java.util.ArrayList ;
import java.util.HashMap ;
import java.util.List ;
import java.util.Map ;
import java.util.Set ;
import javax.annotation.Resource ;
@ -30,12 +39,11 @@ public class MailGunServiceImpl implements MailGunService {
@Resource
private MailGunClient mailGunClient ;
@Resource
private ClientMapper clientMapper ;
@Value ( "${mail.mailgun.default.merchantlist}" )
private String mailListDefault ;
@Resource
private AttachmentClient attachmentClient ;
@Override
public void dealNotify ( String nofityString ) throws Exception {
@ -68,34 +76,67 @@ public class MailGunServiceImpl implements MailGunService {
JSONObject var = new JSONObject ( ) ;
var . put ( "client_moniker" , client . getString ( "client_moniker" ) ) ;
var . put ( "short_name" , client . getString ( "short_name" ) ) ;
result = mailGunClient . addListMember ( client . getString ( "contact_email" ) , mailListDefault , client . getString ( "contact_person" ) , var ) ;
} catch ( Exception ignore ) {
logger . info ( "add Mail List Failed email:" + client . getString ( "contact_email" ) + " client_moniker:" + client . getString ( "client_moniker" ) ) ;
result = mailGunClient . addListMember ( client . getString ( "contact_email" ) , mailListDefault , client . getString ( "contact_person" ) , var ) ;
} catch ( Exception ignore ) {
logger . info ( "add Mail List Failed email:" + client . getString ( "contact_email" ) + " client_moniker:" + client . getString ( "client_moniker" ) ) ;
}
return result ;
return result ;
}
@Override
public JSONObject updateClientOfMailList ( JSONObject newClient , JSONObject oldClient ) {
JSONObject result = null ;
JSONObject result = null ;
try {
JSONObject var = new JSONObject ( ) ;
var . put ( "client_moniker" , newClient . getString ( "client_moniker" ) ) ;
var . put ( "short_name" , newClient . getString ( "short_name" ) ) ;
result = mailGunClient . updateClientOfMailList ( newClient . getString ( "contact_email" ) , mailListDefault , newClient . getString ( "contact_person" ) , oldClient . getString ( "contact_email" ) , var ) ;
} catch ( Exception ignore ) {
logger . info ( "Modify Mail List Failed oldEmail:" + oldClient . getString ( "contact_email" ) + " client_moniker:" + newClient . getString ( "client_moniker" ) + " newEmail:" + newClient . getString ( "contact_email" ) ) ;
result = mailGunClient . updateClientOfMailList ( newClient . getString ( "contact_email" ) , mailListDefault , newClient . getString ( "contact_person" ) ,
oldClient . getString ( "contact_email" ) , var ) ;
} catch ( Exception ignore ) {
logger . info ( "Modify Mail List Failed oldEmail:" + oldClient . getString ( "contact_email" ) + " client_moniker:" + newClient . getString ( "client_moniker" )
+ " newEmail:" + newClient . getString ( "contact_email" ) ) ;
}
return result ;
return result ;
}
@Override
public JSONObject sendEmail ( String notice_id , String title , Set < String > mailTo , String content ) throws URISyntaxException , IOException {
Document doc = Jsoup . parse ( content ) ;
Elements links = doc . select ( "a[href]" ) ;
List < JSONObject > files = new ArrayList < > ( ) ;
for ( Element link : links ) {
String linkHref = link . attr ( "href" ) ;
String linkText = link . text ( ) ;
Element e = link . previousElementSibling ( ) ;
if ( e ! = null & & "img" . equalsIgnoreCase ( e . tagName ( ) ) ) {
e . remove ( ) ;
}
if ( linkHref . contains ( "mailto" ) ) {
continue ;
}
JSONObject file = new JSONObject ( ) ;
file . put ( "name" , linkText ) ;
file . put ( "byteArr" , attachmentClient . getFileByUrl ( linkHref ) ) ;
files . add ( file ) ;
}
SendMail sendMail = new SendMail ( ) ;
sendMail . setFrom ( "postmaster@dev.showcodes.com" ) ;
sendMail . setTitle ( title ) ;
sendMail . setContent ( doc . outerHtml ( ) ) ;
sendMail . setNotice_id ( notice_id ) ;
sendMail . setMailTos ( mailTo ) ;
sendMail . setAttachFiles ( files ) ;
return mailGunClient . sendMail ( sendMail ) ;
}
public Map < String , String > getQueryMap ( String query ) {
String [ ] params = query . split ( "&" ) ;
Map < String , String > map = new HashMap < > ( ) ;
for ( String param : params ) {
String [ ] tmpArr = param . split ( "=" ) ;
if ( tmpArr . length < 2 ) {
String [ ] tmpArr = param . split ( "=" ) ;
if ( tmpArr . length < 2 ) {
continue ;
}
map . put ( tmpArr [ 0 ] , tmpArr [ 1 ] ) ;