@ -36,6 +36,7 @@ import au.com.royalpay.payment.tools.utils.PageListUtils;
import au.com.royalpay.payment.tools.utils.TimeZoneUtils ;
import com.alibaba.fastjson.JSON ;
import com.alibaba.fastjson.JSONArray ;
import com.alibaba.fastjson.JSONException ;
import com.alibaba.fastjson.JSONObject ;
import com.github.miemiedev.mybatis.paginator.domain.Order ;
import com.github.miemiedev.mybatis.paginator.domain.PageBounds ;
@ -325,6 +326,7 @@ public class CleanServiceImpl implements CleanService, ManagerTodoNoticeProvider
String filename = "Merchant_Settlement_Info_" + dateString + RandomStringUtils . random ( 8 , false , true ) + ".csv" ;
zos . putNextEntry ( new ZipEntry ( filename ) ) ;
List < JSONObject > details = clearingDetailMapper . listReportsOfSettlement ( log . getIntValue ( "clearing_id" ) ) ;
details = mergeBatchSettleClients ( details ) ;
byte [ ] csv = generateSettleCSVFile ( dt , details ) ;
IOUtils . write ( csv , zos ) ;
}
@ -369,6 +371,7 @@ public class CleanServiceImpl implements CleanService, ManagerTodoNoticeProvider
logger . info ( "using newest version test" ) ;
for ( JSONObject log : logs ) {
List < JSONObject > details = clearingDetailMapper . listReportsOfSettlement ( log . getIntValue ( "clearing_id" ) ) ;
details = mergeBatchSettleClients ( details ) ;
exportAllBankXlsFiles ( zos , details , log . getDate ( "operate_time" ) ) ;
}
zos . flush ( ) ;
@ -400,7 +403,9 @@ public class CleanServiceImpl implements CleanService, ManagerTodoNoticeProvider
String filename = "Merchant_Settlement_Info_" + DateFormatUtils . format ( dt , "yyyyMMdd" ) + "_" + fileIndex + ".xlsx" ;
JSONObject file = new JSONObject ( ) ;
file . put ( "name" , filename ) ;
file . put ( "byteArr" , generateSettleXlsxFile ( dt , clearingDetailMapper . listReportsOfSettlement ( log . getIntValue ( "clearing_id" ) ) , bank ) ) ;
List < JSONObject > details = clearingDetailMapper . listReportsOfSettlement ( log . getIntValue ( "clearing_id" ) ) ;
details = mergeBatchSettleClients ( details ) ;
file . put ( "byteArr" , generateSettleXlsxFile ( dt , details , bank ) ) ;
result . add ( file ) ;
fileIndex + + ;
}
@ -409,7 +414,9 @@ public class CleanServiceImpl implements CleanService, ManagerTodoNoticeProvider
String filename = "Merchant_Settlement_Info_" + DateFormatUtils . format ( dt , "yyyyMMdd" ) + ".xlsx" ;
JSONObject file = new JSONObject ( ) ;
file . put ( "name" , filename ) ;
file . put ( "byteArr" , generateSettleXlsxFile ( dt , clearingDetailMapper . listReportsOfSettlement ( log . getIntValue ( "clearing_id" ) ) , bank ) ) ;
List < JSONObject > details = clearingDetailMapper . listReportsOfSettlement ( log . getIntValue ( "clearing_id" ) ) ;
details = mergeBatchSettleClients ( details ) ;
file . put ( "byteArr" , generateSettleXlsxFile ( dt , details , bank ) ) ;
result . add ( file ) ;
}
}
@ -417,6 +424,67 @@ public class CleanServiceImpl implements CleanService, ManagerTodoNoticeProvider
return result ;
}
private List < JSONObject > mergeBatchSettleClients ( List < JSONObject > details ) {
Map < String , List < JSONObject > > sameBankDetails = details . stream ( )
. filter ( this : : detailEnabledMergeSettle )
. collect ( Collectors . groupingBy ( this : : detailGroupingId ) ) ;
List < JSONObject > mergedSettleDetails = sameBankDetails . values ( ) . stream ( )
. map ( this : : mergeDetailGroup ) . collect ( Collectors . toList ( ) ) ;
List < Integer > relatedDetailIds = sameBankDetails . values ( ) . stream ( )
. flatMap ( group - > group . stream ( ) . map ( detail - > detail . getInteger ( "clear_detail_id" ) ) )
. collect ( Collectors . toList ( ) ) ;
details . removeIf ( detail - > relatedDetailIds . contains ( detail . getInteger ( "clear_detail_id" ) ) ) ;
details . addAll ( mergedSettleDetails ) ;
return details ;
}
private JSONObject mergeDetailGroup ( List < JSONObject > details ) {
JSONObject first = details . get ( 0 ) ;
int clientId = details . stream ( ) . map ( detail - > detail . getInteger ( "parent_client_id" ) )
. filter ( Objects : : nonNull )
. findAny ( ) . orElse ( first . getIntValue ( "client_id" ) ) ;
JSONObject cli = clientManager . getClientInfo ( clientId ) ;
String moniker = cli . getString ( "client_moniker" ) ;
String bsb = first . getString ( "bsb_no" ) ;
String accountNo = first . getString ( "account_no" ) ;
String accountName = first . getString ( "account_name" ) ;
String settleBank = first . getString ( "settle_bank" ) ;
BigDecimal clearingAmount = details . stream ( ) . map ( detail - > detail . getBigDecimal ( "clearing_amount" ) )
. reduce ( BigDecimal : : add ) . orElse ( BigDecimal . ZERO ) ;
JSONObject merged = new JSONObject ( ) ;
merged . put ( "client_id" , clientId ) ;
merged . put ( "client_moniker" , moniker ) ;
merged . put ( "bsb_no" , bsb ) ;
merged . put ( "account_no" , accountNo ) ;
merged . put ( "account_name" , accountName ) ;
merged . put ( "settle_bank" , settleBank ) ;
merged . put ( "clearing_amount" , clearingAmount ) ;
return merged ;
}
private String detailGroupingId ( JSONObject detail ) {
String cliPId = detail . getString ( "parent_client_id" ) ;
if ( cliPId = = null ) {
cliPId = detail . getString ( "client_id" ) ;
}
String bsb = detail . getString ( "bsb_no" ) ;
String accountNo = detail . getString ( "account_no" ) ;
return String . join ( "_" , cliPId , bsb , accountNo ) ;
}
private boolean detailEnabledMergeSettle ( JSONObject detail ) {
String extParams = detail . getString ( "ext_params" ) ;
if ( extParams ! = null ) {
try {
JSONObject ext = JSON . parseObject ( extParams ) ;
return ext . getBooleanValue ( "merge_settle" ) ;
} catch ( JSONException e ) {
return false ;
}
}
return false ;
}
@Override
public List < ABAFile > getAba ( Date dt , String bank ) {
List < JSONObject > logs = clearingLogMapper . findByDate ( dt ) ;
@ -427,6 +495,7 @@ public class CleanServiceImpl implements CleanService, ManagerTodoNoticeProvider
List < ABAFile > files = new ArrayList < > ( ) ;
for ( JSONObject log : logs ) {
List < JSONObject > details = clearingDetailMapper . listReportsOfSettlement ( log . getIntValue ( "clearing_id" ) ) ;
details = mergeBatchSettleClients ( details ) ;
files . addAll ( generateSettleAbaFiles ( dt , details , log . getDate ( "operate_time" ) ) ) ;
}
return files . stream ( ) . filter ( file - > bank . equals ( file . bank ( ) ) ) . collect ( Collectors . toList ( ) ) ;
@ -447,7 +516,7 @@ public class CleanServiceImpl implements CleanService, ManagerTodoNoticeProvider
row . createCell ( 0 , Cell . CELL_TYPE_STRING ) . setCellValue ( settle . getString ( "bsb_no" ) ) ;
row . createCell ( 1 , Cell . CELL_TYPE_STRING ) . setCellValue ( settle . getString ( "account_no" ) ) ;
row . createCell ( 2 , Cell . CELL_TYPE_STRING ) . setCellValue ( settle . getString ( "account_name" ) ) ;
row . createCell ( 3 , Cell . CELL_TYPE_STRING ) . setCellValue ( settle . getBigDecimal ( "clearing_amount" ) . setScale ( 2 , BigDecimal. ROUND_ DOWN) . toPlainString ( ) ) ;
row . createCell ( 3 , Cell . CELL_TYPE_STRING ) . setCellValue ( settle . getBigDecimal ( "clearing_amount" ) . setScale ( 2 , RoundingMode. DOWN) . toPlainString ( ) ) ;
}
ByteArrayOutputStream bos = new ByteArrayOutputStream ( ) ;
wb . write ( bos ) ;
@ -466,6 +535,7 @@ public class CleanServiceImpl implements CleanService, ManagerTodoNoticeProvider
for ( JSONObject log : logs ) {
List < JSONObject > details = clearingDetailMapper . listReportsOfSettlement ( log . getIntValue ( "clearing_id" ) ) ;
details = mergeBatchSettleClients ( details ) ;
files . addAll ( generateSettleAbaFiles ( dt , details , log . getDate ( "operate_time" ) ) ) ;
}
OutputStream ous = resp . getOutputStream ( ) ;
@ -502,6 +572,7 @@ public class CleanServiceImpl implements CleanService, ManagerTodoNoticeProvider
throw new NotFoundException ( "Clearing batch " + batchId + " not found" ) ;
}
List < JSONObject > details = clearingDetailMapper . listReportsOfSettlement ( clearingId ) ;
details = mergeBatchSettleClients ( details ) ;
Date settleDate = clearing . getDate ( "settle_date" ) ;
Date opTime = clearing . getDate ( "operate_time" ) ;
String zipName = "Merchant_Settlement_Info_" + DateFormatUtils . format ( opTime , "yyyyMMddHHmmss" ) + "_all.zip" ;
@ -1214,6 +1285,7 @@ public class CleanServiceImpl implements CleanService, ManagerTodoNoticeProvider
String defaultBank = config . getRemainsTo ( ) ;
clearingDetailMapper . updateAllBanks ( defaultBank , clearingId ) ;
List < JSONObject > details = clearingDetailMapper . listReportsOfSettlement ( clearingId ) ;
details = mergeBatchSettleClients ( details ) ;
details . sort ( ( log1 , log2 ) - > {
if ( StringUtils . equals ( defaultBank , log1 . getString ( "account_bank" ) ) = =
StringUtils . equals ( defaultBank , log2 . getString ( "account_bank" ) ) ) {