Java tutorial
package ruige; import java.io.File; import java.util.ArrayList; import java.util.HashMap; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.apache.commons.net.ftp.FTPClient; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import com.dao.DBCommon; import com.dao.DBUtil; import com.util.FTPFunc; import com.util.GlobalFunc; public class GrabFTPDateManager { /** * 108YT FTP * @param fromType * @param userName ftp * @param passWord ftp * @param tempPath * @param savePath * @throws Exception */ public static void getInfoFromYT(String fromType, String userName, String passWord, String tempPath, String savePath, String errorPath) { try { //1 if (!moveFile(tempPath, userName, passWord)) { try { } catch (Exception e1) { e1.printStackTrace(); } } //2FTP handleFileAndInsertDB(tempPath, savePath, fromType, errorPath); if (null != fromType && fromType.equals("0")) { //3 moveDBToRealTable(); } } catch (Exception e) { e.printStackTrace(); } } //1FTP private static Boolean moveFile(String tempPath, String userName, String passWord) throws Exception { FTPFunc ftpFunc = new FTPFunc(); FTPClient ftp = new FTPClient();//FTP try { Boolean isConnect = ftpFunc.getConnectionFTP("61.152.176.30", 21, userName, passWord, "./recv", ftp); // Boolean isConnect = ftpFunc.getConnectionFTP("121.40.65.100",21,userName,passWord,"./recv",ftp); if (true == isConnect) {// ArrayList<String> fileNameList = ftpFunc.getFTPFileList(ftp); if (null != fileNameList) {// for (int i = 0; i < fileNameList.size(); i++) {//FTP Boolean isDownload = ftpFunc.downloadFromFTP(tempPath, fileNameList.get(i), ftp);//FTP if (true == isDownload) { Boolean isDelete = ftpFunc.deleteFromFTP("/recv/", fileNameList.get(i), ftp);//FTP if (false == isDelete) { return false;//false } } else { return false;//false } } } } } catch (Exception e) { e.printStackTrace(); return false; } return true; } //2FTP private static void handleFileAndInsertDB(String tempPath, String savePath, String fromType, String errorPath) throws Exception { String filepath = tempPath.substring(0, tempPath.length() - 1); File file = new File(filepath); ArrayList<String> errorList = new ArrayList<String>(); if (file.isDirectory()) {// String[] filelist = file.list(); for (int i = 0; i < filelist.length; i++) { File readfile = new File(filepath + "\\" + filelist[i]); long len = 0; if (len == readfile.length()) { // continue; } if (!readfile.isDirectory()) { String filename = readfile.getName(); if (filename.contains("DEC") || filename.contains("RCP")) { Boolean isMoveSuccess = readfile.renameTo(new File(errorPath + filename)); if (isMoveSuccess) { readfile.delete(); } } else { //xml Boolean isInsertSuccess = insertIntoDB(tempPath, filename, fromType); if (isInsertSuccess) { // Boolean isMoveSuccess = readfile.renameTo(new File(savePath + filename)); if (isMoveSuccess) { readfile.delete(); } } else { errorList.add(filename); } } } } if (null != errorList && errorList.size() > 0) { GlobalFunc.sendEmail("lijw@regs.com", "", "" + errorList.toString(), 0); } } } //2.1xml private static Boolean insertIntoDB(String filePath, String fileName, String fromType) throws Exception { Boolean result = true; try { DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder(); Document document = db.parse(new File(filePath + fileName));//DOCUMENT Element root = document.getDocumentElement(); //2.1.1-----EP_DEC_HEAD ArrayList<HashMap<String, String>> headInfo = getXMLInfo(root, "EP_DEC_HEAD"); HashMap<String, String> hmHashMap = headInfo.get(0); hmHashMap.put("FileName", fileName); //2.1.2-----EP_DEC_LIST ArrayList<HashMap<String, String>> listInfo = getXMLInfo(root, "EP_DEC_LIST_ITEM"); //2.1.3EP_DEC_CONTAINER_LIST ArrayList<HashMap<String, String>> listInfo2 = getXMLInfo(root, "EP_DEC_CONTAINER_ITEM"); //2.1.4EP_DEC_CERT_LIST ArrayList<HashMap<String, String>> listInfo3 = getXMLInfo(root, "EP_DEC_CERT_ITEM"); //2.1.5EP_DEC_EDOCRELATION_LIST ArrayList<HashMap<String, String>> listInfo4 = getXMLInfo(root, "EP_DEC_EDOCRELATION_ITEM"); //2.1.6EP_DEC_RESULT_LIST ArrayList<HashMap<String, String>> listInfo5 = getXMLInfo(root, "EP_DEC_RESULT_ITEM"); String DeclearTime = ""; for (int i = 0; i < listInfo5.size(); i++) { if ("P".equals(listInfo5.get(i).get("CHANNEL").toString())) { DeclearTime = listInfo5.get(i).get("NOTICE_DATE").toString(); if ("" != DeclearTime && DeclearTime.length() >= 15) { DeclearTime = DeclearTime.substring(0, 4) + "-" + DeclearTime.substring(4, 6) + "-" + DeclearTime.substring(6, 8) + " " + DeclearTime.substring(9, 11) + ":" + DeclearTime.substring(11, 13) + ":" + DeclearTime.substring(13, 15); } } } //edidoc_type-- ArrayList<HashMap<String, String>> deptAndTypeInfo = getXMLInfo(root, "PRE_ENTRY_INFO_ITEM"); if (null != deptAndTypeInfo && deptAndTypeInfo.size() > 0) { HashMap<String, String> deptAndTypeInfoHM = deptAndTypeInfo.get(0); if (null != deptAndTypeInfoHM) { hmHashMap.put("DEC_TYPE", deptAndTypeInfoHM.get("DEC_TYPE")); hmHashMap.put("USER_INFO", deptAndTypeInfoHM.get("USER_INFO")); } } String sql = "select ID from SSRESOrderInfo with(nolock) where PRE_ENTRY_ID ='" + fileName.substring(30, 48) + "'"; String strID = DBCommon.loadVal(sql); if (null != strID && strID.length() > 0) { // //2.1.xxx result = updateInfoDB(hmHashMap, listInfo, listInfo2, listInfo3, listInfo4, DeclearTime, strID, fromType); } else { //2.1.2 result = saveIntoDB(hmHashMap, listInfo, listInfo2, listInfo3, listInfo4, DeclearTime, fromType); } } catch (Exception e) { e.printStackTrace(); return false; } return result; } /*// private static void updateDeclearNum(String Transtools,String HSCode){ try { DBCommon.execute("update XianHuoReceiveOrder set HSCode='"+HSCode+"' where Transtools='"+Transtools+"' "); } catch (Exception e) { e.printStackTrace(); } }*/ //2.1.xxx private static Boolean updateInfoDB(HashMap<String, String> orderHM, ArrayList<HashMap<String, String>> list, ArrayList<HashMap<String, String>> list2, ArrayList<HashMap<String, String>> list3, ArrayList<HashMap<String, String>> list4, String DeclearTime, String orderID, String fromType) throws Exception { boolean result = true; DBUtil db = null; try { if (list == null) { return true; } if (DeclearTime.length() > 0) { orderHM.put("DeclearTime", DeclearTime); orderHM.put("IsUpdatePassTime", "1"); } db = DBUtil.getInstance(); db.setConnectionAttr(false); if ("1".equals(fromType)) { orderHM.put("CompanyType", "1");//1 } orderHM.put("ID", orderID); db.update(orderHM, "SSRESOrderInfo"); //db.insert(orderHM, "SSRESOrderInfo"); db.commitConn(); } catch (Exception e) { db.rollBackConn(); e.printStackTrace(); return false; } return result; } //2.1.1XML private static ArrayList<HashMap<String, String>> getXMLInfo(Element root, String title) { ArrayList<HashMap<String, String>> reList = new ArrayList<HashMap<String, String>>(); try { NodeList nodeList = root.getElementsByTagName(title); for (int i = 0; i < nodeList.getLength(); i++) { Node node = nodeList.item(i); NodeList nodeList2 = node.getChildNodes(); HashMap<String, String> tempMap = new HashMap<String, String>(); for (int j = 0; j < nodeList2.getLength(); j++) { Node node2 = nodeList2.item(j); if (!("#text").equals(node2.getNodeName()) && null != node2.getTextContent() && node2.getTextContent().length() > 0) { tempMap.put(node2.getNodeName(), node2.getTextContent()); } } reList.add(tempMap); } } catch (Exception e) { e.printStackTrace(); return null; } return reList; } //2.1.2 private static Boolean saveIntoDB(HashMap<String, String> orderHM, ArrayList<HashMap<String, String>> list, ArrayList<HashMap<String, String>> list2, ArrayList<HashMap<String, String>> list3, ArrayList<HashMap<String, String>> list4, String DeclearTime, String fromType) throws Exception { boolean result = true; DBUtil db = null; try { if (list == null) { return true; } if (DeclearTime.length() > 0) { orderHM.put("DeclearTime", DeclearTime); orderHM.put("IsUpdatePassTime", "1"); } db = DBUtil.getInstance(); db.setConnectionAttr(false); if ("1".equals(fromType)) { orderHM.put("CompanyType", "1");//1 } String orderID = db.insert(orderHM, "SSRESOrderInfo"); for (int i = 0; i < list.size(); i++) { HashMap<String, String> partHash = (HashMap<String, String>) list.get(i); if (null != partHash.get("GOODS_ID")) { String str = partHash.get("GOODS_ID").toString(); boolean isDigit = false; for (int m = 0; m < str.length(); m++) { // if (!Character.isDigit(str.charAt(m))) {//char isDigit = true; break; } } if (isDigit) { partHash.remove("GOODS_ID"); partHash.put("GOODS_ID", ""); } } partHash.put("OrderID", orderID); partHash.put("TransCode", orderHM.get("BILL_NO")); partHash.put("G_NAME", partHash.get("G_NAME").toString().replace("'", "''")); db.insert(partHash, "SSRESPartsList"); } for (int i = 0; i < list2.size(); i++) { HashMap<String, String> ContrainerHash = (HashMap<String, String>) list2.get(i); ContrainerHash.put("OrderID", orderID); db.insert(ContrainerHash, "SSRESContainerList"); } for (int i = 0; i < list3.size(); i++) { HashMap<String, String> CertHash = (HashMap<String, String>) list3.get(i); CertHash.put("OrderID", orderID); db.insert(CertHash, "SSRESCERTList"); } for (int i = 0; i < list4.size(); i++) { HashMap<String, String> DocHash = (HashMap<String, String>) list4.get(i); DocHash.put("OrderID", orderID); db.insert(DocHash, "SSRESDocList"); } db.commitConn(); } catch (Exception e) { db.rollBackConn(); e.printStackTrace(); return false; } return result; } //3 private static void moveDBToRealTable() throws Exception { DBUtil db = null; StringBuffer sql = new StringBuffer(); try { db = DBUtil.getInstance(); db.setConnectionAttr(false); // sql.append( "update SSRESOrderInfo set HandleStatus = 2 where companyType=0 and (HandleStatus = 0 or HandleStatus = 5)"); db.execute(sql.toString()); sql.delete(0, sql.length()); sql.append( "select a.*,DATEDIFF(d,createtime,getdate()) as diffDay, b.ID as TradeTypeID from SSRESOrderInfo as a ") .append(" left outer join SSTradeType as b on a.TRADE_MODE=b.TypeCode where companyType=0 and a.HandleStatus = 2 "); ArrayList<HashMap<String, String>> OrderList = DBCommon.listBySQL(sql.toString()); for (int i = 0; i < OrderList.size(); i++) { //SSDeclearOrderDetail HashMap<String, String> orderht = (HashMap<String, String>) OrderList.get(i); HashMap<String, String> ht = new HashMap<String, String>(); sql.delete(0, sql.length()); sql.append("select ID, DeclearOrderID from SSDeclearOrderDetail where Declear_NO = '") .append(orderht.get("PRE_ENTRY_ID")).append("'"); HashMap<String, String> detailht = DBCommon.getBySQL(sql.toString()); String OrderID = ""; if (detailht != null && detailht.get("DeclearOrderID") != null && detailht.get("DeclearOrderID").toString().length() > 0) { OrderID = detailht.get("DeclearOrderID").toString(); } if (OrderID.length() == 0) { //seastar sql.delete(0, sql.length()); if (Integer.parseInt(orderht.get("diffDay").toString()) > 7) { sql.append("update SSRESOrderInfo set HandleStatus = 10 where companyType=0 and ID = ") .append(orderht.get("ID")); } else { sql.append("update SSRESOrderInfo set HandleStatus = 5 where companyType=0 and ID = ") .append(orderht.get("ID")); } db.execute(sql.toString()); continue; } // sql.delete(0, sql.length()); sql.append("select count(1) as LianNum from SSRESPartsList where OrderID = ") .append(orderht.get("ID")); String LianDanNum = db.loadVal(sql.toString()); int dLianDanNum = Integer.parseInt(LianDanNum); if (dLianDanNum <= 5) { dLianDanNum = 0; } else if (dLianDanNum > 5 && dLianDanNum <= 10) { //5 dLianDanNum = 1; } else if (dLianDanNum > 10 && dLianDanNum <= 15) { //5 dLianDanNum = 2; } else if (dLianDanNum > 15 && dLianDanNum <= 20) { //5 dLianDanNum = 3; } if (dLianDanNum < 0) { dLianDanNum = 0; } ht.put("DeclearOrderID", OrderID); ht.put("TradeTypeID", orderht.get("TradeTypeID")); ht.put("Declear_NO", orderht.get("PRE_ENTRY_ID")); ht.put("BoxNum", orderht.get("PACK_NO")); ht.put("GrossWeight", orderht.get("GROSS_WT")); ht.put("NetWeight", orderht.get("GROSS_WT")); ht.put("ContractNo", orderht.get("CONTR_NO")); ht.put("CUTMode", orderht.get("CUT_MODE")); ht.put("ManualNO", orderht.get("MANUAL_NO")); ht.put("APPRNO", orderht.get("APPR_NO")); ht.put("LicenseNo", orderht.get("LICENSE_NO")); ht.put("LianDanNum", dLianDanNum + ""); ht.put("Remark", orderht.get("NOTE_S")); ht.put("ID", detailht.get("ID")); db.update(ht, "SSDeclearOrderDetail"); // sql.delete(0, sql.length()); sql.append("select * from SSRESPartsList where OrderID = ").append(orderht.get("ID")); ArrayList<HashMap<String, String>> partsList = DBCommon.listBySQL(sql.toString()); int hasgoods = 0; if (partsList != null && partsList.size() > 0) { // sql.delete(0, sql.length()); sql.append("delete from SSDeclearPartDescription where Declear_NO = '") .append(orderht.get("PRE_ENTRY_ID")).append("'"); db.execute(sql.toString()); for (int j = 0; j < partsList.size(); j++) { HashMap<String, String> partsht = (HashMap<String, String>) partsList.get(j); if (partsht.get("G_NAME") == null || partsht.get("G_NAME").toString().length() == 0) { continue; } // sql.delete(0, sql.length()); sql.append("select ID from SSGoods where GoodsNo = '").append(partsht.get("CODE_T")) .append("' and GoodsName = '").append(partsht.get("G_NAME")).append("'"); String strGoodID = db.loadVal(sql.toString()); if (strGoodID.length() == 0) { HashMap<String, String> goodht = new HashMap<String, String>(); goodht.put("GoodsNo", partsht.get("CODE_T")); goodht.put("GoodsName", partsht.get("G_NAME")); goodht.put("GoodsMode", partsht.get("G_MODEL")); strGoodID = db.insert(goodht, "SSGoods"); } else { // HashMap<String, String> goodht = new HashMap<String, String>(); goodht.put("GoodsNo", partsht.get("CODE_T")); goodht.put("GoodsName", partsht.get("G_NAME")); goodht.put("GoodsMode", partsht.get("G_MODEL")); goodht.put("ID", strGoodID); db.update(goodht, "SSGoods"); } HashMap<String, String> needht = new HashMap<String, String>(); needht.put("OrderId", OrderID); if (partsht.get("MANUAL_NO") != null && partsht.get("MANUAL_NO").toString().length() > 0) { needht.put("ManualNO", orderht.get("MANUAL_NO")); } needht.put("Declear_NO", orderht.get("PRE_ENTRY_ID")); if (partsht.get("G_NO") != null && partsht.get("G_NO").toString().length() > 0) { needht.put("G_NO", partsht.get("G_NO")); } if (partsht.get("CONTR_ITEM") != null && partsht.get("CONTR_ITEM").toString().length() > 0) { needht.put("CONTR_ITEM", partsht.get("CONTR_ITEM")); } needht.put("GoodsID", strGoodID); needht.put("GoodsNo", partsht.get("CODE_T")); needht.put("AddCODE", partsht.get("CODE_S")); needht.put("PartDescription", partsht.get("G_NAME").toString()); needht.put("GoodsMode", partsht.get("G_MODEL")); needht.put("ShenBaoQty", partsht.get("G_QTY")); needht.put("ShenBaoUnit", partsht.get("G_UNIT")); needht.put("MuDiCountry", partsht.get("ORIGIN_COUNTRY")); if (partsht.get("DECL_PRICE") != null && partsht.get("DECL_PRICE").toString().length() > 0) { needht.put("Price", partsht.get("DECL_PRICE")); } needht.put("TotalMoney", partsht.get("TRADE_TOTAL")); needht.put("CurrencyCode", partsht.get("TRADE_CURR")); needht.put("FaDingQty", partsht.get("QTY_1")); needht.put("FaDingUnit", partsht.get("UNIT_1")); needht.put("TradeMode", partsht.get("DUTY_MODE")); // db.insert(needht, "SSDeclearPartDescription"); hasgoods = 1; } } if (hasgoods == 0) { sql.delete(0, sql.length()); sql.append("update SSRESOrderInfo set HandleStatus = 5 where companyType=0 and ID = ") .append(orderht.get("ID")); db.execute(sql.toString()); } } // sql.delete(0, sql.length()); sql.append("update SSRESOrderInfo set HandleStatus = 1 where companyType=0 and HandleStatus = 2 "); db.execute(sql.toString()); db.commitConn(); } catch (Exception e) { System.out.println(sql.toString()); db.rollBackConn(); e.printStackTrace(); } } }