<%@ page import="okhttp3.OkHttpClient" %>
<%@ page import="okhttp3.Request" %>
<%@ page import="okhttp3.Response" %>
<%@ page import="java.io.IOException" %>
<%@ page import="org.apache.commons.lang3.StringUtils" %>
<%@ page import="weaver.conn.RecordSet" %>
<%@ page import="weaver.general.Util" %>
<%@ page import="com.alibaba.fastjson.JSONArray" %>
<%@ page import="weaver.general.BaseBean" %>
<%@ page import="com.engine.workflow.biz.NewRequestBiz" %>
<%@ page import="weaver.workflow.workflow.WorkflowVersion" %>
<%@ page import="weaver.hrm.User" %>
<%@ page import="com.engine.workflow.entity.newRequest.WfType" %>
<%@ page import="java.util.List" %>
<%@ page import="com.alibaba.fastjson.JSON" %>
<%@ page import="com.engine.workflow.entity.newRequest.WfBean" %>
<%@ page import="com.alibaba.fastjson.JSONObject" %>
<%@ page contentType="text/html;charset=UTF-8" %>
<%
    JSONArray jsonArray = new JSONArray();
    BaseBean bb = new BaseBean();
    String pubkey = bb.getPropValue("CHATGPT","pubkey");
    RecordSet rs = new RecordSet();
    String userinfo = request.getParameter("userinfo") ;
    String query = request.getParameter("query");

    out.print("userinfo:"+userinfo+"</br>");
    out.print("query:"+query+"</br>");

    String host = bb.getPropValue("CHATGPT","host");
    String appid = bb.getPropValue("CHATGPT","appid");
    out.print("pubkey:"+pubkey+"</br>");
    out.print("host:"+host+"</br>");
    out.print("appid:"+appid+"</br>");
    String loginid = "" ;
    String userid = "";
    if(StringUtils.isNotBlank(userinfo)){
        String userSql = " select id,loginid from hrmresource where upper(md5(concat(loginid,'"+pubkey+"')))='"+userinfo.toUpperCase()+"'";
        out.print("userSql:"+userSql+"</br>");
        rs.executeQuery(userSql);
        if(rs.next()){
            loginid = Util.null2String(rs.getString("loginid"));
            userid = Util.null2String(rs.getString("id"));
        }
    }
    out.print("loginid:"+loginid+"</br>");
    if(StringUtils.isNotBlank(loginid) && StringUtils.isNotBlank(appid)){
        String token = getToken(host,appid,loginid);
        out.print("token:"+token+"</br>");
        if(StringUtils.isNotBlank(token)){

            String[] workflowArray = query.split("\\$");

            out.print("userid:"+userid+"</br>");
            User user = new User(Util.getIntValue(userid));
            out.print("user:"+user.getLastname()+"</br>");
            String inornot = "in";
            NewRequestBiz wfBiz = new NewRequestBiz(user);
            wfBiz.setNewRequestRange(WorkflowVersion.getAllVersionStringByWFIDs(""));
            wfBiz.setInornot(inornot);
            wfBiz.setIsmobile(false);
            wfBiz.setCustomSqlWhere("");//新建流程扩展条件
            List<WfType> wftypes = wfBiz.getWfInfo("");
//            out.print("wftypes:"+ JSON.toJSON(wftypes));
            for(WfType wfType : wftypes){
                List<WfBean> wfBeanList = wfType.getWfbeans();
                for(WfBean wfBean : wfBeanList){
                    int count = 0;
                    String wf_name = wfBean.getName();
                    String workflowid = wfBean.getId();
                    for(int i=0;i<workflowArray.length;i++) {
                        String workflowname = workflowArray[i];
                        if(!wf_name.contains(workflowname)){
                            count++;
                        }
                    }

                    out.print("wf_name:"+ wf_name+"</br>");
                    out.print("workflowid:"+ workflowid+"</br>");
                    out.print("count:"+ count+"</br>");

                    if(count == 0){
                        JSONObject jsonObject = new JSONObject();
                        jsonObject.put("requestname",wf_name);
                        jsonObject.put("pc_url","/workflow/request/CreateRequestForward.jsp?workflowid="+workflowid+"&ssoToken="+token);
                        jsonObject.put("h5_url","/spa/workflow/static4mobileform/index.html#/req?iscreate=1&workflowid="+workflowid+"&ssoToken="+token);

                        jsonArray.add(jsonObject);
                    }
                }
            }
            out.print("jsonArray:"+ jsonArray.size()+"</br>");
//            String[] workflowArray = query.split("\\$");
//            for(int i=0;i<workflowArray.length;i++){
//                String workflowname = workflowArray[i];
//                out.print("workflowname:"+workflowname+"</br>");
////                if(StringUtils.isNotBlank(workflowname)){
////                    String sql = " select id,workflowname from workflow_base where workflowname like '%"+workflowname+"%'" ;
////                    out.print("sql:"+sql+"</br>");
////                    rs.executeQuery(sql);
////                    while (rs.next()){
////                        String name = Util.null2String(rs.getString("workflowname"));
////                        String workflowid = Util.null2String(rs.getString("id"));
////
////                        JSONObject jsonObject = new JSONObject();
////                        jsonObject.put("requestname",name);
////                        jsonObject.put("pc_url","/workflow/request/CreateRequestForward.jsp?workflowid="+workflowid+"&ssoToken="+token);
////                        jsonObject.put("h5_url","/spa/workflow/static4mobileform/index.html#/req?iscreate=1&workflowid="+workflowid+"&ssoToken="+token);
////                        jsonArray.add(jsonObject);
////                    }
////                }
//            }
        }
    }

%>
<%=jsonArray.toJSONString() %>

<%!

    public String getToken(String host,String appid,String loginid){
        String token = "";
        OkHttpClient client = new OkHttpClient().newBuilder()
                .build();
        Request request = new Request.Builder()
                .url(host+"/ssologin/getToken?appid="+appid+"&loginid="+loginid)
                .method("GET", null)
                .build();
        try {
            Response response = client.newCall(request).execute();

            int code = response.code();
            if(code == 200){
                token = response.body().string();
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return  token;
    }
%>

