package weaver.interfaces.htsc.productionData.job;


import com.alibaba.fastjson.JSON;
import org.apache.commons.lang.StringUtils;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.TimeUtil;
import weaver.general.Util;
import weaver.interfaces.htsc.GlobatTools.JAVATools;
import weaver.interfaces.htsc.GlobatTools.workflow.WorkflowCreateHandler;
import weaver.interfaces.htsc.comInfo.PropBean;
import weaver.interfaces.schedule.BaseCronJob;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class DataDestructionAutoCreateJob extends BaseCronJob {

    @Override
    public void execute() {
        PropBean pb = new PropBean();
        BaseBean bs = new BaseBean();
        RecordSet rs = new RecordSet();

        bs.writeLog("--- DataDestructionAutoCreateJob start ---");
        String workflowId = pb.getWorkflowIdsByUnNumber("schjshtq");
        String nowDate = TimeUtil.getCurrentDateString();
        Calendar calendar = Calendar.getInstance();
        int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK)-2;//正常是-1，但是这里是和下拉框匹配，所以-2
        int dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);
        int month = calendar.get(Calendar.MONTH)+1;

        Calendar firthDayOfQuarter = Calendar.getInstance();
        if(month>=1 && month<=3){
            firthDayOfQuarter.set(Calendar.MONTH, Calendar.JANUARY);
            firthDayOfQuarter.set(Calendar.DAY_OF_MONTH, 1);
        }else if(month<=6){
            firthDayOfQuarter.set(Calendar.MONTH, Calendar.APRIL);
            firthDayOfQuarter.set(Calendar.DAY_OF_MONTH, 1);
        }else if(month<=9){
            firthDayOfQuarter.set(Calendar.MONTH, Calendar.JULY);
            firthDayOfQuarter.set(Calendar.DAY_OF_MONTH, 1);
        }else{
            firthDayOfQuarter.set(Calendar.MONTH, Calendar.OCTOBER);
            firthDayOfQuarter.set(Calendar.DAY_OF_MONTH, 1);
        }
        int dayOfQuarter = calendar.get(Calendar.DAY_OF_YEAR)-firthDayOfQuarter.get(Calendar.DAY_OF_YEAR)+1;

        String tableName = "";
        String sql = "select t1.tablename from workflow_bill t1,workflow_base t2  where t2.formid = t1.id and t2.id = '"+workflowId+"'";
        bs.writeLog(sql);
        rs.executeQuery(sql);
        if(rs.next()){
            tableName = Util.null2String(rs.getString("tablename"));
        }

        //一次
        sql = "select t1.requestid from "+tableName+" t1,workflow_requestbase t2 where t2.requestid = t1.requestid and t2.currentnodetype = '3' and t1.kbzq = '0' and t1.sjsyjzsj = '"+nowDate+"'";
        bs.writeLog(sql);
        rs.executeQuery(sql);
        if(rs.next()){
            String requestid = Util.null2String(rs.getString("requestid"));
            create(requestid);
        }

        //每日
        sql = "select t1.requestid from "+tableName+" t1,workflow_requestbase t2 where t2.requestid = t1.requestid and t2.currentnodetype = '3' and t1.kbzq = '1' and t1.sjsykssj <= '"+nowDate+"' and t1.sjsyjzsj >= '"+nowDate+"'";
        bs.writeLog(sql);
        rs.executeQuery(sql);
        if(rs.next()){
            String requestid = Util.null2String(rs.getString("requestid"));
            create(requestid);
        }

        //每周
        sql = "select t1.requestid from "+tableName+" t1,workflow_requestbase t2 where t2.requestid = t1.requestid and t2.currentnodetype = '3' and t1.kbzq = '2' and t1.mz = '"+dayOfWeek+"' and t1.sjsykssj <= '"+nowDate+"' and t1.sjsyjzsj >= '"+nowDate+"'";
        bs.writeLog(sql);
        rs.executeQuery(sql);
        if(rs.next()){
            String requestid = Util.null2String(rs.getString("requestid"));
            create(requestid);
        }

        //每月
        sql = "select t1.requestid from "+tableName+" t1,workflow_requestbase t2 where t2.requestid = t1.requestid and t2.currentnodetype = '3' and t1.kbzq = '3' and t1.myd = '"+dayOfMonth+"' and t1.sjsykssj <= '"+nowDate+"' and t1.sjsyjzsj >= '"+nowDate+"'";
        bs.writeLog(sql);
        rs.executeQuery(sql);
        if(rs.next()){
            String requestid = Util.null2String(rs.getString("requestid"));
            create(requestid);
        }

        //每季
        sql = "select t1.requestid from "+tableName+" t1,workflow_requestbase t2 where t2.requestid = t1.requestid and t2.currentnodetype = '3' and t1.kbzq = '4' and t1.mjd = '"+dayOfQuarter+"' and t1.sjsykssj <= '"+nowDate+"' and t1.sjsyjzsj >= '"+nowDate+"'";
        bs.writeLog(sql);
        rs.executeQuery(sql);
        if(rs.next()){
            String requestid = Util.null2String(rs.getString("requestid"));
            create(requestid);
        }
    }

    /**
     * 创建生产环境数据提取流程
     * @param requestid
     */
    public void create(String requestid){
        PropBean pb = new PropBean();
        BaseBean bs = new BaseBean();
        RecordSet rs = new RecordSet();

        bs.writeLog("original requestid:"+requestid);

        try{

            String tableName = JAVATools.getMaintable(requestid);
            String workflowId = pb.getWorkflowIdsByUnNumber("schjshtqxh");
            Map<String,String> requestMainMap = new HashMap<>();


            String lcbh = "";
            String lcbt = "";
            String sql = "select requestname,requestmark from workflow_requestbase where requestid = '"+requestid+"'";
            bs.writeLog(sql);
            rs.executeQuery(sql);
            if(rs.next()){
                lcbh = Util.null2String(rs.getString("requestmark"));
                lcbt = Util.null2String(rs.getString("requestname"));
            }
            SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy年MM月dd日");
            String requestName = "生产环境数据销毁-"+lcbh+"-"+lcbt+"-"+sdf1.format(new Date());

            String creatorId = "";
            String szbm = "";
            String szjg = "";
            String jjcd = "";
            String jjyy = "";
            String lxfs = "";
            sql = "select ngr,szbm,szjg,jjcd,jjyy,lxfs from "+tableName+" where requestid =  '"+requestid+"'";
            bs.writeLog(sql);
            rs.executeQuery(sql);
            if(rs.next()){
                creatorId = Util.null2String(rs.getString("ngr"));
                szbm = Util.null2String(rs.getString("szbm")    );
                szjg = Util.null2String(rs.getString("szjg"));
                jjcd = Util.null2String(rs.getString("jjcd"));
                jjyy = Util.null2String(rs.getString("jjyy"));
                lxfs = Util.null2String(rs.getString("lxfs"));
            }
            requestMainMap.put("ngr",creatorId);//拟稿人
            requestMainMap.put("szbm",szbm);//所在部门
            requestMainMap.put("szjg",szjg);//所在机构
            requestMainMap.put("jjcd",jjcd);//缓急程度
            requestMainMap.put("jjyy",jjyy);//加急原因
            requestMainMap.put("lxfs",lxfs);//联系方式
            SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm");
            requestMainMap.put("ngsj",sdf2.format(new Date()));//拟稿时间
            requestMainMap.put("gllc",requestid);//联系方式

            bs.writeLog("requestMainMap:"+ JSON.toJSONString(requestMainMap));
            WorkflowCreateHandler workflowCreateHandler = new WorkflowCreateHandler();
            String newRequestid=  workflowCreateHandler.WorkflowCreateByRequestMap(creatorId, workflowId, requestName, "0", requestMainMap);
            bs.writeLog("new requestid:"+newRequestid);

        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
