package com.huaheng.common.utils; import java.io.UnsupportedEncodingException; import java.lang.reflect.Field; import java.lang.reflect.Method; import java.math.BigDecimal; import java.net.URLEncoder; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Map; public class DataUtils { static SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); /*** * 转化为Example方法名 * @param filedName 字段名 * @param methodSuffix 方法后缀 * @return */ public static String ConvertMethodName(String filedName, String methodSuffix) { String returnValue = null; if (filedName.length() > 0) { char[] nameChar = filedName.toCharArray(); if (nameChar[0] >= 'a' && nameChar[0] <= 'z') { nameChar[0] = (char) (nameChar[0] - 32); } returnValue = "and" + String.valueOf(nameChar) + methodSuffix; } return returnValue; } /** * 将对象转为字符串 * @param object * @return */ public static String getString(Object object) { if (object == null) return null; else return object.toString(); } /** * 将对象转为Integer * @param object * @return */ public static Integer getInteger(Object object) { if (object == null) return null; else return Integer.valueOf(object.toString()); } /** * 将对象转为BigDecimal * @param object * @return */ public static BigDecimal getBigDecimal(Object object) { if (object == null) return null; else return new BigDecimal(object.toString()); } /** * 将对象转为Double * @param object * @return */ public static Double getDouble(Object object) { if (object == null) return null; else return new Double(object.toString()); } /** * 将long转为Integer * @param object * @return */ public static Integer getInteger(long object) { return Integer.valueOf(String.valueOf(object)); } /** * 将对象转为Date * @param object * @return */ public static Date getDateTime(Object object) throws ParseException { if (object == null) { return null; } else { return format.parse(object.toString()); } } /** * 通过一个实体类,给另一个实体类赋值 * @param source 源实体类 * @param target 目标实体类 * @param ignoreProperties 忽略字段(多个字段用逗号隔开) * @throws Exception */ public static void CopyDataByNotNull (Object source, Object target, String ignoreProperties) throws Exception { Field[] sourceFields = source.getClass().getDeclaredFields(); Field[] targetFields = target.getClass().getDeclaredFields(); Field.setAccessible(targetFields, true); for (int i = 0; i < sourceFields.length; i++) { String sourceName = sourceFields[i].getName(); Object sourceValue = sourceFields[i].get(source); if (sourceValue == null || ignoreProperties.indexOf(sourceName) > -1) { continue; } for (Field targetField : targetFields) { if (targetField.getName().equals(sourceName)) { targetField.set(target, sourceValue); break; } } } } /** * 格式化map * @param map * @return */ public static String sendGetFormat(Map<String, Object> map){ StringBuilder reslut = new StringBuilder(); for (Map.Entry<String, Object> a : map.entrySet()) { reslut.append(a.getKey()).append("=").append(EncodingUtil.encodeURIComponent(String.valueOf(a.getValue()))+"&"); } return String.valueOf(reslut); } // /** // * 根据传过来实体类,自动拼接查询Condition,如果是字符类型就用LIKE查询,如果是日期类型就用>=或是<=,其他的用= // * @param entityClass // * @return // * @throws Exception // */ // public static <T> Condition createCondition (T entityClass) throws Exception { // Condition condition = new Condition(entityClass.getClass()); // Field[] fields = entityClass.getClass().getDeclaredFields(); // Field.setAccessible(fields, true); // for (int i = 0; i < fields.length; i++) { // String filedName = fields[i].getName(); // Object filedValue = fields[i].get(entityClass); // if (filedValue != null && filedValue.toString().length() > 0) { // if (filedValue.getClass() == String.class) { // condition.and().andLike(filedName, "%" + filedValue.toString() + "%"); // } else if(filedValue.getClass() == Date.class){ // if (filedName.startsWith("begin") || filedName.startsWith("create")) { // condition.and().andGreaterThanOrEqualTo(filedName, filedValue); // }else if (filedName.startsWith("end")|| filedName.startsWith("update")){ // condition.and().andLessThanOrEqualTo(filedName, filedValue); // } // } else { // condition.and().andEqualTo(filedName, filedValue); // } // } // } // return condition; // } // /** // * 根据传过来实体类,自动拼接查询Example,如果是字符类型就用LIKE查询,如果是日期类型就用>=或是<=,其他的用= // * @param entity 实体 // * @param example 需要生成的Example // * @param <T> 实体类型 // * @param <V> Example类型 // * @throws Exception // */ // public static <T, V> void createCriteria (T entity, V example) throws Exception { // Field[] entityFields = entity.getClass().getDeclaredFields(); // Field.setAccessible(entityFields, true); // Object criteria = example.getClass().getDeclaredMethod("createCriteria").invoke(example); // Method[] exampleMethod = criteria.getClass().getDeclaredMethods(); // for (int i = 0; i < entityFields.length; i++) { // String filedName = entityFields[i].getName(); // Object filedValue = entityFields[i].get(entity); // if (filedName.equals("deleted")) filedValue = false; // if (filedName.equals("warehouseCode")) filedValue = ShiroUtils.getWarehouseCode(); // if (filedValue != null && filedValue.toString().length() > 0) { // String methodName = null; // if (filedValue.getClass() == Integer.class || filedValue.getClass() == Boolean.class || filedName.equals("warehouseCode")) // { // methodName = ConvertMethodName(filedName, "EqualTo"); // } // else if (filedValue.getClass() == String.class) // { // methodName = ConvertMethodName(filedName, "Like"); // filedValue = filedValue.toString() + "%"; // } // else if(filedValue.getClass() == Date.class) // { // if (filedName.startsWith("begin") || filedName.startsWith("create")) { // methodName = ConvertMethodName(filedName, "GreaterThanOrEqualTo"); // }else if (filedName.startsWith("end")|| filedName.startsWith("update")){ // methodName = ConvertMethodName(filedName, "andAddress1LessThanOrEqualTo"); // } // } // for (Method itemMethod : exampleMethod) // { // if (itemMethod.getName().equals(methodName)) { // itemMethod.invoke(criteria, filedValue); // } // } // } // } // } }