package com.engine.htsc.workflow.web;

import com.alibaba.fastjson.JSONObject;
import com.engine.htsc.workflow.util.WorkflowTodoUtil;
import org.apache.commons.lang.StringUtils;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.GCONST;
import weaver.general.Util;
import weaver.hrm.resource.ResourceComInfo;
import weaver.interfaces.htsc.comInfo.PropBean;

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.*;

/**
 * @Title: ecology-9
 * @Company: 泛微软件
 * @author: weaver.K1810007
 * @version: 1.0
 * @CreateDate: 2022-08-22
 * @Description: 移动端待办接口
 * * @ModifyLog:
 *
 **/
public class WorkflowMobileTodoAction {

    /**
     * 查询人员待办
     * @param request
     * @param response
     * @return
     */
    @GET
    @Path("/mobiletodos")
    @Produces({MediaType.TEXT_PLAIN})
    public String getWorkflowTodos(@Context HttpServletRequest request, @Context HttpServletResponse response){

        JSONObject apidatas = new JSONObject();
        BaseBean bb = new BaseBean();
        //获取用户
        String userCode = Util.null2String(request.getParameter("badge"));
        bb.infoLog("WorkflowMobileTodoAction","badge:"+userCode);
        RecordSet rs = new RecordSet();
        List<Map<String,String>> itemList = new ArrayList<Map<String,String>>();


        try{
            //待办
            List<Map<String,String>> todoList = getWorkflowMobileData(userCode);
            itemList.addAll(todoList);
        }catch (Exception e){
            bb.writeLog("任务异常"+e);
            e.printStackTrace();
        }

        //2022-01-18
        apidatas.put("resultMsg","成功");
        apidatas.put("data",itemList);
        apidatas.put("status", "1");
        return apidatas.toJSONString();
    }

    /***
     *
     * @param
     * @return
     */
    public List<Map<String,String>> getWorkflowMobileData(String userCode)
    {
        WorkflowTodoUtil workflowTodoUtil = new WorkflowTodoUtil();
        List<Map<String,String>> itemList = new ArrayList<Map<String,String>>();
        RecordSet rs = new RecordSet();
        BaseBean bb = new BaseBean();
        PropBean pb = new PropBean();
        String mobileHost = pb.getPropName("mobileHost");  //移动端地址
        String mobileJumpUrl = pb.getPropName("mobileJumpUrl");  //移动端url

        ResourceComInfo resourceComInfo = null;
        try {
            resourceComInfo = new ResourceComInfo();

            String excludeFlow = pb.getPropName("noSendWorkflowId");
            String excludeSqlWhere = "";
            if (StringUtils.isNotEmpty(excludeFlow)) {
                excludeSqlWhere = " and t3.id not in(" + excludeFlow + ") \n";
            }

            String useridAll = "";
            rs.executeQuery(" select id from hrmresource where workcode=? and status=1 ",userCode);
            while(rs.next())
            {
                String id = Util.null2String(rs.getString("id"));
                useridAll += useridAll == "" ? id :","+id ;
            }

            if(!"".equals(useridAll)){

                List<String> bqList = workflowTodoUtil.queryLabelData(useridAll);

                List<String> dzyyList = workflowTodoUtil.queryElectPrintData(useridAll);

                List<String> yjList = workflowTodoUtil.queryMailData(useridAll);

                Map<String,String> itemMap = null;
                String sql = "" ;
                String todosql = workflowTodoUtil.getWorkflowMobileTodoData(useridAll,excludeSqlWhere);

                sql =   "  select l.* from (\n" +
                        todosql + "\n"+
                        "   ) l  \n" +
                        " where 1=1 " +
                        " order by l.receiveTime desc\n" ;

//                System.out.println(sql);
                rs.execute(sql);
                while(rs.next()){

                    String title = Util.null2String(rs.getString("requestname"));
                    String requestid = Util.null2String(rs.getString("requestid"));
                    if(bqList.contains(requestid)){
                        continue;
                    }
                    if(dzyyList.contains(requestid)){
                        title = "[E]"+title ;
                    }
                    if(yjList.contains(requestid)){
                        title = "[邮]"+title ;
                    }

                    String creater = Util.null2String(rs.getString("creater"));

                    String createrCode = "";
                    String createrName = "";
                    try {
                        createrCode = resourceComInfo.getWorkcode(creater);
                        createrName = resourceComInfo.getLastname(creater);
                    } catch (Exception e) {
                    }

                    String priority = Util.null2String(rs.getString("requestlevel"));

                    String type = Util.null2String(rs.getString("workflowname"));
                    String action = Util.null2String(rs.getString("nodename"));
                    String createTime = Util.null2String(rs.getString("createtime"));
                    String createDate = Util.null2String(rs.getString("createDate"));
                    String userid = Util.null2String(rs.getString("userid"));

                    String flowLevel = "" ;
                    if("0".equals(priority)){
                        flowLevel = "1";
                    }else if("1".equals(priority)){
                        flowLevel = "2";
                    }else if("2".equals(priority)){
                        flowLevel = "3";
                    }else{
                        flowLevel = "1";
                    }
                    String workflowid = Util.null2String(rs.getString("workflowid"));

                    itemMap = new HashMap<String,String>();
                    itemMap.put("flowId",requestid);
                    itemMap.put("createrid",creater);
                    itemMap.put("baseTitle",title);
                    itemMap.put("flowLevel",flowLevel);
                    itemMap.put("workflowtype",type);
                    itemMap.put("createrName",createrName);
                    itemMap.put("stepName",action);
                    itemMap.put("startTime",createDate+" "+createTime);
                    itemMap.put("createrCode",createrCode);
                    itemMap.put("workflowid",workflowid);
                    itemMap.put("fromSystem","htmoa");

                    //http://linkxt-test.htsc.com.cn:38080/htoa/spa/workflow/static4mobileform/index.html?_random=1600933308124#/req?openWay=messageCenter&requestid=637641&resourcetype=0&resourceid=637641&isFormMsg=1&em_auth_userid=11343&outsysid=b5bbf1092d5b483c91959046a61636fb&em_client_type=mobile&em_auth_code=235b311a7b0745ffb7a604c5425d1279
                    //http://link-test.htsc.com.cn:8990/connect/oauth2/authorize?appid=ww90dba4dc323845a2&redirect_uri=http%3a%2f%2flinkxt-test.htsc.com.cn%3a38080%2fhtsc-mobile-app-web%2fmoaWeChatProcessList.do%3foauth2url%3dtodo&response_type=code&scope=snsapi_userinfo&agentid=1000003&state=phone#wechat_redirect
//                    String redirect_uri = "http://linkxt-test.htsc.com.cn:38080/htoa/interface/jump.jsp" ;
//                    redirect_uri = java.net.URLEncoder.encode(redirect_uri, "UTF-8");
//                    String param = "&response_type=code&scope=snsapi_userinfo&agentid=1000094&state=phone#wechat_redirect" ;
//                    String viewUrl = "http://link-test.htsc.com.cn:8990/connect/oauth2/authorize?appid=ww90dba4dc323845a2&redirect_uri="+redirect_uri+param;
                    //http://linkxt-test.htsc.com.cn:38080/htmoa/wxapi/wxclientmenu/7bfc8bd866a74b0788f6c205a8668877
                    //http://168.61.8.169:8080/htoa/spa/workflow/static4form/index.html?_rdm=1608193181613#/main/workflow/req?iscreate=1&workflowid=24&isagent=0&beagenter=0&f_weaver_belongto_userid=&f_weaver_belongto_usertype=0&menuIds=1,12&_key=0duo7b

                    String viewUrl = mobileHost+mobileJumpUrl+"?requestid="+requestid+"&f_weaver_belongto_userid="+userid;
                    bb.infoLog("WorkflowMobileTodoAction","viewUrl:"+viewUrl);
                    itemMap.put("url",viewUrl);
                    itemList.add(itemMap);
                }
            }
        }catch (Exception e){
            bb.infoLog("WorkflowMobileTodoAction","getWorkflowMobileData--e:"+e);
        }
        return itemList;
    }


    public static void main(String[] args) throws Exception {
        GCONST.setRootPath("WEB-INF/");
        GCONST.setServerName("ecology");
        String userCode = "012901" ;
        WorkflowMobileTodoAction workflowMobileTodoAction = new WorkflowMobileTodoAction();
        workflowMobileTodoAction.getWorkflowMobileData(userCode);
    }



}
