package com.yihu.hos.services;

import com.yihu.hos.common.configuration.GatewayConfiguration;
import com.yihu.hos.core.constants.ExceptionConstant;
import com.yihu.hos.core.exception.ESBException;
import com.yihu.hos.core.http.HttpClientUtil;
import com.yihu.hos.models.GatewayRequestResult;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * Created by chenweida on 2016/1/27.
 */
@Service("GatewayService")
public class GatewayService {
    public static final String BEAN_ID = "GatewayService";

    @Autowired
    private GatewayConfiguration gatewayConfiguration;

    public String getResultData(GatewayRequestResult gatewayRequestResult) throws ESBException {

        String returnData;
        //拼凑出URL
        String url =  HttpClientUtil.getUrl(gatewayConfiguration.getIp(), gatewayConfiguration.getPort(), gatewayRequestResult.getApi());
        try {
            returnData = HttpClientUtil.doPost(url, JSONObject.fromObject(gatewayRequestResult.getParam()), null, null);
            System.out.print(JSONObject.fromObject(gatewayRequestResult.getParam()));
            returnData = HttpClientUtil.doGet(url, JSONObject.fromObject(gatewayRequestResult.getParam()), null, null);
        } catch (Exception e) {
            throw new ESBException(ExceptionConstant.EHREXCEPTION_SYSTEM_TRANSFER, ExceptionConstant.EHREXCEPTION_SYSTEM_TRANSFER_MESSAGE);
        }
        return returnData;
    }

    public void paramsIsNotNull(Object params) throws ESBException {
        if (StringUtils.isEmpty(params)) {
            throw new ESBException(ExceptionConstant.EHREXCEPTION_BUSINESS_PARAMS_EXCEPTION, ExceptionConstant.EHREXCEPTION_BUSINESS_PARAMS_EXCEPTION_MESSAGE);
        }
    }
}
