package weaver.interfaces.htxc.job;

import weaver.conn.RecordSet;
import weaver.conn.RecordSetDataSource;
import weaver.general.Util;

import java.util.ArrayList;
import java.util.List;

public class DocDataSynThread implements Runnable {

    public List<String> fjids = new ArrayList<String>();

    public String requestid;

    public String docBaseTables[] = {"docdetail","docimagefile","imagefile","docshare","shareinnerdoc"};

    public DocDataSynThread(List<String> fjids,String requestid){
        this.fjids = fjids;
        this.requestid = requestid;
    }

    @Override
    public void run() {
        RecordSet rs = new RecordSet();
        RecordSet rs2 = new RecordSet();


        for(int i=0;i<fjids.size();i++){
            String docid = fjids.get(i);
            //同步docdetail
            System.out.println("syndoc docdetail docid=====>"+docid);
            rs.executeQuery(" select * from docdetail where id=? ",docid);
            if(!rs.next()){
                syncTableData("select * from oacodeprd.docdetail where id="+docid,"docdetail");
            }else{
                logExistDocid(requestid,docid);
            }


            //同步 docimagefile
            System.out.println("syndoc docimagefile=====>"+docid);
            rs.executeQuery(" select * from docimagefile where docid=? ",docid);
            if(!rs.next()){
                syncTableData("select * from oacodeprd.docimagefile where docid="+docid,"docimagefile");
            }

            //同步 imagefile
            System.out.println("syndoc imagefile docid=====>"+docid);
            rs.executeQuery(" select * from docimagefile where docid=? ",docid);
            while(rs.next()){
                String imagefileid = Util.null2String(rs.getString("imagefileid"));
                rs2.executeQuery(" select * from imagefile where imagefileid=? ",imagefileid);
                if(!rs2.next()){
                    System.out.println("syndoc imagefile imagefileid=====>"+imagefileid);
                    syncTableData("select * from oacodeprd.imagefile where imagefileid="+imagefileid,"imagefile");
                }

            }

            //同步 docshare
            System.out.println("syndoc docshare=====>"+docid);
            rs.executeQuery(" select * from docshare where docid=? ",docid);
            if(!rs.next()){
                syncTableData("select * from oacodeprd.docshare where docid="+docid,"docshare");
            }


            //同步 shareinnerdoc
            System.out.println("syndoc shareinnerdoc=====>"+docid);
            rs.executeQuery(" select * from shareinnerdoc where sourceid=? ",docid);
            if(!rs.next()){
                syncTableData("select * from oacodeprd.shareinnerdoc where sourceid="+docid,"shareinnerdoc");

            }
        }

    }


    public void syncTableData(String dataSql,String tableName){
        RecordSetDataSource rsd = new RecordSetDataSource("fromdb");
        rsd.execute(dataSql);
        String columnNames[] =  rsd.getColumnName();
        int count = rsd.getCounts();

        if(count>0){
            StringBuffer sb =  new StringBuffer();
            StringBuffer sb2 =  new StringBuffer();
            StringBuffer sb3 =  new StringBuffer();

            String split ="";
            for (int i = 0; i <columnNames.length ; i++) {
                sb2.append(split).append(columnNames[i]);
                sb3.append(split).append("?");
                split=",";
            }
            String insertSql =  sb.append("insert into ").append(tableName)
                    .append("(").append(sb2.toString()).append(") ").append(" values(")
                    .append(sb3.toString()).append(")").toString();
            RecordSet rs = new RecordSet();
            while(rsd.next()){
                List<String> values = new ArrayList<>();
                for (String columnName:columnNames) {
                    String columnValue = rsd.getString(columnName);
                    values.add(columnValue);
                }
                rs.executeUpdate(insertSql,values);

            }
        }
    }


    public void logExistDocid(String requestid,String docid){
        RecordSet rs = new RecordSet();
        rs.executeUpdate(" insert into uf_wqywjtz(lcid,wjid) values(?,?) ",new Object[]{requestid,docid});

    }
}
