import com.alibaba.fastjson.JSON;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import java.io.Serializable;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class BaseMapEntity extends LinkedHashMap implements Serializable {
private static final long serialVersionUID = 1L;
/** 日志 */
protected final Logger logger = LogManager.getLogger(this.getClass());
/**
* 根据map键获取对应的int类型的值
*
* @param mapKey map键
* @return map值
*/
public Integer getInt(String mapKey) {
// 根据key获取值
Object mapValue = this.get(mapKey);
Integer mapValueInt = null;
try {
if (null == mapValue) {
mapValueInt = null;
} else if (mapValue instanceof Integer) {
mapValueInt = (Integer) mapValue;
} else if (mapValue instanceof String) {
mapValueInt = Integer.parseInt((String) mapValue);
} else {
mapValueInt = Integer.parseInt(String.valueOf(mapValue));
}
} catch (Exception e) {
logger.warn("根据map键[" + mapKey + "]获取对应的int类型的值异常,原值[" + mapValue + "]");
}
// 返回结果
return mapValueInt;
}
public int getInt(String mapKey,int defaultValue) {
Integer integer = getInt(mapKey);
if(integer == null)
return defaultValue;
return integer;
}
/**
* 根据map键获取对应的long类型的值
* @param mapKey map键
* @return map值
*/
public Long getLong(String mapKey) {
// 根据key获取值
Object mapValue = this.get(mapKey);
Long mapValueLong = null;
try {
if (null == mapValue) {
mapValueLong = null;
} else if (mapValue instanceof Long) {
mapValueLong = (Long) mapValue;
} else if (mapValue instanceof String) {
mapValueLong = Long.parseLong((String) mapValue);
} else {
mapValueLong = Long.parseLong(String.valueOf(mapValue));
}
} catch (Exception e) {
logger.warn("根据map键[" + mapKey + "]获取对应的long类型的值异常,原值[" + mapValue + "]");
}
// 返回结果
return mapValueLong;
}
/**
* 根据map键获取对应的double类型的值
* @param mapKey map键
* @return map值
*/
public Double getDouble(String mapKey) {
// 根据key获取值
Object mapValue = this.get(mapKey);
Double mapValueDouble = null;
try {
if (null == mapValue) {
mapValueDouble = null;
} else if (mapValue instanceof Double) {
mapValueDouble = (Double) mapValue;
} else if (mapValue instanceof String) {
mapValueDouble = Double.parseDouble((String) mapValue);
} else {
mapValueDouble = Double.parseDouble(String.valueOf(mapValue));
}
} catch (Exception e) {
logger.warn("根据map键[" + mapKey + "]获取对应的double类型的值异常,原值[" + mapValue + "]");
}
// 返回结果
return mapValueDouble;
}
/**
* 根据map键获取对应的String类型的值
*
* @param mapKey map键
* @return map值
*/
public String getString(String mapKey) {
//定义返回结果变量
String resultStr = null;
// 根据key获取值
Object mapValue = this.get(mapKey);
// 变量赋值
if(null == mapValue){
resultStr = null;
} else if (mapValue instanceof String) {
resultStr = (String) mapValue;
}else{
resultStr = String.valueOf(mapValue);
}
// 返回结果
return resultStr;
}
/**
* 根据map键获取对应的Date类型的值
* @param mapKey map键
* @return map值
*/
public Date getDate(String mapKey) {
//定义返回结果变量
Date resultDate = null;
// 根据key获取值
Object mapValue = this.get(mapKey);
try {
// 变量赋值
if(null == mapValue){
resultDate = null;
} else if (mapValue instanceof Date) {
resultDate = (Date) mapValue;
}else{
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
resultDate = sdf.parse(String.valueOf(mapValue));
}
} catch (Exception e) {
logger.warn("根据map键[" + mapKey + "]获取对应的Date类型的值异常,原值[" + mapValue + "]");
}
// 返回结果
return resultDate;
}
/**
* 根据map键获取对应的BigDecimal类型的值
* @param mapKey map键
* @return map值
*/
public BigDecimal getBigDecimal(String mapKey) {
//定义返回结果变量
BigDecimal resultBig = null;
// 根据key获取值
Object mapValue = this.get(mapKey);
try {
// 变量赋值
if(null == mapValue){
resultBig = null;
}else if (mapValue instanceof BigDecimal) {
resultBig = (BigDecimal) mapValue;
}else{
resultBig = new BigDecimal(String.valueOf(mapValue));
}
} catch (Exception e) {
logger.warn("根据map键[" + mapKey + "]获取对应的BigDecimal类型的值异常,原值[" + mapValue + "]");
}
// 返回结果
return resultBig;
}
/**
* 根据map键获取对应类型的值
* @param mapKey map键
* @param classType 类型
* @return map值
*/
public <T> T getObject(String mapKey,Class<T> classType) {
//定义返回结果变量
T resultObject = null;
// 根据key获取值
Object mapValue = this.get(mapKey);
try {
// 变量赋值
if(null == mapValue){
resultObject = null;
}else if (classType.isInstance(mapValue)) {
resultObject = (T) mapValue;
}else{
if(classType == BigDecimal.class){
resultObject = (T)getBigDecimal(mapKey);
}else if(classType == Date.class){
resultObject = (T)getDate(mapKey);
}
}
} catch (Exception e) {
logger.warn("根据map键[" + mapKey + "]获取对应的" + classType + "类型的值异常,原值[" + mapValue + "]");
}
// 返回结果
return resultObject;
}
public void reloadForPage(){
Integer pageStart = this.getInt("start");
Integer pageSize = this.getInt("pageSize");
if(null == pageSize){
return;
}
if(null == pageStart){
Integer pageNo = this.getInt("pageIndex");
if(null == pageNo){
return;
}
pageStart = pageNo * pageSize;
}
int pageLength = pageSize;
this.put("pageStart",pageStart);
this.put("pageLength",pageLength);
}
/**
* 将下划线的字符串转化为驼峰命名
* @param column
* @return
*/
private String renameForUnderlineToHump(String column) {
if(null == column){
return column;
}
if(!column.contains("_")){
return column;
}
column = column.toLowerCase();
StringBuffer sb = new StringBuffer();
Pattern p = Pattern.compile("_(\\w)");
Matcher m = p.matcher(column);
while (m.find()){
m.appendReplacement(sb,m.group(1).toUpperCase());
}
m.appendTail(sb);
return sb.toString();
}
/**
* 重写put方法
* @param key
* @param value
* @return
*/
@Override
public Object put(Object key, Object value) {
if(value instanceof Date){
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
value = sdf.format(value);
}
if(key instanceof String){
String keyStr = (String) key;
String newKey = renameForUnderlineToHump(keyStr);
return super.put(newKey,value);
}
return super.put(key, value);
}
/**
* 重写get方法
* @param key
* @return
*/
@Override
public Object get(Object key) {
if(key instanceof String){
String keyStr = (String) key;
String newKey = renameForUnderlineToHump(keyStr);
return super.get(newKey);
}
return super.get(key);
}
/**
* map转BaseMapEntity
* @param map map对象
* @return BaseMapEntity
*/
public static BaseMapEntity getBaseMapEntity(Map<String, Object> map) {
// 定义resultMapVO
BaseMapEntity resultMapVO = new BaseMapEntity();
if (null == map) {
return resultMapVO;
}
// 将map放入resultMapVO中
resultMapVO.putAll(map);
// 返回结果
return resultMapVO;
}
/**
* obj转BaseMapEntity
* @param obj map对象
* @return BaseMapEntity
*/
public static BaseMapEntity getBaseMapEntityForObj(Object obj) {
if(null == obj){
return null;
}
String objStr = JSON.toJSONString(obj);
BaseMapEntity resultMapVO = JSON.parseObject(objStr,BaseMapEntity.class);
// 返回结果
return resultMapVO;
}
/**
* 转化成页面需要的格式
*/
public void tranToCMSPageObj(){
this.put("recordsTotal",this.get("total"));
this.put("recordsFiltered",this.get("total"));
this.put("start",this.get("pageStart"));
this.put("length",this.get("pageLength"));
}
}