<%@ page language="java" contentType="text/html; charset=UTF-8" %>

<%@ page import="weaver.general.Util,weaver.conn.RecordSet" %>
<%@ page import="weaver.integration.logging.Logger"%>
<%@ page import="weaver.integration.logging.LoggerFactory"%>
<%@ page import="weaver.ofs.bean.OfsSysInfo" %>
<%@ page import="weaver.ofs.service.OfsSysInfoService" %>
<%@ page import="weaver.ofs.manager.utils.OfsTodoDataUtils" %>
<%@ page import="java.util.Map" %>
<%@ page import="weaver.ofs.bean.OfsTodoData" %>
<%@ page import="weaver.ofs.dao.OfsRequestBaseDao" %>
<%@ page import="org.apache.commons.beanutils.BeanUtils" %>
<%@ page import="org.apache.commons.lang3.StringUtils" %>
<%@ page import="com.alibaba.fastjson.JSONObject" %>
<%@ page import="weaver.interfaces.sso.cas.CASLoginUtil" %>
<%@ page import="com.wbi.util.ParamUtil" %>
<%@ include file="/systeminfo/init_wev8.jsp" %>

<%
    Logger log = LoggerFactory.getLogger();
    //补偿  认证 cas ，如果统一认证 开启 自动补偿刷新
    CASLoginUtil.WxDingDingLoginSSO(request , response) ;

    RecordSet rs = new RecordSet();
    String type = request.getParameter("type") ;
    String sysId = request.getParameter("sysId") ;
    String workflowId = request.getParameter("workflowId") ;
    String flowId = request.getParameter("flowId") ;
    if ("".equals(sysId) || "".equals(type) || "".equals(workflowId) || "".equals(flowId)){
        out.println(" parameter error ");
        return;
    }
    int userId = user.getUID() ;
    Map<String, String[]> paramMap =request.getParameterMap() ;

    OfsSysInfoService ofsSysInfoService = new OfsSysInfoService() ;
    OfsSysInfo ofsSysInfo = ofsSysInfoService.getOneBean(Util.getIntValue(sysId , 0)) ;
    if (ofsSysInfo == null || "".equals(Util.null2String(ofsSysInfo.getSysid()))){
        log.error("根据标识："+sysId+"未查询到数据");
        out.println("根据标识："+sysId+"未查询到数据");
        return;
    }

    OfsTodoDataUtils todoDataUtils = new OfsTodoDataUtils() ;
    OfsRequestBaseDao ofsRequestBaseDao = new OfsRequestBaseDao() ;
    OfsTodoData todoData = null;
    String requestId = ofsRequestBaseDao.getRequestid(ofsSysInfo.getSyscode() , Util.getIntValue(workflowId , 0) , flowId , rs.getDBType()) ;
    if(!"".equals(Util.null2String(requestId)))
        todoData = todoDataUtils.getTodoData(requestId , Util.null2String(userId)) ;

    if (todoData == null) {
        todoData = this.getTodoData(flowId, workflowId, sysId, user);
        if (todoData == null) {
            log.error("根据标识：" + ofsSysInfo.getSyscode() + "  workflowId:" + workflowId + "   flowId: " + flowId + "未查询到数据");
            out.println("根据标识：" + ofsSysInfo.getSyscode() + "  workflowId:" + workflowId + "   flowId: " + flowId + "未查询到数据");
            return;
        }
    }
    String toURL = todoDataUtils.getURL("app".equalsIgnoreCase(type)? "1":"0" , ofsSysInfo , todoData , request , paramMap) ;
    log.warn("跳转URL:" + toURL);

    //添加判断,是否为小号,为小号则不更新未读标记
    String loginidweaver = getCookie(request, "loginidweaver");
    log.info("loginidweaver==>"+loginidweaver);

    log.info("paramMap==>{}"+ParamUtil.request2Map(request));
    String ua = Util.null2String(ParamUtil.request2Map(request).get("request_header_user_agent"));
    log.info("loginidweaver.length()=={}"+(loginidweaver.length() == 8));
    log.info(" ua.contains(E-Mobile7) == [}"+ua);
    log.info(" ua.contains(E-Mobile7) == [}"+ua.contains("E-Mobile7") );
    if(loginidweaver.length() == 8 && ua.contains("E-Mobile7") ){
        log.info("update no eceute ==>"+loginidweaver);


    }else {
        log.info("update viewtype=1 ==>"+loginidweaver);

        rs.executeUpdate("update ofs_todo_data set viewtype=1 where requestid=? and userid=? and islasttimes=1" , requestId , userId);

        rs.executeUpdate("update ofs_done_data set viewtype=1 where requestid=? and userid=? " , requestId , userId);

        rs.executeUpdate("delete from SysPoppupRemindInfoNew where userid = ? and requestid = ?", userId, requestId);

    }

%>
<script type="text/javascript">

    location.replace('<%=toURL%>');

</script>

<%!
    Logger log = LoggerFactory.getLogger();

    /**
     * 获取 异构系统流程数据
     * @return
     */
    private OfsTodoData getTodoData(String flowId ,String workflowId , String sysId ,User user ){
        RecordSet rs = new RecordSet() ;

        List<String> userList = new ArrayList<>();
        userList.add(user.getUID() + "");
        if (!"".equals(Util.null2String(user.getBelongtoids())))
            userList.add(user.getBelongtoids());
        //开启 主次账号 ，并且有多个用户
        rs.executeQuery("select * from ofs_todo_data where flowid=? and workflowid=? and sysid=? and userid in ( "+String.join("," , userList)+" )",
                flowId , workflowId , sysId);
        OfsTodoData ofsTodoData = this.putValue2Bean(rs) ;
        if (StringUtils.isBlank(ofsTodoData.getFlowid())){
            rs.executeQuery("select * from ofs_done_data where flowid=? and workflowid=? and sysid=? and userid in ( "+String.join("," , userList)+" )",
                    flowId , workflowId , sysId);
            OfsTodoData ofsDoneData = this.putValue2Bean(rs);
            if (StringUtils.isBlank(ofsDoneData.getFlowid())){
                log.error("flowId : "+ flowId +" workflowId : "+ workflowId +" sysId : "+ sysId +"  userId:"+user.getUID() + "  没有对应数据");
                return null ;
            }
            log.error("ofsTodoData : "+ JSONObject.toJSONString(ofsDoneData));
            return ofsDoneData ;
        }
        log.error("ofsTodoData : "+ JSONObject.toJSONString(ofsTodoData));
        return ofsTodoData ;


    }
    /**
     * 将待办信息从数据库查询出来放入bean中
     * @param rs
     */
    private OfsTodoData putValue2Bean(RecordSet rs) {
        rs.next();
        OfsTodoData ofsTodoData = new OfsTodoData();
        try{
            BeanUtils.setProperty(ofsTodoData,"id", Util.getIntValue(rs.getString("id"), 0)+"");
            BeanUtils.setProperty(ofsTodoData,"sysid", Util.getIntValue(rs.getString("sysid"), 0)+"");
            BeanUtils.setProperty(ofsTodoData,"syscode", Util.null2String(rs.getString("syscode"), ""));
            BeanUtils.setProperty(ofsTodoData,"requestid", Util.null2String(rs.getString("requestid")));
            BeanUtils.setProperty(ofsTodoData,"flowid", Util.null2String(rs.getString("flowid")));
            //	BeanUtils.setProperty(ofsTodoData,"flowguid", Util.null2String(rs.getString("flowguid")));
            BeanUtils.setProperty(ofsTodoData,"requestname", Util.null2String(rs.getString("requestname")));
            BeanUtils.setProperty(ofsTodoData,"workflowname", Util.null2String(rs.getString("workflowname")));
            BeanUtils.setProperty(ofsTodoData,"workflowid", Util.null2String(rs.getString("workflowid")));
            BeanUtils.setProperty(ofsTodoData,"nodename", Util.null2String(rs.getString("nodename")));
            BeanUtils.setProperty(ofsTodoData,"isremark", Util.null2String(rs.getString("isremark")));
            BeanUtils.setProperty(ofsTodoData,"viewtype", Util.null2String(rs.getString("viewtype")));
            BeanUtils.setProperty(ofsTodoData,"islasttimes", Util.null2String(rs.getString("islasttimes")));
            BeanUtils.setProperty(ofsTodoData,"iscomplete", Util.null2String(rs.getString("iscomplete")));
            BeanUtils.setProperty(ofsTodoData,"hrmtransrule", Util.null2String(rs.getString("hrmtransrule")));
            BeanUtils.setProperty(ofsTodoData,"pcurl", Util.null2String(rs.getString("pcurl")));
            BeanUtils.setProperty(ofsTodoData,"appurl", Util.null2String(rs.getString("appurl")));

            BeanUtils.setProperty(ofsTodoData,"creator", Util.null2String(rs.getString("creator")));
            BeanUtils.setProperty(ofsTodoData,"creatorid", Util.null2String(rs.getString("creatorid")));
            BeanUtils.setProperty(ofsTodoData,"createdate", Util.null2String(rs.getString("createdate")));
            BeanUtils.setProperty(ofsTodoData,"createtime", Util.null2String(rs.getString("createtime")));

            BeanUtils.setProperty(ofsTodoData,"userid", Util.null2String(rs.getString("userid")));
            BeanUtils.setProperty(ofsTodoData,"receiver", Util.null2String(rs.getString("receiver")));
            BeanUtils.setProperty(ofsTodoData,"receivedate", Util.null2String(rs.getString("receivedate")));
            BeanUtils.setProperty(ofsTodoData,"receivetime", Util.null2String(rs.getString("receivetime")));

            BeanUtils.setProperty(ofsTodoData,"modifier", Util.null2String(rs.getString("modifier")));
            BeanUtils.setProperty(ofsTodoData,"modifydate", Util.null2String(rs.getString("modifydate")));
            BeanUtils.setProperty(ofsTodoData,"modifytime", Util.null2String(rs.getString("modifytime")));
            BeanUtils.setProperty(ofsTodoData,"pcurlsrc", Util.null2String(rs.getString("pcurlsrc")));
            BeanUtils.setProperty(ofsTodoData,"appurlsrc", Util.null2String(rs.getString("appurlsrc")));
            BeanUtils.setProperty(ofsTodoData,"requestlevel", Util.null2String(rs.getString("requestlevel")));
            BeanUtils.setProperty(ofsTodoData,"receivets", Util.null2String(rs.getString("receivets")));
            BeanUtils.setProperty(ofsTodoData,"pcurlsrc", Util.null2String(rs.getString("pcurlsrc")));
            BeanUtils.setProperty(ofsTodoData,"appurlsrc", Util.null2String(rs.getString("appurlsrc")));
            return ofsTodoData;
        }catch(Exception e){
            log.error(e);
            return null;
        }
    }


    //根据名称获取cookie的值
    private String getCookie(HttpServletRequest request,String name){
        try {
            Cookie[] cookies = request.getCookies();
            if (cookies != null) {
                for (Cookie cookie : cookies) {
                    if(name.equals(cookie.getName())){
                        return Util.null2String(cookie.getValue());
                    }
                }
            }
        }catch (Exception e){
            log.error("getCookieError"+e.getMessage());
            e.printStackTrace();
        }
        return "";
    }

    //
%>
