<%@ 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="org.apache.commons.lang3.StringUtils" %>
<%@ page import="weaver.interfaces.outter.CheckIpNetWorkForUpcoming" %>
<%@ include file="/systeminfo/init_wev8.jsp" %>

<%
    Logger log = LoggerFactory.getLogger();

    RecordSet rs = new RecordSet();

    String type = Util.null2String(request.getParameter("type")) ;

    if ("pc".equalsIgnoreCase(type)){

    }else{

    }

    String workflowId = request.getParameter("workflowid");
    String sysId = request.getParameter("sysid");

    String forURL ;
    if(StringUtils.isNotBlank(workflowId)){
        //新建异构系统
        log.error("workflowid="+workflowId);
        rs.executeQuery("select * from ofs_workflow where workflowid = ?",workflowId);
        rs.next();
        if ("pc".equalsIgnoreCase(type)){
            forURL = rs.getString("pccwurl");
        }else{
            forURL = rs.getString("APPCWURL");
        }

        if ("".equals(Util.null2String(sysId))){
            sysId = rs.getString("SYSID");
        }
    }else{
        //访问异构系统流程
        String todoDataId = request.getParameter("tododataid");
        log.error("todoDataId="+todoDataId);
        String isRemark = request.getParameter("isremark");
        log.error("isRemark="+isRemark);

        if( "0".equals(isRemark) || "8".equals(isRemark)){
            rs.executeQuery("select * from ofs_todo_data where id = ?",todoDataId);
            rs.next();
            if ("pc".equalsIgnoreCase(type)){
                forURL =  Util.null2String(rs.getString("pcurlsrc"));
            }else{
                forURL =  Util.null2String(rs.getString("appurlsrc"));
            }

            if ("".equals(Util.null2String(sysId))){
                sysId = rs.getString("SYSID");
            }
        }else{
            rs.executeQuery("select * from ofs_done_data where id = ?",todoDataId);
            rs.next();
            if ("pc".equalsIgnoreCase(type)){
                forURL =  Util.null2String(rs.getString("pcurlsrc"));
            }else{
                forURL =  Util.null2String(rs.getString("appurlsrc"));
            }

            if ("".equals(Util.null2String(sysId))){
                sysId = rs.getString("SYSID");
            }
        }
    }

    rs.executeQuery("select * from ofs_sysinfo where sysid = ?" , sysId);
    if(rs.next()){
        String prefixURL ;

        if ("pc".equalsIgnoreCase(type)){
            //自动内外网登录
            String clientIp = Util.getIpAddr(request);
            boolean notInOuter = this.notInOuter(rs.getString("syscode") , clientIp);

            if (notInOuter){
                prefixURL = Util.null2String(rs.getString("pcouterfixurl"));
            }else{
                prefixURL = Util.null2String(rs.getString("pcprefixurl"));
            }
        }else{
            prefixURL = Util.null2String(rs.getString("Appprefixurl"));
        }

        StringBuilder url = new StringBuilder() ;
        if(forURL.startsWith("http://") || forURL.startsWith("https://")){
            url.append(forURL);
        }else{
            url.append(prefixURL).append(forURL);
        }

        if(url.toString().contains("?")){
            url.append("&");
        }else{
            url.append("?");
        }
        url.append("_random_ofs=").append(System.currentTimeMillis()) ;

        String toURL = url.toString() ;
        String typeName ;
        if ("pc".equalsIgnoreCase(type)){
            typeName = "PC端" ;
        }else{
            typeName = "APP端" ;
        }
        log.error(typeName +"访问异构系统地址："+toURL);
%>
<script type="text/javascript">
    window.location.replace('<%=toURL%>');

</script>
<%

    }else{
        log.error("根据标识："+sysId+"未查询到数据");
        return;
    }


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

            //外网地址返回 true ,内网 false
            private boolean notInOuter(String sysCode , String clientIp){

                //0代表不开启，则所有通过内网访问
                //1代表开启，并且有设置网段
                //2代表开启，但是没有设置网段
                RecordSet rs = new RecordSet();
                rs.executeQuery("SELECT * FROM autologin_status WHERE syscode= ? " , sysCode) ;
                if (rs.next()){
                    String status = Util.null2String(rs.getString("status"),"0");
                    if ("0".equals(status)){
                        return false ;
                    }else if ("2".equals(status)){
                        return true ;
                    }
                }
                //检测IP
                CheckIpNetWorkForUpcoming checkIpNetWorkForUpcoming = new CheckIpNetWorkForUpcoming();
                return checkIpNetWorkForUpcoming.checkIpSeg(clientIp);//不在网段策略中 返回true
            }
%>