Parcourir la source

增加得尔达库存获取

koul il y a 5 ans
Parent
commit
d8c06ba6c0

+ 15 - 16
src/main/java/com/uas/HSLApplication.java

@@ -1,5 +1,6 @@
 package com.uas;
 
+import com.uas.main.DownloadStcockQty;
 import com.uas.util.BaseUtil;
 import com.uas.util.JdbcUtil;
 import org.springframework.boot.SpringApplication;
@@ -24,26 +25,25 @@ public class HSLApplication {
     @Scheduled(fixedRate=1000*60*5)
     public void ediSendAndReceive() {
         uploadRun();
-        System.out.println("begin"+new Date());
         downloadRun();
-        try{ //关闭数据库连接
-            if(JdbcUtil.connection!=null){
-                JdbcUtil.connection.close();
-            }
-        }catch(Exception e){
-            BaseUtil.getLogger().error(e.toString());
-            e.printStackTrace();
-        }finally{
-            JdbcUtil.connection = null;
-        }
-        System.out.println("run over!");
+        closeJDBC();
+    }
+
+    @Scheduled(fixedRate=1000*60*60)
+    public void downloadStcockQty() {
+        DownloadStcockQty.downloadQty();
+        closeJDBC();
     }
 
+
     /*@Scheduled(fixedRate=1000*60*60)
     public void filePath() {
-        System.out.println("begin download  "+new Date());
         runDownloadFilePath();
-        try{
+        closeJDBC();
+     }*/
+
+    private void closeJDBC(){
+        try{ //关闭数据库连接
             if(JdbcUtil.connection!=null){
                 JdbcUtil.connection.close();
             }
@@ -53,7 +53,6 @@ public class HSLApplication {
         }finally{
             JdbcUtil.connection = null;
         }
-        System.out.println("download over!");
-    }*/
+    }
 
 }

+ 94 - 0
src/main/java/com/uas/main/DownloadStcockQty.java

@@ -0,0 +1,94 @@
+package com.uas.main;
+
+import com.uas.util.BaseUtil;
+import com.uas.util.FileUtil;
+import com.uas.util.SFTPUtil;
+import org.apache.commons.net.ftp.FTPClient;
+
+import java.io.File;
+import java.sql.Connection;
+import java.sql.SQLException;
+import java.sql.Statement;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+
+import static com.uas.util.FtpUtil.*;
+import static com.uas.util.JdbcUtil.*;
+
+/**
+ * @author koul
+ * @email koul@usoftchina.com
+ * @date 2020-10-19 15:06
+ */
+public class DownloadStcockQty {
+    public static void downloadQty() {
+        //深圳华商龙-准时达
+        downloadStcockQty("N_HUASL_SZ-DRD", "N_HUASL_SZ");
+        //上海宇声-准时达
+        downloadStcockQty("N_E_SHINE_SH-DRD", "N_E_SHINE_SH");
+        //重庆华商龙-准时达
+        downloadStcockQty("HSL_CQ-DRD", "HSL_CQ");
+        //北京华商龙-准时达
+        downloadStcockQty("HSL_BJ-DRD", "HSL_BJ");
+        //青岛华商龙-准时达
+        downloadStcockQty("N_HUASL_QD-DRD", "N_HUASL_QD");
+    }
+
+    public static void downloadStcockQty(String depot,String sob) {
+        FTPClient client = null;
+        Connection connect = null;
+        try {
+            Map<String,Object> servMap = getFtpConfigs();
+            Map<String,Object> ftpMap = (Map<String,Object>)servMap.get(depot);
+            client = connect(ftpMap);
+            if(client!=null) {
+                List<File> files = null;
+                files = downloadFileByDirectory(client,ftpMap.get("qty").toString(), "XML");
+                String fileName = "";
+                if (files != null && files.size() > 0) {
+                    connect = getConnectBySob(sob);
+                    if (connect != null) {
+                        for (File file : files) {
+                            try {
+                                fileName += "," + file.getName();
+                                boolean b = FileUtil.getDataAndSqlByXml(connect, file, sob);
+                                if (b) {
+                                    //移动处理完的文件到bak文件夹下
+                                    client.rename(file.getName(), "bak/" + file.getName());
+                                }
+                            } 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){
+                closeFtpClient(client);
+                client = null;
+            }
+            //把当前的连接关闭
+            try {
+                if(connect!=null){
+                    connect.close();
+                }
+            } catch (SQLException e) {
+                e.printStackTrace();
+            }finally{
+                connect = null;
+            }
+        }
+    }
+
+}

+ 93 - 1
src/main/java/com/uas/util/BaseUtil.java

@@ -4,10 +4,15 @@ import org.apache.log4j.Logger;
 import org.apache.log4j.PropertyConfigurator;
 
 import java.io.IOException;
-import java.util.Properties;
+import java.text.SimpleDateFormat;
+import java.util.*;
 
 
 public class BaseUtil {
+	public final static String REG_D = "\\d{4}-\\d{2}-\\d{2}";
+	public final static String REG_DT = "\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}";
+	public final static String REG_DTS = "\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2}";
+	public final static String REG_TS = "\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2}.\\d{1}";
 	public static Logger logger;
 	
 	/**
@@ -32,4 +37,91 @@ public class BaseUtil {
 	public static void logDownload(String fileName,String ip,String downloadpath){
 		getLogger().info("download " + fileName + " from " + ip + " path:" + downloadpath);
 	}
+
+	public static List<String> getInsertSqlbyMaps(List<Map<String, Object>> maps, String table,String mkey,String seq) {
+		List<String> sqls = new ArrayList<String>();
+		StringBuffer sb1 = null;
+		StringBuffer sb2 = null;
+		for (Map<String, Object> map : maps) {
+			Set<String> keys = map.keySet();
+			sb1 = new StringBuffer("INSERT into " + table + " ("+mkey+",");
+			sb2 = new StringBuffer(" "+seq+",");
+			for (Object key : keys) {
+				String field = (String) key;
+				Object value = map.get(key);
+				sb1.append(field);
+				sb1.append(",");
+				if (value != null) {
+					String val = value.toString();
+					if (val.matches(REG_D)) {// 判断是否是形如yyyy-mm-dd格式的日期类型数据
+						sb2.append(parseDateToOracleString("yyyy-MM-dd", val));
+					} else if (val.matches(REG_DT)) {
+						sb2.append(parseDateToOracleString("yyyy-MM-dd HH:mm", val));
+					} else if (val.matches(REG_DTS)) {
+						sb2.append(parseDateToOracleString("yyyy-MM-dd HH:mm:ss", val));
+					} else if (val.matches(REG_TS)) {
+						sb2.append(parseDateToOracleString("yyyy-MM-dd HH:mm:ss", val.substring(0, val.lastIndexOf("."))));
+					} else if ("null".equals(val)) {
+						sb2.append("null");
+					} else if ("".equals(val.trim())) {
+						sb2.append("''");
+					} else if (val.contains("%n")) {
+						sb2.append("'" + val.replaceAll("%n", "\n") + "'");
+					} else if (val.contains("'")) {
+						sb2.append("'" + value.toString().replaceAll("'", "''") + "'");
+					} else {
+						sb2.append("'" + value + "'");
+					}
+				} else {
+					sb2.append("null");
+				}
+				sb2.append(",");
+			}
+			sqls.add(sb1.substring(0, sb1.length() - 1) + ") VALUES (" + sb2.substring(0, sb2.length() - 1) + ")");
+		}
+		return sqls;
+	}
+
+	/**
+	 * 日期转化成oracle格式to_date('2012-12-12', 'yyyy-MM-dd')
+	 *
+	 * @param format
+	 *            format type,for example: 'yyyy-MM-dd', 'yyyy-MM-dd HH:mm:ss'
+	 * @param dateString
+	 *            date{type=string}
+	 */
+	public static String parseDateToOracleString(String format, String dateString) {
+		if (format == null) {
+			format = "yyyy-MM-dd";
+		}
+		if (dateString == null || "".equals(dateString) || "null".equals(dateString)) {
+			SimpleDateFormat sdf = new SimpleDateFormat(format);
+			dateString = sdf.format(new Date());
+		}
+		if ("yyyy-MM-dd HH:mm".toUpperCase().equals(format.toUpperCase())) {
+			format = "yyyy-MM-dd HH24:mi";
+		} else if ("yyyy-MM-dd HH:mm:ss".toUpperCase().equals(format.toUpperCase())) {
+			format = "yyyy-MM-dd HH24:mi:ss";
+		} else {
+			format = "yyyy-MM-dd";
+		}
+		return "to_date('" + dateString + "','" + format + "')";
+	}
+
+	/**
+	 * 时间戳转换成日期格式字符串
+	 * @param seconds
+	 * @param format
+	 * @return
+	 */
+	public static String timeStamp2Date(Long seconds,String format) {
+		if(seconds == null || seconds==0){
+			return "";
+		}
+		if(format == null || format.isEmpty()){
+			format = "yyyy-MM-dd HH:mm:ss";
+		}
+		SimpleDateFormat sdf = new SimpleDateFormat(format);
+		return sdf.format(new Date(seconds));
+	}
 }

+ 92 - 2
src/main/java/com/uas/util/FileUtil.java

@@ -1,15 +1,20 @@
 package com.uas.util;
 
 import org.apache.commons.io.FileUtils;
+import org.dom4j.Document;
+import org.dom4j.Element;
+import org.dom4j.io.SAXReader;
 import org.springframework.web.multipart.MultipartFile;
 
 import java.io.*;
+import java.sql.Connection;
 import java.sql.ResultSet;
 import java.sql.SQLException;
 import java.sql.Statement;
 import java.text.SimpleDateFormat;
-import java.util.Date;
-import java.util.UUID;
+import java.util.*;
+
+import static com.uas.util.BaseUtil.getInsertSqlbyMaps;
 
 /**
  * @author koul
@@ -118,4 +123,89 @@ public class FileUtil {
 		}
 		return result;
 	}
+
+    /**
+     * 解析xml文件,生成数据
+     * @param file xml文件
+     * @return
+     */
+    @SuppressWarnings("unchecked")
+    public static boolean getDataAndSqlByXml(Connection con , File file,String sob){
+        boolean bool = false;
+        List<String> sqls = new ArrayList<String>();
+        InputStream in = null;
+        try {
+            in = new FileInputStream(file);
+        } catch (FileNotFoundException e1) {
+            e1.printStackTrace();
+        }
+        SAXReader reader = new SAXReader();
+        Document doc = null;
+        try {
+            doc = reader.read(in);
+            in.close();
+        } catch (Exception e1) {
+            e1.printStackTrace();
+        }
+        Element root = doc.getRootElement();
+        List<Element> e = root.elements();
+        List<Map<String, Object>> maps = new ArrayList<>();
+        Object DocumentDateTime =null;
+        Object DocumentNumber = null;
+        for(Element el:e){
+            String name = el.getName();
+            if(name=="Header"){
+                List<Element> elements = el.elements();
+                for (Element ee:elements) {
+                    String name1 = ee.getName();
+                    if(name1=="DocumentNumber"){
+                        DocumentNumber=ee.getData();
+                    }else if(name1=="DocumentDateTime"){
+                        DocumentDateTime=ee.getData();
+                    }
+                }
+            }else if(name=="Detail"){
+                List<Element> elements = el.elements();
+                for (Element ee:elements) {
+                    List<Element> elements1 = ee.elements();
+                    String name1 = ee.getName();
+                    if (name1=="Item") {
+                        Map<String, Object> detailMap = new HashMap<>();
+                        for (Element eel : elements1) {
+                            String name2 = eel.getName();
+                            if (name2 == "LineNo") {
+                                detailMap.put("sq_lineno",eel.getData());
+                            } else if (name2 == "WhouseCode") {
+                                detailMap.put("sq_whousecode",eel.getData());
+                            } else if (name2 == "WhouseName") {
+                                detailMap.put("sq_whousename",eel.getData());
+                            } else if (name2 == "ProdCode") {
+                                detailMap.put("sq_prodcode",eel.getData());
+                            } else if (name2 == "PrDetail") {
+                                detailMap.put("sq_prdetail",eel.getData());
+                            } else if (name2 == "Spec") {
+                                detailMap.put("sq_spec",eel.getData());
+                            } else if (name2 == "Brand") {
+                                detailMap.put("sq_brand",eel.getData());
+                            } else if (name2 == "StcockQty") {
+                                detailMap.put("sq_stcockqty",eel.getData());
+                            } else if (name2 == "Unit") {
+                                detailMap.put("sq_unit",eel.getData());
+                            }
+
+                        }
+                        detailMap.put("sq_code",DocumentNumber);
+                        detailMap.put("sq_date",BaseUtil.timeStamp2Date(Long.parseLong(DocumentDateTime.toString()),""));
+                        detailMap.put("sq_indate",BaseUtil.timeStamp2Date(System.currentTimeMillis(),""));
+                        maps.add(detailMap);
+                    }
+                }
+            }
+        }
+        if (maps!=null&&maps.size()>0){
+            sqls = getInsertSqlbyMaps(maps, sob+".StcockQty","sq_id",sob+".STCOCKQTY_SEQ.nextval");
+            bool = JdbcUtil.executeSqls(con, sqls);
+        }
+        return bool;
+    }
 }

+ 45 - 1
src/main/java/com/uas/util/FtpUtil.java

@@ -229,5 +229,49 @@ public class FtpUtil {
 			}
 		}		
 	}
-	
+
+
+	/**
+	 *
+	 * @param ftpClient
+	 * @return 获取连接到的ftp站点下的文件夹所有文件
+	 */
+	public static List<File> downloadFileByDirectory(FTPClient ftpClient,String directory,String fileType) {
+		List<File> files = new ArrayList<File>();
+		try {
+			ftpClient.enterLocalPassiveMode();
+			ftpClient.changeWorkingDirectory("/" + directory);// 转移到FTP服务器目录
+			FTPFile[] ftpFiles = ftpClient.listFiles();
+			if (ftpFiles != null && ftpFiles.length > 0) {
+				ArrayList<FTPFile> list = new ArrayList<FTPFile>(Arrays.asList(ftpFiles));
+				Collections.sort(list, new Comparator<FTPFile>() {
+					@Override
+					public int compare(FTPFile o1, FTPFile o2) {
+						long time = o1.getTimestamp().getTime().getTime();
+						long time1 = o2.getTimestamp().getTime().getTime();
+						if (time > time1) {
+							return 1;
+						}
+						if (time == time1) {
+							return 0;
+						}
+						return -1;
+					}
+				});
+				for (FTPFile file : list) {
+					if (file.getName().toUpperCase().endsWith("." + fileType)) {
+						File localFile = new File(System.getProperty("java.io.tmpdir") + File.separator + file.getName());
+						OutputStream os = new FileOutputStream(localFile);
+						ftpClient.retrieveFile(file.getName(), os);
+						os.close();
+						files.add(localFile);
+					}
+				}
+			}
+		} catch (Exception e) {
+			BaseUtil.getLogger().error(e.toString());
+			e.printStackTrace();
+		}
+		return files;
+	}
 }

+ 10 - 5
src/main/resources/properties/ftpconfig.properties

@@ -5,7 +5,8 @@
         "user":"yitoa",
         "password":"yitoa@2020",
         "downloadpath":"Ack/SZHSL",
-        "in":"Order/SZHSL"
+        "in":"Order/SZHSL",
+        "qty":"Inv/SZHSL"
     },
     "N_E_SHINE_SH-DRD":{
         "ip":"58.211.24.196",
@@ -13,7 +14,8 @@
         "user":"yitoa",
         "password":"yitoa@2020",
         "downloadpath":"Ack/SHYS",
-        "in":"Order/SHYS"
+        "in":"Order/SHYS",
+        "qty":"Inv/SHYS"
     },
     "N_HUASL_QD-DRD":{
         "ip":"58.211.24.196",
@@ -21,7 +23,8 @@
         "user":"yitoa",
         "password":"yitoa@2020",
         "downloadpath":"Ack/QDHSL",
-        "in":"Order/QDHSL"
+        "in":"Order/QDHSL",
+        "qty":"Inv/QDHSL"
     },
     "HSL_BJ-DRD":{
         "ip":"58.211.24.196",
@@ -29,7 +32,8 @@
         "user":"yitoa",
         "password":"yitoa@2020",
         "downloadpath":"Ack/BJHSL",
-        "in":"Order/BJHSL"
+        "in":"Order/BJHSL",
+        "qty":"Inv/BJHSL"
     },
     "HSL_CQ-DRD":{
         "ip":"58.211.24.196",
@@ -37,7 +41,8 @@
         "user":"yitoa",
         "password":"yitoa@2020",
         "downloadpath":"Ack/CQHSL",
-        "in":"Order/CQHSL"
+        "in":"Order/CQHSL",
+        "qty":"Inv/CQHSL"
     },
     "N_BJKG-FS":{
         "ip":"113.100.137.106",