@ -8,6 +8,7 @@ import au.com.royalpay.payment.manage.mappers.system.ClientMapper;
import au.com.royalpay.payment.manage.mappers.system.ClientBDMapper ;
import au.com.royalpay.payment.manage.notice.core.MailService ;
import au.com.royalpay.payment.manage.riskbusiness.core.RiskBusinessService ;
import au.com.royalpay.payment.manage.tradelog.core.TradeLogService ;
import au.com.royalpay.payment.tools.env.PlatformEnvironment ;
import au.com.royalpay.payment.tools.exceptions.BadRequestException ;
import au.com.royalpay.payment.tools.threadpool.RoyalThreadPoolExecutor ;
@ -16,10 +17,13 @@ import com.alibaba.fastjson.JSONObject;
import com.github.miemiedev.mybatis.paginator.domain.Order ;
import com.github.miemiedev.mybatis.paginator.domain.PageBounds ;
import com.github.miemiedev.mybatis.paginator.domain.PageList ;
import org.apache.commons.codec.binary.Base64 ;
import org.apache.commons.lang3.RandomStringUtils ;
import org.apache.commons.lang3.StringUtils ;
import org.apache.commons.lang3.time.DateFormatUtils ;
import org.apache.commons.lang3.time.DateUtils ;
import org.apache.poi.ss.usermodel.* ;
import org.apache.poi.xssf.usermodel.XSSFWorkbook ;
import org.springframework.beans.factory.annotation.Autowired ;
import org.springframework.data.redis.core.StringRedisTemplate ;
import org.springframework.stereotype.Service ;
@ -27,8 +31,10 @@ import org.thymeleaf.context.Context;
import org.thymeleaf.spring4.SpringTemplateEngine ;
import javax.servlet.http.HttpServletResponse ;
import java.io.ByteArrayOutputStream ;
import java.io.IOException ;
import java.io.InputStream ;
import java.math.BigDecimal ;
import java.net.URL ;
import java.util.ArrayList ;
import java.util.Date ;
@ -62,6 +68,8 @@ public class RiskBusinessServiceImpl implements RiskBusinessService {
@Resource
private MailService mailService ;
@Resource
private TradeLogService tradeLogService ;
@Resource
private RoyalThreadPoolExecutor royalThreadPoolExecutor ;
private final String UPLOAD_MAIL_PREFIX = "UPLOAD_MAIL" ;
@ -80,7 +88,6 @@ public class RiskBusinessServiceImpl implements RiskBusinessService {
@Override
public JSONObject getRiskEventDetail ( String riskId ) {
JSONObject riskEventDetail = riskEventMapper . findById ( riskId ) ;
// 获取商户信息
String clientMoniker = riskEventDetail . getString ( "client_moniker" ) ;
JSONObject client = clientMapper . findClientByMonikerAll ( clientMoniker ) ;
@ -157,15 +164,17 @@ public class RiskBusinessServiceImpl implements RiskBusinessService {
}
@Override
public void sendUploadEmail ( String riskId ) {
public void sendUploadEmail ( String riskId ) throws IOException {
JSONObject event = getRiskEventDetail ( riskId ) ;
Context ctx = getMailContext ( event ) ;
final List < String > emails = ( List < String > ) ctx . getVariable ( "emails" ) ;
final List < String > emailsTos = ( List < String > ) ctx . getVariable ( "emailsTos" ) ;
final List < String > emailsCcs = ctx . getVariable ( "emailsCcs" ) = = null ? new ArrayList < > ( ) : ( List < String > ) ctx . getVariable ( "emailsCcs" ) ;
final String title = ( String ) ctx . getVariable ( "title" ) ;
final String content = thymeleaf . process ( "mail/risk_upload_mail.html" , ctx ) ;
royalThreadPoolExecutor . execute ( ( ) - > {
try {
String emailId = mailService . sendRiskEmail ( "Your merchants needs to submit risk materials" , email s. isEmpty ( ) ? "" : StringUtils . join ( email s, "," ) ,
"" , content , event . getIntValue ( "order_type" ) ) ;
String emailId = mailService . sendRiskEmail ( title , emailsTo s. isEmpty ( ) ? "" : StringUtils . join ( email sTo s, "," ) ,
emailsCcs . isEmpty ( ) ? "" : StringUtils . join ( emailsCcs , "," ) , content , event . getIntValue ( "order_type" ) = = 3 ? ( List < JSONObject > ) ctx . getVariable ( "files" ) : null , event . getIntValue ( "order_type" ) ) ;
event . put ( "email_status" , 1 ) ;
event . put ( "result_type" , 1 ) ;
riskEventMapper . update ( event ) ;
@ -176,16 +185,17 @@ public class RiskBusinessServiceImpl implements RiskBusinessService {
}
@Override
public void sendRefuseEmail ( String riskId ) {
public void sendRefuseEmail ( String riskId ) throws IOException {
JSONObject event = getRiskEventDetail ( riskId ) ;
Context ctx = getMailContext ( event ) ;
ctx . setVariable ( "refuse" , true ) ;
final List < String > emails = ( List < String > ) ctx . getVariable ( "emails" ) ;
final List < String > emailsTos = ( List < String > ) ctx . getVariable ( "emailsTos" ) ;
final List < String > emailsCcs = ctx . getVariable ( "emailsCcs" ) = = null ? new ArrayList < > ( ) : ( List < String > ) ctx . getVariable ( "emailsCcs" ) ;
final String content = thymeleaf . process ( "mail/risk_upload_mail.html" , ctx ) ;
royalThreadPoolExecutor . execute ( ( ) - > {
try {
String emailId = mailService . sendRiskEmail ( "You r merchants needs to resubmit risk materials", email s. isEmpty ( ) ? "" : StringUtils . join ( email s, "," ) ,
"" , content , event . getIntValue ( "order_type" ) ) ;
String emailId = mailService . sendRiskEmail ( "You need to resubmit risk materials", email sTo s. isEmpty ( ) ? "" : StringUtils . join ( email sTo s, "," ) ,
emailsCcs . isEmpty ( ) ? "" : StringUtils . join ( emailsCcs , "," ) , content , event . getIntValue ( "order_type" ) = = 3 ? ( List < JSONObject > ) ctx . getVariable ( "files" ) : null , event . getIntValue ( "order_type" ) ) ;
event . put ( "email_status" , 2 ) ;
event . put ( "result_type" , 4 ) ;
riskEventMapper . update ( event ) ;
@ -195,7 +205,7 @@ public class RiskBusinessServiceImpl implements RiskBusinessService {
} ) ;
}
private Context getMailContext ( JSONObject event ) {
private Context getMailContext ( JSONObject event ) throws IOException {
JSONObject client = clientMapper . findClientByMonikerAll ( event . getString ( "client_moniker" ) ) ;
if ( client = = null ) {
throw new InvalidShortIdException ( ) ;
@ -206,11 +216,14 @@ public class RiskBusinessServiceImpl implements RiskBusinessService {
}
String codeKeyValue = RandomStringUtils . random ( 10 , true , true ) ;
String expireDay = "7" ;
if ( event . getIntValue ( "order_type" ) > 2 ) {
expireDay = "3" ;
}
stringRedisTemplate . boundValueOps ( getRiskUploadKey ( codeKey ) ) . set ( codeKeyValue , Long . parseLong ( expireDay ) , TimeUnit . DAYS ) ;
String uploadUrl = PlatformEnvironment . getEnv ( ) . concatUrl ( "/risk/upload/" ) + event . getString ( "risk_id" ) + "/" + codeKey ;
List < JSONObject > bds = clientBDMapper . listClientBDInfoAvailable ( client . getIntValue ( "client_id" ) , new Date ( ) ) ;
List < String > bdNames = new ArrayList < > ( ) ;
List < String > e mails = new ArrayList < > ( ) ;
List < String > bdE mails = new ArrayList < > ( ) ;
for ( JSONObject bd : bds ) {
String bdName = bd . getString ( "display_name" ) ;
if ( StringUtils . isNotEmpty ( bdName ) ) {
@ -218,30 +231,175 @@ public class RiskBusinessServiceImpl implements RiskBusinessService {
}
String email = bd . getString ( "email" ) ;
if ( StringUtils . isNotEmpty ( email ) ) {
e mails. add ( email ) ;
bdE mails. add ( email ) ;
}
}
List < String > clientEmails = new ArrayList < > ( ) ;
clientEmails . add ( client . getString ( "contact_email" ) ) ;
String bdNamesStr = bdNames . isEmpty ( ) ? "" : StringUtils . join ( bdNames , "," ) ;
String reply_date = DateFormatUtils . format ( DateUtils . addDays ( event . getDate ( "reply_email_date" ) , - 1 ) , "yyyy年MM月dd日" ) ;
String [ ] orderIds = event . getString ( "order_ids" ) . split ( "," ) ;
List < JSONObject > orders = new ArrayList ( ) ;
for ( String orderId : orderIds ) {
JSONObject order = orderMapper . findOrderById ( orderId ) ;
if ( order = = null ) {
throw new BadRequestException ( "Order: " + orderId + " not exists" ) ;
}
orders . add ( order ) ;
}
String reply_date_english = DateFormatUtils . format ( DateUtils . addDays ( event . getDate ( "reply_email_date" ) , - 1 ) , "dd/MM/yyyy" ) ;
Context ctx = new Context ( ) ;
ctx . setVariable ( "order_type" , event . getIntValue ( "order_type" ) ) ;
ctx . setVariable ( "bdNamesStr" , bdNamesStr ) ;
ctx . setVariable ( "reply_date" , reply_date ) ;
ctx . setVariable ( " orders", orders ) ;
ctx . setVariable ( "reply_date_english" , reply_date_english ) ;
ctx . setVariable ( "client" , client ) ;
ctx . setVariable ( "uploadUrl" , uploadUrl ) ;
ctx . setVariable ( "emails" , emails ) ;
ctx . setVariable ( "royalpay_order_type" , event . getIntValue ( "royalpay_order_type" ) ) ;
ctx . setVariable ( "warning_order_type" , event . getIntValue ( "warning_order_type" ) ) ;
switch ( event . getIntValue ( "order_type" ) ) {
case 1 :
case 2 :
String [ ] orderIds = event . getString ( "order_ids" ) . split ( "," ) ;
List < JSONObject > orders = new ArrayList ( ) ;
for ( String orderId : orderIds ) {
JSONObject order = orderMapper . findOrderById ( orderId ) ;
if ( order = = null ) {
throw new BadRequestException ( "Order: " + orderId + " not exists" ) ;
}
orders . add ( order ) ;
}
ctx . setVariable ( "orders" , orders ) ;
ctx . setVariable ( "title" , "Your merchants needs to submit risk materials" ) ;
ctx . setVariable ( "emailsTos" , bdEmails ) ;
break ;
case 3 :
List < JSONObject > attachList = new ArrayList < > ( ) ;
JSONObject file = new JSONObject ( ) ;
file . put ( "name" , client . getString ( "short_name" ) + "被查单号相关信息.xlsx" ) ;
file . put ( "content" , Base64 . encodeBase64String ( generateRiskOrders ( event ) ) ) ;
attachList . add ( file ) ;
ctx . setVariable ( "files" , attachList ) ;
case 4 :
ctx . setVariable ( "title" , "RoyalPay风控调查 — " + client . getString ( "short_name" ) ) ;
ctx . setVariable ( "emailsCcs" , bdEmails ) ;
ctx . setVariable ( "emailsTos" , clientEmails ) ;
break ;
}
return ctx ;
}
private String getRiskUploadKey ( String codeKey ) {
return UPLOAD_MAIL_PREFIX + codeKey ;
}
private byte [ ] generateRiskOrders ( JSONObject event ) throws IOException {
String [ ] orderIds = event . getString ( "order_ids" ) . split ( "," ) ;
Workbook wb = new XSSFWorkbook ( ) ;
for ( String orderId : orderIds ) {
JSONObject orderDetail = tradeLogService . getOrderDetail ( new JSONObject ( ) , event . getString ( "client_moniker" ) , orderId , null ) ;
Sheet sheet = wb . createSheet ( orderId ) ;
sheet . setDefaultColumnWidth ( ( short ) 40 ) ;
Row row0 = sheet . createRow ( 0 ) ;
Row row1 = sheet . createRow ( 1 ) ;
Row row2 = sheet . createRow ( 2 ) ;
Row row3 = sheet . createRow ( 3 ) ;
Row row4 = sheet . createRow ( 4 ) ;
Row row5 = sheet . createRow ( 5 ) ;
Row row6 = sheet . createRow ( 6 ) ;
Row row7 = sheet . createRow ( 7 ) ;
Row row8 = sheet . createRow ( 8 ) ;
Row row9 = sheet . createRow ( 9 ) ;
Row row10 = sheet . createRow ( 10 ) ;
Row row11 = sheet . createRow ( 11 ) ;
Row row12 = sheet . createRow ( 12 ) ;
Row row13 = sheet . createRow ( 13 ) ;
Row row14 = sheet . createRow ( 14 ) ;
Row row15 = sheet . createRow ( 15 ) ;
row0 . createCell ( 0 , Cell . CELL_TYPE_STRING ) . setCellValue ( "Partner" ) ;
row0 . createCell ( 1 , Cell . CELL_TYPE_STRING ) . setCellValue ( orderDetail . getJSONObject ( "client" ) . getString ( "short_name" ) + "(" + orderDetail . getJSONObject ( "client" ) . getString ( "client_moniker" ) + ")" ) ;
row1 . createCell ( 0 , Cell . CELL_TYPE_STRING ) . setCellValue ( "Order ID" ) ;
row1 . createCell ( 1 , Cell . CELL_TYPE_STRING ) . setCellValue ( orderDetail . getString ( "order_id" ) ) ;
row2 . createCell ( 0 , Cell . CELL_TYPE_STRING ) . setCellValue ( "Platform Transaction ID" ) ;
row2 . createCell ( 1 , Cell . CELL_TYPE_STRING ) . setCellValue ( orderDetail . getString ( "system_transaction_id" ) ) ;
row3 . createCell ( 0 , Cell . CELL_TYPE_STRING ) . setCellValue ( "Order Description" ) ;
row3 . createCell ( 1 , Cell . CELL_TYPE_STRING ) . setCellValue ( StringUtils . defaultString ( orderDetail . getString ( "order_description" ) ) ) ;
row4 . createCell ( 0 , Cell . CELL_TYPE_STRING ) . setCellValue ( "Customer ID" ) ;
row4 . createCell ( 1 , Cell . CELL_TYPE_STRING ) . setCellValue ( orderDetail . getString ( "customer_id" ) ) ;
row5 . createCell ( 0 , Cell . CELL_TYPE_STRING ) . setCellValue ( "IP" ) ;
row5 . createCell ( 1 , Cell . CELL_TYPE_STRING ) . setCellValue ( orderDetail . getString ( "customer_ip" ) ) ;
row6 . createCell ( 0 , Cell . CELL_TYPE_STRING ) . setCellValue ( "Total Amount" ) ;
row6 . createCell ( 1 , Cell . CELL_TYPE_STRING ) . setCellValue ( orderDetail . getString ( "currency" ) + " " + orderDetail . getString ( "total_amount" ) ) ;
row7 . createCell ( 0 , Cell . CELL_TYPE_STRING ) . setCellValue ( "Input Amount" ) ;
row7 . createCell ( 1 , Cell . CELL_TYPE_STRING ) . setCellValue ( orderDetail . getString ( "currency" ) + " " + orderDetail . getString ( "display_amount" ) ) ;
row8 . createCell ( 0 , Cell . CELL_TYPE_STRING ) . setCellValue ( "Pay Amount" ) ;
row8 . createCell ( 1 , Cell . CELL_TYPE_STRING ) . setCellValue ( orderDetail . getString ( "currency" ) + " " + orderDetail . getString ( "customer_payment_amount" ) ) ;
row9 . createCell ( 0 , Cell . CELL_TYPE_STRING ) . setCellValue ( "Exchange Rate" ) ;
row9 . createCell ( 1 , Cell . CELL_TYPE_STRING ) . setCellValue ( orderDetail . getString ( "exchange_rate" ) ) ;
row10 . createCell ( 0 , Cell . CELL_TYPE_STRING ) . setCellValue ( "Clearing Amount" ) ;
row10 . createCell ( 1 , Cell . CELL_TYPE_STRING ) . setCellValue ( "AUD " + orderDetail . getString ( "clearing_amount" ) ) ;
row11 . createCell ( 0 , Cell . CELL_TYPE_STRING ) . setCellValue ( "Gateway" ) ;
row11 . createCell ( 1 , Cell . CELL_TYPE_STRING ) . setCellValue ( getGateWay ( orderDetail . getIntValue ( "gateway" ) ) ) ;
row12 . createCell ( 0 , Cell . CELL_TYPE_STRING ) . setCellValue ( "Create Time" ) ;
row12 . createCell ( 1 , Cell . CELL_TYPE_STRING ) . setCellValue ( orderDetail . getString ( "create_time" ) ) ;
row13 . createCell ( 0 , Cell . CELL_TYPE_STRING ) . setCellValue ( "Status" ) ;
row13 . createCell ( 1 , Cell . CELL_TYPE_STRING ) . setCellValue ( getStatus ( orderDetail . getIntValue ( "status" ) ) ) ;
row14 . createCell ( 0 , Cell . CELL_TYPE_STRING ) . setCellValue ( "Pay Time" ) ;
row14 . createCell ( 1 , Cell . CELL_TYPE_STRING ) . setCellValue ( orderDetail . getString ( "transaction_time" ) ) ;
row15 . createCell ( 0 , Cell . CELL_TYPE_STRING ) . setCellValue ( "Order Detail" ) ;
row15 . createCell ( 1 , Cell . CELL_TYPE_STRING ) . setCellValue ( StringUtils . defaultString ( orderDetail . getString ( "order_detail" ) ) ) ;
}
ByteArrayOutputStream bos = new ByteArrayOutputStream ( ) ;
wb . write ( bos ) ;
bos . flush ( ) ;
return bos . toByteArray ( ) ;
}
private String getGateWay ( int gateWay ) {
switch ( gateWay ) {
case 0 :
return "线下扫码" ;
case 1 :
return "线下扫码" ;
case 2 :
return "商户静态码" ;
case 3 :
return "线上网关" ;
case 4 :
return "JSAPI网关" ;
case 5 :
return "线下网关" ;
case 6 :
return "线下网关" ;
case 7 :
return "商户静态码" ;
case 8 :
return "Mobile H5" ;
case 9 :
return "第三方网关" ;
case 10 :
return "APP网关" ;
case 11 :
return "账单码" ;
case 12 :
return "小程序" ;
case 13 :
return "原生二维码" ;
case 14 :
return "账单链接" ;
}
return "" ;
}
private String getStatus ( int status ) {
switch ( status ) {
case 0 :
return "Creating" ;
case 1 :
return "Failed Create Order" ;
case 2 :
return "Wait For Payment" ;
case 3 :
return "Closed" ;
case 4 :
return "Payment Failed" ;
case 5 :
return "Payment Success" ;
case 6 :
return "Partial Refund" ;
case 7 :
return "Full Refund" ;
}
return "" ;
}
}