package com.malk.mcli.config;

import com.malk.mcli.service.IH3yunWebService;
import com.malk.mcli.service.impl.H3yunWebServiceImpl;
import org.apache.cxf.Bus;
import org.apache.cxf.bus.spring.SpringBus;
import org.apache.cxf.jaxws.EndpointImpl;
import org.apache.cxf.phase.Phase;
import org.apache.cxf.transport.servlet.CXFServlet;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.xml.ws.Endpoint;


@Configuration
public class CxfConfig {

    /**
     * 访问路径: http://localhost:9001/api/webService/api?wsdl
     * -
     * invoke: mc可自定义, test为方法名称, arg0第一次参数
     * -
     * <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:mc="http://service.hangshi.malk.com">
     * 　　<soap:Body>
     * 　　　　<mc:test>
     * 　　　　　　<arg0>[{"assetNo":"1","barCode":"1","belongDept":"1","boxBarCode":"1","cap":"1","chkDate"madeDate":158688000000}]</arg0>
     * 　　　　</mc:test>
     * 　　</soap:Body>
     * </soap:Envelope>
     * -
     *
     * @WebParam 自定义参数名称, 否则就是 arg0, arg1, ...
     */

    @Bean
    public ServletRegistrationBean disServlet() {
        return new ServletRegistrationBean(new CXFServlet(), "/hangshi/ws/*");
    }

    @Bean(name = Bus.DEFAULT_BUS_ID)
    public SpringBus springBus() {
        return new SpringBus();
    }

//    @Bean
//    public InvoiceWebService demoService() {
//        return new InvoiceWebServiceImpl();
//    }

    @Bean
    public IH3yunWebService demoService() {
        return new H3yunWebServiceImpl();
    }

    @Bean
    public Endpoint endpoint() {
        EndpointImpl endpoint = new EndpointImpl(springBus(), demoService());
        // 请求拦截器
        endpoint.getInInterceptors().add(new WsInInterceptor(Phase.RECEIVE));
        // 响应拦截器
        endpoint.getOutInterceptors().add(new WsOutInterceptor(Phase.PRE_STREAM));
        endpoint.publish("/invoke.asmx");
        return endpoint;
    }

}