@ -3,6 +3,8 @@ package com.glxp.api.idc.service.impl;
import java.io.File ;
import java.io.FileOutputStream ;
import java.io.IOException ;
import java.io.InputStream ;
import java.io.OutputStream ;
import java.util.ArrayList ;
import java.util.Date ;
import java.util.HashMap ;
@ -13,6 +15,7 @@ import java.util.concurrent.TimeUnit;
import javax.annotation.Resource ;
import javax.servlet.http.HttpServletRequest ;
import javax.servlet.http.HttpServletResponse ;
import org.apache.commons.lang3.StringUtils ;
import org.apache.commons.text.WordUtils ;
@ -142,8 +145,6 @@ public class IdcServiceImpl implements IdcService {
@Override
public void asyncFetchTask ( ) {
fetchTask ( true ) ;
}
@ -210,7 +211,7 @@ public class IdcServiceImpl implements IdcService {
boolean sync = true ;
String [ ] tnames = t . split ( "/" ) ;
String lastUpdateTime = getUpdateTime ( tnames [ 2 ] ) ;
String lastUpdateTime = getUpdateTime ( tnames [ 3 ] ) ;
Date nowUpdateTime = new Date ( ) ;
if ( ! StringUtils . isEmpty ( tnames [ 0 ] ) ) {
@ -219,7 +220,7 @@ public class IdcServiceImpl implements IdcService {
Map < String , Object > map = new HashMap < String , Object > ( ) ;
String sqlWhere = "" ;
if ( ! StringUtils . isEmpty ( tnames [ 6 ] ) ) {
sqlWhere + = " " + tnames [ 6 ] + " >=cast('"+ lastUpdateTime + "' as datetime)" ;
sqlWhere + = " " + tnames [ 6 ] + " between cast('"+ lastUpdateTime + "' as datetime) and cast('" + DateUtil . formatDate ( nowUpdateTime , "yyyy-MM-dd HH:mm:ss" ) + "' as datetime)" ;
} else {
sqlWhere = "not exists (select fkId from idc_record where type='" + tnames [ 2 ] + "' and fkId=" + tnames [ 2 ] + "." + keyColumn + ")" ;
map . put ( "isEnd" , "1" ) ;
@ -272,11 +273,12 @@ public class IdcServiceImpl implements IdcService {
Response response = client . newCall ( request ) . execute ( ) ;
result = response . body ( ) . string ( ) ;
JSONObject json = JSONObject . parseObject ( result ) ;
if ( isLastLevel ( ) ) {
if ( json . get ( "code" ) ! = null ) {
success = analyToDB ( json. getJSONObject ( "data" ) , files ) ;
success = analyToDB ( host, json. getJSONObject ( "data" ) , files , false ) ;
} else {
success = analyToDB ( json, files ) ;
success = analyToDB ( host, json, files , false ) ;
}
if ( ! success )
logger . info ( "fetchData-->解析失败" ) ;
@ -339,14 +341,15 @@ public class IdcServiceImpl implements IdcService {
String secretKey = request . getHeader ( "secretKey" ) ;
/*******/
JSONObject json = JSONObject . parseObject ( content ) ;
String filePathSlash = filePath . substring ( filePath . length ( ) - 1 ) . equals ( "/" ) ? "" : "/" ;
String backFileSlash = backFilePath . substring ( filePath . length ( ) - 1 ) . equals ( "/" ) ? "" : "/" ;
String datePath = DateUtil . formatDate ( new Date ( ) , "yyyy-MM-dd" ) ;
String fileName = filePat h + datePath + "/" + json . getString ( "messageType" ) + "-" + json . getString ( "messageId" ) + ".udi" ;
String backFileName = backFilePath + datePath + "/" + json . getString ( "messageType" ) + "-" + json . getString ( "messageId" ) + ".udi" ;
if ( ! FileUtils . makeDirectory ( filePath + datePath) )
IDCUtils . createDirectory ( filePath + datePath+ "/" ) ;
if ( ! FileUtils . makeDirectory ( backFilePath + datePath) )
IDCUtils . createDirectory ( filePath + datePath+ "/" ) ;
String fileName = filePath + filePat hSlas h + datePath + "/" + json . getString ( "messageType" ) + "-" + json . getString ( "messageId" ) + ".udi" ;
String backFileName = backFilePath + backFileSlash + datePath + "/" + json . getString ( "messageType" ) + "-" + json . getString ( "messageId" ) + ".udi" ;
if ( ! FileUtils . makeDirectory ( filePath + filePathSlash + datePath) )
IDCUtils . createDirectory ( filePath + filePathSlash + datePath+ "/" ) ;
if ( ! FileUtils . makeDirectory ( backFilePath + backFileSlash + datePath) )
IDCUtils . createDirectory ( filePath + backFileSlash + datePath+ "/" ) ;
FileUtils . SaveFileAs ( content , fileName ) ;
FileUtils . SaveFileAs ( content , backFileName ) ;
@ -356,9 +359,10 @@ public class IdcServiceImpl implements IdcService {
int n = 0 ;
try {
for ( MultipartFile file : files ) {
saveFiles [ n ] = filePath + datePath + "/" + json . getString ( "messageId" ) + "/" + file . getOriginalFilename ( ) ;
writeFile ( file . getBytes ( ) , filePath + datePath + "/" + json . getString ( "messageId" ) + "/" , file . getOriginalFilename ( ) ) ;
writeFile ( file . getBytes ( ) , backFilePath + datePath + "/" + json . getString ( "messageId" ) + "/" , file . getOriginalFilename ( ) ) ;
String imageName = filePath + filePathSlash + "register/file/image2/" + file . getOriginalFilename ( ) ;
saveFiles [ n ] = imageName ;
writeFile ( file . getBytes ( ) , filePath + filePathSlash + "register/file/image2/" , file . getOriginalFilename ( ) ) ;
writeFile ( file . getBytes ( ) , backFilePath + backFileSlash + datePath + "register/file/image2/" , file . getOriginalFilename ( ) ) ;
n + + ;
}
} catch ( IOException e ) {
@ -373,7 +377,7 @@ public class IdcServiceImpl implements IdcService {
syncAddTaskStatus ( json , true , true ) ;
/*解析入库*/
if ( isLastLevel ( ) ) {
if ( ! analyToDB ( json , files ) )
if ( ! analyToDB ( "" , json , files , true ) )
return ResultVOUtils . error ( 9000 , "解析失败" ) ;
} else {
//需要转发
@ -407,6 +411,21 @@ public class IdcServiceImpl implements IdcService {
return ResultVOUtils . error ( 9999 , "" ) ;
}
@Override
public void downloadFile ( String fileName , HttpServletResponse response ) {
OutputStream os ;
String filePathSlash = filePath . substring ( filePath . length ( ) - 1 ) . equals ( "/" ) ? "" : fileName . substring ( 0 , 1 ) . equals ( "/" ) ? "" : "/" ;
try {
byte [ ] bytes = FileUtils . readFileByBytes ( filePath + filePathSlash + fileName ) ;
os = response . getOutputStream ( ) ;
os . write ( bytes ) ;
os . flush ( ) ;
os . close ( ) ;
} catch ( IOException e ) {
// TODO Auto-generated catch block
e . printStackTrace ( ) ;
}
}
private void saveUploadStatus ( Map < String , Object > params ) {
Map < String , Object > map = new HashMap < String , Object > ( ) ;
@ -496,7 +515,7 @@ public class IdcServiceImpl implements IdcService {
orderNum + + ;
String messageType = params . get ( "messageType" ) . toString ( ) ;
saveIdcLog ( messageType , "" , tableName + ">" + where , 0 , total ) ;
String filePathSlash = filePath . substring ( filePath . length ( ) - 1 ) . equals ( "/" ) ? "" : "/" ;
if ( total > 0 ) {
success = true ;
@ -519,7 +538,7 @@ public class IdcServiceImpl implements IdcService {
if ( list ! = null & & list . size ( ) > 0 ) {
for ( int m = 0 ; m < list . size ( ) ; m + + ) {
if ( ! StringUtils . isEmpty ( filePathColumn ) & & list . get ( m ) . get ( filePathColumn ) ! = null ) {
files [ m ] = list. get ( m ) . get ( filePathColumn ) . toString ( ) ;
files [ m ] = filePath+ filePathSlash + "register/file/image2/" + list. get ( m ) . get ( filePathColumn ) . toString ( ) ;
}
recordSql + = ! StringUtils . isEmpty ( recordSql ) ? "," : "" ;
recordSql + = "('" + UUID . randomUUID ( ) . toString ( ) . replaceAll ( "-" , "" ) + "','" + tableName + "','" + list . get ( m ) . get ( keyColumn ) + "','" + isEnd + "',now())" ;
@ -533,7 +552,6 @@ public class IdcServiceImpl implements IdcService {
List < Map < String , Object > > childList = getList ( chidTnames [ 2 ] , childMap ) ;
list . get ( m ) . put ( "tableName" + z , chidTnames [ 2 ] ) ;
list . get ( m ) . put ( "data" + z , childList ) ;
logger . info ( "child-data-->" + JSON . toJSONString ( childList ) ) ;
}
}
}
@ -554,6 +572,7 @@ public class IdcServiceImpl implements IdcService {
if ( ! StringUtils . isEmpty ( recordSql ) ) {
dbDao . save ( "insert into idc_record (id,type,fkId,isEnd,createTime) values " + recordSql ) ;
}
saveDownloadStatus ( messageType , json . getString ( "reqNo" ) , messageType + " 记录数:" + list . size ( ) + "/" + total ) ;
saveIdcLog ( messageType , "" , tableName + ">success" , i * limit , total ) ;
} else {
success = false ;
@ -610,27 +629,28 @@ public class IdcServiceImpl implements IdcService {
/*解析到数据库*/
private boolean analyToDB ( JSONObject jsonObject , MultipartFile [ ] files ) {
private boolean analyToDB ( String host , JSONObject jsonObject , MultipartFile [ ] files , boolean isUpload ) {
if ( jsonObject . get ( "data" ) ! = null ) {
logger . error ( "json>>>>>>>>>" ) ;
String tableName = "" ;
String uniqueColumn = "" ;
String filePathColumn = "" ;
Object object = jsonObject . get ( "data" ) ;
List < Map < String , Object > > list = new ArrayList < > ( ) ;
if ( object instanceof List < ? > ) {
list = jsonObject . getObject ( "data" , List . class ) ;
tableName = jsonObject . getString ( "tableName" ) ;
uniqueColumn = jsonObject . getString ( "uniqueColumn" ) ;
filePathColumn = jsonObject . getString ( "filePathColumn" ) ;
} else {
JSONObject obj = jsonObject . getJSONObject ( "data" ) ;
list = obj . getObject ( "data" , List . class ) ;
tableName = obj . getString ( "tableName" ) ;
uniqueColumn = obj . getString ( "uniqueColumn" ) ;
filePathColumn = obj . getString ( "filePathColumn" ) ;
}
logger . error ( "table-->" + tableName ) ;
if ( ! StringUtils . isEmpty ( tableName ) ) {
return analyData ( tableName, uniqueColumn , list) ;
return analyData ( host, tableName, uniqueColumn , filePathColumn, list, isUpload ) ;
} else {
logger . error ( "数据格式错误:无数据标记" ) ;
}
@ -639,9 +659,8 @@ public class IdcServiceImpl implements IdcService {
return false ;
}
/*按表名解析数据到数据库,子表暂未处理*/
private boolean analyData ( String tableName, String uniqueColumn , List< Map < String , Object > > list ) {
private boolean analyData ( String host, String tableName, String uniqueColumn , String filePathColumn , List< Map < String , Object > > list , boolean isUpload ) {
String tName = DBAUtils . tableRealName ( tableName ) ;
String sql = "replace " + tName + "(" ;
@ -677,10 +696,15 @@ public class IdcServiceImpl implements IdcService {
if ( list ! = null & & list . size ( ) > 0 ) {
int n = 0 ;
int d = 0 ;
String [ ] files = new String [ list . size ( ) ] ;
for ( int i = 0 ; i < list . size ( ) ; i + + ) {
String operateMode = list . get ( i ) . get ( "operateMode" ) ! = null ? list . get ( i ) . get ( "operateMode" ) . toString ( ) : "A" ;
String updateWhere = "" ;
String updateSet = "" ;
if ( ! StringUtils . isEmpty ( filePathColumn ) ) {
files [ i ] = list . get ( i ) . get ( filePathColumn ) ! = null ? list . get ( i ) . get ( filePathColumn ) . toString ( ) : "" ;
}
if ( "A,D,U" . contains ( operateMode ) ) {
for ( int z = 0 ; z < keyColumn . length ; z + + ) {
if ( list . get ( i ) . get ( keyColumn [ z ] ) ! = null & & ! StringUtils . isEmpty ( list . get ( i ) . get ( keyColumn [ z ] ) . toString ( ) ) ) {
@ -747,7 +771,7 @@ public class IdcServiceImpl implements IdcService {
for ( Object o : ( List < ? > ) obj ) {
chList . add ( ( Map < String , Object > ) o ) ;
}
analyData ( list. get ( i ) . get ( "tableName" + m ) . toString ( ) , "" , chList ) ;
analyData ( host, list. get ( i ) . get ( "tableName" + m ) . toString ( ) , "" , "" , chList , isUpload ) ;
} else {
break ;
}
@ -759,6 +783,9 @@ public class IdcServiceImpl implements IdcService {
if ( ! result )
logger . error ( tableName + "-->fetchSave Fail" ) ;
}
if ( ! isUpload & & ! StringUtils . isEmpty ( filePathColumn ) ) {
batchDownloadFile ( host , files ) ;
}
}
return result ;
@ -818,6 +845,68 @@ public class IdcServiceImpl implements IdcService {
return result ;
}
/*下载文件*/
private boolean batchDownloadFile ( String syncIp , String [ ] files ) {
boolean success = true ;
for ( String fileName : files ) {
if ( ! StringUtils . isEmpty ( fileName ) ) {
if ( ! signleDownloadFile ( syncIp , fileName ) )
success = false ;
}
}
return success ;
}
private boolean signleDownloadFile ( String syncIp , String fileName ) {
OkHttpClient client = new OkHttpClient ( ) ;
RequestBody requestBody = new MultipartBody . Builder ( )
. setType ( MultipartBody . FORM )
. addFormDataPart ( "fileName" , fileName )
. build ( ) ;
Request request = new Request . Builder ( )
. url ( syncIp + "/spssync/common/downloadFile" )
. post ( requestBody )
. build ( ) ;
try {
Response result = client . newCall ( request ) . execute ( ) ;
if ( MediaType . parse ( "application/force-download" ) . equals ( result . body ( ) . contentType ( ) ) ) {
try ( InputStream inputStream = result . body ( ) . byteStream ( ) ) {
String filePathSlash = filePath . substring ( filePath . length ( ) - 1 ) . equals ( "/" ) ? "" : "/" ;
FileOutputStream outputStream = new FileOutputStream ( filePath + filePathSlash + fileName ) ;
byte b [ ] = new byte [ 1024 ] ;
int len = 0 ;
while ( ( len = inputStream . read ( b ) ) ! = - 1 ) {
outputStream . write ( b , 0 , len ) ;
}
outputStream . flush ( ) ;
} catch ( Exception e ) {
}
}
} catch ( IOException e ) {
// TODO Auto-generated catch block
e . printStackTrace ( ) ;
}
return true ;
}
/*获取转发服务地址, 当前值允许单向, 只使用参数upper_server_ip*/
private String getNextHost ( ) {
SystemParamConfigEntity systemParamConfigEntity = systemParamConfigService . selectByParamKey ( "upper_server_ip" ) ;
@ -884,7 +973,6 @@ public class IdcServiceImpl implements IdcService {
systemParamConfigEntity . setParamType ( 0 ) ;
systemParamConfigService . insert ( systemParamConfigEntity ) ;
}
System . out . print ( JSON . toJSONString ( systemParamConfigEntity ) + "\n" ) ;
return systemParamConfigEntity ;
}
private void writeFile ( byte [ ] file , String filePath , String fileName ) throws Exception {
@ -941,6 +1029,10 @@ public class IdcServiceImpl implements IdcService {
if ( map ! = null & & map . get ( "id" ) ! = null ) {
updateTime = DateUtil . formatDate ( IDCUtils . parseDate ( map . get ( "statusTime" ) . toString ( ) ) , "yyyy-MM-dd HH:mm:ss" ) ;
} else {
//国家库数据
if ( id . equals ( "productinfo" ) ) {
}
updateTime = "2000-01-01 00:00:00" ;
String sql = "insert into idc_status (id,statusTime) values ('" + id + "',cast('" + updateTime + "' as datetime))" ;
dbDao . save ( sql ) ;