package com.weaver.seconddev.interfaces.eipoa;

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

@Component
public class GetSystemList extends BaseBean {

    private static final Logger logger_a4ce8d9d = LoggerFactory.getLogger(GetSystemList.class);
    @Autowired
    private ConfigProp configProp;
    public String getList(String uid) {
        HttpPost post = null;
        String result = "E";
        try {
            HttpClient client = new DefaultHttpClient();
//            String url = Util.null2String(I18nContextUtilcus.getBean(Environment.class).getProperty("htportaloaUrl.getSystemListUrl")).trim();
            String url =configProp.getGetSystemListUrl();
            logger_a4ce8d9d.error(String.valueOf("GetSystemList start uid == " + uid));
            logger_a4ce8d9d.error(String.valueOf("GetSystemList url == " + url));
            logger_a4ce8d9d.error(String.valueOf("GetSystemList getUrl_password == " + configProp.getUrl_password()));
            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", Util.null2String(I18nContextUtilcus.getBean(Environment.class).getProperty("htportaloaUrl.url_password")).trim());
            post.setHeader("Authorization", configProp.getUrl_password());
            JSONObject returnJson = new JSONObject();
            returnJson.put("doAction", "listSystem");
            returnJson.put("uid", uid);
            logger_a4ce8d9d.error(String.valueOf("GetSystemList returnJson == " + returnJson));
            StringEntity entity = new StringEntity(returnJson.toString(), "UTF-8");
            post.setEntity(entity);
            HttpResponse response = client.execute(post);
            int rspCode = response.getStatusLine().getStatusCode();
            if (rspCode == 200) {
                result = EntityUtils.toString(response.getEntity(), "UTF-8");
            }
        } catch (Exception e) {
            logger_a4ce8d9d.error(String.valueOf("调用门户查询已开户系统接口出错" + e.toString()));
        } finally {
            if (post != null) {
                post.releaseConnection();
            }
        }
        return result;
    }
}
