package com.vci.ubcs.starter.web.util;
|
|
//
|
// Source code recreated from a .class file by IntelliJ IDEA
|
// (powered by FernFlower decompiler)
|
//
|
|
import cn.hutool.core.lang.Snowflake;
|
import cn.hutool.core.util.IdUtil;
|
import com.alibaba.fastjson.JSONObject;
|
import com.alibaba.fastjson.serializer.SerializerFeature;
|
import com.baomidou.mybatisplus.annotation.TableField;
|
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
|
import com.vci.ubcs.starter.exception.VciBaseException;
|
import com.vci.ubcs.starter.web.enumpck.BooleanEnum;
|
import com.vci.ubcs.starter.web.enumpck.UserSecretEnum;
|
import com.vci.ubcs.starter.web.pagemodel.SessionInfo;
|
import com.vci.ubcs.starter.web.toolmodel.DateConverter;
|
import com.vci.ubcs.starter.web.wrapper.VciQueryWrapperForDO;
|
import org.slf4j.Logger;
|
import org.slf4j.LoggerFactory;
|
import org.springblade.core.tool.utils.Func;
|
import org.springframework.util.CollectionUtils;
|
import org.springframework.util.ResourceUtils;
|
|
import java.beans.BeanInfo;
|
import java.beans.IntrospectionException;
|
import java.beans.Introspector;
|
import java.beans.PropertyDescriptor;
|
import java.io.File;
|
import java.io.IOException;
|
import java.lang.Character.UnicodeBlock;
|
import java.lang.annotation.Annotation;
|
import java.lang.reflect.Field;
|
import java.lang.reflect.InvocationTargetException;
|
import java.lang.reflect.Method;
|
import java.math.BigDecimal;
|
import java.math.BigInteger;
|
import java.net.InetAddress;
|
import java.net.NetworkInterface;
|
import java.util.*;
|
import java.util.regex.Matcher;
|
import java.util.regex.Pattern;
|
import java.util.stream.Collectors;
|
|
public class VciBaseUtil {
|
|
private static Logger log = LoggerFactory.getLogger(VciBaseUtil.class);
|
public static final String NOTIN = "not in";
|
private static String localIp = null;
|
|
public VciBaseUtil() {
|
}
|
|
public static String getPk() {
|
return String.valueOf(getPKLong(1,1));
|
}
|
|
public static Long getPKLong(long workerId,long dataCenterId){
|
Snowflake snowflake = IdUtil.getSnowflake(workerId,dataCenterId);
|
return snowflake.nextId();
|
}
|
|
public static int getIntForBoolean(boolean b) {
|
return b ? 1 : 0;
|
}
|
|
public static int getIntFromHibernateResult(Object obj) {
|
if (obj == null) {
|
return 0;
|
} else if (obj instanceof BigDecimal) {
|
return ((BigDecimal)obj).intValue();
|
} else if (obj instanceof BigInteger) {
|
return ((BigInteger)obj).intValue();
|
} else if (obj instanceof Double) {
|
return ((Double)obj).intValue();
|
} else if (obj instanceof Long) {
|
return ((Long)obj).intValue();
|
} else if (obj instanceof Short) {
|
return ((Short)obj).intValue();
|
} else if (obj instanceof Float) {
|
return ((Float)obj).intValue();
|
} else if (obj instanceof String) {
|
try {
|
return Integer.valueOf(obj.toString());
|
} catch (Exception var2) {
|
return 0;
|
}
|
} else {
|
return 0;
|
}
|
}
|
public static SessionInfo getCurrentUserSessionInfo() throws VciBaseException {
|
SessionInfo si = getCurrentUserSessionInfoNotException();
|
if (si == null) {
|
throw new VciBaseException("noLogin", new String[]{"没有当前用户信息"});
|
} else {
|
return si;
|
}
|
}
|
/**
|
* 设置当前线程中的用户对象
|
* @param sessionInfo 用户对象
|
*/
|
public static void setCurrentUserSessionInfo(SessionInfo sessionInfo){
|
WebThreadLocalUtil.getCurrentUserSessionInfoInThread().set(sessionInfo);
|
}
|
public static long getLong(String s) {
|
long l = 0L;
|
if (s == null) {
|
return 0L;
|
} else {
|
try {
|
l = Long.parseLong(s);
|
return l;
|
} catch (Exception var4) {
|
return 0L;
|
}
|
}
|
}
|
|
public static Integer getCurrentUserSecret() {
|
SessionInfo currentUserSession = getCurrentUserSessionInfoNotException();
|
return currentUserSession != null ? getInt(currentUserSession.getUserSecret()) : UserSecretEnum.NONE.getValue();
|
}
|
|
public static float getFloat(String s) {
|
float f = 0.0F;
|
if (s == null) {
|
return 0.0F;
|
} else {
|
try {
|
f = Float.parseFloat(s);
|
return f;
|
} catch (Exception var3) {
|
return 0.0F;
|
}
|
}
|
}
|
|
public static int getInt(String string) {
|
//int i = false;
|
if (string != null && !"".equals(string.trim())) {
|
if (string.contains(".")) {
|
string = string.substring(0, string.indexOf("."));
|
}
|
|
try {
|
int i = Integer.parseInt(string);
|
return i;
|
} catch (Exception var3) {
|
return 0;
|
}
|
} else {
|
return 0;
|
}
|
}
|
|
public static double getDouble(String s) {
|
double d = 0.0D;
|
if (isNull(s)) {
|
return 0.0D;
|
} else {
|
try {
|
d = Double.parseDouble(s);
|
return d;
|
} catch (Exception var4) {
|
return 0.0D;
|
}
|
}
|
}
|
|
|
public static boolean getBoolean(String s) {
|
return BooleanEnum.TRUE.getValue().equals(s);
|
}
|
|
public static String removeComma(String s) {
|
if (s != null && s.trim().length() != 0) {
|
if (s.startsWith(",")) {
|
s = s.substring(1, s.length());
|
}
|
|
if (s.endsWith(",")) {
|
s = s.substring(0, s.length() - 1);
|
}
|
|
return s;
|
} else {
|
return s;
|
}
|
}
|
|
public static String toInSql(String s) {
|
s = removeComma(s);
|
if (s != null && s.trim().length() != 0) {
|
String[] temp = s.split(",");
|
return toInSql(temp);
|
} else {
|
return "";
|
}
|
}
|
|
public static String toInSql(String[] s) {
|
if (s != null && s.length > 0) {
|
StringBuilder sb = new StringBuilder();
|
if (s != null && s.length > 0) {
|
for(int i = 0; i < s.length; ++i) {
|
if (s[i] != null && s[i].trim().length() > 0 && !s[i].startsWith("'")) {
|
sb.append("'").append(s[i]).append("',");
|
}
|
}
|
}
|
|
return removeComma(sb.toString());
|
} else {
|
return "";
|
}
|
}
|
|
public static String toInSql(String field, String s) {
|
return !StringUtils.isBlank(field) && !StringUtils.isBlank(s) ? toInSql(field, removeComma(s).split(","), "") : "";
|
}
|
|
public static String toInSql(String field, String[] s) {
|
return toInSql(field, s, "in");
|
}
|
|
public static String toInSql(String field, String[] s, String operation) {
|
if (!StringUtils.isBlank(field) && s != null && s.length != 0) {
|
StringBuilder sb = new StringBuilder();
|
if (s != null && s.length > 0) {
|
String andOr = "or";
|
if (operation.trim().toLowerCase().equals("not in")) {
|
andOr = "and";
|
}
|
|
for(int i = 0; i < s.length; ++i) {
|
if (s[i] != null && s[i].trim().length() > 0 && !s[i].startsWith("'")) {
|
if (i == 0) {
|
sb.append(field).append(" ").append(operation).append(" (");
|
}
|
|
if (i % 500 == 0 && i != 0) {
|
sb.append(" ").append(andOr).append(" ").append(field).append(" ").append(operation).append(" (");
|
}
|
|
sb.append("'").append(s[i]).append("'");
|
if (i % 500 != 499 && i != s.length - 1) {
|
sb.append(",");
|
}
|
|
if (i % 500 == 499 || i == s.length - 1) {
|
sb.append(") ");
|
}
|
}
|
}
|
}
|
|
return sb.toString();
|
} else {
|
return "";
|
}
|
}
|
|
/** @deprecated */
|
@Deprecated
|
public static String arrayToString(String[] array) {
|
if (array != null && array.length > 0) {
|
StringBuilder sb = new StringBuilder();
|
|
for(int i = 0; i < array.length; ++i) {
|
String record = array[i];
|
if (StringUtils.isNotBlank(record)) {
|
sb.append(record).append(",");
|
}
|
}
|
|
return removeComma(sb.toString());
|
} else {
|
return "";
|
}
|
}
|
|
public static String collectionToString(Collection collection) {
|
if (CollectionUtils.isEmpty(collection)) {
|
return "";
|
} else {
|
StringBuilder sb = new StringBuilder();
|
Iterator it = collection.iterator();
|
|
while(true) {
|
Object record;
|
do {
|
do {
|
if (!it.hasNext()) {
|
return removeComma(sb.toString());
|
}
|
|
record = it.next();
|
} while(record == null);
|
} while(record instanceof String && !StringUtils.isNotBlank((String)record));
|
|
sb.append(record.toString()).append(",");
|
}
|
}
|
}
|
|
public static String toUpForFirst(String s) {
|
if (s != null && s.trim().length() != 0) {
|
String temp = s.substring(0, 1);
|
temp = temp.toUpperCase();
|
return temp + s.substring(1, s.length());
|
} else {
|
return "";
|
}
|
}
|
|
public static String toLowForFirst(String s) {
|
if (s != null && s.trim().length() != 0) {
|
String temp = s.substring(0, 1);
|
temp = temp.toLowerCase();
|
return temp + s.substring(1, s.length());
|
} else {
|
return "";
|
}
|
}
|
|
public static int countOfString(String s, char findC) {
|
Map<String, Integer> charMap = new HashMap();
|
char[] cs = s.toCharArray();
|
char[] var4 = cs;
|
int var5 = cs.length;
|
|
for(int var6 = 0; var6 < var5; ++var6) {
|
char c = var4[var6];
|
charMap.put(String.valueOf(c), !charMap.containsKey(String.valueOf(c)) ? 1 : (Integer)charMap.get(String.valueOf(c)) + 1);
|
}
|
|
return (Integer)charMap.get(String.valueOf(findC));
|
}
|
|
public static List<String> str2List(String s) {
|
if (isNull(s)) {
|
return null;
|
} else {
|
List<String> l = new ArrayList();
|
Collections.addAll(l, removeComma(s).split(","));
|
return l;
|
}
|
}
|
|
public static String list2String(List<String> ls) {
|
return ls != null && ls.size() != 0 ? (String)ls.stream().collect(Collectors.joining(",")) : "";
|
}
|
|
public static boolean isNull(String o) {
|
return StringUtils.isEmpty(o);
|
}
|
|
public static boolean isNotNull(String o) {
|
return !isNull(o);
|
}
|
|
public static boolean isNullOrNullString(String o) {
|
return StringUtils.isBlank(o);
|
}
|
|
public static boolean inArray(String[] arr, String s) {
|
if (arr != null && s != null) {
|
String[] var2 = arr;
|
int var3 = arr.length;
|
|
for(int var4 = 0; var4 < var3; ++var4) {
|
String a = var2[var4];
|
if (s.trim().equalsIgnoreCase(a)) {
|
return true;
|
}
|
}
|
}
|
|
return false;
|
}
|
|
public static boolean inArray(Object[] arr, Object o) {
|
if (arr != null && o != null) {
|
Object[] var2 = arr;
|
int var3 = arr.length;
|
|
for(int var4 = 0; var4 < var3; ++var4) {
|
Object a = var2[var4];
|
if (a.equals(o)) {
|
return true;
|
}
|
}
|
}
|
|
return false;
|
}
|
|
public static double round(double value, int scale, int roundingMode) {
|
BigDecimal bd = new BigDecimal(value);
|
bd = bd.setScale(scale, roundingMode);
|
double d = bd.doubleValue();
|
bd = null;
|
return d;
|
}
|
|
public static double round(double value, int scale) {
|
return round(value, scale, 4);
|
}
|
|
public static boolean isEmail(String string) {
|
if (StringUtils.isBlank(string)) {
|
return false;
|
} else {
|
String regEx1 = "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
|
Pattern p = Pattern.compile(regEx1);
|
Matcher m = p.matcher(string);
|
return m.matches();
|
}
|
}
|
|
public static boolean isNumber(String s) {
|
return StringUtils.isNotBlank(s) && s.matches("\\d+\\.?\\d*");
|
}
|
|
public static Field getFieldForObject(String fieldName, List<Field> fieldsList) {
|
if (StringUtils.isBlank(fieldName)) {
|
return null;
|
} else {
|
if (fieldsList != null && fieldsList.size() > 0) {
|
Iterator var2 = fieldsList.iterator();
|
|
while(var2.hasNext()) {
|
Field field = (Field)var2.next();
|
if (field.getName().toLowerCase().equals(fieldName.toLowerCase())) {
|
return field;
|
}
|
}
|
}
|
|
return null;
|
}
|
}
|
|
public static Method getSetmethod(Class c, String fieldName) {
|
if (c != null && StringUtils.isNotBlank(fieldName)) {
|
try {
|
PropertyDescriptor pd = new PropertyDescriptor(fieldName, c);
|
return pd.getWriteMethod();
|
} catch (SecurityException var3) {
|
if (log.isErrorEnabled()) {
|
log.error("获取getter出错", var3);
|
}
|
} catch (IntrospectionException var4) {
|
if (log.isErrorEnabled()) {
|
log.error("获取getter出错", var4);
|
}
|
}
|
}
|
|
return null;
|
}
|
|
public static Method getGetmethod(Class c, String fieldName) {
|
if (c != null && StringUtils.isNotBlank(fieldName)) {
|
try {
|
PropertyDescriptor pd = new PropertyDescriptor(fieldName, c);
|
return pd.getReadMethod();
|
} catch (SecurityException var3) {
|
if (log.isErrorEnabled()) {
|
log.error("获取getter出错", var3);
|
}
|
} catch (IntrospectionException var4) {
|
if (log.isErrorEnabled()) {
|
log.error("获取getter出错", var4);
|
}
|
}
|
}
|
|
return null;
|
}
|
|
public static String getColumnAnnotaionNameValue(Annotation[] fieldAnnotaions) {
|
return getAnnotationValue("com.vci.starter.web.annotation.Column,javax.persistence.Column", "name", fieldAnnotaions);
|
}
|
|
public static String getAnnotationValue(String annotationName, String methodName, Annotation[] fieldAnnotaions) {
|
String[] annotaionNameArray = annotationName.split(",");
|
Annotation[] var4 = fieldAnnotaions;
|
int var5 = fieldAnnotaions.length;
|
|
for(int var6 = 0; var6 < var5; ++var6) {
|
Annotation annotation = var4[var6];
|
String anname = annotation.annotationType().getName();
|
if (inArray(annotaionNameArray, anname)) {
|
String name = null;
|
|
try {
|
name = (String)annotation.getClass().getMethod(methodName).invoke(annotation);
|
} catch (IllegalAccessException var11) {
|
var11.printStackTrace();
|
} catch (InvocationTargetException var12) {
|
var12.printStackTrace();
|
} catch (NoSuchMethodException var13) {
|
var13.printStackTrace();
|
}
|
|
return name;
|
}
|
}
|
|
return null;
|
}
|
|
public static Object getValueFromField(String fieldName, Object sourceObject) {
|
if (StringUtils.isNotBlank(fieldName)) {
|
try {
|
Method getMethod = getGetmethod(sourceObject.getClass(), fieldName);
|
if (getMethod != null) {
|
return getMethod.invoke(sourceObject);
|
}
|
|
Field field = getFieldForObject(fieldName, sourceObject);
|
if (field != null) {
|
field.setAccessible(true);
|
return field.get(sourceObject);
|
}
|
} catch (SecurityException var4) {
|
if (log.isErrorEnabled()) {
|
log.error("从属性上获取值", var4);
|
}
|
} catch (IllegalAccessException var5) {
|
if (log.isErrorEnabled()) {
|
log.error("从属性上获取值", var5);
|
}
|
} catch (IllegalArgumentException var6) {
|
if (log.isErrorEnabled()) {
|
log.error("从属性上获取值", var6);
|
}
|
} catch (InvocationTargetException var7) {
|
if (log.isErrorEnabled()) {
|
log.error("从属性上获取值", var7);
|
}
|
}
|
}
|
|
return null;
|
}
|
|
public static void setValueForMethod(Field field, Object obj, Object value) {
|
try {
|
if (field != null) {
|
field.setAccessible(true);
|
Method setMethod = getSetmethod(field.getDeclaringClass(), field.getName());
|
setMethod.invoke(obj, value);
|
}
|
} catch (Exception var4) {
|
log.error("反射调用方法出现了错误,", var4);
|
}
|
|
}
|
|
public static void alertNotNull(Object... s) throws VciBaseException {
|
if (s != null && s.length > 0) {
|
for(int i = 0; i < s.length; ++i) {
|
Object obj = s[i];
|
String param = "";
|
|
try {
|
++i;
|
param = s[i].toString();
|
} catch (Exception var5) {
|
}
|
|
if (obj == null) {
|
throw new VciBaseException("参数[{0}]不能为空", new String[]{param});
|
}
|
|
if (obj instanceof Collection) {
|
if (CollectionUtils.isEmpty((Collection)obj)) {
|
throw new VciBaseException("参数[{0}]不能为空", new String[]{param});
|
}
|
} else if (StringUtils.isBlank(obj.toString())) {
|
throw new VciBaseException("参数[{0}]不能为空", new String[]{param});
|
}
|
}
|
}
|
|
}
|
|
/** @deprecated */
|
@Deprecated
|
public static void alertCollectionNotNull(String param, Collection collections) throws VciBaseException {
|
if (CollectionUtils.isEmpty(collections)) {
|
throw new VciBaseException("参数{0}不能为空", new String[]{param});
|
}
|
}
|
|
public static <T> Set<Set<T>> switchSetForOracleIn(Set<T> set) {
|
Set<Set<T>> listHasList = new HashSet();
|
if (set == null) {
|
return listHasList;
|
} else {
|
int muti = 1;
|
if (set.size() > 500) {
|
int balance = set.size() % 500;
|
muti = (set.size() - balance) / 500 + (balance == 0 ? 0 : 1);
|
}
|
|
List list = new ArrayList();
|
Iterator it = set.iterator();
|
|
while(it.hasNext()) {
|
list.add(it.next());
|
}
|
|
for(int i = 0; i < muti; ++i) {
|
int start = i * 500;
|
int end = start + 500;
|
if (i == muti - 1 || end > set.size()) {
|
end = set.size();
|
}
|
|
List subList = list.subList(start, end);
|
Set subSet = new HashSet();
|
Iterator var10 = subList.iterator();
|
|
while(var10.hasNext()) {
|
Object obj = var10.next();
|
subSet.add(obj);
|
}
|
|
listHasList.add(subSet);
|
}
|
|
return listHasList;
|
}
|
}
|
|
public static <T> Vector<Vector<T>> switchVectorForOracleIn(Vector<T> vector) {
|
Vector<Vector<T>> listHasList = new Vector();
|
if (vector == null) {
|
return listHasList;
|
} else {
|
int muti = 1;
|
if (vector.size() > 500) {
|
int balance = vector.size() % 500;
|
muti = (vector.size() - balance) / 500 + (balance == 0 ? 0 : 1);
|
}
|
|
List list = new ArrayList();
|
Iterator it = vector.iterator();
|
|
while(it.hasNext()) {
|
list.add(it.next());
|
}
|
|
for(int i = 0; i < muti; ++i) {
|
int start = i * 500;
|
int end = start + 500;
|
if (i == muti - 1 || end > vector.size()) {
|
end = vector.size();
|
}
|
|
List subList = list.subList(start, end);
|
Vector subSet = new Vector();
|
Iterator var10 = subList.iterator();
|
|
while(var10.hasNext()) {
|
Object obj = var10.next();
|
subSet.add(obj);
|
}
|
|
listHasList.add(subSet);
|
}
|
|
return listHasList;
|
}
|
}
|
|
public static <T> T jsonString2JavaBean(String jsonString, Class<T> beanClass) {
|
return JSONObject.parseObject(jsonString, beanClass);
|
}
|
|
public static void ifNullSetDefautl(String str, String defaultValue) {
|
if (isNull(str)) {
|
;
|
}
|
|
}
|
|
public static <T> T getFristObjectFromJson(String jsonString, Class<T> beanClass) {
|
return !isNull(jsonString) ? JSONObject.parseArray(jsonString, beanClass).get(0) : null;
|
}
|
|
public static String getJSONStringWithDateFormat(Object obj) {
|
return JSONObject.toJSONStringWithDateFormat(obj, "yyyy-MM-dd HH:mm:ss.SSS", new SerializerFeature[]{SerializerFeature.WriteDateUseDateFormat});
|
}
|
|
public static Map<String, Object> objectToMap(Object o) {
|
Map<String, Object> map = new HashMap();
|
if (o != null) {
|
String jsonString = JSONObject.toJSONStringWithDateFormat(o, "yyyy-MM-dd HH:mm:ss.SSS", new SerializerFeature[]{SerializerFeature.WriteDateUseDateFormat});
|
if (StringUtils.isNotBlank(jsonString)) {
|
JSONObject jsonObject = JSONObject.parseObject(jsonString);
|
if (jsonObject != null) {
|
Iterator var4 = jsonObject.keySet().iterator();
|
|
while(var4.hasNext()) {
|
String key = (String)var4.next();
|
map.put(key, jsonObject.get(key));
|
}
|
}
|
}
|
}
|
|
return map;
|
}
|
|
public static Map<String, String> objectToMapString(Object o) {
|
Map<String, String> map = new HashMap();
|
if (o != null) {
|
String jsonString = JSONObject.toJSONStringWithDateFormat(o, "yyyy-MM-dd HH:mm:ss.SSS", new SerializerFeature[]{SerializerFeature.WriteDateUseDateFormat});
|
if (StringUtils.isNotBlank(jsonString)) {
|
JSONObject jsonObject = JSONObject.parseObject(jsonString);
|
if (jsonObject != null) {
|
Iterator var4 = jsonObject.keySet().iterator();
|
|
while(var4.hasNext()) {
|
String key = (String)var4.next();
|
map.put(key, jsonObject.getString(key));
|
}
|
}
|
}
|
}
|
|
return map;
|
}
|
|
public static boolean isNotNullForField(Object obj, Field f) {
|
return !"serialVersionUID".equalsIgnoreCase(f.getName()) && !"DEFAULT_INITIAL_CAPACITY".equalsIgnoreCase(f.getName()) && null != obj && !isNullOrNullString(obj.toString());
|
}
|
|
public static String array2String(String[] array) {
|
if (null != array && array.length != 0) {
|
String ss = "";
|
String[] var2 = array;
|
int var3 = array.length;
|
|
for(int var4 = 0; var4 < var3; ++var4) {
|
String s = var2[var4];
|
ss = ss + s + ",";
|
}
|
|
return removeComma(ss);
|
} else {
|
return "";
|
}
|
}
|
|
public static void copyValueForMap(Map source, Map target, String[] copyField) {
|
Map<String, String> copyFieldMap = new HashMap();
|
String[] var4 = copyField;
|
int var5 = copyField.length;
|
|
for(int var6 = 0; var6 < var5; ++var6) {
|
String field = var4[var6];
|
copyFieldMap.put(field, field);
|
}
|
|
copyValueForMap(source, target, (Map)copyFieldMap);
|
}
|
|
public static void copyValueForMap(Map source, Map target, Map<String, String> copyField) {
|
try {
|
Iterator it = copyField.keySet().iterator();
|
|
while(it.hasNext()) {
|
String field = (String)it.next();
|
target.put(field, source.get(copyField.get(field)));
|
}
|
} catch (Exception var5) {
|
}
|
|
}
|
|
public static Double getDoubleFromMap(String field, Map<String, Object> record) {
|
if (!isNullOrNullString(field) && record != null && record.containsKey(field)) {
|
Object v = record.get(field);
|
if (v instanceof BigDecimal) {
|
return ((BigDecimal)v).doubleValue();
|
} else {
|
return v instanceof Double ? (Double)v : getDouble((String)v);
|
}
|
} else {
|
return null;
|
}
|
}
|
|
public static String getDataByKey(String key, Map<String, Object> data) {
|
String value = "";
|
if (data.containsKey(key)) {
|
value = (String)data.get(key);
|
}
|
|
if (value == null) {
|
value = "";
|
}
|
|
return value;
|
}
|
|
public static Map getNotNullMap(Map map) {
|
if (map == null) {
|
return new HashMap();
|
} else {
|
Iterator it = map.keySet().iterator();
|
HashMap unNullMap = new HashMap();
|
|
while(true) {
|
while(true) {
|
String newKey;
|
Object value;
|
do {
|
if (!it.hasNext()) {
|
return unNullMap;
|
}
|
|
Object key = it.next();
|
newKey = key.toString().toLowerCase();
|
value = map.get(key);
|
} while(value == null);
|
|
if (value instanceof String && isNotNull(value.toString())) {
|
unNullMap.put(newKey, value);
|
} else if (!(value instanceof String)) {
|
unNullMap.put(newKey, value);
|
}
|
}
|
}
|
}
|
}
|
|
public static List<Field> getAllFieldForObj(Class c) {
|
List<Field> allField = new ArrayList();
|
Set<String> fieldNameSet = new HashSet();
|
|
for(Class classz = c; classz != Object.class; classz = classz.getSuperclass()) {
|
Field[] thisClassField = classz.getDeclaredFields();
|
Field[] var5 = thisClassField;
|
int var6 = thisClassField.length;
|
|
for(int var7 = 0; var7 < var6; ++var7) {
|
Field field = var5[var7];
|
if (!field.getName().equals("serialVersionUID")) {
|
String fieldLowerName = field.getName().toLowerCase();
|
if (!fieldNameSet.contains(fieldLowerName)) {
|
fieldNameSet.add(fieldLowerName);
|
allField.add(field);
|
}
|
}
|
}
|
}
|
|
return allField;
|
}
|
|
public static String getTableName(String btmname) {
|
return (VciQueryWrapperForDO.USER_TABLE_COMPATIBILITY ? "vcibt_" : "pl_code_") + btmname.trim().toLowerCase();
|
}
|
|
public static Field getTsField(Class c) {
|
List<Field> allField = getAllFieldForObj(c);
|
if (allField != null && allField.size() > 0) {
|
Iterator var2 = allField.iterator();
|
|
while(var2.hasNext()) {
|
Field field = (Field)var2.next();
|
if (field.getName().equals("ts")) {
|
return field;
|
}
|
}
|
}
|
|
return null;
|
}
|
|
public static Field getFieldForObject(String fieldName, Object obj) {
|
return obj == null ? null : getFieldForObject(fieldName, obj.getClass());
|
}
|
|
public static Field getFieldForObject(String fieldName, Class c) {
|
List<Field> allField = getAllFieldForObj(c);
|
if (allField != null && allField.size() > 0) {
|
Iterator var3 = allField.iterator();
|
|
while(var3.hasNext()) {
|
Field field = (Field)var3.next();
|
if (field.getName().toLowerCase().equalsIgnoreCase(fieldName.toLowerCase())) {
|
return field;
|
}
|
}
|
}
|
|
return null;
|
}
|
|
public static Method getSetmethod(Class c, Field field) {
|
return getSetmethod(c, field.getName());
|
}
|
|
public static Method getGetmethod(Class c, Field field) {
|
return getGetmethod(c, field.getName());
|
}
|
|
public static Method getMethodByName(Class<?> classObj, String methodName) {
|
alertNotNull(classObj, "获取方法的对象所属的类", methodName, "方法的名字");
|
Method[] methods = classObj.getMethods();
|
List<Method> sameMethods = (List)Arrays.stream(methods).filter((method) -> {
|
return method.getName().equalsIgnoreCase(methodName);
|
}).collect(Collectors.toList());
|
return !CollectionUtils.isEmpty(sameMethods) ? (Method)sameMethods.get(0) : null;
|
}
|
|
public static Map<String, String> whereSql2Map(String whereSql) {
|
Map<String, String> map = new HashMap();
|
if (isNotNull(whereSql)) {
|
String[] selects = whereSql.split("and");
|
if (selects != null && selects.length > 0) {
|
String[] var3 = selects;
|
int var4 = selects.length;
|
|
for(int var5 = 0; var5 < var4; ++var5) {
|
String s = var3[var5];
|
s = s.trim();
|
map.put(s.substring(0, s.indexOf(" ")).trim(), s.substring(s.indexOf(" ") + 1).trim());
|
}
|
}
|
}
|
|
return map;
|
}
|
|
public static synchronized String getRoundFilename(String prefix) {
|
if (prefix == null) {
|
prefix = "";
|
}
|
|
return prefix + System.currentTimeMillis();
|
}
|
|
public static String getLocalIp() {
|
if (localIp == null) {
|
try {
|
InetAddress inetAddress = getLocalHostLANAddress();
|
if (inetAddress == null) {
|
localIp = "127.0.0.1";
|
} else {
|
localIp = inetAddress.getHostAddress();
|
}
|
} catch (Exception var1) {
|
localIp = "127.0.0.1";
|
}
|
}
|
|
return localIp;
|
}
|
|
private static InetAddress getLocalHostLANAddress() throws Exception {
|
try {
|
InetAddress candidateAddress = null;
|
Enumeration ifaces = NetworkInterface.getNetworkInterfaces();
|
|
while(ifaces.hasMoreElements()) {
|
NetworkInterface iface = (NetworkInterface)ifaces.nextElement();
|
Enumeration inetAddrs = iface.getInetAddresses();
|
|
while(inetAddrs.hasMoreElements()) {
|
InetAddress inetAddr = (InetAddress)inetAddrs.nextElement();
|
if (!inetAddr.isLoopbackAddress()) {
|
if (inetAddr.isSiteLocalAddress()) {
|
return inetAddr;
|
}
|
|
if (candidateAddress == null) {
|
candidateAddress = inetAddr;
|
}
|
}
|
}
|
}
|
|
if (candidateAddress != null) {
|
return candidateAddress;
|
} else {
|
InetAddress jdkSuppliedAddress = InetAddress.getLocalHost();
|
return jdkSuppliedAddress;
|
}
|
} catch (Exception var5) {
|
var5.printStackTrace();
|
return null;
|
}
|
}
|
|
public static String getProjectPath() {
|
String path = "";
|
|
try {
|
String jarFilePath = ResourceUtils.getURL("classpath:").getPath();
|
if (jarFilePath.contains("!")) {
|
path = (new File(jarFilePath)).getParentFile().getParentFile().getParent();
|
} else {
|
path = (new File(jarFilePath)).getParent();
|
}
|
} catch (IOException var2) {
|
throw new VciBaseException("获取当前服务所在的文件夹出现了错误");
|
}
|
|
if (path.startsWith("file:\\")) {
|
path = path.substring(6);
|
}
|
|
return path;
|
}
|
|
public static Map<String, String> getParamsByUrl(String url) {
|
if (StringUtils.isBlank(url)) {
|
return new HashMap();
|
} else {
|
String[] array = url.split("&");
|
Map<String, String> params = new HashMap();
|
String[] var3 = array;
|
int var4 = array.length;
|
|
for(int var5 = 0; var5 < var4; ++var5) {
|
String temp = var3[var5];
|
if (temp.contains("=")) {
|
String[] keyValues = temp.split("=");
|
params.put(keyValues[0], keyValues[1]);
|
} else {
|
params.put(temp, "");
|
}
|
}
|
|
return params;
|
}
|
}
|
|
public static boolean isChinese(char c) {
|
UnicodeBlock ub = UnicodeBlock.of(c);
|
return ub == UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS || ub == UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS || ub == UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A || ub == UnicodeBlock.GENERAL_PUNCTUATION || ub == UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION || ub == UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS;
|
}
|
|
public static boolean containsKeyUnCaseForMap(Map map, String key) {
|
if (!CollectionUtils.isEmpty(map)) {
|
boolean[] finded = new boolean[]{false};
|
map.forEach((k, value) -> {
|
if (k instanceof String && k.toString().toLowerCase(Locale.ROOT).equalsIgnoreCase(key)) {
|
finded[0] = true;
|
}
|
});
|
return finded[0];
|
} else {
|
return false;
|
}
|
}
|
|
public static boolean isChinese(String str) {
|
char[] ch = str.toCharArray();
|
char[] var2 = ch;
|
int var3 = ch.length;
|
|
for(int var4 = 0; var4 < var3; ++var4) {
|
char c = var2[var4];
|
if (isChinese(c)) {
|
return true;
|
}
|
}
|
|
return false;
|
}
|
|
public static String toDBC(String input) {
|
if (StringUtils.isBlank(input)) {
|
return input;
|
} else {
|
char[] c = input.toCharArray();
|
|
for(int i = 0; i < c.length; ++i) {
|
if (c[i] == 12288) {
|
c[i] = ' ';
|
} else if (c[i] > '\uff00' && c[i] < '⦅') {
|
c[i] -= 'ﻠ';
|
}
|
}
|
|
return new String(c);
|
}
|
}
|
|
public static String toSBC(String input) {
|
if (StringUtils.isBlank(input)) {
|
return input;
|
} else {
|
char[] c = input.toCharArray();
|
|
for(int i = 0; i < c.length; ++i) {
|
if (c[i] == 12288) {
|
c[i] = ' ';
|
} else if (c[i] > '\uff00' && c[i] < '⦅') {
|
c[i] += 'ﻠ';
|
}
|
}
|
|
return new String(c);
|
}
|
}
|
|
public static String getStringValueFromObject(Object obj) {
|
if (obj == null) {
|
return "";
|
} else if (!(obj instanceof Integer) && !(obj instanceof Float) && !(obj instanceof Long) && !(obj instanceof Double)) {
|
return obj instanceof Date ? VciDateUtil.date2Str((Date)obj, "yyyy-MM-dd HH:mm:ss.SSS") : obj.toString();
|
} else {
|
if (obj instanceof Double) {
|
Double aDouble = (Double)obj;
|
if (aDouble != null && aDouble % 1.0D == 0.0D) {
|
return String.valueOf(aDouble.intValue());
|
}
|
}
|
|
return String.valueOf(obj);
|
}
|
}
|
|
public static SessionInfo getCurrentUserSessionInfoNotException() {
|
return (SessionInfo) WebThreadLocalUtil.getCurrentUserSessionInfoInThread().get();
|
}
|
|
|
public static <T> Collection<Collection<T>> switchCollectionForOracleIn(Collection<T> list) {
|
return switchCollectionForOracleIn(list, 500);
|
}
|
/**
|
* oracle in 查询不能超过1000,转换一下集合
|
* 由于SQL语句1000个可能很长,超过oracle10g,所以牺牲性能分配为500个数组
|
* @param list 需要转换的列表内容
|
* @return 分组后的list
|
*/
|
public static <T> List<List<T>> switchListForOracleIn(List<T> list) {
|
List<List<T>> listHasList = new ArrayList<List<T>>();
|
if(list == null){
|
return listHasList;
|
}
|
List<T> newList = new ArrayList<T>();
|
for(Object obj : list){
|
//为了让list还可以添加内容,因为使用sublist后,list不能再Add了
|
newList.add((T)obj);
|
}
|
int muti = 1;
|
if(newList.size() >500){
|
int balance = newList.size()%500;
|
muti = (newList.size() - balance)/500 + (balance == 0?0:1);
|
}
|
for(int i = 0 ; i < muti; i ++){
|
int start = i*500;
|
int end = start + 500;
|
if(i == muti-1 || end >newList.size() ){
|
end = newList.size();
|
}
|
List subList = newList.subList(start,end);
|
listHasList.add(subList);
|
}
|
return listHasList;
|
}
|
public static <T> Collection<Collection<T>> switchCollectionForOracleIn(Collection<T> collection, int preSize) {
|
Collection<Collection<T>> listHasList = new ArrayList();
|
if (collection == null) {
|
return listHasList;
|
} else {
|
List<T> newList = new ArrayList();
|
Iterator var4 = collection.iterator();
|
|
while(var4.hasNext()) {
|
Object obj = var4.next();
|
newList.add((T) obj);
|
}
|
|
int muti = 1;
|
int i;
|
if (newList.size() > preSize) {
|
i = newList.size() % preSize;
|
muti = (newList.size() - i) / preSize + (i == 0 ? 0 : 1);
|
}
|
|
for(i = 0; i < muti; ++i) {
|
int start = i * preSize;
|
int end = start + preSize;
|
if (i == muti - 1 || end > newList.size()) {
|
end = newList.size();
|
}
|
|
List subList = newList.subList(start, end);
|
listHasList.add(subList);
|
}
|
|
return listHasList;
|
}
|
}
|
public static short getShort(String s) {
|
// short i = false;
|
if (s == null) {
|
return 0;
|
} else {
|
try {
|
short i = Short.parseShort(s);
|
return i;
|
} catch (Exception var3) {
|
return 0;
|
}
|
}
|
}
|
|
public static void setValueForField(Field field, Object obj, String value) {
|
try {
|
if (field != null && StringUtils.isNotBlank(value)) {
|
field.setAccessible(true);
|
Method setMethod = getSetmethod(field.getDeclaringClass(), field.getName());
|
Class type = field.getType();
|
Object valueObj = null;
|
if (!type.equals(Integer.TYPE) && !type.equals(Integer.class)) {
|
if (!type.equals(Float.TYPE) && !type.equals(Float.class)) {
|
if (!type.equals(Long.TYPE) && !type.equals(Long.class)) {
|
if (!type.equals(Double.class) && !type.equals(Double.TYPE)) {
|
if (type.equals(Date.class)) {
|
DateConverter dateConverter = new DateConverter();
|
dateConverter.setAsText(value);
|
valueObj = dateConverter.getValue();
|
} else if (type.equals(String.class)) {
|
valueObj = value;
|
} else {
|
valueObj = value;
|
if (log.isErrorEnabled()) {
|
log.error("不支持的类型" + type.toString());
|
}
|
}
|
} else {
|
valueObj = getDouble(value);
|
}
|
} else {
|
valueObj = getLong(value);
|
}
|
} else {
|
valueObj = getFloat(value);
|
}
|
} else {
|
valueObj = getInt(value);
|
}
|
|
if (setMethod != null) {
|
setMethod.invoke(obj, valueObj);
|
} else {
|
field.set(obj, valueObj);
|
}
|
}
|
} catch (Throwable var7) {
|
if (log.isErrorEnabled()) {
|
log.error("设置属性的值出错了错误", var7);
|
}
|
}
|
|
}
|
|
public static void setValueForField(String fieldName, Object obj, String value) {
|
try {
|
Field field = getFieldForObject(fieldName, obj);
|
if (field != null) {
|
setValueForField(field, obj, value);
|
}
|
} catch (Exception var4) {
|
if (log.isErrorEnabled()) {
|
log.error("设置属性的值出错了错误", var4);
|
}
|
}
|
|
}
|
|
/**
|
* 将 JavaBean对象转化为 Map
|
* @author wyply115
|
* @param bean 要转化的类型
|
* @return Map对象
|
* @version 2016年3月20日 11:03:01
|
*/
|
public static Map convertBean2Map(Object bean,Set<String> existFild) throws Exception {
|
Class type = bean.getClass();
|
Map returnMap = new HashMap();
|
BeanInfo beanInfo = Introspector.getBeanInfo(type);
|
PropertyDescriptor[] propertyDescriptors = beanInfo
|
.getPropertyDescriptors();
|
Field[] declaredFields = type.getDeclaredFields();
|
Map<String, String> fieldMap = new HashMap();
|
String existField = "";
|
for (Field declaredField : declaredFields) {
|
declaredField.setAccessible(true);
|
|
// 获取字段的值
|
boolean isTableField = declaredField.isAnnotationPresent(TableField.class);
|
if (isTableField) {
|
TableField tableField = declaredField.getAnnotation(TableField.class);
|
Boolean fieldValue = tableField.exist();
|
if(fieldValue == false){
|
existField += declaredField.getName().toLowerCase()+",";
|
}
|
String value = tableField.value();
|
if(net.logstash.logback.encoder.org.apache.commons.lang3.StringUtils.isNotBlank(value)){
|
fieldMap.put(declaredField.getName(), value);
|
}
|
}
|
}
|
Map mapData = new HashMap<>();
|
for (int i = 0, n = propertyDescriptors.length; i <n ; i++) {
|
PropertyDescriptor descriptor = propertyDescriptors[i];
|
String propertyName = descriptor.getName();
|
|
if (!propertyName.equals("class") &&
|
((!"".equals(existField) && !existField.contains(propertyName.toLowerCase()+","))||"data".equals(propertyName))) {
|
Method readMethod = descriptor.getReadMethod();
|
Object result = readMethod.invoke(bean, new Object[0]);
|
|
if (result != null) {
|
if ("data".equals(propertyName)){
|
mapData = (Map) result;
|
}else if(existFild.contains((fieldMap.containsKey(propertyName)?fieldMap.get(propertyName):propertyName).toLowerCase())){
|
returnMap.put(fieldMap.containsKey(propertyName)?fieldMap.get(propertyName).toLowerCase():propertyName.toLowerCase(), result);
|
}
|
} else if(existFild.contains((fieldMap.containsKey(propertyName)?fieldMap.get(propertyName):propertyName).toLowerCase())){
|
returnMap.put(fieldMap.containsKey(propertyName)?fieldMap.get(propertyName).toLowerCase():propertyName.toLowerCase(), "");
|
}
|
}
|
}
|
//作用主要用于已MAP中的数据为准,bean里面遇到字段相同且map里面数据为空,则使用bean里面的值,
|
// Map resulMapChild = (Map) mapData;
|
for (Object o : mapData.keySet()) {
|
if(existFild == null || existFild.contains(String.valueOf(o).toLowerCase())){
|
if((Func.isNotBlank(String.valueOf(returnMap.get(String.valueOf(o).toLowerCase())))
|
&& returnMap.get(String.valueOf(o).toLowerCase()) !=null)
|
&& Func.isBlank(String.valueOf(mapData.get(o)))){
|
continue;
|
}
|
returnMap.put(String.valueOf(o).toLowerCase(),mapData.get(o));
|
}
|
}
|
return returnMap;
|
}
|
|
public static <T> List<T> mapToBean(List<Map> maps, Class<T> tClass) {
|
List<T> beanList = new ArrayList<>();
|
try {
|
for (Map map : maps) {
|
T t = tClass.newInstance();
|
BeanInfo beanInfo = Introspector.getBeanInfo(tClass);
|
PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
|
|
for (PropertyDescriptor property : propertyDescriptors) {
|
|
String key = property.getName();
|
if ("class".equals(key)) {
|
continue;
|
}
|
|
// String sqlField = camelToUnderscore(key);
|
|
|
if (map.containsKey(key.toUpperCase(Locale.ROOT))||map.containsKey(key.toLowerCase(Locale.ROOT))) {
|
try {
|
Object value="";
|
if (map.containsKey(key.toUpperCase(Locale.ROOT))) {
|
value = map.get(key.toUpperCase());
|
}else if(map.containsKey(key.toLowerCase(Locale.ROOT))){
|
value = map.get(key.toLowerCase());
|
}
|
// 得到property对应的setter方法
|
Method setter = property.getWriteMethod();
|
Class<?> type = property.getPropertyType();
|
// 强转为字段的类型,不需要时可以去除,依赖commons-beanutilss-beanutils
|
//Object convert = ConvertUtils.convert(String.valueOf(value), type);
|
//setter.invoke(t, convert);
|
setter.invoke(t, value);
|
} catch (Exception e) {
|
throw new RuntimeException("【赋值异常】", e);
|
}
|
}
|
|
}
|
beanList.add(t);
|
}
|
} catch (Exception e) {
|
throw new RuntimeException("【Map转换实体异常】", e);
|
}
|
return beanList;
|
}
|
|
|
public static String camelToUnderscore(String name) {
|
if (name == null && name.length() <= 0) {
|
return name;
|
}
|
StringBuilder sb = new StringBuilder();
|
String lowerName = name.toLowerCase();
|
for (int i = 0; i < lowerName.length(); i++) {
|
String nameChar = name.substring(i, i + 1);
|
String lowerChar = lowerName.substring(i, i + 1);
|
if (!nameChar.equals(lowerChar)) {
|
sb.append("_").append(lowerChar);
|
} else {
|
sb.append(nameChar);
|
}
|
}
|
|
return sb.toString();
|
}
|
|
}
|