package com.jwipc.job.handler;

import com.jwipc.job.prop.JobProp;
import com.jwipc.job.util.SSLClientCustom;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpResponse;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.util.EntityUtils;
import org.quartz.*;
import org.springframework.scheduling.quartz.QuartzJobBean;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.Calendar;

/**
 * 每10分执行一次，从redis缓存中取sap推过来的数据，直到取完为止。
 */
@PersistJobDataAfterExecution
@DisallowConcurrentExecution
@Slf4j
@Component
//@RequiredArgsConstructor
public class CreateShortagePlanHanaDataJob extends QuartzJobBean implements InterruptableJob {

    @Resource
    private JobProp jobProp;
    @Override
    public void interrupt() throws UnableToInterruptJobException {

    }

    @Override
    protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
        Calendar clBegin = Calendar.getInstance();
        log.info("开始抽取欠料计划数据。。。。" + clBegin.getTime());
        String host =  "http://10.100.103.71:8888/sap/api/createShortageHanaPlan";
        log.info("host==>" + host);//        String host = "http://srm.jwipc.com:8888/sap/api/createShortageHanaPlan";
        HttpPost method = null;
        CloseableHttpClient httpclient = null;
        try {
            httpclient = SSLClientCustom.getHttpClient();
            method = new HttpPost(host);
            RequestConfig requestConfig = RequestConfig.custom().setConnectionRequestTimeout(10000)
                    .setSocketTimeout(10000).setConnectTimeout(20000).build();
            method.setConfig(requestConfig);
            final String encodeToString = Base64.getEncoder().encodeToString(("srm" + ":" + "JWIPC@2024!jwipc").getBytes());
            String authHeader = "Basic " + encodeToString;
//            Map<String, String> headers = new HashMap<>();
//            headers.put("Content-Type", "application/json");
//            headers.put("Authorization", authHeader);
            method.setHeader("Content-Type", "application/json");
            method.setHeader("Authorization", authHeader);
            HttpResponse response = httpclient.execute(method);
            String content = EntityUtils.toString(response.getEntity(),  StandardCharsets.UTF_8);
            if(content==null){
//                return new JSONObject();
            }
//            result= JSON.parseObject(content);
            // 完全消耗
            EntityUtils.consume(response.getEntity());
//            return result;
        } catch (Exception e) {
            e.printStackTrace();
            log.error("httppost上传报错:"+host,e.getMessage());
//            result.put("code",202);
//            result.put(MESSAGE,e.getMessage());
        } finally {
            try {
                method.releaseConnection();
            } catch (Exception e) {
                log.error("httppost 释放连接出错", e);
            }
            try {
                httpclient.close();
            } catch (IOException e) {
                log.error("httppost 关闭连接出错", e);
            }
        }
        Calendar clEnd = Calendar.getInstance();
        log.info("抽取欠料数据成功，用时" + (clEnd.getTimeInMillis() - clBegin.getTimeInMillis()) / 1000);
    }
}
