package com.qy.worksheetsystem.util;


import cn.hutool.json.JSONUtil;
import org.apache.axis.Constants;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.encoding.ser.BeanDeserializerFactory;
import org.apache.axis.encoding.ser.BeanSerializerFactory;
import org.apache.axis.message.SOAPHeaderElement;

import javax.xml.namespace.QName;
import javax.xml.rpc.ParameterMode;
import javax.xml.rpc.ServiceException;
import java.net.MalformedURLException;
import java.net.URL;
import java.rmi.RemoteException;
import java.util.HashMap;
import java.util.Map;

public class CilentWsdlUtil {
    private String url;

    private String dbName;
    private int dbType=0;
    private String name;
    private String pwd;

    public String getUrl() {
        return url;
    }



    public String getDbName() {
        return dbName;
    }

    public int getDbType() {
        return dbType;
    }

    public String getName() {
        return name;
    }

    public String getPwd() {
        return pwd;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public void setDbName(String dbName) {
        this.dbName = dbName;
    }

    public void setDbType(int dbType) {
        this.dbType = dbType;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setPwd(String pwd) {
        this.pwd = pwd;
    }

    public CilentWsdlUtil(String url, String name, String pwd, String dbName, int dbType) throws ServiceException {

        this.url=url;
        this.name=name;
        this.pwd=pwd;
        this.dbName=dbName;
        this.dbType=dbType;
    }
    public CilentWsdlUtil(String url,String dbName) throws ServiceException {

        this.url=url;
        this.dbName=dbName;
        this.name="user";
        this.pwd="kduser";
    }

    public String EASLogin(Call call) throws MalformedURLException, RemoteException {
        String easAddws = this.url+"/ormrpc/services/EASLogin?wsdl";
        QName qName = new QName(easAddws, "login");
        call.setOperationName("login");
        call.setTargetEndpointAddress(new URL(easAddws));
        call.setReturnType(new QName("urn:client", "WSContext"),WSContext.class);
        call.setMaintainSession(true);
        call.addParameter("userName", Constants.XSD_STRING, ParameterMode.IN);
        call.addParameter("password", Constants.XSD_STRING, ParameterMode.IN);
        call.addParameter("slnName", Constants.XSD_STRING, ParameterMode.IN);
        call.addParameter("dcName", Constants.XSD_STRING, ParameterMode.IN);
        call.addParameter("language", Constants.XSD_STRING, ParameterMode.IN);
        call.addParameter("dbType", Constants.XSD_INT, ParameterMode.IN);

        WSContext result  = (WSContext) call.invoke(new Object[]{this.name, this.pwd, "eas", this.dbName, "l2", this.dbType});
        //Object obj = call.invoke(new Object[] {"user","kduser","eas","WYHR","l2",0 });
        System.out.println("登录：url:" +easAddws+";"+ result.getSessionId());
        String sessionid=result.getSessionId();
        return sessionid;
    }

    public String callOSF(Call call, String sessionId, String serviceName, Map<String, Object> params) throws Exception {
        String paramsStr = JSONUtil.toJsonStr(params);
        System.out.println("Call OSF Service: " + serviceName + ", params is: " + paramsStr);
        call.clearOperation();
        call.setOperationName("proceedOSF");
        call.setTargetEndpointAddress(this.url + "/ormrpc/services/WSOSFWebserviceFacade?wsdl");
        call.setReturnQName(new QName("", "proceedOSFReturn"));
        call.setTimeout(600000);
        call.setMaintainSession(true);
        SOAPHeaderElement header = new SOAPHeaderElement("http://login.webservice.bos.kingdee.com", "SessionId", sessionId);
        call.addHeader(header);
        return (String)call.invoke(new Object[]{serviceName, paramsStr});
    }
    public String proceedOSF(String serviceName, Map<String, Object> params) throws ServiceException {
        String result = null;
        String sessionId = null;
        Service service = new Service();
        Call call = (Call)service.createCall();
        try {
            sessionId = this.EASLogin(call);
            if (sessionId == null || sessionId.trim().length() == 0) {
                System.out.println("webservice login fail, username: " + this.name + ", datacenter: " + this.dbName);
            }
            result = this.callOSF(call,sessionId, serviceName, params);

        } catch (Exception var12) {
            System.err.println("Call OSF error: " + var12);
            String errorInfo = var12.getCause() == null ? var12.getMessage() : var12.getCause().toString();
            result = "{ \"error\": \"Call OSF error: " + errorInfo + "\"}";
            var12.printStackTrace();
        }

        return result;
    }
    public static void main(String[] args){
        try {
            CilentWsdlUtil cwu = new CilentWsdlUtil("http://oa.ysstech.com:5887", "刘龙祥", "asd13870704258", "sutpc86", 0);
            Service service = new Service();
            Call call = (Call) service.createCall();
            String result = cwu.EASLogin(call);
            System.out.println(result);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
