net.wicp.tams.cas.convert.ConvertValueOpt Maven / Gradle / Ivy
package net.wicp.tams.cas.convert;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;
import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.collections.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import lombok.extern.slf4j.Slf4j;
import net.wicp.tams.cas.bean.models.SysOptitem;
import net.wicp.tams.cas.dao.SysOptitemMapper;
import net.wicp.tams.common.apiext.StringUtil;
import net.wicp.tams.common.callback.IConvertValue;
import net.wicp.tams.common.redis.pool.AbsPool;
import net.wicp.tams.component.services.ISupportedLocales;
import redis.clients.jedis.Jedis;
/***
* 从redis上取国际化数据,没取到就取数据库再放redis
*
* @author Andy.zhou
*
*/
@Slf4j
public class ConvertValueOpt implements IConvertValue {
private final String lan;
@Autowired
private AbsPool absPool;
@Autowired
private SysOptitemMapper sysOptitemMapper;
public ConvertValueOpt(ISupportedLocales supportedLocales) {
String curLan = supportedLocales.getCurLocale().getLanguage();
this.lan = StringUtil.toUpperCaseFirstOne(curLan);
}
@Override
public String getStr(Long key) {
if (StringUtil.isNull(key)) {
return "";
}
Jedis jedis = absPool.getResource();
try {
String colNameForRedis = String.format("name%s", lan);
Map mapByField = AbsPool.getMapByField(jedis, String.valueOf(key), colNameForRedis);
if (MapUtils.isNotEmpty(mapByField)) {
return mapByField.get(colNameForRedis);
}
SysOptitem opt = sysOptitemMapper.selectByPrimaryKey(key);
if (opt != null) {
AbsPool.putObjByMap(jedis, String.valueOf(key), opt);
}
Object retobj = PropertyUtils.getProperty(opt, colNameForRedis);
return String.valueOf(retobj);
} catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
log.error("option:" + key + " get error", e);
return String.valueOf(key);
} finally {
AbsPool.returnResource(jedis);
}
}
}