package weaver.interfaces.htsc.cronjob;

import weaver.conn.RecordSetDataSource;
import weaver.general.BaseBean;
import weaver.interfaces.schedule.BaseCronJob;

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class ProcedureCronJob3 extends BaseCronJob {
    @Override
    public void execute() {


        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        BaseBean bb = new BaseBean();
        RecordSetDataSource rsd = new RecordSetDataSource("ASSET");
        List<Map<String,String>> list = new ArrayList<Map<String,String>>();
        Map<String,String> map = new HashMap<String,String>();

        //        公司发文    P_DOC_DISPATCH            p_sta_common('Z_DOC_DISPATCH')
        map.put("proc1","P_DOC_DISPATCH");
        map.put("proc2","p_sta_common");
        map.put("param2","Z_DOC_DISPATCH");
        list.add(map);

        //        商旅付款     p_travel_payment          p_sta_common('Z_TRAVEL_PAYMENT')
        map = new HashMap<String,String>();
        map.put("proc1","p_travel_payment");
        map.put("proc2","p_sta_common");
        map.put("param2","Z_TRAVEL_PAYMENT");
        list.add(map);

        //        IT立项      P_PRO_ITPROJECT            p_sta_common('Z_PRO_ITPROJECT')
        map = new HashMap<String,String>();
        map.put("proc1","P_PRO_ITPROJECT");
        map.put("proc2","p_sta_common");
        map.put("param2","Z_PRO_ITPROJECT");
        list.add(map);

//        采购        P_PRO_ITPURCHASE           p_sta_common('Z_PRO_ITPURCHASE')
        map = new HashMap<String,String>();
        map.put("proc1","P_PRO_ITPURCHASE");
        map.put("proc2","p_sta_common");
        map.put("param2","Z_PRO_ITPURCHASE");
        list.add(map);

//        IT需求      P_IT_ITREQUIREMENT         p_sta_common('Z_IT_ITREQUIREMENT')
        map = new HashMap<String,String>();
        map.put("proc1","P_IT_ITREQUIREMENT");
        map.put("proc2","p_sta_common");
        map.put("param2","Z_IT_ITREQUIREMENT");
        list.add(map);

//        公司收文    P_DOC_COLLECTDOC           p_sta_common('Z_DOC_COLLECTDOC')
        map = new HashMap<String,String>();
        map.put("proc1","P_DOC_COLLECTDOC");
        map.put("proc2","p_sta_common");
        map.put("param2","Z_DOC_COLLECTDOC");
        list.add(map);


//        生产数据    P_PROD_DATA                 p_sta_common('Z_PROD_DATA')
        map = new HashMap<String,String>();
        map.put("proc1","P_PROD_DATA");
        map.put("proc2","p_sta_common");
        map.put("param2","Z_PROD_DATA");
        list.add(map);


//        IT系统      P_IT_SYSTEM                p_sta_common('Z_IT_SYSTEM')
        map = new HashMap<String,String>();
        map.put("proc1","P_IT_SYSTEM");
        map.put("proc2","p_sta_common");
        map.put("param2","Z_IT_SYSTEM");
        list.add(map);

//        涨乐申请    P_ZhangLe                    p_sta_common('Z_ZhangLe')
        map = new HashMap<String,String>();
        map.put("proc1","P_ZhangLe");
        map.put("proc2","p_sta_common");
        map.put("param2","Z_ZhangLe");
        list.add(map);


//        用印流程    P_SEAL_APPLY               p_sta_common('Z_SEAL_APPLY')
        map = new HashMap<String,String>();
        map.put("proc1","P_SEAL_APPLY");
        map.put("proc2","p_sta_common");
        map.put("param2","Z_SEAL_APPLY");
        list.add(map);


//        邮递信息    P_POST_MESSAGE             p_sta_common('Z_POST_MESSAGE')
        map = new HashMap<String,String>();
        map.put("proc1","P_POST_MESSAGE");
        map.put("proc2","p_sta_common");
        map.put("param2","Z_POST_MESSAGE");
        list.add(map);


//        监管协同    P_REGULATORY_SYNERGY       p_sta_common('Z_REGULATORY_SYNERGY')
        map = new HashMap<String,String>();
        map.put("proc1","P_REGULATORY_SYNERGY");
        map.put("proc2","p_sta_common");
        map.put("param2","Z_REGULATORY_SYNERGY");
        list.add(map);

        bb.writeLog("list3:"+list.size());

        ExecutorService fixedThreadPool = Executors.newSingleThreadExecutor();
        try{
            for (int i = 0; i < list.size(); i++) {
                int d = i;
                fixedThreadPool.execute(() -> {

                    try{
                        Map<String,String> datamap = list.get(d);
                        String proc1 = datamap.get("proc1");
                        String proc2 = datamap.get("proc2");
                        String param2 = datamap.get("param2");
                        bb.writeLog("proc1:"+proc1+"   proc2:"+proc2+"   param2:"+param2);
                        bb.writeLog("dateTime:"+sdf.format(new Date())+"   TheadName:"+Thread.currentThread().getName());
                        if(!"".equals(proc1)){

                            String sql = "{CALL "+proc1+"()}";
                            rsd.execute(sql);
                        }
                        bb.writeLog("dateTime:"+sdf.format(new Date())+"   TheadName:"+Thread.currentThread().getName());
                        if(!"".equals(proc2) && !"".equals(param2)){

                            String sql = "{CALL "+proc2+"('"+param2+"')}";
                            rsd.execute(sql);
                        }
                        bb.writeLog("dateTime:"+sdf.format(new Date())+"   TheadName:"+Thread.currentThread().getName());

                    }catch (Exception e){
                        bb.writeLog("ProcedureCronJob--e:"+e);

                    }
                });
            }
        }catch (Exception e){
            bb.writeLog("ProcedureCronJob--e2:"+e);
        }finally {
            fixedThreadPool.shutdown();  //手动关闭线程池
        }

    }
}