package com.foreverwin.mesnac.common.util;

import com.foreverwin.mesnac.common.erp.ZWS_ZMES_PRODORDCONF_ServiceLocator;
import com.foreverwin.mesnac.common.erp.Zws_zmes_PRODORDCONF;
import com.foreverwin.mesnac.meapi.util.StringUtils;
import org.apache.axis.client.Stub;

import javax.xml.rpc.ServiceException;

public class ERPAPI {

    public static Zws_zmes_PRODORDCONF erpWebService() {
        try {
            ZWS_ZMES_PRODORDCONF_ServiceLocator locator = new ZWS_ZMES_PRODORDCONF_ServiceLocator();
            locator.setZWS_ZMES_PRODORDCONFEndpointAddress(getApiUrl());
            Zws_zmes_PRODORDCONF service = locator.getZWS_ZMES_PRODORDCONF();

            apply(service);
            return service;
        } catch (ServiceException e) {
            throw new RuntimeException(e);
        }
    }

    public static String getApiUrl() {
        String server = System.getProperty("ws.erp.server");
        String port = System.getProperty("ws.erp.port");

        return String.format("http://%s:%s/sap/bc/srt/rfc/sap/zws_zmes_prodordconf/900/zws_zmes_prodordconf/zws_zmes_prodordconf", server, port);
    }

    public static void apply(Object stubImpl) {
        Stub stub = (Stub) stubImpl;

        String username = System.getProperty("ws.erp.username");
        String password = System.getProperty("ws.erp.password");
        if (StringUtils.notBlank(username) && StringUtils.notBlank(password)) {
            stub.setUsername(username);
            stub.setPassword(password);
        }

        //修改超时时间：5分钟
        stub.setTimeout(300000);
    }
}
