package weaver.interfaces.htxc.job;

import weaver.conn.RecordSet;
import weaver.conn.RecordSetDataSource;
import weaver.general.GCONST;
import weaver.interfaces.htsc.comInfo.PropBean;
import weaver.interfaces.schedule.BaseCronJob;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

public class LogDataSyn extends BaseCronJob {

    @Override
    public void execute() {

        //PropBean pb = new PropBean();
        //String sysdate  = LocalDate.now().toString();
        String systime = LocalDateTime.now().minusHours(2).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
//        String customSystime = pb.getPropName("datasyntime");
//        if(!"".equals(customSystime)){
//            systime = customSystime;
//        }

        systime = "2021-09-01 00:00:00";
        //关闭触发器
        controlTrigger(0);
        doHrmLogDataSyn(systime);
        doDocLogDataSyn(systime);
        //开启触发器
        controlTrigger(1);
    }




    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 doHrmLogDataSyn(String systime){

        RecordSet rs = new RecordSet();
        RecordSetDataSource rsd = new RecordSetDataSource("fromdb");

        try {

            //"hrmsysmaintenancelog","sysmaintenancelog",
            //"hrmonlineavg","hrmonlinecount","hrmrefuseavg",
            //String systime = LocalDateTime.now().minusHours(2).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));

            //同步 hrmsysmaintenancelog
            rs.executeUpdate(" delete from hrmsysmaintenancelog where  (operatedate || ' '||operatetime)>= '"+systime+"' " );
            rsd.execute("select a.* from oacodeprd.hrmsysmaintenancelog a,oacodeprd.hrmresource b " +
                    "where  relatedid=b.id and b.departmentid=15 and (operatedate || ' '||operatetime)>= '"+systime+"' " +
                    " and clienttype=1 ");
            while(rsd.next()){
                int id = rsd.getInt("id");
                rs.executeUpdate(" delete from hrmsysmaintenancelog where id=? ",id);
            }
            syncTableData("select a.* from oacodeprd.hrmsysmaintenancelog a,oacodeprd.hrmresource b " +
                    "where  relatedid=b.id and b.departmentid=15 and (operatedate || ' '||operatetime)>= '"+systime+"' " +
                    " and clienttype=1 ","hrmsysmaintenancelog");


            //同步 sysmaintenancelog
            rs.executeUpdate(" delete from sysmaintenancelog where  (operatedate || ' '||operatetime)>= '"+systime+"'" );
            rsd.execute(" select a.* from oacodeprd.sysmaintenancelog a,oacodeprd.hrmresource b where operateuserid=b.id and b.departmentid=15 " +
                    "and (operatedate || ' '||operatetime)>= '"+systime+"' and clienttype=1");
            while(rsd.next()){
                int id = rsd.getInt("id");
                rs.executeUpdate(" delete from sysmaintenancelog where id=? ",id);
            }
            syncTableData(" select a.* from oacodeprd.sysmaintenancelog a,oacodeprd.hrmresource b where operateuserid=b.id and b.departmentid=15 " +
                    "and (operatedate || ' '||operatetime)>= '"+systime+"' and clienttype=1","sysmaintenancelog");

        }catch (Exception e) {
            //rst.rollback();;
            e.printStackTrace();
        }


    }


    public void doDocLogDataSyn(String systime){

        RecordSet rs = new RecordSet();
        RecordSetDataSource rsd = new RecordSetDataSource("fromdb");

        try {

            //"downloadlog","docreadtag","docdetaillog"

            //同步 downloadlog
            rs.executeUpdate(" delete from downloadlog where  downloadtime>= '"+systime+"'" );
            syncTableData("select a.* from oacodeprd.downloadlog a,oacodeprd.hrmresource b where a.userid=b.id " +
                    "and  b.departmentid=15 and downloadtime>= '"+systime+"' " ,"downloadlog");

            //同步 docdetaillog
            rs.executeUpdate(" delete from docdetaillog where  (operatedate || ' '||operatetime)>= '"+systime+"'" );
            rsd.execute(" select a.* from oacodeprd.docdetaillog a,oacodeprd.hrmresource b where a.operateuserid=b.id and b.departmentid=15 " +
                    " and (operatedate || ' '||operatetime)>= '"+systime+"' ");
            while(rsd.next()){
                int id = rsd.getInt("id");
                rs.executeUpdate(" delete from docdetaillog where id=? ",id);
            }
            syncTableData(" select a.* from oacodeprd.docdetaillog a,oacodeprd.hrmresource b where a.operateuserid=b.id and b.departmentid=15 " +
                    " and (operatedate || ' '||operatetime)>= '"+systime+"' ","docdetaillog");

            //同步 docreadtag
            rsd.execute(" select * from oacodeprd.docreadtag a where exists ( select 1 from oacodeprd.docdetaillog b,oacodeprd.hrmresource c " +
                    "where a.docid=b.docid and b.operateuserid=c.id and (operatedate || ' '||operatetime)>= '"+systime+"''  ) ");
            while(rsd.next()){
                int id = rsd.getInt("id");
                rs.executeUpdate(" delete from docreadtag where id=? ",id);
            }
            syncTableData(" select * from oacodeprd.docreadtag a where exists ( select 1 from oacodeprd.docdetaillog b,oacodeprd.hrmresource c " +
                    "where a.docid=b.docid and b.operateuserid=c.id and (operatedate || ' '||operatetime)>= '"+systime+"''  ) " ,"docreadtag");


        }catch (Exception e) {
            //rst.rollback();;
            e.printStackTrace();
        }


    }


    /**
     * @param flag 0关闭触发器  1开启触发器
     */
    public void controlTrigger(int flag){
        String operation = "enable";
        if(flag==0){
            operation = "disable";
        }

        RecordSet rs = new RecordSet();
        //关闭开启流程基础表触发器
        String hrmLogTables[] = {"hrmsysmaintenancelog","sysmaintenancelog"};
        for(int i=0;i<hrmLogTables.length;i++){
            rs.executeUpdate(" alter table "+hrmLogTables[i]+" "+operation+" all triggers ");
        }


        //关闭打开文档表触发器
        String docLogTables[] = {"downloadlog","docreadtag","docdetaillog"};
        for(int i=0;i<docLogTables.length;i++){
            rs.executeUpdate(" alter table "+docLogTables[i]+" "+operation+" all triggers ");
        }


    }
    

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

        LogDataSyn logDataSyn = new LogDataSyn();
        logDataSyn.execute();





    }
}
