package com.bocloud.ims.service.internal;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.bocloud.common.enums.BoCloudMethod;
import com.bocloud.common.enums.BoCloudService;
import com.bocloud.common.model.BocloudResult;
import com.bocloud.common.model.BocloudToken;
import com.bocloud.common.utils.MapTools;
import com.bocloud.service.core.CurrentService;
import com.bocloud.service.core.Service;
import com.bocloud.service.http.core.ServiceFactory;
import com.bocloud.service.http.model.RemoteService;

/**
 * cop服务内部访问
 *
 * @author weiwei
 * @version 1.0
 * @since 2021年5月10日
 */
@Component
public class CopInternalService implements InitializingBean {

	@Autowired
	private ServiceFactory serviceFactory;
	@Autowired
	private CurrentService currentService;
	private static Service service = Service.create(BoCloudService.COP);
	private Map<String, Object> headers = new HashMap<>(4);

	public BocloudResult stats(String code, String dataType) {
		Map<String, Object> paramMap = MapTools.simpleMap("code", code);
		paramMap.put("dataType", dataType);
		RemoteService remote = serviceFactory.build(service, "/v1/tasks/stats", BoCloudMethod.GET, headers, paramMap);
		return remote.invoke();
	}

	@Override
	public void afterPropertiesSet() throws Exception {
		this.headers.put(BocloudToken.INTERNAL_TOKEN, currentService.getToken());
		this.headers.put("source", currentService.getService().getName());
	}
}
