|
|
@@ -1,6 +1,7 @@
|
|
|
-package com.uas.main;
|
|
|
+package com.uas.service.Impl;
|
|
|
|
|
|
import com.jcraft.jsch.JSchException;
|
|
|
+import com.uas.service.DownloadService;
|
|
|
import com.uas.util.BaseUtil;
|
|
|
import com.uas.util.FtpUtil;
|
|
|
import com.uas.util.JdbcUtil;
|
|
|
@@ -10,6 +11,8 @@ import org.codehaus.jackson.map.ObjectMapper;
|
|
|
import org.dom4j.Document;
|
|
|
import org.dom4j.Element;
|
|
|
import org.dom4j.io.SAXReader;
|
|
|
+import org.springframework.scheduling.annotation.Async;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
|
|
|
import java.io.File;
|
|
|
import java.io.FileInputStream;
|
|
|
@@ -19,11 +22,138 @@ import java.sql.Connection;
|
|
|
import java.sql.ResultSet;
|
|
|
import java.sql.SQLException;
|
|
|
import java.sql.Statement;
|
|
|
-import java.util.*;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
|
|
|
-public class Download{
|
|
|
+@Service
|
|
|
+public class DownloadServiceImpl implements DownloadService {
|
|
|
|
|
|
- public static Boolean checkXmlFileToSqlToEnterprise(File file,String enterprise){
|
|
|
+ @Override
|
|
|
+ public void downloadRun(){
|
|
|
+ //联合创泰-富森
|
|
|
+ downloadByDepotAndSob("N_YITOA_LHCT-FS","N_YITOA_LHCT","聯合創泰科技有限公司");
|
|
|
+ //联合创泰-顺丰
|
|
|
+ downloadByDepotAndSob("N_YITOA_LHCT-SF","N_YITOA_LHCT","聯合創泰科技有限公司");
|
|
|
+ //联合创泰-准时达
|
|
|
+ downloadByDepotAndSob("N_YITOA_LHCT-ZSD","N_YITOA_LHCT","聯合創泰科技有限公司");
|
|
|
+ //联合创泰-SLC上海保税仓
|
|
|
+ downloadByDepotAndSob("N_YITOA_LHCT-SLC","N_YITOA_LHCT","聯合創泰科技有限公司");
|
|
|
+ //联合创泰-中芯
|
|
|
+ //downloadByDepotAndSob("N_YITOA_LHCT-ZX","N_YITOA_LHCT","聯合創泰科技有限公司");
|
|
|
+ //联合创泰-泓明
|
|
|
+ downloadByDepotAndSob("N_YITOA_LHCT-HM","N_YITOA_LHCT","聯合創泰科技有限公司");
|
|
|
+ //英唐创泰香港-富森
|
|
|
+ downloadByDepotAndSob("N_YITOA_LHCT_HK-FS","N_YITOA_LHCT_HK","英唐創泰香港科技有限公司");
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ @SuppressWarnings("unchecked")
|
|
|
+ @Override
|
|
|
+ @Async("taskExecutor")
|
|
|
+ public void downloadByDepotAndSob(String depot,String sob,String enterprise){
|
|
|
+ BaseUtil.getLogger().info("download xml" + " from "+depot);
|
|
|
+ FTPClient client = null;
|
|
|
+ SFTPUtil sftp = null;
|
|
|
+ List<String> sqls = new ArrayList<String>();
|
|
|
+ boolean executeRes = false;
|
|
|
+ Connection connect = null;
|
|
|
+
|
|
|
+ try {
|
|
|
+ Map<String,Object> servMap = JdbcUtil.getFtpConfigs();
|
|
|
+ Map<String,Object> ftpMap = (Map<String,Object>)servMap.get(depot);
|
|
|
+ if ("N_YITOA_LHCT-SF".equals(depot)){
|
|
|
+ sftp = new SFTPUtil(ftpMap.get("user").toString(),ftpMap.get("password").toString(),
|
|
|
+ ftpMap.get("ip").toString(),
|
|
|
+ Integer.parseInt(ftpMap.get("port").toString()));
|
|
|
+ try {
|
|
|
+ sftp.login();
|
|
|
+ } catch (JSchException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }else {
|
|
|
+ client = FtpUtil.connect(ftpMap);
|
|
|
+ }
|
|
|
+ if(client!=null||sftp!=null) {
|
|
|
+ List<File> files = null;
|
|
|
+ if ("N_YITOA_LHCT-SF".equals(depot)) {
|
|
|
+ files = sftp.download(ftpMap.get("downloadpath").toString());
|
|
|
+
|
|
|
+ }else if("N_YITOA_LHCT-SLC".equals(depot)){
|
|
|
+ files = FtpUtil.downloadDirFileByType(client, "XML",
|
|
|
+ ftpMap.get("downloadpath").toString());
|
|
|
+ }else{
|
|
|
+ files = FtpUtil.downloadAllFileByType(client, "XML");
|
|
|
+ }
|
|
|
+ String fileName = "";
|
|
|
+ if (files != null && files.size() > 0) {
|
|
|
+ connect = JdbcUtil.getConnectBySob(sob);
|
|
|
+ if (connect != null) {
|
|
|
+ for (File file : files) {
|
|
|
+ try {
|
|
|
+ fileName += "," + file.getName();
|
|
|
+ //判断当前帐套和文件是否一致
|
|
|
+ if (!checkXmlFileToSqlToEnterprise(file, enterprise)) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ Statement statement = connect.createStatement();
|
|
|
+ sqls = parseXmlFileToSqls(file, statement);
|
|
|
+ if (sqls == null) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ executeRes = JdbcUtil.executeSqls(connect, sqls);
|
|
|
+
|
|
|
+ if (executeRes) {
|
|
|
+ if ("N_YITOA_LHCT-SF".equals(depot)) {
|
|
|
+ String s = file.getName().substring(0, file.getName().indexOf(".")) + ".xml";
|
|
|
+ Boolean rename = sftp.rename(ftpMap.get("downloadpath").toString() + "/" + s, "goodsreceipt/backup/" + s);
|
|
|
+ } else {
|
|
|
+ //移动处理完的文件到bak文件夹下
|
|
|
+ client.rename(file.getName(), "bak/" + file.getName());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ statement.close();
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ BaseUtil.getLogger().error(e.toString());
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ if (!"".equals(fileName)) {
|
|
|
+ BaseUtil.logDownload(fileName.substring(1), ftpMap.get("ip").toString(), ftpMap.get("downloadpath").toString());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ BaseUtil.getLogger().error(e.toString());
|
|
|
+ e.printStackTrace();
|
|
|
+ }finally{
|
|
|
+ if(client!=null){
|
|
|
+ FtpUtil.closeFtpClient(client);
|
|
|
+ client = null;
|
|
|
+ }
|
|
|
+ if (sftp!=null){
|
|
|
+ sftp.logout();
|
|
|
+ }
|
|
|
+ //把当前的连接关闭
|
|
|
+ try {
|
|
|
+ if(connect!=null){
|
|
|
+ connect.close();
|
|
|
+ }
|
|
|
+ } catch (SQLException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }finally{
|
|
|
+ connect = null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private Boolean checkXmlFileToSqlToEnterprise(File file,String enterprise){
|
|
|
String ent = null;
|
|
|
Element root = getElementRoot(file); //获取根节点
|
|
|
Map<String, String> mainData = getMainData(root); //获取主表数据 {tag:data}
|
|
|
@@ -38,7 +168,7 @@ public class Download{
|
|
|
/*
|
|
|
* 通过配置解析xml为sql语句
|
|
|
*/
|
|
|
- public static List<String> parseXmlFileToSqls(File file,Statement statement){
|
|
|
+ private List<String> parseXmlFileToSqls(File file,Statement statement){
|
|
|
List<String> sqls = new ArrayList<String>();
|
|
|
String mainTableUpdateSql = null;
|
|
|
String updateDetailConditon = null;
|
|
|
@@ -164,7 +294,7 @@ public class Download{
|
|
|
/*
|
|
|
* 获取明细表更新sql
|
|
|
*/
|
|
|
- public static String getUpdateDetailSql(Map<String, String> data,Map<String,String> xmlConfig,List<Map<String,String>> detailXmlConfig){
|
|
|
+ private String getUpdateDetailSql(Map<String, String> data,Map<String,String> xmlConfig,List<Map<String,String>> detailXmlConfig){
|
|
|
StringBuffer sb = new StringBuffer();
|
|
|
String sql = null;
|
|
|
String condition = null;
|
|
|
@@ -185,7 +315,7 @@ public class Download{
|
|
|
/*
|
|
|
* 获取添加临时表数据sql
|
|
|
*/
|
|
|
- public static String getInsertDetailSql(String maincode,Map<String, String> data,Map<String,String> xmlConfig,List<Map<String,String>> detailXmlConfig){
|
|
|
+ private String getInsertDetailSql(String maincode,Map<String, String> data,Map<String,String> xmlConfig,List<Map<String,String>> detailXmlConfig){
|
|
|
StringBuffer sb = new StringBuffer();
|
|
|
StringBuffer sb2 =new StringBuffer();
|
|
|
String sql = null;
|
|
|
@@ -223,7 +353,7 @@ public class Download{
|
|
|
/*
|
|
|
* 获取明细表和主表关联的更新条件
|
|
|
*/
|
|
|
- public static String getUpdateDetailConditionByMainTable(Map<String, String> mainData,Map<String,String> xmlConfig){
|
|
|
+ private String getUpdateDetailConditionByMainTable(Map<String, String> mainData,Map<String,String> xmlConfig){
|
|
|
String mainConditionField = xmlConfig.get("xd_main_conditionfield");
|
|
|
String mainConditionTag = xmlConfig.get("xd_main_conditiontag");
|
|
|
String[] fields = mainConditionField.split(",");
|
|
|
@@ -247,7 +377,7 @@ public class Download{
|
|
|
/*
|
|
|
* 获取更新主表字段的sql
|
|
|
*/
|
|
|
- public static String getUpdateMainFields(Map<String, String> mainData,Map<String,String> xmlConfig,List<Map<String,String>> mainXmlConfig){
|
|
|
+ private String getUpdateMainFields(Map<String, String> mainData,Map<String,String> xmlConfig,List<Map<String,String>> mainXmlConfig){
|
|
|
StringBuffer sb = new StringBuffer();
|
|
|
String update = "update " + xmlConfig.get("xd_table") + " set ";
|
|
|
for(Map<String,String> map:mainXmlConfig){
|
|
|
@@ -263,7 +393,7 @@ public class Download{
|
|
|
/*
|
|
|
* 获取主表更新条件
|
|
|
*/
|
|
|
- public static String getUpdateMainCondtion(Map<String, String> mainData,Map<String,String> xmlConfig){
|
|
|
+ private String getUpdateMainCondtion(Map<String, String> mainData,Map<String,String> xmlConfig){
|
|
|
String mainConditionField = xmlConfig.get("xd_main_conditionfield");
|
|
|
String mainConditionTag = xmlConfig.get("xd_main_conditiontag");
|
|
|
String[] fields = mainConditionField.split(",");
|
|
|
@@ -284,7 +414,7 @@ public class Download{
|
|
|
/*
|
|
|
* 获取更新明细表的条件
|
|
|
*/
|
|
|
- public static String getUpdateDetailCondition(Map<String, String> mainData,Map<String,String> xmlConfig){
|
|
|
+ private String getUpdateDetailCondition(Map<String, String> mainData,Map<String,String> xmlConfig){
|
|
|
String mainConditionField = xmlConfig.get("xd_detail_conditionfield");
|
|
|
String mainConditionTag = xmlConfig.get("xd_detail_conditiontag");
|
|
|
String[] fields = mainConditionField.split(",");
|
|
|
@@ -305,7 +435,7 @@ public class Download{
|
|
|
/*
|
|
|
* 获取xmldataconfigdetail配置
|
|
|
*/
|
|
|
- public static List<Map<String,String>> getXmlConfigDetail(Statement statement,String id,String type,String conditiontag){
|
|
|
+ private List<Map<String,String>> getXmlConfigDetail(Statement statement,String id,String type,String conditiontag){
|
|
|
List<Map<String,String>> list = new ArrayList<Map<String,String>>();
|
|
|
|
|
|
ResultSet rs = null;
|
|
|
@@ -328,7 +458,7 @@ public class Download{
|
|
|
/*
|
|
|
* 获取xmldataconfigdetail配置
|
|
|
*/
|
|
|
- public static List<Map<String,String>> getXmlConfigDetail(Statement statement,String id,String type){
|
|
|
+ private List<Map<String,String>> getXmlConfigDetail(Statement statement,String id,String type){
|
|
|
List<Map<String,String>> list = new ArrayList<Map<String,String>>();
|
|
|
|
|
|
ResultSet rs = null;
|
|
|
@@ -351,7 +481,7 @@ public class Download{
|
|
|
/*
|
|
|
* 获取xmldataconfig配置
|
|
|
*/
|
|
|
- public static Map<String,String> getXmlConfig(Statement statement,String docType,String filePrefix){
|
|
|
+ private Map<String,String> getXmlConfig(Statement statement,String docType,String filePrefix){
|
|
|
Map<String,String> map = new HashMap<String,String>();
|
|
|
ResultSet rs = null;
|
|
|
try{
|
|
|
@@ -383,7 +513,7 @@ public class Download{
|
|
|
/*
|
|
|
* 获取根节点
|
|
|
*/
|
|
|
- public static Element getElementRoot(File file){
|
|
|
+ private Element getElementRoot(File file){
|
|
|
Element element = null;
|
|
|
try {
|
|
|
InputStream in = new FileInputStream(file);
|
|
|
@@ -427,124 +557,5 @@ public class Download{
|
|
|
data.add(detail);
|
|
|
}
|
|
|
return data;
|
|
|
- }
|
|
|
- public static void downloadRun(){
|
|
|
- //联合创泰-富森
|
|
|
- downloadByDepotAndSob("N_YITOA_LHCT-FS","N_YITOA_LHCT","聯合創泰科技有限公司");
|
|
|
- //联合创泰-顺丰
|
|
|
- downloadByDepotAndSob("N_YITOA_LHCT-SF","N_YITOA_LHCT","聯合創泰科技有限公司");
|
|
|
- //联合创泰-准时达
|
|
|
- downloadByDepotAndSob("N_YITOA_LHCT-ZSD","N_YITOA_LHCT","聯合創泰科技有限公司");
|
|
|
- //联合创泰-SLC上海保税仓
|
|
|
- downloadByDepotAndSob("N_YITOA_LHCT-SLC","N_YITOA_LHCT","聯合創泰科技有限公司");
|
|
|
- //联合创泰-中芯
|
|
|
- //downloadByDepotAndSob("N_YITOA_LHCT-ZX","N_YITOA_LHCT","聯合創泰科技有限公司");
|
|
|
- //联合创泰-泓明
|
|
|
- downloadByDepotAndSob("N_YITOA_LHCT-HM","N_YITOA_LHCT","聯合創泰科技有限公司");
|
|
|
- //英唐创泰香港-富森
|
|
|
- downloadByDepotAndSob("N_YITOA_LHCT_HK-FS","N_YITOA_LHCT_HK","英唐創泰香港科技有限公司");
|
|
|
-
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- @SuppressWarnings("unchecked")
|
|
|
- public static void downloadByDepotAndSob(String depot,String sob,String enterprise){
|
|
|
- BaseUtil.getLogger().info("download xml" + " from "+depot);
|
|
|
- FTPClient client = null;
|
|
|
- SFTPUtil sftp = null;
|
|
|
- List<String> sqls = new ArrayList<String>();
|
|
|
- boolean executeRes = false;
|
|
|
- Connection connect = null;
|
|
|
-
|
|
|
- try {
|
|
|
- Map<String,Object> servMap = JdbcUtil.getFtpConfigs();
|
|
|
- Map<String,Object> ftpMap = (Map<String,Object>)servMap.get(depot);
|
|
|
- if ("N_YITOA_LHCT-SF".equals(depot)){
|
|
|
- sftp = new SFTPUtil(ftpMap.get("user").toString(),ftpMap.get("password").toString(),
|
|
|
- ftpMap.get("ip").toString(),
|
|
|
- Integer.parseInt(ftpMap.get("port").toString()));
|
|
|
- try {
|
|
|
- sftp.login();
|
|
|
- } catch (JSchException e) {
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
- }else {
|
|
|
- client = FtpUtil.connect(ftpMap);
|
|
|
- }
|
|
|
- if(client!=null||sftp!=null) {
|
|
|
- List<File> files = null;
|
|
|
- if ("N_YITOA_LHCT-SF".equals(depot)) {
|
|
|
- files = sftp.download(ftpMap.get("downloadpath").toString());
|
|
|
-
|
|
|
- }else if("N_YITOA_LHCT-SLC".equals(depot)){
|
|
|
- files = FtpUtil.downloadDirFileByType(client, "XML",
|
|
|
- ftpMap.get("downloadpath").toString());
|
|
|
- }else{
|
|
|
- files = FtpUtil.downloadAllFileByType(client, "XML");
|
|
|
- }
|
|
|
- String fileName = "";
|
|
|
- if (files != null && files.size() > 0) {
|
|
|
- connect = JdbcUtil.getConnectBySob(sob);
|
|
|
- if (connect != null) {
|
|
|
- for (File file : files) {
|
|
|
- try {
|
|
|
- fileName += "," + file.getName();
|
|
|
- //判断当前帐套和文件是否一致
|
|
|
- if (!checkXmlFileToSqlToEnterprise(file, enterprise)) {
|
|
|
- continue;
|
|
|
- }
|
|
|
- Statement statement = connect.createStatement();
|
|
|
- sqls = parseXmlFileToSqls(file, statement);
|
|
|
- if (sqls == null) {
|
|
|
- continue;
|
|
|
- }
|
|
|
-
|
|
|
- executeRes = JdbcUtil.executeSqls(connect, sqls);
|
|
|
-
|
|
|
- if (executeRes) {
|
|
|
- if ("N_YITOA_LHCT-SF".equals(depot)) {
|
|
|
- String s = file.getName().substring(0, file.getName().indexOf(".")) + ".xml";
|
|
|
- Boolean rename = sftp.rename(ftpMap.get("downloadpath").toString() + "/" + s, "goodsreceipt/backup/" + s);
|
|
|
- } else {
|
|
|
- //移动处理完的文件到bak文件夹下
|
|
|
- client.rename(file.getName(), "bak/" + file.getName());
|
|
|
- }
|
|
|
- }
|
|
|
- statement.close();
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
- BaseUtil.getLogger().error(e.toString());
|
|
|
- continue;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- }
|
|
|
- if (!"".equals(fileName)) {
|
|
|
- BaseUtil.logDownload(fileName.substring(1), ftpMap.get("ip").toString(), ftpMap.get("downloadpath").toString());
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- } catch (Exception e) {
|
|
|
- BaseUtil.getLogger().error(e.toString());
|
|
|
- e.printStackTrace();
|
|
|
- }finally{
|
|
|
- if(client!=null){
|
|
|
- FtpUtil.closeFtpClient(client);
|
|
|
- client = null;
|
|
|
- }
|
|
|
- if (sftp!=null){
|
|
|
- sftp.logout();
|
|
|
- }
|
|
|
- //把当前的连接关闭
|
|
|
- try {
|
|
|
- if(connect!=null){
|
|
|
- connect.close();
|
|
|
- }
|
|
|
- } catch (SQLException e) {
|
|
|
- e.printStackTrace();
|
|
|
- }finally{
|
|
|
- connect = null;
|
|
|
- }
|
|
|
- }
|
|
|
}
|
|
|
}
|