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;

/**
 * SOC内部访问
 * 
 * @author wangyu
 * @version 1.0
 * @since 2020年2月5日
 *
 */
@Component
public class SmsInternalService implements InitializingBean {

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

	public String getToken(Long targetId, String catalog) {
		Map<String, Object> paramMap = MapTools.simpleMap("targetId", targetId);
		paramMap.put("catalog", catalog);
		RemoteService remoteService = serviceFactory.build(service, "/v1/certification", BoCloudMethod.GET, headers,
				paramMap);
		BocloudResult result = remoteService.invoke();
		if (result.isFailed()) {
			return null;
		}
		return String.valueOf(result.getData());
	}

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

}
