package com.ag.secp.platform.common;

import com.wb.util.DbUtil;
import com.wb.util.WebUtil;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.usermodel.*;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.apache.poi.xwpf.usermodel.XWPFTableCell;
import org.apache.poi.xwpf.usermodel.XWPFTableRow;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.util.Iterator;
import java.util.List;

public class ReadWord {

   static String billNBr="", shipper="",Consignee="",notify="",customNo="",shipvoyage="" ,shipNam="",voyageNo="",startport=""
            ,disport="",destport="",Receipt="",mark="",pkgs="",cargoNam="",kgs="",vol="";

    public static void upload(HttpServletRequest request,
                                 HttpServletResponse response) throws Exception {
        billNBr=""; shipper="";Consignee="";notify="";customNo="";shipvoyage="";shipNam="";voyageNo="";startport="";
         disport="";destport="";Receipt="";mark="";pkgs="";cargoNam="";kgs="";vol="";
        InputStream stream = (InputStream) request.getAttribute("file");
        String filename = (String) request.getAttribute("filename");
        if (filename == null)
            filename = (String) request.getAttribute("file__name");
        int count=0;
        testWord(stream,filename);
        File destPath, syncPath;
        shipNam=  shipvoyage.split("V.")[0];
        voyageNo=  shipvoyage.split("V.")[1];
       String num=  pkgs.split(" ")[0];
        String pagekge=  pkgs.split(" ")[1];
        int piece=Integer.parseInt(num);
        Double wgk=Double.parseDouble(kgs.replace("KGS",""));
        Double volem=Double.parseDouble(vol.replace("CBM",""));
        Connection conn=null;
        PreparedStatement s=null;
        try{
            conn= DbUtil.getConnection();
            s= conn.prepareStatement("update F_VBILL_QD_HEAD set E_SHIPPER_NAM=? ,E_CONSIGNEE_NAM=?,E_NOTIFY_NAM=?,VESSEL_NAME=?  ,VESSEL_VOY='"+voyageNo+"' , POL_NAME ='"+startport+"'" +
                    ",POD_NAME='"+disport+"',PODEST_NAME='"+destport+"',POS_NAME='"+Receipt+"', TD_MARKS_STR=?,TD_E_CARGO_NAM=?,TD_PIECE_NUM="+piece+",TD_GWEIGHT_TON="+wgk+",TD_GVOL_NUM="+volem+" where  LINE_BK_NO='"+billNBr+"' ");
        //    String sql ="update F_VBILL_QD_HEAD set E_SHIPPER_NAM='"+shipper+"' ,E_CONSIGNEE_NAM='"+Consignee+"',E_NOTIFY_NAM='"+notify+"',VESSEL_NAME='"+shipNam+"'  ,VESSEL_VOY='"+voyageNo+"' , POL_NAME ='"+startport+"'" +
        //            ",POD_NAME='"+disport+"',PODEST_NAME='"+destport+"',POS_NAME='"+Receipt+"', TD_MARKS_STR='"+mark+"',TD_E_CARGO_NAM='"+cargoNam+"',TD_PIECE_NUM="+piece+",TD_GWEIGHT_TON="+wgk+",TD_GVOL_NUM="+volem+" where  LINE_BK_NO='"+billNBr+"' ";
            s.setString(1,shipper);
            s.setString(2,Consignee);
            s.setString(3,notify);
            s.setString(4,shipNam);
            s.setString(5,mark);
            s.setString(6,cargoNam);
            count= s.executeUpdate();


        }
        catch(Exception e){
                e.printStackTrace();
        }
        finally {
                 DbUtil.close(s);
                 DbUtil.close(conn);

            WebUtil.send(response,count+"",true);
        }

    }
    public static void main(String[] args) {
        ReadWord test = new ReadWord();
        //String filePath="E:\\java??word??.doc";
        String filePath="E:\\23512.doc";
      //  test.testWord(filePath);
    }
    public static void testWord(InputStream in, String filePath ){

        try{
        //  FileInputStream in = new FileInputStream(filePath);//????
            //???office2007  docx??
            if(filePath.toLowerCase().endsWith("docx")){
                //word 2007 ???????? ????????????????
                XWPFDocument xwpf = new XWPFDocument(in);//??word?????
//               List<XWPFParagraph> listParagraphs = xwpf.getParagraphs();//??????
                Iterator<XWPFTable> it = xwpf.getTablesIterator();//??word????
                while(it.hasNext()){
                    XWPFTable table = it.next();
                    List<XWPFTableRow> rows=table.getRows();
                    //???????
                    for (int i = 0; i < rows.size(); i++) {
                        XWPFTableRow row = rows.get(i);
                        //???????
                        List<XWPFTableCell> cells = row.getTableCells();
                        for (int j = 0; j < cells.size(); j++) {
                            XWPFTableCell cell=cells.get(j);
                            //???????????
                            System.out.println(cell.getText());
                        }
                    }

                }
            }else{
                //???office2003  doc??
                POIFSFileSystem pfs = new POIFSFileSystem(in);
                HWPFDocument hwpf = new HWPFDocument(pfs);
                Range range = hwpf.getRange();//?????????
                TableIterator it = new TableIterator(range);
                //????????
                while (it.hasNext()) {
                    Table tb = (Table) it.next();
                    //???????0??
                    for (int i = 0; i < tb.numRows(); i++) {
                        TableRow tr = tb.getRow(i);
                        //???????0??
                        for (int j = 0; j < tr.numCells(); j++) {
                            TableCell td = tr.getCell(j);//?????

                            //????????
                            for(int k=0;k<td.numParagraphs();k++){
                                Paragraph para =td.getParagraph(k);

                                String s = para.text();
                                //?????????
                                if(null!=s&&!"".equals(s)){
                                    s=s.substring(0, s.length()-1);
                                    getString( i, j , s);
                                 //  System.out.print(s);
                                }
                            }
                        }
                    }
                }
            }
        }catch(Exception e){
            e.printStackTrace();
        }
    //    System.out.print(billNBr+"=="+shipper+"=="+Consignee+"=="+notify+"=="+customNo+"=="+shipvoyage+"=="+startport+ "=="+disport+"=="+destport+"=="+Receipt+"=="+mark+"=="+pkgs+"=="+cargoNam+"=="+kgs+"=="+vol);
    }

    private static void getString(int i,int j ,String s)
    {
        if(i==2&&j==2)
            billNBr=s;
        else if(i==3&&j==0)
            shipper+=s;
        else if(i==8&&j==0)
            Consignee+=s;
        else if(i==12&&j==0)
            notify+=s;
        else if(i==5&&j==0)
            customNo+=s;
        else if(i==15&&j==1)
            Receipt+=s;
        else if(i==17&&j==0)
            shipvoyage+=s;
        else if(i==17&&j==0)
            shipvoyage+=s;
        else if(i==17&&j==1)
            startport+=s;
        else if(i==19&&j==0)
            disport+=s;
        else if(i==19&&j==1)
            destport+=s;
        else if(i==21&&j==0)
            mark+=s;
        else if(i==21&&j==1)
            pkgs+=s;
        else if(i==21&&j==2)
            cargoNam+=s;
        else if(i==21&&j==3)
            kgs+=s;
        else if(i==21&&j==4)
            vol+=s;

    }
}
