|
|
@@ -0,0 +1,384 @@
|
|
|
+package com.uas.main;
|
|
|
+
|
|
|
+import java.io.BufferedReader;
|
|
|
+import java.io.File;
|
|
|
+import java.io.FileInputStream;
|
|
|
+import java.io.FileOutputStream;
|
|
|
+import java.io.FileReader;
|
|
|
+import java.io.IOException;
|
|
|
+import java.io.InputStream;
|
|
|
+import java.io.InputStreamReader;
|
|
|
+import java.io.OutputStream;
|
|
|
+import java.io.Reader;
|
|
|
+import java.sql.Connection;
|
|
|
+import java.sql.SQLException;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.LinkedList;
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+import org.apache.commons.csv.CSVFormat;
|
|
|
+import org.apache.commons.csv.CSVParser;
|
|
|
+import org.apache.commons.csv.CSVRecord;
|
|
|
+import org.apache.commons.net.ftp.FTPClient;
|
|
|
+
|
|
|
+import com.uas.util.BaseUtil;
|
|
|
+import com.uas.util.FtpUtil;
|
|
|
+import com.uas.util.JdbcUtil;
|
|
|
+import com.uas.util.PraseCsvUtil;
|
|
|
+
|
|
|
+public class CsvParse{
|
|
|
+
|
|
|
+ public static List<String> parseCsvFileToSqlsForStockin(File file){
|
|
|
+ List<String> sqls = null;
|
|
|
+ Reader in = null;
|
|
|
+ try{
|
|
|
+ in = new FileReader(file);
|
|
|
+ sqls = new ArrayList<String>();
|
|
|
+ CSVParser csvParser = CSVFormat.EXCEL.parse(in);
|
|
|
+ List<CSVRecord> records = csvParser.getRecords();
|
|
|
+ boolean skip = true;
|
|
|
+ String sql = "";
|
|
|
+ String value = "";
|
|
|
+ String custCode = "";
|
|
|
+ String poCode = "";
|
|
|
+ String qty = "";
|
|
|
+ String invoiceNo = "";
|
|
|
+ String lineNo = "";
|
|
|
+ String sob = "";
|
|
|
+ String fileName = file.getName();
|
|
|
+ for(int i=0;i<records.size();i++){
|
|
|
+ CSVRecord record = records.get(i);
|
|
|
+ sob = "";
|
|
|
+ for(int j=0;j<record.size();j++){
|
|
|
+ if(skip){
|
|
|
+ if("Customer Code".equals(record.get(j))){
|
|
|
+ skip = false;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }else{
|
|
|
+ String rec = record.get(j);
|
|
|
+ if(j==1){
|
|
|
+ if(rec.startsWith("E")){
|
|
|
+ rec = rec.replace("E", "HK-");
|
|
|
+ }else if(rec.startsWith("NSZPO")){
|
|
|
+ sob = "YHND_SZ.";
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if(!"".equals(rec)){
|
|
|
+ value = getSqlValueForStockin(rec,value);
|
|
|
+ }
|
|
|
+ if(j==0){
|
|
|
+ custCode = rec;
|
|
|
+ }else if(j==1){
|
|
|
+ poCode = rec;
|
|
|
+ }else if(j==5){
|
|
|
+ qty = rec;
|
|
|
+ }else if(j==7){
|
|
|
+ invoiceNo = rec;
|
|
|
+ }else if(j==14){
|
|
|
+ lineNo = rec;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if(!"".equals(value)){
|
|
|
+ sql = "insert into "+sob+"csvdatalog(cl_id,cl_cucode,cl_pucode,cl_orispeccode,cl_cardno,cl_countrycode,"
|
|
|
+ + "cl_qty,cl_shippingdate,cl_invoiceno,cl_currency,cl_price,cl_ycweight,cl_yhweight,cl_gyhweight,"
|
|
|
+ + "cl_cbm,cl_lineno,cl_rate,import_,filename_) select CSVDATALOG_SEQ.NEXTVAL";
|
|
|
+ sqls.add(sql + value + ",0,'"+fileName.replace("'", "''")+"' from dual where not exists (select 1 from "+sob+"csvdatalog where nvl(cl_cucode,' ')='" + custCode.replace("'", "''")
|
|
|
+ + "' and nvl(cl_pucode,' ')='"+poCode.replace("'", "''")+"' and nvl(cl_qty,0)='"+qty + "' and nvl(cl_invoiceno,' ')='" + invoiceNo
|
|
|
+ +"' and nvl(cl_lineno,0)='"+lineNo+"')");
|
|
|
+ value = "";
|
|
|
+ }
|
|
|
+ }
|
|
|
+ in.close();
|
|
|
+ in = null;
|
|
|
+ }catch(Exception e){
|
|
|
+ BaseUtil.getLogger().error(e.toString());
|
|
|
+ e.printStackTrace();
|
|
|
+ }finally{
|
|
|
+ try {
|
|
|
+ if(in!=null){
|
|
|
+ in.close();
|
|
|
+ }
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ in = null;
|
|
|
+ }
|
|
|
+ return sqls;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String getSqlValueForStockin(String rec,String value){ //日期转换
|
|
|
+ if(rec.matches("\\d{4}[-/]\\d{1,2}[-/]\\d{1,2}")){
|
|
|
+ String split = "/";
|
|
|
+ if(rec.contains("-")){
|
|
|
+ split = "-";
|
|
|
+ }
|
|
|
+ String[] ymd = rec.split(split);
|
|
|
+ if(ymd[1].length()==1){
|
|
|
+ rec = rec.replace(split+ymd[1], split+"0" + ymd[1]);
|
|
|
+ }
|
|
|
+ if(ymd[2].length()==1){
|
|
|
+ rec = rec.replace(split+ymd[2], split+"0" + ymd[2]);
|
|
|
+ }
|
|
|
+ value += ",to_date('"+rec+"','yyyy"+split+"mm"+split+"dd')";
|
|
|
+ }else{
|
|
|
+ value += ",'"+rec+"'";
|
|
|
+ }
|
|
|
+ return value;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static List<String> parseCsvFileToSqlsForFirm(File file){
|
|
|
+ List<String> sqls = null;
|
|
|
+ Reader in = null;
|
|
|
+ try{
|
|
|
+ in = new FileReader(file);
|
|
|
+ sqls = new ArrayList<String>();
|
|
|
+ CSVParser csvParser = CSVFormat.EXCEL.parse(in);
|
|
|
+ List<CSVRecord> records = csvParser.getRecords();
|
|
|
+ boolean skip = true;
|
|
|
+ String sql = "";
|
|
|
+ String value = "";
|
|
|
+ String sob = "";
|
|
|
+ String fileName = file.getName();
|
|
|
+ for(int i=0;i<records.size();i++){
|
|
|
+ CSVRecord record = records.get(i);
|
|
|
+ sob = "";
|
|
|
+ for(int j=0;j<24;j++){
|
|
|
+ if(skip){
|
|
|
+ if("FIRM".equals(record.get(j))){
|
|
|
+ skip = false;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }else{
|
|
|
+ String rec = record.get(j);
|
|
|
+ if(!"".equals(rec)){
|
|
|
+ value = getSqlValueForFirm(rec,value);
|
|
|
+ }else{
|
|
|
+ value=value+",''";
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if(!"".equals(value)){
|
|
|
+ sql = "insert into "+sob+"csvdataloga(ID,FIRM,CLERK_CODE,CUSTOMER,CUSTOMER_PART_NO,EXTERN_MURATA_TYPE,PURCHASE_CARD_NO,"
|
|
|
+ + "CARD_NO,SUPPLIER_CARD_NO,BACKLOG_QTY_PC,CUSTOMER_PURCH_ORDER_NO,Purchase_Order_Line_No,CUSTOMER_REQUESTED_DATE,LATEST_STOCK_IN_DATE,"
|
|
|
+ + "PURCH_ORDER_DATE,SEQUNECE_NO,PURCHASE_SEQ_NO,PICKING_NOTE_NO,SP_IN_SALES_CURRENCY,SALES_CURRENCY,ORIGIN,DESCRIPTION,"
|
|
|
+ + "SALES_MANAGER_DESC,SALESMAN_DESC,SALES_PERSON,import_,filename_) "
|
|
|
+ + "select CSVDATALOGA_SEQ.NEXTVAL";
|
|
|
+ sqls.add(sql + value + ",0,'"+fileName.replace("'", "''")+"' from dual");
|
|
|
+ value = "";
|
|
|
+ }
|
|
|
+ }
|
|
|
+ in.close();
|
|
|
+ in = null;
|
|
|
+ }catch(Exception e){
|
|
|
+ BaseUtil.getLogger().error(e.toString());
|
|
|
+ e.printStackTrace();
|
|
|
+ }finally{
|
|
|
+ try {
|
|
|
+ if(in!=null){
|
|
|
+ in.close();
|
|
|
+ }
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ in = null;
|
|
|
+ }
|
|
|
+ System.err.println(sqls);
|
|
|
+ return sqls;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static List<String> parseCsvFileToSqlsForWarehouse(File file){
|
|
|
+ List<String> sqls = null;
|
|
|
+ Reader in = null;
|
|
|
+ try{
|
|
|
+ in = new FileReader(file);
|
|
|
+ sqls = new ArrayList<String>();
|
|
|
+ CSVParser csvParser = CSVFormat.EXCEL.parse(in);
|
|
|
+ List<CSVRecord> records = csvParser.getRecords();
|
|
|
+ boolean skip = true;
|
|
|
+ String sql = "";
|
|
|
+ String value = "";
|
|
|
+ String fileName = file.getName();
|
|
|
+ String sob = "";
|
|
|
+ for(int i=0;i<records.size();i++){
|
|
|
+ CSVRecord record = records.get(i);
|
|
|
+ sob = "";
|
|
|
+ for(int j=0;j<18;j++){
|
|
|
+ if(skip){
|
|
|
+ if("Warehouse Code".equals(record.get(j))){
|
|
|
+ skip = false;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }else{
|
|
|
+ String rec = record.get(j);
|
|
|
+ if(!"".equals(rec)){
|
|
|
+ value = getSqlValueForWarehouse(rec,value);
|
|
|
+ }else{
|
|
|
+ value=value+",''";
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if(!"".equals(value)){
|
|
|
+ sql = "insert into "+sob+"csvdatalogb(ID,WAREHOUSE_CODE,FIRM_CODE,CUSTOMER_CODE,CUSTOMER_NAME,INVOICE_NUMBER,PICKING_NOTE,"
|
|
|
+ + "ADDRESS_CODE,TOTAL_QTY,EXCHANGE_RATE,CURRENCY,TOTAL_AMOUNT,HKD,HKD_AMOUNT,MURATA_CLERK,INVOICE_DATE,CREATE_DATE,USER_,"
|
|
|
+ + "SECTION_MANAGER,IMPORT_,filename_) "
|
|
|
+ + "select CSVDATALOGA_SEQ.NEXTVAL";
|
|
|
+ sqls.add(sql + value + ",0,'"+fileName.replace("'", "''")+"' from dual");
|
|
|
+ value = "";
|
|
|
+ }
|
|
|
+ }
|
|
|
+ in.close();
|
|
|
+ in = null;
|
|
|
+ }catch(Exception e){
|
|
|
+ BaseUtil.getLogger().error(e.toString());
|
|
|
+ e.printStackTrace();
|
|
|
+ }finally{
|
|
|
+ try {
|
|
|
+ if(in!=null){
|
|
|
+ in.close();
|
|
|
+ }
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ in = null;
|
|
|
+ }
|
|
|
+ return sqls;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String getSqlValueForFirm(String rec,String value){ //日期转换
|
|
|
+ if(rec.matches("\\d{4}[-/]\\d{1,2}[-/]\\d{1,2}")){
|
|
|
+ String split = "/";
|
|
|
+ if(rec.contains("-")){
|
|
|
+ split = "-";
|
|
|
+ }
|
|
|
+ String[] ymd = rec.split(split);
|
|
|
+ if(ymd[1].length()==1){
|
|
|
+ rec = rec.replace(split+ymd[1], split+"0" + ymd[1]);
|
|
|
+ }
|
|
|
+ if(ymd[2].length()==1){
|
|
|
+ rec = rec.replace(split+ymd[2], split+"0" + ymd[2]);
|
|
|
+ }
|
|
|
+ value += ",to_date('"+rec+"','yyyy"+split+"mm"+split+"dd')";
|
|
|
+ }else if("0".equals(rec)){
|
|
|
+ value+=",null ";
|
|
|
+ }
|
|
|
+ else{
|
|
|
+ value += ",'"+rec.replace("'", "")+"'";
|
|
|
+ }
|
|
|
+ return value;
|
|
|
+ }
|
|
|
+ public static String getSqlValueForWarehouse(String rec,String value){ //日期转换
|
|
|
+ if(rec.matches("\\d{4}[-/]\\d{1,2}[-/]\\d{1,2}")){
|
|
|
+ String split = "/";
|
|
|
+ if(rec.contains("-")){
|
|
|
+ split = "-";
|
|
|
+ }
|
|
|
+ String[] ymd = rec.split(split);
|
|
|
+ if(ymd[1].length()==1){
|
|
|
+ rec = rec.replace(split+ymd[1], split+"0" + ymd[1]);
|
|
|
+ }
|
|
|
+ if(ymd[2].length()==1){
|
|
|
+ rec = rec.replace(split+ymd[2], split+"0" + ymd[2]);
|
|
|
+ }
|
|
|
+ value += ",to_date('"+rec+"','yyyy"+split+"mm"+split+"dd')";
|
|
|
+ }/*else if("0".equals(rec)){
|
|
|
+ value+=",null ";
|
|
|
+ }*/
|
|
|
+ else{
|
|
|
+ value += ",'"+rec.replace("'", "")+"'";
|
|
|
+ }
|
|
|
+ return value;
|
|
|
+ }
|
|
|
+
|
|
|
+ @SuppressWarnings({"unused" })
|
|
|
+ public static void main(String[] args){
|
|
|
+ FTPClient client = null;
|
|
|
+ String filePath = "C:\\mendelson\\opensource\\as2\\messages\\Mantech\\inbox";
|
|
|
+ String backupPath = "C:\\mendelson\\opensource\\as2\\messages\\Mantech\\inbox_bak\\in\\";
|
|
|
+ List<String> filePaths = new ArrayList<String>();
|
|
|
+ Connection connect = null;
|
|
|
+ boolean execute = false;
|
|
|
+ try {
|
|
|
+ File inbox = new File(filePath);
|
|
|
+ if(inbox.exists()){
|
|
|
+ File[] files = inbox.listFiles();
|
|
|
+ String fileName = "";
|
|
|
+ if(files!=null&&files.length>0){
|
|
|
+ connect = JdbcUtil.getConnectBySob("HK");
|
|
|
+ for(File file:files){
|
|
|
+ if(file.isDirectory()){
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ try{
|
|
|
+ InputStream in = new FileInputStream(file);
|
|
|
+ BufferedReader bfin = new BufferedReader(new InputStreamReader(in));
|
|
|
+ String line = "";
|
|
|
+ int i=0;
|
|
|
+ char[] diff = new char[10];
|
|
|
+ boolean csvFile = false;
|
|
|
+ String type = null;
|
|
|
+ bfin.read(diff,0,10);
|
|
|
+ if(String.valueOf(diff).trim().startsWith("{payment}")){
|
|
|
+ csvFile = true;
|
|
|
+ type="payment";
|
|
|
+ }else if(String.valueOf(diff).trim().startsWith("FIRM")){
|
|
|
+ csvFile = true;
|
|
|
+ type="firm";
|
|
|
+ }else if(String.valueOf(diff).trim().startsWith("Warehouse")){
|
|
|
+ csvFile = true;
|
|
|
+ type="warehouse";
|
|
|
+ }else if(String.valueOf(diff).trim().startsWith("{stockin}")){
|
|
|
+ csvFile = true;
|
|
|
+ type="stockin";
|
|
|
+ }
|
|
|
+
|
|
|
+ if(csvFile&&type!=null){
|
|
|
+ BaseUtil.getLogger().info("process file:" + file.getName() + " type:"+type+" start!");
|
|
|
+
|
|
|
+ List<String> sqls=new LinkedList<String>();
|
|
|
+ if("payment".equals(type)){
|
|
|
+ sqls = PraseCsvUtil.parseCsvFileToSqlsForPayment(file);
|
|
|
+ }else if("warehouse".equals(type)){
|
|
|
+ sqls = parseCsvFileToSqlsForWarehouse(file);
|
|
|
+ }else if("firm".equals(type)){
|
|
|
+ sqls = parseCsvFileToSqlsForFirm(file);
|
|
|
+ }else if("stockin".equals(type)){
|
|
|
+ sqls = parseCsvFileToSqlsForStockin(file);
|
|
|
+ }
|
|
|
+ execute = JdbcUtil.executeSqls(connect,sqls);
|
|
|
+ };
|
|
|
+ bfin.close();
|
|
|
+ bfin = null;
|
|
|
+ in.close();
|
|
|
+ in = null;
|
|
|
+
|
|
|
+ if(csvFile&&execute){
|
|
|
+ boolean bol = file.renameTo(new File(backupPath+file.getName()));
|
|
|
+ System.out.println(bol);
|
|
|
+ }
|
|
|
+ }catch(Exception e){
|
|
|
+ e.printStackTrace();
|
|
|
+ BaseUtil.getLogger().error(e.toString());
|
|
|
+ }finally{
|
|
|
+ BaseUtil.getLogger().info("process file:" + file.getName() + " end!");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ BaseUtil.getLogger().error(e.toString());
|
|
|
+ e.printStackTrace();
|
|
|
+ }finally{
|
|
|
+ if(connect!=null){
|
|
|
+ try {
|
|
|
+ connect.close();
|
|
|
+ } catch (SQLException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ BaseUtil.getLogger().error(e.toString());
|
|
|
+ }
|
|
|
+ connect = null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|