ZimPlatfProject/src/com/zchd/base/BaseService.java

88 lines
2.6 KiB
Java

package com.zchd.base;
import com.zchd.base.util.Utils;
import net.tccn.zhub.ZHubClient;
import org.redkale.convert.json.JsonConvert;
import org.redkale.service.AbstractService;
import org.redkale.service.RetResult;
import org.redkale.source.DataJdbcSource;
import org.redkale.source.DataSource;
import org.redkale.source.PoolSource;
import org.redkale.util.AnyValue;
import org.redkale.util.Sheet;
import javax.annotation.Resource;
import java.io.File;
import java.sql.Connection;
import java.util.List;
import java.util.logging.Logger;
public class BaseService extends AbstractService {
protected final Logger logger = Logger.getLogger(this.getClass().getSimpleName());
protected final static RetResult RET_SUCCESS = RetResult.success();
protected final static RetResult RET_EMPTY_SHEET = RetResult.success(new Sheet<>(0, List.of()));
protected final static RetResult RET_EMPTY_LIST = RetResult.success(List.of());
protected final JsonConvert convert = JsonConvert.root();
@Resource(name = "zhub")
protected ZHubClient zhub;
/*@Resource(name = "redis")
protected RedissionCacheSourcex<Integer> intCache;
@Resource(name = "redis")
protected RedissionCacheSourcex<Long> longCache;*/
@Resource(name = "redis")
protected RedissionCacheSourcex redisCache;
@Resource(name = "z_im")
protected DataSource zimSource;
@Resource(name = "APP_HOME")
protected File APP_HOME;
@Resource(name = "APP_NAME")
protected String APP_NAME = "";
@Override
public void init(AnyValue config) {
PoolSource<Connection> source = ((DataJdbcSource) zimSource).getReadPoolSource();
// postgresql 设置当前schema
if ("postgresql".equals(source.getDbtype())) {
String url = source.getUrl();
String schema = getCurrentSchema(url);
if (Utils.isEmpty(schema)) {
schema = "public";
}
((DataJdbcSource) zimSource).directExecute(String.format("SET search_path TO %s;", schema));
}
}
protected RetResult retError(String info) {
return new RetResult<>(100, info);
}
protected RetResult retError(int code, String info) {
return new RetResult<>(code, info);
}
/**
* 获取 postgresql url当前schema
*
* @param url
* @return
*/
protected String getCurrentSchema(String url) {
String[] arr = url.split("currentSchema=");
if (arr.length > 1) {
String[] arr2 = arr[1].split("&");
if (arr2.length > 0) {
return arr2[0];
}
}
return "";
}
}