package com.engine.htsc.gsfw.web;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import weaver.general.Util;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.hrm.company.DepartmentComInfo;
import weaver.hrm.resource.ResourceComInfo;
import weaver.interfaces.htsc.comInfo.PropBean;
import weaver.workflow.request.wfAgentCondition;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import java.util.*;

public class GsFwControlAction {

    /**
     * 查询人员待办
     * @param request
     * @param response
     * @return
     */
    @GET
    @Path("/getNextJudget")
    @Produces({MediaType.TEXT_PLAIN})
    public String getNextJudget(@Context HttpServletRequest request, @Context HttpServletResponse response){
        String requestid = Util.null2String(request.getParameter("requestid"));
        BaseBean bb = new BaseBean();
        PropBean pb = new PropBean();
//        String sxfxg = pb.getPropName("GSFW_SXFXG_NODEID");
//        String hgzj = pb.getPropName("GSFW_HGZJ_NODEID");

        String gsfw_sxfxg = pb.getPropName("gsfw_sxfxg");
        String sxfxg = pb.getNodeIdsByUnNumber(gsfw_sxfxg); //首席风险官

        String gsfw_hgzj = pb.getPropName("gsfw_hgzj");
        String hgzj = pb.getNodeIdsByUnNumber(gsfw_hgzj);

        bb.writeLog("sxfxg:"+sxfxg);
        bb.writeLog("hgzj:"+hgzj);
        RecordSet rs = new RecordSet();
        JSONObject json = new JSONObject();
        json.put("result","no");
        json.put("smesg","");
        if(!requestid.equals("-1"))
        {
            boolean fsFlag = false;
            boolean hgFlag = false;
            int fs_int = 0;
            String sql = " select logid from workflow_requestlog where requestid=" + requestid + " and nodeid in(" + sxfxg+")";
            bb.writeLog("sql:" + sql);
            rs.execute(sql);
            if (rs.next()) {
                fs_int++;
            }
            if (fs_int > 0) {
                fsFlag = true;
            } else {
                json.put("smesg", "请先送完首席风险官");
            }

            if (fsFlag)
            {
                int hg_int = 0;
                sql = " select logid from workflow_requestlog where requestid=" + requestid + " and nodeid in(" + hgzj +")";
                bb.writeLog("sql:" + sql);
                rs.execute(sql);
                if (rs.next()) {
                    hg_int++;
                }
                if (hg_int > 0) {
                    hgFlag = true;
                } else {
                    json.put("smesg", "请先送完合规总监");
                }
            }
            if (fsFlag && hgFlag) {
                json.put("result", "yes");
            }
        }
        return JSONObject.toJSONString(json);
    }

    /***
     * 公司发文(执行秘书)节点判断
     * @param request
     * @param response
     * @return
     */
    @GET
    @Path("/getNextIshq")
    @Produces({MediaType.TEXT_PLAIN})
    public String getNextIshq(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        String requestid = Util.null2String(request.getParameter("requestid"));
        RecordSet rs = new RecordSet();
        String whqgs = "0";

        JSONObject jsonObject = new JSONObject();
        if(!"".equals(requestid))
        {
            String formid = "";
            String tablename =  "formtable_main_";
            String sqltable =   " select abs(t.formid) as formid " +
                    " from workflow_base t " +
                    " inner join workflow_requestbase l on l.workflowid = t.id " +
                    " where l.requestid=" + requestid + "";
            rs.execute(sqltable);
            if (rs.next()) {
                formid = Util.null2String(rs.getString("formid"));
            }
            tablename += formid;
            String tabledetailname =  tablename + "_dt2";
            String sql = " select count(*) as detailsum from " + tabledetailname + " where mainid in (select id from " +  tablename+
                        " where requestid='" + requestid + "') " +
                        " and (yhq='0' or yhq is null)";
            rs.execute(sql);
            if (rs.next())
            {
                whqgs = Util.null2String(rs.getString("detailsum"));
            }
        }
        jsonObject.put("whqgs",whqgs);
        return jsonObject.toJSONString();

    }
    /***
     *
     * @param request
     * @param response
     * @return
     */
    @GET
    @Path("/getNextValue")
    @Produces({MediaType.TEXT_PLAIN})
    public String getNextValue(@Context HttpServletRequest request, @Context HttpServletResponse response){
        String requestid = Util.null2String( request.getParameter( "requestid" ) );
        int formid = Util.getIntValue(Util.null2String( request.getParameter("formid")));
        JSONObject json = new JSONObject();
//        List<String> valList = new ArrayList<String>();
//        valList.add("0");
//        valList.add("1");

        String sval = "0,1,7";
        BaseBean bb = new BaseBean();
        RecordSet rs = new RecordSet();
        if(!requestid.equals("-1")){
            //---获取所有参与审批人的部门---
            String alldept = "";
            String sql = " select distinct h.departmentid as departmentid from hrmresource h where h.id in(select r.userid from workflow_currentoperator r where  r.requestid='"+requestid+"')";
            rs.executeQuery( sql );
            while(rs.next()){
                alldept += alldept=="" ? Util.null2String(rs.getString("departmentid")) : ","+Util.null2String(rs.getString("departmentid"));
            }
            alldept = ","+alldept + ",";

            //---判断是否是债务融资部拟稿--
            if (alldept.indexOf(",670,") > -1)
            {
                String formtable = "" ;
                sql = " select 'formtable_main_'||abs(l.formid) as formtable from workflow_requestbase t\n" +
                        "inner join workflow_base l on l.id = t.workflowid\n" +
                        "where requestid="+requestid ;
                rs.execute(sql);
                if(rs.next()){
                    formtable = Util.null2String(rs.getString("formtable"));
                }
                String lx = "" ;
                if(!"".equals(formtable)){
                    sql = " select lx from "+formtable+" where requestid="+requestid ;
                    rs.execute(sql);
                    if(rs.next()){
                        lx = Util.null2String(rs.getString("lx"));
                    }
                }
                if("22".equals(lx)){
//                    valList.add("2");
                    sval = sval + ",2";
                }
            }

            List<String> list = new ArrayList<String>();
            String szbm = "";
            String hqbm = "";
            String formtable = "formtable_main_"+Math.abs(formid);
            sql = " select szbm,hqbm from "+ formtable+" where requestid="+requestid;
            rs.execute(sql);
            if(rs.next()){
                szbm = Util.null2String(rs.getString("szbm"));
                hqbm = Util.null2String(rs.getString("hqbm"));
            }

            bb.writeLog("szbm:"+szbm);
            if(!"".equals(szbm))
            {
                list.addAll(Arrays.asList(szbm.split(",")));
            }

            //---获取风险管理部门id、合规法律部id---
            String fs_deptid = "";
            String hg_deptid = "";
            sql = "select id,departmentcode from hrmdepartment where departmentcode in('ZZ323310','ZZ323311')";
            rs.executeQuery( sql );
            while(rs.next()){
                String code = Util.null2String(rs.getString("departmentcode"));
                if(code.equals("ZZ323310")){
                    fs_deptid = Util.null2String(rs.getString("id"));
                }else{
                    hg_deptid = Util.null2String(rs.getString("id"));
                }
            }

            if(list !=null && list.size() >0)
            {
                if(list.contains(fs_deptid) ){
//                    valList.add("3");
                    sval = sval+",3";
                }

                if(list.contains(hg_deptid) ){
//                    valList.add("4");
                    sval = sval+",4";
                }
            }

            List<String> hqList = new ArrayList<String>();
            if(!"".equals(hqbm))
            {
                sql = " select t.userid,h.departmentid from workflow_currentoperator t\n" +
                      " inner join hrmresource h on h.id = t.userid\n" +
                      " where nodeid < 0 and userid in( select id from hrmresource h where departmentid in("+hqbm+")) and requestid="+requestid;
                rs.execute(sql);
                while(rs.next()){
                    String departmentid = Util.null2String(rs.getString("departmentid"));
                    hqList.add(departmentid);
                }
            }

            if(hqList !=null && hqList.size() >0)
            {
                if(hqList.contains(fs_deptid)){
                    if(!sval.contains("3")){
//                        valList.add("3");
                        sval = sval+",3";
                    }
                }

                if(hqList.contains(hg_deptid)){
                    if(!sval.contains("4")){
//                        valList.add("4");
                        sval = sval+",4";
                    }
                }
            }


//		//---判断是计划财务部、资金运营部参与审批---
            String jhcwbid = "";
            String zjyybid = "";
            sql = "select id,departmentcode from hrmdepartment where departmentcode in('ZZ001016','ZZ323314')"; //计划财务部、资金运营部
            rs.executeQuery( sql );
            while(rs.next()){
                String code = Util.null2String(rs.getString("departmentcode"));
                if(code.equals("ZZ001016")){
                    jhcwbid = Util.null2String(rs.getString("id"));
                }else{
                    zjyybid = Util.null2String(rs.getString("id"));
                }
            }

            if(list !=null && list.size() >0)
            {
                boolean bool = false;
                if(list.contains(jhcwbid) || list.contains(zjyybid) ){
                    bool = true;
                }
                if(bool)
                {
//                    valList.add("5");
                    sval = sval+",5";
                }
            }

            if(hqList !=null && hqList.size() >0)
            {
                if(hqList.contains(jhcwbid) || hqList.contains(zjyybid)){
                    if(!sval.contains("5")){
//                        valList.add("5");
                        sval = sval+",5";
                    }
                }
            }

//            int sfty = 0;
//            sql = " select * from (\n" +
//                    "select remark,row_number() over(partition by nodeid order by logid desc) as xh\n" +
//                    " from workflow_requestlog\n" +
//                    "where requestid = " + requestid +
//                    "and nodeid in(select id from workflow_nodebase \n" +
//                    "where unnumber in('gsfw_sxcwg','gsfw_hgzj','gsfw_sxfxg'))\n" +
//                    ") l where l.xh=1 and remark like '不同意%' ";
//
//            rs.execute(sql);
//            if(rs.next()){
//                sfty++;
//            }
//            if(sfty == 0){
//                valList.add("6");
//                sval = sval+",6";
//            }
            sval = sval+",6";

        }
        json.put("result",sval);
        return JSONObject.toJSONString(json);
    }

    @GET
    @Path("/judgeBgszrApprove")
    @Produces({MediaType.TEXT_PLAIN})
    public String judgeBgszrApprove(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        String requestid = Util.null2String( request.getParameter( "requestid" ) );
        RecordSet rs = new RecordSet();
        PropBean pb = new PropBean();
        JSONObject json = new JSONObject();
        json.put("result","no");

        try{
            if(!requestid.equals("-1"))
            {
                boolean fsFlag = false;

                //---公司发文办公室主任审核节点id---
//            String gsfw_bgszr_nodeid  = Util.null2String(pb.getPropName("GSFW_BGSZR_NODEID"));
//            String sql = "select distinct nodeid as nodes from workflow_currentoperator where requestid='"+requestid+"'";
//            rs.executeQuery( sql );
//            while(rs.next()){
//                String nodes = Util.null2String(rs.getString("nodes"));
//                if(nodes.equals(gsfw_bgszr_nodeid)){
//                    fsFlag = true;
//                    break;
//                }
//            }

                String gsfw_bgszrsh  = Util.null2String(pb.getPropName("gsfw_bgszrsh"));
                String gsfw_bgszr_nodeid = pb.getNodeIdsByUnNumber(gsfw_bgszrsh);

                String sql = " select id from workflow_currentoperator where requestid='"+requestid+"' and nodeid in("+gsfw_bgszr_nodeid+")";
                rs.executeQuery( sql );
                if(rs.next()){
                    fsFlag = true;
                }

                if(fsFlag){
                    json.put("result","yes");
                }
            }
        }catch (Exception e){

        }
        return JSONObject.toJSONString(json);
    }


    @GET
    @Path("/getldqf2")
    @Produces({MediaType.TEXT_PLAIN})
    public String getldqf2(@Context HttpServletRequest request, @Context HttpServletResponse response) {

        BaseBean bb = new BaseBean();
        RecordSet rs = new RecordSet();
        DepartmentComInfo departmentComInfo = null;
        ResourceComInfo resourceComInfo = null;
        JSONObject jsonObject = new JSONObject();
        try{
            departmentComInfo = new DepartmentComInfo();
            resourceComInfo = new ResourceComInfo();

            String szbm = Util.null2String(request.getParameter("szbm"));
            String szbmcode = departmentComInfo.getDepartmentCode(szbm);

            String excludeId = "ZZ001014,ZZ323472" ; //信息技术部 , 数字化运营部
            String definUserCode = "001008";  //周易
            String definUserid = "";
            String sql = " select id from hrmresource where loginid='"+definUserCode+"'";
            rs.execute(sql);
            if(rs.next()){
                definUserid = Util.null2String(rs.getString("id"));
            }
            String definUsername = resourceComInfo.getLastname(definUserid);
            if((","+excludeId+",").contains(","+szbmcode+","))
            {
                jsonObject.put("ldqf2value",definUserid);
                jsonObject.put("ldqf2name",definUsername);

                String deptleaders = "";
                sql = " select deptleaders from hrmdepartmentdefined where deptid = "+szbm;
                rs.execute(sql);
                if(rs.next()){
                    deptleaders = Util.null2String(rs.getString("deptleaders"));
                }
                if(deptleaders.indexOf(",")>-1){
                    deptleaders = deptleaders.substring(0,deptleaders.indexOf(","));
                }
                String deptleadersname = "";
                if(!"".equals(deptleaders)){
                    deptleadersname = resourceComInfo.getLastname(deptleaders);
                }
                jsonObject.put("ldsp2value",deptleaders);
                jsonObject.put("ldsp2name",deptleadersname);

            }else{
                String deptleaders = "";
                sql = " select deptleaders from hrmdepartmentdefined where deptid = "+szbm;
                rs.execute(sql);
                if(rs.next()){
                    deptleaders = Util.null2String(rs.getString("deptleaders"));
                }
                if(deptleaders.indexOf(",")>-1){
                    deptleaders = deptleaders.substring(0,deptleaders.indexOf(","));
                }
                String deptleadersname = "";
                if(!"".equals(deptleaders)){
                    deptleadersname = resourceComInfo.getLastname(deptleaders);
                }

                jsonObject.put("ldqf2value",deptleaders);
                jsonObject.put("ldqf2name",deptleadersname);

                jsonObject.put("ldsp2value","");
                jsonObject.put("ldsp2name","");
            }
        }catch (Exception e){
            bb.writeLog("e:"+e);
        }
        return JSONObject.toJSONString(jsonObject);
    }


    @GET
    @Path("/getldsp2")
    @Produces({MediaType.TEXT_PLAIN})
    public String getldsp2(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        BaseBean bb = new BaseBean();
        RecordSet rs = new RecordSet();
        ResourceComInfo resourceComInfo = null;
        Map<String,Object> dataMap = new HashMap<String,Object>();
        try{
            resourceComInfo = new ResourceComInfo();
            String yz = Util.null2String(request.getParameter("yz"));
            String ldsp2 = Util.null2String(request.getParameter("ldsp2"));
            int bool = 0;
            String[] yzs = yz.split(",");
            for(int i=0;i<yzs.length;i++)
            {
                if("3".equals(yzs[i]))
                {
                    String definUserCode = "015150";
                    String definUserid = "20605";
                    String sql = " select id from hrmresource where loginid='"+definUserCode+"'";
                    rs.execute(sql);
                    if(rs.next()){
                        definUserid = Util.null2String(rs.getString("id"),"20605");
                    }
                    if(!(","+ldsp2+",").contains(","+definUserid+",")){
                        ldsp2 += ldsp2 == "" ? definUserid : ","+definUserid;
                    }
                    bool++ ;
                }
            }

            if(bool>0)
            {
                List<String> speciaList = Arrays.asList(ldsp2.split(","));
                bb.writeLog("speciaList:"+speciaList.size());
                String values = "";
                JSONArray speciaArray = new JSONArray();
                for(int i=0;i<speciaList.size();i++)
                {
                    JSONObject leaderObject = new JSONObject();
                    String id = speciaList.get(i);
                    leaderObject.put("id",id);
                    leaderObject.put("name",resourceComInfo.getLastname(speciaList.get(i)));
                    speciaArray.add(leaderObject);
                    values += values=="" ? id : ","+id ;
                }
                dataMap.put("specialobj",speciaArray);
                dataMap.put("value",values);
            }
        }catch (Exception e){
            bb.writeLog("e:"+e);
        }
        return net.sf.json.JSONObject.fromObject(dataMap).toString();
    }


    @GET
    @Path("/getwzlx")
    @Produces({MediaType.TEXT_PLAIN})
    public String getwzlx(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        BaseBean bb = new BaseBean();
        RecordSet rs = new RecordSet();
        ResourceComInfo resourceComInfo = null;
        DepartmentComInfo departmentComInfo = null;
        JSONObject dataJson = new JSONObject();
        try{
            departmentComInfo = new DepartmentComInfo();
            resourceComInfo = new ResourceComInfo();

            String lx = Util.null2String( request.getParameter("lx") );//申报类型
            int requestid = Util.getIntValue(Util.null2String(request.getParameter("requestid")));//申报类型
            String deptid = Util.null2String( request.getParameter("szbm"));//部门ID

            bb.writeLog("requestid:"+requestid);
            bb.writeLog("lx:"+lx);
            String qzsydx ="";
            String gd = "";

            String qzsyry ="";
            String bjhqbm ="";
            String sql = " select qzsyry,bjhqbm,qzsydx from uf_gsfwsyfw where lx="+lx ;
            bb.writeLog("sql:"+sql);
            rs.executeQuery( sql );
            if(rs.next()){
                qzsyry = Util.null2String(rs.getString("qzsyry"));
                bjhqbm =Util.null2String(rs.getString("bjhqbm"));
                qzsydx =Util.null2String(rs.getString("qzsydx"));
            }

            List<String> hqbmArry = Arrays.asList(bjhqbm.split(","));
            JSONArray hqbmJsonArray = new JSONArray();
            for (String hqbm2 : hqbmArry)
            {
                if(!hqbm2.equals(deptid)){
                    JSONObject jsonObject = new JSONObject();
                    String hqbmname = departmentComInfo.getDepartmentName(hqbm2);//必经会签部门名称
                    jsonObject.put( "id", hqbm2 );
                    jsonObject.put( "name", hqbmname );
                    hqbmJsonArray.add( jsonObject );
                }
            }

            List<String> tzyddxArry = Arrays.asList(qzsyry.split(","));
            JSONArray tzyddxJsonArray = new JSONArray();
            for (String tzyddx2 : tzyddxArry)
            {
                JSONObject jsonObject = new JSONObject();
                String tzyddxname = resourceComInfo.getLastname(tzyddx2);//强制送阅人员
                jsonObject.put( "id", tzyddx2 );
                jsonObject.put( "name", tzyddxname );
                tzyddxJsonArray.add( jsonObject );
                if(gd.equals("")){
                    gd = "hrm_"+tzyddx2;
                }else{
                    gd = gd + ",hrm_"+tzyddx2;
                }
            }

            if(!qzsydx.equals("")){
                if(gd.equals("")){
                    gd = "dept_"+qzsydx;
                }else{
                    gd = gd + ",dept_"+qzsydx;
                }
            }

            dataJson.put("hqbm",bjhqbm);
            dataJson.put("hqbmspec",hqbmJsonArray);
            dataJson.put("hqbm_gd",bjhqbm);

            dataJson.put("tzyddx",qzsyry);
            dataJson.put("tzyddxspec",tzyddxJsonArray);
            dataJson.put("tzyddx_gd",gd);

        }catch (Exception e){
            bb.writeLog("e:"+e);
        }
        return dataJson.toJSONString();
    }


    @GET
    @Path("/getbeagent")
    @Produces({MediaType.TEXT_PLAIN})
    public String getBeagent(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        BaseBean bb = new BaseBean();
        JSONObject json = new JSONObject();
        ResourceComInfo resourceComInfo = null;
        try{
            resourceComInfo = new ResourceComInfo();
            String requestid = Util.null2String(request.getParameter("requestid"));
            String workflowid = Util.null2String(request.getParameter("workflowid"));
            String operatorid = Util.null2String(request.getParameter("operatorid"));
            bb.writeLog("requestid:"+requestid);
            bb.writeLog("workflowid:"+workflowid);
            bb.writeLog("operatorid:"+operatorid);

            wfAgentCondition wfAgentCondition = new wfAgentCondition();
            JSONArray jsonArray = new JSONArray();
            Map<String,String> map =  wfAgentCondition.getAgentInfoByResouce(workflowid,operatorid,requestid);
            for(Map.Entry<String, String> entry : map.entrySet())
            {
                String key = entry.getKey();
                String value = entry.getValue();
                String name = "" ;
                if(value !="")
                {
                    name = resourceComInfo.getLastname(value);
                }
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("operatorid",key);
                jsonObject.put("beagentid",value);
                jsonObject.put("beagentname",name);
                jsonArray.add(jsonObject);
            }
            json.put("data",jsonArray);
        }catch (Exception e){
            bb.writeLog("e:"+e);
        }
        return json.toJSONString();
    }


    @GET
    @Path("/getszwrznh")
    @Produces({MediaType.TEXT_PLAIN})
    public String getSzWrznh(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        BaseBean bb = new BaseBean();
        RecordSet rs = new RecordSet();
        JSONObject dataJson = new JSONObject();
        try{
            String hgflbnbsh = Util.null2String( request.getParameter("hgflbnbsh") );//合规法律部内部审核
            int requestid = Util.getIntValue(Util.null2String(request.getParameter("requestid")));//流程requestid
            String logid ="";
            if(requestid >0)
            {
                String sql = " select logid from workflow_requestlog where requestid="+requestid+" and nodeid in("+hgflbnbsh+") and logtype=0" ;
                bb.writeLog("sql:"+sql);
                rs.executeQuery(sql);
                if(rs.next())
                {
                    logid = Util.null2String(rs.getString("logid"));
                }
            }
            dataJson.put("logid",logid);
        }catch (Exception e){
            bb.writeLog("e:"+e);
        }
        return dataJson.toJSONString();
    }



    @GET
    @Path("/getNextValueForLx")
    @Produces({MediaType.TEXT_PLAIN})
    public String getNextValueForLx(@Context HttpServletRequest request, @Context HttpServletResponse response) {

        RecordSet rs = new RecordSet();
        BaseBean bb = new BaseBean();
        String requestid = Util.null2String( request.getParameter( "requestid" ) );
        int formid = Util.getIntValue(Util.null2String( request.getParameter("formid")));
        String lx = Util.null2String( request.getParameter( "lx" ) );
        JSONObject json = new JSONObject();
//        List<String> valList = new ArrayList<String>();
//        valList.add("0");
//        valList.add("1");

        String sval = "0,1";
        if(!requestid.equals("-1"))
        {
            if("".equals(lx))
            {
                //---获取所有参与审批人的部门---
                String alldept = "";
                String sql = " select distinct h.departmentid as departmentid from hrmresource h where h.id in(select r.userid from workflow_currentoperator r where  r.requestid='"+requestid+"')";
                rs.executeQuery( sql );
                while(rs.next()){
                    alldept += alldept=="" ? Util.null2String(rs.getString("departmentid")) : ","+Util.null2String(rs.getString("departmentid"));
                }
                alldept = ","+alldept + ",";

                //---判断是否是债务融资部拟稿--
                if (alldept.indexOf(",670,") > -1)
                {
                    String formtable = "" ;
                    sql = " select 'formtable_main_'||abs(l.formid) as formtable from workflow_requestbase t\n" +
                            "inner join workflow_base l on l.id = t.workflowid\n" +
                            "where requestid="+requestid ;
                    rs.execute(sql);
                    if(rs.next()){
                        formtable = Util.null2String(rs.getString("formtable"));
                    }
                    if(!"".equals(formtable)){
                        sql = " select lx from "+formtable+" where requestid="+requestid ;
                        rs.execute(sql);
                        if(rs.next()){
                            lx = Util.null2String(rs.getString("lx"));
                        }
                    }
                }
            }
            if("22".equals(lx))
            {
//                valList.add("2");
                sval = sval + ",2";
            }

            List<String> list = new ArrayList<String>();
            String szbm = "";
            String hqbm = "";
            String formtable = "formtable_main_"+Math.abs(formid);
            String sql = " select szbm,hqbm from "+ formtable+" where requestid="+requestid;
            rs.execute(sql);
            if(rs.next()){
                szbm = Util.null2String(rs.getString("szbm"));
                hqbm = Util.null2String(rs.getString("hqbm"));
            }

            bb.writeLog("szbm:"+szbm);
            if(!"".equals(szbm))
            {
                list.addAll(Arrays.asList(szbm.split(",")));
            }

            //---获取风险管理部门id、合规法律部id---
            String fs_deptid = "";
            String hg_deptid = "";
            sql = "select id,departmentcode from hrmdepartment where departmentcode in('ZZ323310','ZZ323311')";
            rs.executeQuery( sql );
            while(rs.next()){
                String code = Util.null2String(rs.getString("departmentcode"));
                if(code.equals("ZZ323310")){
                    fs_deptid = Util.null2String(rs.getString("id"));
                }else{
                    hg_deptid = Util.null2String(rs.getString("id"));
                }
            }

            if(list !=null && list.size() >0)
            {
                if(list.contains(fs_deptid) ){
//                    valList.add("3");
                    sval = sval+",3";
                }

                if(list.contains(hg_deptid) ){
//                    valList.add("4");
                    sval = sval+",4";
                }
            }

            List<String> hqList = new ArrayList<String>();
            if(!"".equals(hqbm))
            {
                sql = " select t.userid,h.departmentid from workflow_currentoperator t\n" +
                        " inner join hrmresource h on h.id = t.userid\n" +
                        " where nodeid < 0 and userid in( select id from hrmresource h where departmentid in("+hqbm+")) and requestid="+requestid;
                rs.execute(sql);
                while(rs.next()){
                    String departmentid = Util.null2String(rs.getString("departmentid"));
                    hqList.add(departmentid);
                }
            }

            if(hqList !=null && hqList.size() >0)
            {
                if(hqList.contains(fs_deptid)){
                    if(!sval.contains("3")){
//                        valList.add("3");
                        sval = sval+",3";
                    }
                }

                if(hqList.contains(hg_deptid)){
                    if(!sval.contains("4")){
//                        valList.add("4");
                        sval = sval+",4";
                    }
                }
            }


//		//---判断是计划财务部、资金运营部参与审批---
            String jhcwbid = "";
            String zjyybid = "";
            sql = "select id,departmentcode from hrmdepartment where departmentcode in('ZZ001016','ZZ323314')"; //计划财务部、资金运营部
            rs.executeQuery( sql );
            while(rs.next()){
                String code = Util.null2String(rs.getString("departmentcode"));
                if(code.equals("ZZ001016")){
                    jhcwbid = Util.null2String(rs.getString("id"));
                }else{
                    zjyybid = Util.null2String(rs.getString("id"));
                }
            }

            if(list !=null && list.size() >0)
            {
                boolean bool = false;
                if(list.contains(jhcwbid) || list.contains(zjyybid) ){
                    bool = true;
                }
                if(bool)
                {
//                    valList.add("5");
                    sval = sval+",5";
                }
            }

            if(hqList !=null && hqList.size() >0)
            {
                if(hqList.contains(jhcwbid) || hqList.contains(zjyybid)){
                    if(!sval.contains("5")){
//                        valList.add("5");
                        sval = sval+",5";
                    }
                }
            }

//            int sfty = 0;
//            sql = " select * from (\n" +
//                    "select remark,row_number() over(partition by nodeid order by logid desc) as xh\n" +
//                    " from workflow_requestlog\n" +
//                    "where requestid = " + requestid +
//                    "and nodeid in(select id from workflow_nodebase \n" +
//                    "where unnumber in('gsfw_sxcwg','gsfw_hgzj','gsfw_sxfxg'))\n" +
//                    ") l where l.xh=1 and remark like '不同意%' ";
//
//            rs.execute(sql);
//            if(rs.next()){
//                sfty++;
//            }
//            if(sfty == 0){
//                valList.add("6");
//                sval = sval+",6";
//            }
            sval = sval+",6";
        }
        json.put("result",sval);
        return JSONObject.toJSONString(json);
    }


    @GET
    @Path("/getJudgeSendZxms")
    @Produces({MediaType.TEXT_PLAIN})
    public String getJudgeSendZxms(@Context HttpServletRequest request, @Context HttpServletResponse response) {

        RecordSet rs = new RecordSet();
        BaseBean bb = new BaseBean();
        String requestid = Util.null2String(request.getParameter("requestid"));
        int sfty = 0;
        String nodenames = "" ;
        if(!"".equals(requestid)){
            String sql =" select l.*,p.nodename from (\n" +
                    "   select nodeid,logtype,row_number() over(partition by nodeid order by logid desc) as xh\n" +
                    "   from workflow_requestlog t\n" +
                    "   where requestid =" + requestid+
                    "   and nodeid in(select id from workflow_nodebase \n" +
                    "   where unnumber in('gsfw_sxcwg','gsfw_hgzj','gsfw_sxfxg'))\n" +
                    " ) l \n" +
                    " left join workflow_nodebase p on p.id = l.nodeid\n" +
                    " where l.xh=1 and logtype = 3 ";


            rs.execute(sql);
            while(rs.next()){
                nodenames += nodenames=="" ? Util.null2String(rs.getString("nodename")) : "、"+Util.null2String(rs.getString("nodename")) ;
                sfty++;
            }
        }
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("sfty",sfty);
        jsonObject.put("nodenames",nodenames);

        return JSONObject.toJSONString(jsonObject);
    }



}
