package weaver.interfaces.htxc.job;

import weaver.conn.RecordSet;
import weaver.conn.RecordSetTrans;
import weaver.docs.docs.DocComInfo;
import weaver.general.GCONST;
import weaver.general.Util;
import weaver.interfaces.schedule.BaseCronJob;
import weaver.interfaces.htsc.util.SqlUtil;
import weaver.interfaces.htsc.util.WfUtil;
import weaver.workflow.workflow.WorkflowRequestComInfo;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

public class ProDataSyn extends BaseCronJob {

    @Override
    public void execute() {
        doWfDataSyn();
        doFileDataSyn();
    }

    /**
     * 判断流程是否已存在
     * @param requestid
     * @return
     */
    public boolean hasWf(String requestid){
        String sql = " select 1 from workflow_requestbase where requestid=? ";
        RecordSet rs = new RecordSet();
        rs.executeQuery(sql,requestid);
        return rs.next();
    }

    /**
     * 查询所有明细表
     * @param requestid
     * @return
     */
    public static List<String> getWfAllDetailTables(String requestid){
        List<String> tables = new ArrayList<>();
        RecordSet rs = new RecordSet();
        String sql = " select distinct detailtable from workflow_requestbase@HTSCOA a,workflow_base@HTSCOA b, workflow_billfield@HTSCOA c  " +
                "where a.workflowid=b.id and b.formid=c.billid  and (detailtable is not null or detailtable='') and a.requestid=? order by detailtable ";
        rs.executeQuery(sql,requestid);
        while (rs.next()){
            String detailTable = Util.null2String(rs.getString("detailtable"));
            tables.add(detailTable);
        }
        return tables;

    }


    public void doWfDataSyn(){
        //查询所有需要同步的流程数据
        String  sysdate  = LocalDate.now().minusDays(1l).toString();
       // String sql = " select requestid from workflow_requestbase@HTSCOA where lastoperatedate>='2021-11-22' and workflowid=4021";
        String sql = " select requestid from workflow_requestbase@HTSCOA where requestid in(3822841) ";
        RecordSet rs = new RecordSet();
        RecordSet rs2 = new RecordSet();
        RecordSetTrans rst = new RecordSetTrans();
        //rst.setAutoCommit(false);
        //rs.executeQuery(sql,sysdate);
        rs.executeQuery(sql);

        try {
            while(rs.next()) {

                String requestid = Util.null2String(rs.getString("requestid"));
                String mainTableSql = " SELECT TABLENAME FROM WORKFLOW_REQUESTBASE@HTSCOA T1,WORKFLOW_BASE@HTSCOA T2,WORKFLOW_BILL@HTSCOA T3\n" +
                        "WHERE T1.WORKFLOWID=T2.ID AND T2.FORMID=T3.ID AND T1.REQUESTID=? ";
                String maintable = SqlUtil.querySingleField(mainTableSql,requestid);
                List<String> detailTables = getWfAllDetailTables(requestid);

                String synMainId = SqlUtil.querySingleField("select id from "+maintable+"@HTSCOA where requestid=? ",requestid);


                boolean isExist = hasWf(requestid);
                //如果存在先删除历史数据再同步
                if(isExist){
                    //删除表单数据
                    int mainid = WfUtil.getMainId(requestid);
                    rst.executeUpdate(" delete from "+maintable+" where requestid=? ", requestid);

                    for(String dtable:detailTables){
                        rst.executeUpdate(" delete from "+dtable+" where mainid=? ", mainid);
                    }

                    //删除流程 workflow_requestbase 表数据
                    rst.executeUpdate(" delete from workflow_requestbase where requestid=? ", requestid);

                    //删除流程 workflow_form 表数据
                    rst.executeUpdate(" delete from workflow_form where requestid=? ", requestid);

                    //删除流程 workflow_currentoperator 表数据
                    rst.executeUpdate(" delete from workflow_currentoperator where requestid=? ", requestid);

                    //删除流程 workflow_requestlog 表数据
                    rst.executeUpdate(" delete from workflow_requestlog where requestid=? ", requestid);

                    //删除流程 workflow_nownode 表数据
                    rst.executeUpdate(" delete from workflow_nownode where requestid=? ", requestid);

                    //删除流程 workflow_freenode 表数据
                    rst.executeUpdate(" delete from workflow_freenode where requestid=? ", requestid);

                    //删除流程 workflow_freenode_group 表数据
                    rst.executeUpdate(" delete from workflow_freenode_group where requestid=? ", requestid);

                    //删除流程 workflow_reqbrowextrainfo 表数据
                    rst.executeUpdate(" delete from workflow_reqbrowextrainfo where requestid=? ", requestid);

                }

                //删除同id的数据
                rst.executeUpdate(" delete   from "+maintable+" a   where exists " +
                        "( select 1 from "+maintable+"@HTSCOA b where a.id = b.id and b.requestid=? )" , requestid);
                for(String dtable:detailTables){
                    rst.executeUpdate(" delete from "+dtable+" a   where exists " +
                            "( select 1 from "+dtable+"@HTSCOA b where a.id = b.id and b.mainid=? )", synMainId);
                }
                rst.executeUpdate(" delete from workflow_currentoperator a where exists " +
                        "( select 1 from workflow_currentoperator@HTSCOA b where a.id = b.id and b.requestid=? ) ", requestid);

                rst.executeUpdate(" delete from workflow_requestlog a where exists " +
                        "( select 1 from workflow_requestlog@HTSCOA b where a.logid = b.logid and b.requestid=? ) ", requestid);

                rst.executeUpdate(" delete from workflow_freenode a where exists " +
                        "( select 1 from workflow_freenode@HTSCOA b where a.id = b.id and b.requestid=? ) ", requestid);

                rst.executeUpdate(" delete from workflow_freenode_group a where exists " +
                        "( select 1 from workflow_freenode_group@HTSCOA b where a.id = b.id and b.requestid=? ) ", requestid);

                rst.executeUpdate(" delete from workflow_reqbrowextrainfo a where exists " +
                        "( select 1 from workflow_reqbrowextrainfo@HTSCOA b where a.id = b.id and b.requestid=? ) ", requestid);


                //开始同步数据
                //同步表 workflow_requestbase
                rst.executeUpdate(" insert into workflow_requestbase " +
                        " select * from workflow_requestbase@HTSCOA where requestid=?",requestid);

                //同步表 workflow_form
                rst.executeUpdate(" insert into workflow_form " +
                        " select * from workflow_form@HTSCOA where requestid=?",requestid);

                //同步表 workflow_currentoperator
                rst.executeUpdate(" insert into workflow_currentoperator " +
                        " select * from workflow_currentoperator@HTSCOA where requestid=?",requestid);

                //同步表 workflow_requestlog
                rst.executeUpdate(" insert into workflow_requestlog " +
                        " select * from workflow_requestlog@HTSCOA where requestid=?",requestid);

                //同步表 workflow_nownode
                rst.executeUpdate(" insert into workflow_nownode " +
                        " select * from workflow_nownode@HTSCOA where requestid=?",requestid);

                //同步表 workflow_freenode
                rst.executeUpdate(" insert into workflow_freenode " +
                        " select * from workflow_freenode@HTSCOA where requestid=?",requestid);

                //同步表 workflow_freenode_group
                rst.executeUpdate(" insert into workflow_freenode_group " +
                        " select * from workflow_freenode_group@HTSCOA where requestid=?",requestid);

                //同步表 workflow_reqbrowextrainfo
                rst.executeUpdate(" insert into workflow_reqbrowextrainfo " +
                        " select * from workflow_reqbrowextrainfo@HTSCOA where requestid=?",requestid);

                //同步表单数据
                rst.executeUpdate(" insert into "+maintable+" " +
                        " select * from "+maintable+"@HTSCOA where requestid=?",requestid);

                for(String dtable:detailTables){
                    rst.executeUpdate(" insert into "+dtable+" " +
                            " select * from "+dtable+"@HTSCOA where mainid=?",synMainId);
                }

                //rst.commit();
                WorkflowRequestComInfo wfReqComInfo = new WorkflowRequestComInfo();
                wfReqComInfo.addRequestInfoCache(requestid);
            }
        }catch (Exception e) {
            //rst.rollback();;
            e.printStackTrace();
        }


    }


    public void doFileDataSyn(){

        RecordSet rs = new RecordSet();
        RecordSet rs2 = new RecordSet();
        RecordSetTrans rst = new RecordSetTrans();
        try {
            String  sysdate  = LocalDate.now().minusDays(1l).toString();
            //rst.setAutoCommit(false);


            rs.executeQuery(" select id from docdetail@HTSCOA where doclastmoddate>=? ",sysdate);
            while(rs.next()){
                int docid = rs.getInt("id");

                //同步docdetail
                rst.executeUpdate(" delete from docdetail where id=? ",docid);
                rst.executeUpdate(" insert into docdetail select * from docdetail@HTSCOA where id=? ",docid);

                //同步 docimagefile
                rst.executeUpdate(" delete from docimagefile where docid=? ",docid);
                rst.executeUpdate(" delete from docimagefile a where exists " +
                        "( select 1 from docimagefile@HTSCOA b where a.id = b.id and b.docid=? ) ",docid);
                rst.executeUpdate(" insert into docimagefile select * from docimagefile@HTSCOA where docid=? ",docid);

                //同步 imagefile
                rs2.executeQuery(" select * from docimagefile@HTSCOA where docid=? ",docid);
                while(rs2.next()){
                    String imagefileid = Util.null2String(rs2.getString("imagefileid"));
                    rst.executeUpdate(" delete from imagefile where imagefileid=? ",imagefileid);
                    rst.executeUpdate(" insert into imagefile select * from imagefile@HTSCOA where imagefileid=? ",imagefileid);
                }

                //同步 docshare
                rst.executeUpdate(" delete from docshare where docid=? ",docid);
                rst.executeUpdate(" delete from docshare a where exists " +
                        "( select 1 from docshare@HTSCOA b where a.id = b.id and b.docid=? ) ",docid);
                rst.executeUpdate(" insert into docshare select * from docshare@HTSCOA where docid=? ",docid);

                //同步 shareinnerdoc
                rst.executeUpdate(" delete from shareinnerdoc where sourceid=? ",docid);
                rst.executeUpdate(" delete from shareinnerdoc a where exists " +
                        "( select 1 from shareinnerdoc@HTSCOA b where a.id = b.id and b.sourceid=? ) ",docid);
                rst.executeUpdate(" insert into shareinnerdoc select * from shareinnerdoc@HTSCOA where sourceid=? ",docid);


                DocComInfo docComInfo = new DocComInfo();
                docComInfo.addDocInfoCache(docid+"");
            }

            //rst.commit();

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


    }



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

        ProDataSyn proDataSyn = new ProDataSyn();
        proDataSyn.execute();



    }
}
