package weaver.interfaces.htxc.job;

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;

public class HistoryDataSyn 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(){


        RecordSet rs = new RecordSet();
        RecordSet rs2 = new RecordSet();
        RecordSet rs3 = 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"};
        for(int i=0;i<wfBaseTables.length;i++){
            //关闭触发器
            System.out.println("开始处理table "+wfBaseTables[i]);
            System.out.println("关闭table "+wfBaseTables[i]+"触发器");
            rs.executeUpdate(" alter table "+wfBaseTables[i]+" disable all triggers ");
            //同步表单数据
            rs.executeUpdate(" truncate table "+wfBaseTables[i]);
            System.out.println("清空table "+wfBaseTables[i]+"数据");
            syncTableData("select * from "+wfBaseTables[i] ,wfBaseTables[i]);
            //开启触发器
            System.out.println("同步table "+wfBaseTables[i]+"数据结束");
            rs.executeUpdate(" alter table "+wfBaseTables[i]+" enable all triggers ");

            System.out.println("开启table "+wfBaseTables[i]+"触发器");
        }

        //关闭打开表单触发器
        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);
            System.out.println("开始处理table "+maintable);
            //关闭主表触发器
            System.out.println("关闭table "+maintable+"触发器");
            rs2.executeUpdate(" alter table "+maintable+" disable all triggers ");

            System.out.println("清空table "+maintable+"数据");
            rs2.executeUpdate(" truncate table "+maintable);
            //同步流程主表单数据
            syncTableData("select * from "+maintable ,maintable);
            System.out.println("同步table "+maintable+"数据结束");
            //开启主表触发器
            rs2.executeUpdate(" alter table "+maintable+" enable all triggers ");
            System.out.println("开启table "+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"));
                //关闭明细表触发器
                System.out.println("开始处理table "+detailTable);
                System.out.println("关闭table "+detailTable+"触发器");
                rs3.executeUpdate(" alter table "+detailTable+" disable all triggers ");

                System.out.println("清空table "+detailTable+"数据");
                rs3.executeUpdate(" truncate table "+detailTable);
                //同步流程明细表单数据
                syncTableData("select * from "+detailTable ,detailTable);
                System.out.println("同步table "+detailTable+"数据结束");
                //开启明细表触发器
                rs3.executeUpdate(" alter table "+detailTable+" enable all triggers ");
                System.out.println("开启table "+detailTable+"触发器");

            }
        }


    }


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

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





    }
}
