| | |
| | | |
| | | 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.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.math.BigInteger; |
| | | import java.net.InetAddress; |
| | | import java.net.NetworkInterface; |
| | | import java.sql.Timestamp; |
| | | import java.text.SimpleDateFormat; |
| | | import java.util.*; |
| | | import java.util.regex.Matcher; |
| | | import java.util.regex.Pattern; |
| | |
| | | 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; |
| | | } |
| | | } |
| | | } |
| | | |
| | | public static Integer getCurrentUserSecret() { |
| | | SessionInfo currentUserSession = getCurrentUserSessionInfoNotException(); |
| | | return currentUserSession != null ? getInt(currentUserSession.getUserSecret()) : UserSecretEnum.NONE.getValue(); |
| | | } |
| | | |
| | | public static float getFloat(String s) { |
| | |
| | | return 0.0D; |
| | | } |
| | | } |
| | | } |
| | | |
| | | |
| | | public static boolean getBoolean(String s) { |
| | | return BooleanEnum.TRUE.getValue().equals(s); |
| | | } |
| | | |
| | | public static String removeComma(String s) { |
| | |
| | | return allField; |
| | | } |
| | | |
| | | public static String getTableName(String btmname) { |
| | | return (VciQueryWrapperForDO.USER_TABLE_COMPATIBILITY ? "pl_code_" : "vcibt_") + btmname.trim().toLowerCase(); |
| | | } |
| | | |
| | | public static Field getTsField(Class c) { |
| | | List<Field> allField = getAllFieldForObj(c); |
| | | if (allField != null && allField.size() > 0) { |
| | |
| | | |
| | | } |
| | | |
| | | 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) throws Exception { |
| | | Class type = bean.getClass(); |
| | | Map returnMap = new HashMap(); |
| | | BeanInfo beanInfo = Introspector.getBeanInfo(type); |
| | | PropertyDescriptor[] propertyDescriptors = beanInfo |
| | | .getPropertyDescriptors(); |
| | | Field[] declaredFields = type.getDeclaredFields(); |
| | | 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()+","; |
| | | } |
| | | } |
| | | } |
| | | |
| | | 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)){ |
| | | returnMap.putAll((Map) result); |
| | | }else { |
| | | returnMap.put(propertyName, result); |
| | | } |
| | | } else { |
| | | returnMap.put(propertyName, ""); |
| | | } |
| | | } |
| | | } |
| | | 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())) { |
| | | try { |
| | | Object value = map.get(key.toUpperCase()); |
| | | // 得到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(); |
| | | } |
| | | |
| | | } |
| | | |