<%@ 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="java.net.URL" %>
<%@ page import="java.net.URLConnection" %>
<%@ page import="org.apache.commons.lang3.StringUtils" %>
<%@ page import="weaver.interfaces.HrmTransferDao" %>
<%@ page import="java.io.*" %>
<%@ page import="weaver.sm.SM4Utils" %>
<%@ include file="/systeminfo/init_wev8.jsp" %>

<%
    Logger log = LoggerFactory.getLogger();

    RecordSet rs = new RecordSet();

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

    int userId = user.getUID() ;
    String appURL ;
    if(StringUtils.isNotBlank(workflowId)){
        //新建异构系统
        log.error("workflowid="+workflowId);
        rs.executeQuery("select * from ofs_workflow where workflowid = ?",workflowId);
        rs.next();
        appURL = 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();
            appURL =  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();
            appURL =  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 = Util.null2String(rs.getString("Appprefixurl"));
        String hrmTransRule = Util.null2String(rs.getString("HRMTRANSRULE"));//人员转换关系

        HrmTransferDao hrmTransferDao = new HrmTransferDao();
        String loginId = hrmTransferDao.getHrmResourceIdByHrmTransRule(hrmTransRule, Util.null2String(userId));

        long stamp = System.currentTimeMillis();
        String token = this.getToken(prefixURL, loginId, stamp);
        String toURL = this.getURL(prefixURL, appURL, token, loginId, stamp);
        log.info("==============APP端访问异构系统地址："+toURL);
%>
<script type="text/javascript">

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

</script>
<%

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


%>



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

    private String getURL(String prefixURL, String toURL, String token,String loginId, long stamp) throws UnsupportedEncodingException {
        StringBuilder url = new StringBuilder() ;
        if(toURL.startsWith("http://") || toURL.startsWith("https://")){
            url.append(toURL);
        }else{
            url.append(prefixURL).append(toURL);
        }
        if(url.toString().contains("#")){
            String[] split = toURL.split("#");
            url.append(split[0]).append("&loginid="+loginId+"&stamp="+stamp+"&token=").append(URLEncoder.encode(token,"UTF-8")).append("#").append(split[1]) ;
        }else{
            if(toURL.contains("?")){
                url.append("&");
            }else{
                url.append("?");
            }
            url.append("loginid="+loginId+"&stamp="+stamp+"&token=").append(URLEncoder.encode(token,"UTF-8")) ;
        }
        return url.toString() ;
    }


    private String getToken(String prefixURL, String loginId, long stamp){

        //加密就是SM4  ECB模式，NoPadding 不填充。加密key是bankoftianjincom
        //String key = "bankoftianjincom";
        SM4Utils sm4Utils = new SM4Utils();
        String result = sm4Utils.encrypt(loginId + "" + stamp, "bankoftianjincom", "SM4");

        log.info("=============apptransferforee9_token:"+result);

        return result ;
    }

%>
