package com.engine.htsc.workflow.web;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.engine.htsc.workflow.util.WorkflowUtil;
import org.apache.commons.lang3.StringUtils;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.hrm.HrmUserVarify;
import weaver.hrm.User;
import weaver.hrm.company.DepartmentComInfo;
import weaver.hrm.resource.ResourceComInfo;
import weaver.interfaces.htsc.comInfo.PropBean;
import weaver.mobile.webservices.workflow.WorkflowServiceUtil;
import weaver.system.RequestDefaultComInfo;
import weaver.workflow.workflow.WorkflowConfigComInfo;
import weaver.workflow.workflow.WorkflowNodeComInfo;

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 WorkflowBaseInfoAction {


    /**
     * 查询流程基本信息
     *
     * @param request
     * @param response
     * @return
     */
    @GET
    @Path("/getWorkflowBaseInfo")
    @Produces({MediaType.TEXT_PLAIN})
    public String getWorkflowBaseInfo(@Context HttpServletRequest request, @Context HttpServletResponse response) {

        Map<String, Object> apidatas = new HashMap<String, Object>();
        //获取用户
        String requestids = Util.null2String(request.getParameter("requestids"));
        List<Map<String, String>> dataList = new ArrayList<Map<String, String>>();
        if ("".equals(requestids)) {
            apidatas.put("datas", dataList);
            apidatas.put("success", true);
            return JSONObject.toJSONString(apidatas);
        }

        try {
            String dataSql = " select * from workflow_requestbase where requestid in (" + requestids + ") ";
            RecordSet rs = new RecordSet();
            rs.execute(dataSql);
            while (rs.next()) {
                Map<String, String> item = new HashMap();
                String requestid = Util.null2String(rs.getString("requestid"));
                String workflowid = Util.null2String(rs.getString("workflowid"));
                String requestname = Util.null2String(rs.getString("requestname"));
                String requestnamenew = Util.null2String(rs.getString("requestnamenew"));
                String requestmark = Util.null2String(rs.getString("requestmark"));
                String requestlevel = Util.null2String(rs.getString("requestlevel"));
                String creater = Util.null2String(rs.getString("requestid"));
                String createdate = Util.null2String(rs.getString("createdate"));
                String createtime = Util.null2String(rs.getString("createtime"));

                item.put("requestid", requestid);
                item.put("workflowid", workflowid);
                item.put("requestname", requestname);
                item.put("requestnamenew", requestnamenew);
                item.put("requestmark", requestmark);
                item.put("requestlevel", requestlevel);
                item.put("creater", creater);
                item.put("createdate", createdate);
                item.put("createtime", createtime);

                dataList.add(item);
            }
            apidatas.put("datas", dataList);
            apidatas.put("success", true);
        } catch (Exception e) {
            e.printStackTrace();
            apidatas.put("success", false);
        }
        return JSONObject.toJSONString(apidatas);
    }


    /**
     * 查询流程节点基本信息
     *
     * @param request
     * @param response
     * @return
     */
    @GET
    @Path("/getNodeBaseInfo")
    @Produces({MediaType.TEXT_PLAIN})
    public String getNodeBaseInfo(@Context HttpServletRequest request, @Context HttpServletResponse response) {

        Map<String, Object> apidatas = new HashMap<String, Object>();
        //获取用户
        User user = HrmUserVarify.getUser(request, response);
        String requestid = request.getParameter("requestid");
        int nodeid = Util.getIntValue(request.getParameter("nodeid"));
        String nodetype = Util.null2String(request.getParameter("nodetype"));
        String creater = Util.null2String(request.getParameter("creater"));
        String f_weaver_belongto_userid = Util.null2String(request.getParameter("f_weaver_belongto_userid"));
        if ("0".equals(nodetype)) {
            creater = f_weaver_belongto_userid;
        }


        try {
            ResourceComInfo resourceComInfo = new ResourceComInfo();
            Map<String,Object> dataMap = new HashMap<String,Object>();
            //拟稿人的安全级别
            dataMap.put("seclevel",resourceComInfo.getSeclevel(creater));

            //获取流程搁置状态
            int lcdqzt = WorkflowUtil.getShelveStatus(requestid, user);
            dataMap.put("shelveStatus", lcdqzt);

            //获取节点是否是指定处理人
            boolean isAssignhandler = WorkflowUtil.getAssignhandler(requestid, nodeid);
            dataMap.put("isAssignhandler", isAssignhandler);

            //获取流程终止状态
            String isstopped = WorkflowUtil.getWorkflowIsStop(requestid);
            dataMap.put("isstopped", isstopped);

            //获取发起人相关信息
            Map<String,Object> userInfoMap = WorkflowUtil.getWfCreateUserInfo(creater);
            dataMap.put("tlevel",userInfoMap.get("tlevel"));
            dataMap.put("deptLevelCode",userInfoMap.get("deptLevelCode"));
            dataMap.put("deptTypeCode",userInfoMap.get("deptTypeCode"));
            dataMap.put("parentSaleCode",userInfoMap.get("parentsalecode"));
            dataMap.put("deptDirectors",userInfoMap.get("deptDirectors"));


            apidatas.put("datas", dataMap);
            apidatas.put("success", true);
        } catch (Exception e) {
            e.printStackTrace();
            apidatas.put("success", false);
        }
        return JSONObject.toJSONString(apidatas);
    }

    /**
     * 查询拟稿人相关信息
     * @param request
     * @param response
     * @return
     */
    @GET
    @Path("/getDraftUserInfo")
    @Produces({MediaType.TEXT_PLAIN})
    public String getDraftUserInfo(@Context HttpServletRequest request, @Context HttpServletResponse response){

        Map<String, Object> apidatas = new HashMap<String, Object>();
        int ngr = Util.getIntValue(request.getParameter("ngr"));
        Map<String, Object> draftUserInfo = new HashMap<String, Object>();

        try {
            if(ngr>0) {
                RecordSet rs = new RecordSet();
                String sql = " select a.id,a.lastname,seclevel,departmentid,departmentname,supdepid,deptDepth,deptDirectors,deptLeaders " +
                        " from hrmresource a, hrmdepartment b,hrmdepartmentdefined c " +
                        " where a.departmentid=b.id and b.id=c.deptid and a.id=? ";
                rs.executeQuery(sql,ngr);
                if(rs.next()){
                    String userid = Util.null2String(rs.getString("id"));
                    String lastname = Util.null2String(rs.getString("lastname"));
                    String departmentid = Util.null2String(rs.getString("departmentid"));
                    String departmentname = Util.null2String(rs.getString("departmentname"));
                    String seclevel = Util.null2String(rs.getString("seclevel"));
                    String supdepid = Util.null2String(rs.getString("supdepid"));
                    String deptDepth = Util.null2String(rs.getString("deptDepth"));
                    String deptDirectors = Util.null2String(rs.getString("deptDirectors"));
                    List directUserList = WorkflowUtil.getUserInfo(deptDirectors);
                    String deptLeaders = Util.null2String(rs.getString("deptLeaders"));
                    List leaderList = WorkflowUtil.getUserInfo(deptLeaders);

                    draftUserInfo.put("userid",userid);
                    draftUserInfo.put("lastname",lastname);
                    draftUserInfo.put("seclevel",seclevel);
                    draftUserInfo.put("departmentid",departmentid);
                    draftUserInfo.put("departmentname",departmentname);
                    draftUserInfo.put("deptDepth",deptDepth);
                    draftUserInfo.put("deptLeaders",leaderList);

                    if("1".equals(deptDepth)){
                        draftUserInfo.put("deptDirectors",directUserList);
                    }
                    if("2".equals(deptDepth)){//二级部门
                        draftUserInfo.put("secDeptDirectors",directUserList);
                        rs.executeQuery(" select deptDirectors from hrmdepartmentdefined where deptid=?",supdepid);
                        rs.next();
                        deptDirectors = Util.null2String(rs.getString("deptDirectors"));
                        directUserList = WorkflowUtil.getUserInfo(deptDirectors);
                        draftUserInfo.put("deptDirectors",directUserList);
                    }
                }
            }
            apidatas.put("datas",draftUserInfo);
            apidatas.put("success", true);

        } catch (Exception e) {
            e.printStackTrace();
            apidatas.put("success", false);
        }

        return JSONObject.toJSONString(apidatas);
    }


    /**
     * 查询流程适用范围对应的数据链接地址
     *
     * @param request
     * @param response
     * @return
     */
    @GET
    @Path("/getUseScopeUrl")
    @Produces({MediaType.TEXT_PLAIN})
    public String getUseScopeUrl(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        Map<String, Object> apidatas = new HashMap<String, Object>();
        String wfunnumber = Util.null2String(request.getParameter("wfunnumber"));
        try {
            String dataSql = "  select * from uf_syfwpp  where lcxx=? ";
            RecordSet rs = new RecordSet();
            rs.executeQuery(dataSql, new Object[]{wfunnumber});
            if (rs.next()) {
                String linkUrl = Util.null2String(rs.getString("syfwdz"));
                apidatas.put("wfunnumber", wfunnumber);
                apidatas.put("linkUrl", linkUrl);
            }
            apidatas.put("success", true);
        } catch (Exception e) {
            e.printStackTrace();
            apidatas.put("success", false);
        }
        return JSONObject.toJSONString(apidatas);
    }


    /**
     * 查询流程未操作者
     *
     * @param request
     * @param response
     * @return
     */
    @GET
    @Path("/getUnoperators")
    @Produces({MediaType.TEXT_PLAIN})
    public String getUnoperators(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        Map<String, Object> apidatas = new HashMap<String, Object>();
        String requestid = Util.null2String(request.getParameter("requestid"));
        String userid = Util.null2String(request.getParameter("userid"));
        WorkflowUtil workflowUtil = new WorkflowUtil();
        RecordSet rs = new RecordSet();
        String allRequestids[] = requestid.split(",");
        List<Map<String, String>> dataList = new ArrayList<>();
        try {
            for (int i = 0; i < allRequestids.length; i++) {
                if (!"".equals(allRequestids[i])) {
                    Map<String, String> item = new HashMap<>();
                    String unoperators = "";
                    String dataSql = "  select * from workflow_requestbase where requestid=? or mainrequestid=? ";
                    rs.executeQuery(dataSql, new Object[]{allRequestids[i], allRequestids[i]});
                    while (rs.next()) {
                        String reqid = Util.null2String(rs.getString("requestid"));
                        unoperators += workflowUtil.getUnoperators(reqid, userid) + " ";
                    }
                    //去除子流程的重复数据
                    List list = Arrays.asList(unoperators.split(" "));
                    Set set = new HashSet(list);
                    unoperators = StringUtils.join(set.toArray(), " ");
                    item.put(allRequestids[i], unoperators);
                    dataList.add(item);
                }

            }
            apidatas.put("unoperators", dataList);
            apidatas.put("success", true);
        } catch (Exception e) {
            e.printStackTrace();
            apidatas.put("success", false);
        }
        return JSONObject.toJSONString(apidatas);
    }


    /**
     * 查询流程当前状态
     *
     * @param request
     * @param response
     * @return
     */
    @GET
    @Path("/getWfStatus")
    @Produces({MediaType.TEXT_PLAIN})
    public String getWfStatus(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        Map<String, Object> apidatas = new HashMap<String, Object>();
        String requestid = Util.null2String(request.getParameter("requestid"));
        String allRequestids[] = requestid.split(",");
        List<Map<String, String>> dataList = new ArrayList<>();
        RecordSet rs = new RecordSet();
        try {
            for (int i = 0; i < allRequestids.length; i++) {
                if (!"".equals(allRequestids[i])) {
                    Map<String, String> item = new HashMap<>();
                    String wfstatus = "";
                    String dataSql = "  select currentnodetype,isstopped from workflow_requestbase where requestid=? ";
                    rs.executeQuery(dataSql, new Object[]{allRequestids[i]});
                    if (rs.next()) {
                        String currentnodetype = Util.null2String(rs.getString("currentnodetype"));
                        String isstopped = Util.null2String(rs.getString("isstopped"));
                        if ("1".equals(isstopped)) {
                            wfstatus = "终止";
                        } else {
                            if ("0".equals(currentnodetype)) {
                                rs.executeQuery("  select logid from workflow_requestlog where requestid=? ", new Object[]{allRequestids[i]});
                                if (rs.next()) {
                                    wfstatus = "进行中";
                                } else {
                                    wfstatus = "拟稿";
                                }
                            } else if ("3".equals(currentnodetype)) {
                                wfstatus = "办结";
                            } else {
                                rs.executeQuery("select id from uf_lcgzb  where lcid=? and lcdqzt=1 ", new Object[]{allRequestids[i]});
                                if (rs.next()) {
                                    wfstatus = "搁置";
                                } else {
                                    wfstatus = "进行中";
                                }
                            }
                        }
                    }
                    item.put(allRequestids[i], wfstatus);
                    dataList.add(item);
                }

            }
            apidatas.put("wfstatus", dataList);
            apidatas.put("success", true);
        } catch (Exception e) {
            e.printStackTrace();
            apidatas.put("success", false);
        }
        return JSONObject.toJSONString(apidatas);
    }


    /**
     * 查询流程付款状态
     * @param request
     * @param response
     * @return
     */
    @GET
    @Path("/getWfFkStatus")
    @Produces({MediaType.TEXT_PLAIN})
    public String getWfFkStatus(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        BaseBean bb = new BaseBean();
        Map<String, Object> apidatas = new HashMap<String, Object>();
//        所有总部数组
        String[] zbsz = {"zbtybx", "zbitfk", "zbghfk", "yfkfyqr", "zbtybx", "sljsfk", "zbclbx", "zbtyfk"};
//        所有分支机构数组
        String[] fzjgsz = {"fzjgtybx", "fzjgclbx", "fzjgtyfk", "fzjgsljsfk"};
//        所有子公司数组
        String[] zgssz = {"xgtybx", "zjtybx", "zgtybx", "zgstybx"};
//        联合通用报销数组
        String[] lhtybx = {"lhtybx"};
        String requestid = Util.null2String(request.getParameter("requestid"));
        bb.writeLog("获取的requestid是：" + requestid);
        String[] allRequestids = requestid.split(",");
        List<Map<String, String>> dataList = new ArrayList<>();
        String tableNumber = "";
        String tableName = "";
        RecordSet rs = new RecordSet();
        try {
            for (String everyid : allRequestids) {
//            根据requestid获取流程unnumber
                bb.writeLog("处理的id是：" + everyid);
                rs.execute("select unnumber from workflow_base wb left join workflow_requestbase wr on wb.id = wr.workflowid where wr.requestid = '" + everyid + "'");
                if (rs.next()) {
                    tableNumber = Util.null2String(rs.getString("unnumber"));
                    if (!"".equals(tableNumber)) {
                        bb.writeLog("tableNumber:" + tableNumber);
                        bb.writeLog(Arrays.asList(fzjgsz).contains(tableNumber) + "''''''");
                        if (Arrays.asList(zbsz).contains(tableNumber)) {
                            bb.writeLog("属于总部");
                            tableName = "uf_zbfk";
                        }
                        if (Arrays.asList(fzjgsz).contains(tableNumber)) {
                            bb.writeLog("属于分支机构");
                            tableName = "uf_fzjgfk";
                        }
                        if (Arrays.asList(zgssz).contains(tableNumber)) {
                            bb.writeLog("属于子公司");
                            tableName = "uf_zgsfk";
                        }
                        if (Arrays.asList(lhtybx).contains(tableNumber)) {
                            bb.writeLog("属于联合通用报销");
                            tableName = "uf_zfmx";
                        }
                        bb.writeLog("此时的tableNumber是：" + tableNumber + ",此时的tableName是：" + tableName);
                        if (!"".equals(tableName)) {
                            String fieldSql = " select b.id from workflow_bill a,workflow_billfield b \n" +
                                    " where a.id=b.billid and a.tablename='" + tableName + "' and b.fieldname='fkzt' ";
                            rs.execute(fieldSql);
                            if (rs.next()) {
                                String fieldid = Util.null2String(rs.getString("id"));
                                if (!"".equals(everyid) && !"".equals(fieldid)) {
                                    Map<String, String> item = new HashMap<>();
                                    String fkstatus = "";
                                    String dataSql = "  select selectname fkzt from " + tableName + " a,workflow_selectitem b " +
                                            "where a.fkzt=b.selectvalue and b.fieldid=? and a.requtid=? ";
                                    rs.executeQuery(dataSql, new Object[]{fieldid, everyid});
                                    if (rs.next()) {
                                        fkstatus = Util.null2String(rs.getString("fkzt"));
                                    }
                                    item.put(everyid, fkstatus);
                                    dataList.add(item);
                                } else {
                                    apidatas.put("message", "found error:field is null");
                                }
                            } else {
                                apidatas.put("message", "found error while doing sql to get fieldid");
                            }
                        } else {
                            apidatas.put("message", "found error:tableName is null");
                        }
                    } else {
                        apidatas.put("message", "found error:tableNumber is null");
                    }
                } else {
                    apidatas.put("message", "found error while getting unnumber");
                }
            }
            apidatas.put("success", true);
            apidatas.put("fkstatus", dataList);
            bb.writeLog("返回给前端的结果：" + JSON.toJSONString(dataList));
        } catch (Exception e) {
            e.printStackTrace();
            apidatas.put("success", false);
        }
        return JSONObject.toJSONString(apidatas);
    }



    /**
     * 获取人员显示格式：人员（部门）
     *
     * @param request
     * @param response
     * @return
     */
    @GET
    @Path("/getHrmDeptBrowserValue")
    @Produces({MediaType.TEXT_PLAIN})
    public String getHrmDeptBrowserValue(@Context HttpServletRequest request, @Context HttpServletResponse response) {

        Map<String, Object> dataMap = new HashMap<String, Object>();
        String fieldvalue = Util.null2String(request.getParameter("fieldvalue"));

        RecordSet rs = new RecordSet();
        String sql = "select a.id,lastname,workcode,departmentname from hrmresource a " +
                " left join hrmdepartment b on a.departmentid=b.id where a.id in (" + fieldvalue + ") order by instr('" + fieldvalue + "',id)";
        rs.execute(sql);
        List<Map<String, String>> dataList = new ArrayList<Map<String, String>>();
        while (rs.next()) {
            String id = Util.null2String(rs.getString("id"));
            String name = Util.null2String(rs.getString("lastname"));
            String workcode = Util.null2String(rs.getString("workcode"));
            String departmentname = Util.null2String(rs.getString("departmentname"));

            Map<String, String> itemMap = new HashMap<String, String>();
            itemMap.put("id", id);
            itemMap.put("name", name + "(" + departmentname + ")");

            dataList.add(itemMap);

        }

        dataMap.put("value", fieldvalue);
        dataMap.put("specialobj", dataList);
        return JSONObject.toJSONString(dataMap);
    }


    /**
     * 获取部门显示格式：人员（部门/一级部门简称）
     *
     * @param request
     * @param response
     * @return
     */
    @GET
    @Path("/getHrmShortDeptBrowserValue")
    @Produces({MediaType.TEXT_PLAIN})
    public String getHrmShortDeptBrowserValue(@Context HttpServletRequest request, @Context HttpServletResponse response) {

        String fieldvalue = Util.null2String(request.getParameter("fieldvalue"));
        String type = Util.null2String(request.getParameter("type"));
        Map<String, Object> dataMap = new HashMap<String, Object>();
        List<Map<String, String>> dataList = new ArrayList<Map<String, String>>();

        try {
            ResourceComInfo resourceComInfo = new ResourceComInfo();
            DepartmentComInfo departmentComInfo = new DepartmentComInfo();
            String[] idArr = fieldvalue.split(",");
            for (int i = 0; i < idArr.length; i++) {
                String showname = "";
                String shortname = "";
                if ("hrm".equals(type)) {
                    String deptid = resourceComInfo.getDepartmentID(idArr[i]);
                    shortname = WorkflowUtil.getTopDeptShortNameNew(deptid);
                    showname = resourceComInfo.getResourcename(idArr[i]) + shortname;
                } else {
                    shortname = WorkflowUtil.getTopDeptShortNameNew(idArr[i]);
                    showname = departmentComInfo.getDepartmentname(idArr[i]) + shortname;
                }

                Map<String, String> itemMap = new HashMap<String, String>();
                itemMap.put("id", idArr[i]);
                itemMap.put("name", showname);
                dataList.add(itemMap);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        dataMap.put("value", fieldvalue);
        dataMap.put("specialobj", dataList);
        return JSONObject.toJSONString(dataMap);
    }


    /**
     * 获取部门显示格式：人员（部门/一级部门简称）
     *
     * @param request
     * @param response
     * @return
     */
    @GET
    @Path("/getDeptBrowserValue")
    @Produces({MediaType.TEXT_PLAIN})
    public String getDeptTopDeptShortNameBrowserValue(@Context HttpServletRequest request, @Context HttpServletResponse response) {

        String fieldvalue = Util.null2String(request.getParameter("fieldvalue"));
        Map<String, Object> dataMap = new HashMap<String, Object>();
        List<Map<String, String>> dataList = new ArrayList<Map<String, String>>();

        try {
            DepartmentComInfo departmentComInfo = new DepartmentComInfo();
            String[] idArr = fieldvalue.split(",");
            for (int i = 0; i < idArr.length; i++) {
                String shortname = WorkflowUtil.getTopDeptShortName(idArr[i]);
                String showname = departmentComInfo.getDepartmentname(idArr[i]);
                if (!"".equals(shortname)) {
                    showname = showname + "(" + shortname + ")";
                }
                Map<String, String> itemMap = new HashMap<String, String>();
                itemMap.put("id", idArr[i]);
                itemMap.put("name", showname);
                dataList.add(itemMap);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        dataMap.put("value", fieldvalue);
        dataMap.put("specialobj", dataList);
        return JSONObject.toJSONString(dataMap);
    }

    /**
     * 获取部门显示格式：部门（所在机构）
     *
     * @param request
     * @param response
     * @return
     */
    @GET
    @Path("/getDeptOrgBrowserValue")
    @Produces({MediaType.TEXT_PLAIN})
    public String getDeptOrgBrowserValue(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        String fieldvalue = Util.null2String(request.getParameter("fieldvalue"));
        String fieldid = Util.null2String(request.getParameter("fieldid"));
        String requestid = Util.null2String(request.getParameter("requestid"));
        if(fieldid.startsWith("field")){
            fieldid = fieldid.substring(5);
        }
        String type = Util.null2String(request.getParameter("type"));
        Map<String, Object> dataMap = new HashMap<String, Object>();
        List<Map<String, String>> dataList = new ArrayList<Map<String, String>>();

        try {
            DepartmentComInfo departmentComInfo = new DepartmentComInfo();
            String[] idArr = fieldvalue.split(",");
            for (int i = 0; i < idArr.length; i++) {
                //String showname = departmentComInfo.getDepartmentname(idArr[i]);
                String showname = WorkflowUtil.getWfHistoryDeptName(requestid,fieldid,idArr[i]);
                if("".equals(showname)){
                    showname = departmentComInfo.getDepartmentname(idArr[i]);
                }

                String orgid = "";
                RecordSet rs = new RecordSet();
                String sql = " select l.id from (select t.id from HrmDepartment t where t.supdepid is null or t.supdepid = 0 start with t.id=" + idArr[i] + " connect by prior t.supdepid=t.id ) l";
                rs.execute(sql);
                if (rs.next()) {
                    orgid = com.weaver.general.Util.null2String(rs.getString("id"));
                }
                if (!idArr[i].equals(orgid)) {
                    String orgName = "";
                    if ("short".equals(type)) {
                        orgName = WorkflowUtil.getTopDeptShortName(orgid);
                    }
                    if ("".equals(orgName)) {
                        orgName = departmentComInfo.getDepartmentname(orgid);
                    }
                    showname = showname + "(" + orgName + ")";
                }

                Map<String, String> itemMap = new HashMap<String, String>();
                itemMap.put("id", idArr[i]);
                itemMap.put("name", showname);
                dataList.add(itemMap);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        dataMap.put("value", fieldvalue);
        dataMap.put("specialobj", dataList);
        return JSONObject.toJSONString(dataMap);
    }


    /**
     * 查询流程已审批节点
     *
     * @param request
     * @param response
     * @return
     */
    @GET
    @Path("/getApprovedNodes")
    @Produces({MediaType.TEXT_PLAIN})
    public String getApprovedNodes(@Context HttpServletRequest request, @Context HttpServletResponse response) {

        String requestid = Util.null2String(request.getParameter("requestid"));
        Map<String, Object> apidatas = new HashMap<String, Object>();
        List<Map<String, String>> dataList = new ArrayList<Map<String, String>>();

        try {
            if (!"".equals(requestid)) {
                String dataSql = " select distinct nodeid from workflow_requestlog where requestid=? ";
                RecordSet rs = new RecordSet();
                rs.executeQuery(dataSql, requestid);
                while (rs.next()) {
                    Map<String, String> dataItem = new HashMap<String, String>();
                    String nodeid = Util.null2String(rs.getString("nodeid"));
                    String nodeunnumber = new PropBean().getNodeUnNumberByid(nodeid);
                    dataItem.put("ndoeid", nodeid);
                    dataItem.put("nodeunnumber", nodeunnumber);
                    dataList.add(dataItem);
                }
            }
            apidatas.put("nodes", dataList);
            apidatas.put("success", true);
        } catch (Exception e) {
            e.printStackTrace();
            apidatas.put("success", false);
        }
        return JSONObject.toJSONString(apidatas);
    }

    /**
     * 查询流程已审批节点
     * @param request
     * @param response
     * @return
     */
    @GET
    @Path("/getPhraseInfo")
    @Produces({MediaType.TEXT_PLAIN})
    public String getPhraseInfo(@Context HttpServletRequest request, @Context HttpServletResponse response){

        Map<String, Object> apidatas = new HashMap<String, Object>();
        User user = HrmUserVarify.getUser(request,response);


        boolean isMobileForm = "1".equals(Util.null2String(request.getParameter("isMobile")));
        try{

            RecordSet rs = new RecordSet();
            //查询当前人员所有账号
            String userSql = " select wm_concat_old(b.id) userid  from hrmresource a,hrmresource b \n" +
                    " where a.workcode=b.workcode and a.id=? ";
            rs.executeQuery(userSql,user.getUID());
            rs.next();
            String useridAll = Util.null2String(rs.getString("userid"));

            //是否按使用频率排序
            int phraseOrderByFrequency = 0;
            RequestDefaultComInfo requestDefaultComInfo = new RequestDefaultComInfo();
            phraseOrderByFrequency = Util.getIntValue(requestDefaultComInfo.getPhraseOrderByFrequency(user.getUID()+""));
            String querySql = "select a.id,a.hrmId,a.phraseShort,a.phraseDesc,a.groupid,a.dsporder," +
                    "case when (a.groupid = '2' and b.status is null) then '1' when (a.groupid = '2' and b.status is not null) then b.status else a.status end as status," +
                    "case when (a.groupid = '2' and b.frequency is null) then 0 when (a.groupid = '2') then b.frequency else a.frequency end as frequency " +
                    "from sysPhrase a left join PublicPhraseStatus b on a.id = b.id and b.hrmid in("+useridAll+") " +
                    "where (a.hrmId in("+useridAll+") or a.hrmid=-1) and ((a.status='1' and a.groupid='1') or (a.groupid='2' and (b.status is null or b.status='1'))) ";
            if(phraseOrderByFrequency == 1){
                querySql += " order by frequency desc,a.dsporder,a.id";
            }else {
                querySql += " order by a.dsporder,a.id";
            }
            rs.execute(querySql);
            List<Map<String, String>> phraseInfo = new ArrayList<Map<String, String>>();
            WorkflowConfigComInfo configComInfo = new WorkflowConfigComInfo();
            boolean mobileSignInputRichText = "1".equals(Util.null2String(configComInfo.getValue("mobile_signinput_richtext")));
            boolean pcTextareaSignInput = "1".equals(Util.null2String(configComInfo.getValue("use_textarea_signinput")));
            String groupid = "";
            //是否有编辑权限
            boolean hasRight = HrmUserVarify.checkUserRight("PublicPhrase:all", user);
            while (rs.next()) {
                Map<String, String> phrases = new HashMap<String, String>();
                phrases.put("workflowPhrases", Util.null2String(rs.getString("phraseShort")));

                String phraseDesc = Util.null2String(rs.getString("phrasedesc"));

                if((isMobileForm && !mobileSignInputRichText) || (!isMobileForm && pcTextareaSignInput)) {
                    String phraseStr = WorkflowServiceUtil.splitAndFilterString(phraseDesc,phraseDesc.length());
                    if(phraseStr.endsWith("\n\n"))		//替换后产生两个换行\n\n，去掉一个
                        phraseStr = phraseStr.substring(0, phraseStr.length()-2);
                    phraseDesc = phraseStr;
                } else {
                    phraseDesc = Util.toHtml(phraseDesc);
                }
                phrases.put("workflowPhrasesContent", phraseDesc);
                phrases.put("id",rs.getString("id"));
                groupid = Util.null2String(rs.getString("groupid"));
                //是否有编辑权限
                if("2".equals(groupid)){
                    phrases.put("right", hasRight ? "1" : "0");
                }else if("1".equals(groupid)){
                    phrases.put("right","1");
                }else{
                    phrases.put("right","0");
                }
                phrases.put("groupid",groupid);
                phrases.put("status",rs.getString("status"));
                phraseInfo.add(phrases);
            }


            apidatas.put("phraseInfo", phraseInfo);
            apidatas.put("success", true);
        }catch (Exception e){
            e.printStackTrace();
            apidatas.put("success", false);
        }
        return JSONObject.toJSONString(apidatas);
    }


}
