package com.bocloud.ims.service.internal;

import com.alibaba.fastjson.JSONObject;
import com.bocloud.common.enums.BoCloudMethod;
import com.bocloud.common.enums.BoCloudService;
import com.bocloud.common.model.BocloudResult;
import com.bocloud.common.model.BocloudResult;
import com.bocloud.common.model.BocloudToken;
import com.bocloud.ims.entity.common.Department;
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;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

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

/**
 * SOC组织机构访问
 * 
 * @author wangyu
 * @version 1.0
 * @since 2020年2月5日
 *
 */
@Component
public class SmsDepartmentService 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 BocloudResult<Department> detail(Long id) {
		RemoteService remote = serviceFactory.build(service, "/v1/departments/" + id, BoCloudMethod.GET, headers, null);
		BocloudResult result = remote.invoke();
		if (result.isFailed()) {
			return new BocloudResult<>(false, result.getMessage());
		}
		String data = JSONObject.toJSONString(result.getData());
		return new BocloudResult<>(true, JSONObject.parseObject(data, Department.class), "success");
	}

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