package weaver.interfaces.htxc.job;

import com.weaver.formmodel.util.DateHelper;
import weaver.conn.RecordSet;
import weaver.conn.RecordSetDataSource;
import weaver.general.GCONST;
import weaver.general.Util;
import weaver.interfaces.schedule.BaseCronJob;
import weaver.interfaces.htsc.util.SqlUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class HistoryDataSynThread extends BaseCronJob {


    @Override
    public void execute() {
        doTableDataSyn();
    }






    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();
            sb.append("insert into ").append(tableName).append(" values(");
            String split ="";
            for (int i = 0; i <columnNames.length ; i++) {
                sb.append(split).append("?");
                split=",";
            }
            sb.append(")");
            String insertSql = sb.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 doTableDataSyn(){

        ExecutorService threadPool= Executors.newFixedThreadPool(5);


        RecordSet rs = new RecordSet();
        RecordSet rs2 = new RecordSet();
        
        //关闭开启流程基础表触发器
        String wfBaseTables[] = {"workflow_form","workflow_requestbase","workflow_currentoperator","workflow_requestlog","workflow_nownode",
                "workflow_freenode","workflow_freenode_group","workflow_reqbrowextrainfo","workflow_requestviewlog","workflow_autoflowlog",
                "workflow_requestoperatelog","record_formfield_history"};
//        String wfBaseTables[] = {"record_formfield_history"};
        for(int i=0;i<wfBaseTables.length;i++){

            int tableIndex = i;

            threadPool.execute(new TableDataSynThead(wfBaseTables[tableIndex]));

        }

        //关闭打开表单触发器
        String wfSql = "select * from workflow_base where unnumber in('gsfw','dgtfw','zhfw','fzjgfw','zbggfb'," +
                "'ggfbfzjg','gssw','gsswldsy','gsswxbbm','gsswyz','gsswzbbm') and isvalid=1  ";
        rs.executeQuery(wfSql);
        while(rs.next()){
            //主表同步
            int formid = rs.getInt("formid");
            String mainTableSql = " select tablename from workflow_bill where id=? ";
            String maintable = SqlUtil.querySingleField(mainTableSql,formid);
            threadPool.execute(new TableDataSynThead(maintable));

            //明细表同步
            String dtTableSql = " select distinct detailtable from workflow_billfield  " +
                    " where billid=? and (detailtable is not null or detailtable='')  order by detailtable ";
            rs2.executeQuery(dtTableSql,formid);
            while (rs2.next()){
                String detailTable = Util.null2String(rs2.getString("detailtable"));
                threadPool.execute(new TableDataSynThead(detailTable));

            }
        }

        //关闭打开文档表触发器
//        String docBaseTables[] = {"docdetail","docimagefile","imagefile","docshare","shareinnerdoc"};
//        for(int i=0;i<docBaseTables.length;i++){
//            int tableIndex = i;
//            threadPool.execute(new TableDataSynThead(docBaseTables[tableIndex]));
//
//        }


    }


    public class TableDataSynThead implements Runnable{
        String tableName;

        TableDataSynThead(String tableName){
            this.tableName = tableName;
        }

        @Override
        public void run() {
            RecordSet rs = new RecordSet();
            //关闭明细表触发器
            System.out.println("开始处理table "+tableName);
            System.out.println("关闭table "+tableName+"触发器");
            rs.executeUpdate(" alter table "+tableName+" disable all triggers ");

            System.out.println("清空table "+tableName+"数据");
            String timstamp = DateHelper.getTimestamp();
            rs.execute(" create table "+tableName+"_"+timstamp+" as select * from "+tableName);
            rs.executeUpdate(" truncate table "+tableName);
            //同步流程明细表单数据
            syncTableData("select * from oacodeprd."+tableName ,tableName);
            System.out.println("同步table "+tableName+"数据结束");
            //开启明细表触发器
            rs.executeUpdate(" alter table "+tableName+" enable all triggers ");
            System.out.println("开启table "+tableName+"触发器");
        }
    }


    public static void main(String[] args) {
        GCONST.setRootPath("./");
        GCONST.setServerName("ecology");

        HistoryDataSynThread historyDataSyn = new HistoryDataSynThread();
        historyDataSyn.execute();

    }
}
