package com.weaver.seconddev.interfaces.pmoa;

import com.weaver.seconddev.interfaces.prop.ConfigProp;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import java.util.Base64;
import com.weaver.common.i18n.tool.util.I18nContextUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import java.lang.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.weaver.verupgrade.general.BaseBean;
import com.weaver.verupgrade.general.Util;
import org.springframework.stereotype.Component;

@Component
public class PMService extends BaseBean {
    @Autowired
    private ConfigProp configProp;
    private static final org.slf4j.Logger logger_8a709bef = LoggerFactory.getLogger(PMService.class);

    public String call(String json) {
        logger_8a709bef.error("PMService  call start " );
        HttpPost post = null;
        String result = "E";
        try {
            CloseableHttpClient httpClient = HttpClients.createDefault();
//            String url = Util.null2String(I18nContextUtilcus.getBean(Environment.class).getProperty("htpmoasync.projectConclusion")).trim();
//            String account = Util.null2String(I18nContextUtilcus.getBean(Environment.class).getProperty("htpmoasync.account")).trim();
//            String password = Util.null2String(I18nContextUtilcus.getBean(Environment.class).getProperty("htpmoasync.password")).trim();
            String url = configProp.getProjectConclusion();
            String account = configProp.getHtpmoasync_account();
            String password = configProp.getHtpmoasync_password();
            logger_8a709bef.error("url = "+ url );
            logger_8a709bef.error("account = "+ account );
            logger_8a709bef.error("password = "+ password );

            logger_8a709bef.error("json = "+ json );
            post = new HttpPost(url);
            post.setHeader(HTTP.CONTENT_TYPE, "application/json; charset=UTF-8");
            post.setHeader("Accept", "application/json; charset=UTF-8");
            post.setHeader("Authorization", "basic " + Base64.getEncoder().encodeToString((account + ":" + password).getBytes()));
            StringEntity entity = new StringEntity(json, "UTF-8");
            post.setEntity(entity);


            HttpResponse response = httpClient.execute(post);
            int rspCode = response.getStatusLine().getStatusCode();
            logger_8a709bef.error(String.valueOf("调用PM系统接口返回状态码：" + rspCode));
            if (rspCode == 200) {
                result = EntityUtils.toString(response.getEntity(), "UTF-8");
            }
        } catch (Exception e) {
            logger_8a709bef.error(String.valueOf("调用PM系统接口出错" + e.toString()));
        } finally {
            if (post != null) {
                post.releaseConnection();
            }
        }
        return result;
    }
}
