package com.ruoyi.web.webservice.contract;

import com.ruoyi.contract.domain.Contract;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

import java.net.Authenticator;
import java.net.PasswordAuthentication;
import java.util.List;

@Component
@PropertySource("classpath:/common.yml")
public class contractUtils {

    private static String user;
    public static String getUser() {
        return user;
    }

    @Value("${sapWebservice.user}")
    public void setUser(String user) {
        contractUtils.user = user;
    }

    private static String psw;
    public static String getPsw() {
        return psw;
    }

    @Value("${sapWebservice.psw}")
    public void setPsw(String psw) {
        contractUtils.psw = psw;
    }

    private static String host;
    public static String getHost() {
        return host;
    }

    @Value("${sapWebservice.host}")
    public void setHost(String host) {
        contractUtils.host = host;
    }

    public static List<DTOASDHTCXRESP.HTDATA> data(Contract contract) {
        Authenticator.setDefault(new Authenticator() {

            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(user, psw.toCharArray());
            }
        });

        SIOASDHTCXOUTService ss = new SIOASDHTCXOUTService();
        ss.setHost(host);

        SIOASDHTCXOUT si = ss.getHTTPPort();
        DTOASDHTCXREQ d = new DTOASDHTCXREQ();

        String beginDate = contract.getContractDateRange()[0];
        beginDate = beginDate.substring(0, 4) + beginDate.substring(5, 7) + beginDate.substring(8);

        String endDate = contract.getContractDateRange()[1];
        endDate = endDate.substring(0, 4) + endDate.substring(5, 7) + endDate.substring(8);

        d.setHTQDRQS(beginDate);
        d.setHTQDRQE(endDate);
        d.setKUNNR(contract.getContractCustom());
        d.setHTBH2(contract.getContractNo());
        d.setZYWY(contract.getContractSalesmanNo());

        DTOASDHTCXRESP dt = si.siOASDHTCXOUT(d);
        List<DTOASDHTCXRESP.HTDATA> HTDATA = dt.getHTDATA();
        return HTDATA;

    }
}
