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;

/**
 * 每个月1号凌晨1点执行一次,生成当月的付款计划。
 */
@PersistJobDataAfterExecution
@DisallowConcurrentExecution
@Slf4j
@Component
public class CreatePaymentPlanDataJob extends QuartzJobBean implements InterruptableJob {

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

    }

    @Override
    protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
        java.util.Calendar clBegin = java.util.Calendar.getInstance();
        log.info("每个月1号凌晨1点执行一次,生成当月采购付款计划." + clBegin.getTime());
        String host = jobProp.getHostUrl() + "/sap/api/createPaymentPlan";
        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;
            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());
        } finally {
            try {
                method.releaseConnection();
            } catch (Exception e) {
                log.error("httppost 释放连接出错", e);
            }
            try {
                httpclient.close();
            } catch (IOException e) {
                log.error("httppost 关闭连接出错", e);
            }
        }
        java.util.Calendar clEnd = Calendar.getInstance();
        log.info("生成付款计划数据成功，用时" + (clEnd.getTimeInMillis() - clBegin.getTimeInMillis()) / 1000);
    }
}
