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.Common;
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 4.1.0-SNAPSHOT
 * @since 2019/4/15
 */
@Component
public class SmsSystemConfigService 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 getByCode(String code) {
		RemoteService remote = serviceFactory.build(service, "/v1/sysconfigs", BoCloudMethod.GET, headers,
				MapTools.simpleMap(Common.CODE, code));
		BocloudResult result = remote.invoke();
		if (result.isFailed() || null == result.getData()) {
			return null;
		}
		return String.valueOf(result.getData());
	}

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