org.zodiac.mybatisplus.binding.binder.remote.RemoteBindingManager Maven / Gradle / Ivy
package org.zodiac.mybatisplus.binding.binder.remote;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.zodiac.commons.api.R;
import org.zodiac.commons.support.SpringContextHolder;
import org.zodiac.commons.util.serialize.JsonUtil;
import org.zodiac.core.exception.IllegalUsageException;
import org.zodiac.mybatisplus.binding.JoinsBinder;
/**
* 远程绑定manager。
*
*/
public class RemoteBindingManager {
private static Logger log = LoggerFactory.getLogger(JoinsBinder.class);
/**
* 远程绑定接口 实例缓存。
*/
private static Map MODULE_PROVIDER_MAP;
/**
* 从远程接口抓取 Entity List。
*
* @param module 模块
* @param remoteBindDTO 绑定DTO
* @param entityClass 实体类型
* @param 类型
* @return 实体列表
*/
public static List fetchEntityList(String module, RemoteBindDTO remoteBindDTO, Class entityClass) {
remoteBindDTO.setResultType("Entity");
RemoteBindingProvider bindingProvider = getRemoteBindingProvider(module);
R result = bindingProvider.loadBindingData(remoteBindDTO);
//if (ObjectUtil.eq(jsonResult.getResultCode().getStatusCode(), HttpResponseStatus.OK.value())) {
if (result.getResultCode().isSuccess()) {
log.debug("获取到绑定数据: {}", result.getData());
List entityList = JsonUtil.toObjectList(result.getData(), entityClass);
return entityList;
} else {
log.warn("获取绑定数据失败: {}", result.getMsg());
return Collections.emptyList();
}
}
/**
* 获取远程绑定接口。
*
* @param module 模块
* @return {@link RemoteBindingProvider}对象
*/
private synchronized static RemoteBindingProvider getRemoteBindingProvider(String module) {
if (MODULE_PROVIDER_MAP == null) {
MODULE_PROVIDER_MAP = new ConcurrentHashMap<>();
}
return MODULE_PROVIDER_MAP.computeIfAbsent(module, key -> {
RemoteBindingProviderFactory factory = SpringContextHolder.getBean(RemoteBindingProviderFactory.class);
if (factory == null) {
throw new IllegalUsageException("RemoteBindingProviderFactory 未实现,无法使用远程绑定功能!");
}
return factory.create(module);
});
}
}