package weaver.interfaces.htsc.seal;

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

public class GeneralDeptCoDirectorNoticeAction 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();
        String workflowtype = requestInfo.getRequestManager().getWorkflowtype();
        String tablename = requestInfo.getRequestManager().getBillTableName();
        writeLog("----GeneralDeptCoDirectorNoticeAction-------" + requestid);
        try{
            String sql = "";
            String wfUnnumber = "";
            sql="select UNNUMBER from workflow_base where id = " + workflowid;
            writeLog(sql);
            rs.execute(sql);
            if(rs.next()){
                wfUnnumber = Util.null2String(rs.getString("UNNUMBER"));
            }
            if (StringUtils.isBlank(wfUnnumber)){
                writeLog("ERROR:nodeUnnumber is blank");
                return Action.FAILURE_AND_CONTINUE;
            }
            String bmfzr_node = pb.getPropName("bmfzr_node");
            JSONObject jsonObject = JSONObject.parseObject(bmfzr_node);
            String nodeUnnumber = jsonObject.get(wfUnnumber).toString();
            String nodeid = pb.getActionNodeIdByUnNumber(nodeUnnumber);

            //联席负责人
            String cohead = "";
            sql = "select t2.cohead,t1.* from "+tablename+" t1 left join hrmdepartmentdefined t2 on t1.szbm = t2.deptid where t1.requestid = '"+requestid+"'";
            writeLog(sql);
            rs.executeQuery(sql);
            if(rs.next()){
                cohead = Util.null2String(rs.getString("cohead"));
            }

            if(StringUtils.isNotEmpty(cohead)){
                for(String id:cohead.split(",")){
                    char flag = 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;
        }
    }
}
