package com.prolog.cs.biz.outbound.service.impl.platform;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.Method;
import com.alibaba.fastjson.JSONObject;
import com.prolog.framework.common.message.RestMessage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.validation.constraints.NotBlank;

import static cn.hutool.http.HttpRequest.post;
import static com.prolog.constant.EisInterfaceConstant.INTEGRATION_PREFIX;

@Slf4j
@Component
public class InterfacePlatform {

	@Value("${prolog.integration.url}")
	private String integrationUrl;


	public RestMessage call(String url, String params) {

		String fullUrl = getUrl(url);

		return invokeInterfacePlatform(params, fullUrl, Method.POST);
	}

	private String getUrl(@NotBlank String url) {
		return integrationUrl + INTEGRATION_PREFIX + url;
	}


	/**
	 * 拼装请求地址
	 */
	private RestMessage invokeInterfacePlatform(@NotBlank String params, @NotBlank String url, Method method) {
		log.info("url:{},params:{}", url, params);
		HttpRequest request;
		if (method == Method.POST) {
			request = post(url);
		} else {
			request = new HttpRequest(url);
		}
		String response = request.body(params).execute().body();
		log.info("response:{}", response);
		return JSONObject.parseObject(response, RestMessage.class);
	}

}
