package com.uas.eis.sdk.webservice;

import com.uas.eis.sdk.dto.SubPickmtrlReq;
import net.sf.json.JSONObject;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.encoding.XMLType;
import org.apache.axis.message.SOAPHeaderElement;
import org.apache.axis.soap.SOAPConstants;

import javax.xml.namespace.QName;
import javax.xml.rpc.ParameterMode;
import javax.xml.soap.SOAPElement;
import javax.xml.soap.SOAPException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author: zhouy
 * @date: 2021/10/15 14:46
 * @desc: WebService 请求工具类
 * POST /MES/mes_k3.asmx HTTP/1.1
 * Host: 183.62.131.97
 * Content-Type: text/xml; charset=utf-8
 * Content-Length: length
 * SOAPAction: "http://tempuri.org/MES_K3"
 *
 */
public class SoapUtil {

    public static void main(String[] args) throws IOException {
        callWebserviceASMX();
    }
    public static void callWebserviceASMX() throws IOException {
        //获取webservice接口地址
        String url = "http://183.62.131.97:9880/MES/mes_k3.asmx";
        //获取域名地址，server定义的，一般默认是http://tempuri.org/，如果不对，找对方索要
        String soapaction = "http://tempuri.org/";
        String method = "MES_K3";
        Service service = new Service();
        String result = "";
        try {
            Call call = (Call) service.createCall();
            call.setTargetEndpointAddress(url);
            call.addHeader(initSOAPHeaderElement());
            //设置要调用的方法
            call.setOperationName(new QName(soapaction, method));
            call.addParameter(new QName(soapaction, "type"), XMLType.XSD_STRING, ParameterMode.IN);
            call.addParameter(new QName(soapaction, "data"), XMLType.XSD_ENTITY, ParameterMode.IN);
            call.setEncodingStyle(null);
            //设置要返回的数据类型（标准的类型）
            call.setReturnType(XMLType.XSD_STRING);
            call.setUseSOAPAction(true);
            call.setSOAPVersion(SOAPConstants.SOAP12_CONSTANTS);
            call.setSOAPActionURI(soapaction + method);
            call.setEncodingStyle("GB2312");
            //调用方法并传递参数
            String type = "SUB_PICKMTRL";

            SubPickmtrlReq req = new SubPickmtrlReq();
            req.setSUPPLIERNO("D.02.008");
            List<SubPickmtrlReq.SubPickmtrlENTRY> entryList = new ArrayList<SubPickmtrlReq.SubPickmtrlENTRY>();
            SubPickmtrlReq.SubPickmtrlENTRY entry = new SubPickmtrlReq.SubPickmtrlENTRY();
            entry.setENTRY_FNUMBER("01.EC0.3R3C201H10A");
            entry.setENTRY_Unitno("Pcs");
            entry.setENTRY_FActualQty(30);
            entry.setENTRY_StockNO("20.01");
            entry.setENTRY_ParentNO("02.PMA.D1C8192B003");
            entry.setENTRY_FSrcInterId((long)147784);
            entry.setENTRY_FSrcInterId((long)147784);
            entry.setENTRY_FSrcEntryId((long)4860434);
            entry.setENTRY_FSubReqId((long)127323) ;
            entry.setENTRY_FSRCBIZENTRYID((long)138737) ;
            entry.setENTRY_FSubReqEntryId((long)138737) ;
            entry.setENTRY_FPPbomBillNo("SUBBOM00035139");
            entry.setENTRY_FPOOrderBillNo("P202109160004");
            entry.setENTRY_FPOOrderSeq(2);
            entry.setENTRY_FSubReqEntrySeq(1);
            entry.setENTRY_FSUBREQBILLNO("W20210916023");
            entry.setENTRY_FSRCENTRYSEQ(1);
            entryList.add(entry);
            req.setENTRY(entryList);
            System.out.println(req.toJSONString());
           System.out.println( call.invoke(new Object[]{type, req.toJSONString()}));
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    private static SOAPHeaderElement initSOAPHeaderElement(){
        SOAPHeaderElement soapHeaderElement = new SOAPHeaderElement(new QName("http://tempuri.org/","UserValidationSoapHeader"));
        try {
           SOAPElement element = soapHeaderElement.addChildElement("UserName");
            element.addTextNode("YR_MES");
            element = soapHeaderElement.addChildElement("Password");
            element.addTextNode("YR_MES!#%123");
            return soapHeaderElement;

        } catch (SOAPException e) {

            e.printStackTrace();
        }
        return null;

    }
}
