package weaver.interfaces.htsc.seal;

import com.alibaba.fastjson.JSONObject;
import com.weaver.general.BaseBean;
import com.weaver.general.TimeUtil;
import org.apache.commons.lang.StringUtils;
import weaver.conn.RecordSet;
import weaver.general.Util;
import weaver.interfaces.htsc.comInfo.PropBean;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;

import java.util.ArrayList;
import java.util.List;

public class GeneralNoticeSignTestAction extends BaseBean implements Action {

    @Override
    public String execute(RequestInfo requestInfo) {
        RecordSet rs = new RecordSet();
        PropBean pb = new PropBean();
        String requestid = requestInfo.getRequestid();
        int workflowid = requestInfo.getRequestManager().getWorkflowid();
        //int nodeid = requestInfo.getRequestManager().getNodeid();
        String workflowtype = requestInfo.getRequestManager().getWorkflowtype();
        String tablename = requestInfo.getRequestManager().getBillTableName();
        writeLog("----GeneralNoticeSignAction-------" + requestid);
        try{

            String sql = "";
            int nodeid = 0;

            sql = "select NOWNODEID from workflow_nownode where REQUESTID = "+requestid;
            writeLog(sql);
            rs.executeQuery(sql);
            if(rs.next()){
                nodeid = Integer.parseInt(Util.null2String(rs.getString("NOWNODEID")));
            }
            writeLog("nodeid --> "+nodeid);

            List<String> hqbmList = new ArrayList<>();
            String dt2 = tablename + "_dt2";
            sql = "select d.HQBM from "+dt2+" d inner join "+tablename+" t \n" +
                    " on d.MAINID = t.ID  \n" +
                    "where t.REQUESTID = '"+requestid+"' and d.SFHQ = 1";
            writeLog(sql);
            rs.executeQuery(sql);
            while(rs.next()){
                hqbmList.add(Util.null2String(rs.getString("hqbm")));
            }
            writeLog("hqbmList --> "+hqbmList);

            if(null != hqbmList && hqbmList.size() != 0){
                for(String deptid:hqbmList){
                    //联席负责人
                    String cohead = "";
                    sql = "select cohead from hrmdepartmentdefined where deptid = '"+deptid+"'";
                    writeLog(sql);
                    rs.executeQuery(sql);
                    if(rs.next()){
                        cohead = Util.null2String(rs.getString("cohead"));
                    }
                    writeLog("cohead --> "+cohead);
                    if(StringUtils.isNotEmpty(cohead)){
                        for(String id:cohead.split(",")){
                            char flag = com.weaver.general.Util.getSeparator();
                            String operatorgroup = "0";
                            String creatertype = "0";
                            String currentdate = TimeUtil.getCurrentDateString();
                            String currenttime =  TimeUtil.getCurrentTimeString().substring(11,19);
                            String isremarktemp = "9";
                            String lastnodeid = ""+nodeid;
                            String groupdetailid = "-1";
                            String useridtemp = id;//抄送人
                            String Procparatemp = "" + requestid + flag + useridtemp + flag + operatorgroup + flag + workflowid + flag
                                    + workflowtype + flag + creatertype + flag + isremarktemp + flag + lastnodeid + flag + -1
                                    + flag + "0" + flag + -1 + flag + groupdetailid+flag+currentdate+flag+currenttime;
                            writeLog("chaosong Procparatemp====="+Procparatemp);
                            boolean executesuccess = rs.executeProc("workflow_CurrentOperator_I2", Procparatemp);

                            if(executesuccess){
                                writeLog("CC SUCCESS:"+executesuccess);
                            }else{
                                writeLog("CC FAIL:"+executesuccess);
                            }
                        }
                    }
                }
            }

            return Action.SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            writeLog( "异常参考:" + e );
            requestInfo.getRequestManager().setMessageid( "111111" );
            requestInfo.getRequestManager().setMessagecontent( "流程提交失败" );
            return Action.FAILURE_AND_CONTINUE;
        }
    }
}
