beanutil

package com.suning.rps.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.suning.rps.entity.resume.PreviewResume;
import com.suning.rps.unit.DateUtils;
import com.suning.rps.unit.ListUtil;
import com.suning.rps.unit.ResumeCompareModuleEnum;

 /**关于beanutil的常用的方法*/
public class BeanHelpUtils {
    private static final Logger LOG = LoggerFactory.getLogger(BeanHelpUtils.class);
    private static final Long ZERO_LONG = Long.valueOf(0);
    private static final Integer ZERP_INT = Integer.valueOf(0);
    
    private BeanHelpUtils(){}
    
    /**
     * 判断vo类 主键 是否有值
     * @param obj
     * @return
     */
    /*public static boolean hasId(Object obj) {
        return getId(obj) != null;
    }*/
    public static boolean hasId(Object obj) {
        if(null == obj) { return false;}
        boolean flag = false;
        for(Field f : obj.getClass().getDeclaredFields()) {
            Id ann = f.getAnnotation(Id.class);
            if(null == ann) { continue; }
            try {
                f.setAccessible(true);
                Object id = f.get(obj);
                if(null == id || "".equals(id) || Long.valueOf(0).equals(id)
                        || Integer.valueOf(0).equals(id)) {
                    return false;
                }
                return true;
            } catch (IllegalArgumentException e) {
                LOG.debug(e.getMessage(), e);
            } catch (IllegalAccessException e) {
                LOG.debug(e.getMessage(), e);
            }
        }
        for(Method me : obj.getClass().getDeclaredMethods()) {
            Id ann = me.getAnnotation(Id.class);
            if(null == ann) { continue; }
            try {
                Object id = me.invoke(obj);
                if(null == id || "".equals(id) || Long.valueOf(0).equals(id)
                        || Integer.valueOf(0).equals(id)) {
                    return false;
                }
                return true;
            } catch (IllegalArgumentException e) {
                LOG.debug(e.getMessage(), e);
            } catch (IllegalAccessException e) {
                LOG.debug(e.getMessage(), e);
            } catch (InvocationTargetException e) {
                LOG.debug(e.getMessage(), e);
            }
        }
        return flag;
    }
    /**
     * 对象obj中是否 含有 非空 属性 fieldName
     * @param fieldName
     * @return
     */
    @SuppressWarnings("rawtypes")
    public static boolean hasField(Object obj, String fieldName) {
        if(null == fieldName) { return false;}
        Object value = null;
        try {
            Class clzss = null;
            if(obj instanceof Class) {
                clzss = (Class) obj;
                Field f = clzss.getDeclaredField(fieldName);
                value = f.toString();
            } else {
                clzss = obj.getClass();
                Field f = clzss.getDeclaredField(fieldName);
                f.setAccessible(true);
                value = f.get(obj);
            }
        } catch (NoSuchFieldException e) {
            LOG.debug(e.getMessage(), e);
        } catch (SecurityException e) {
            LOG.debug(e.getMessage(), e);
        } catch (IllegalArgumentException e) {
            LOG.debug(e.getMessage(), e);
        } catch (IllegalAccessException e) {
            LOG.debug(e.getMessage(), e);
        }
        return !StringUtil.hasNull(value);
    }
    
    /**
     * 判断vo类 主键 是否有值
     * @param obj
     * @return
     */
    public static Object getId(Object obj) {
        if(null == obj) { return null;}
        for(Field f : obj.getClass().getDeclaredFields()) {
            Id ann = f.getAnnotation(Id.class);
            if(null == ann) { continue; }
            try {
                f.setAccessible(true);
                Object id = f.get(obj);
                if(null == id || "".equals(id) || Long.valueOf(0).equals(id)
                        || Integer.valueOf(0).equals(id)) {
                    return null;
                }
                return id;
            } catch (IllegalArgumentException e) {
                LOG.debug(e.getMessage(), e);
            } catch (IllegalAccessException e) {
                LOG.debug(e.getMessage(), e);
            }
        }
        for(Method me : obj.getClass().getDeclaredMethods()) {
            Id ann = me.getAnnotation(Id.class);
            if(null == ann) { continue; }
            try {
                Object id = me.invoke(obj);
                if(null == id || "".equals(id) || Long.valueOf(0).equals(id)
                        || Integer.valueOf(0).equals(id)) {
                    return null;
                }
                return id;
            } catch (IllegalArgumentException e) {
                LOG.debug(e.getMessage(), e);
            } catch (IllegalAccessException e) {
                LOG.debug(e.getMessage(), e);
            } catch (InvocationTargetException e) {
                LOG.debug(e.getMessage(), e);
            }
        }
        return null;
    }
    
    /**
     * map 中属性 转换 成 bean
     * @param parameterMap
     * @param clazz
     * @return
     */
    public static <T> T parseBean(Map<String, Object> parameterMap, Class<T> clazz){
        return parseBean(parameterMap, clazz, 0);
    }
    /**
     * 
     * @param parameterMap
     * @param clazz
     * @param type bean的名称 0:原样,1:小写 ;2:大写
     * @return
     */
    public static <T> T parseBean(Map<String, Object> parameterMap, Class<T> clazz, int type){
        if(null == parameterMap || null == clazz) {return null;}
        parameterMap = format(parameterMap, type);
        T t = createInstance(clazz);
        if(null == t) {return null;}
        for(Field f : clazz.getDeclaredFields()) {
            if(isSkip(f)) {
                continue;
            }
            String key = null;
            if(0 == type) {
                key = f.getName();
            } else if(1==type) {
                key = f.getName().toLowerCase();
            } else if(2==type) {
                key = f.getName().toUpperCase();
            }
            Object value = parameterMap.get(key);
            value = getSingleObj(value);
            if(null == value) {
                continue;
            }
            setVal(t, f, String.valueOf(value));
        }
        return t;
    }
    
    /**
     * 为对象设值
     * @param t
     * @param f
     * @param value
     */
    private static <T> void setVal(T t, Field f, String value) {
        f.setAccessible(true);
        try {
            f.set(t, convert(value, f.getType()));
        } catch (IllegalArgumentException e) {
            LOG.debug(e.getMessage(), e);
        } catch (IllegalAccessException e) {
            LOG.debug(e.getMessage(), e);
        }
    }
    
    /**
     * 为对象设值
     * @param t
     * @param f
     * @param value
     */
    public static boolean setVal2(Object t, Field f, String value) {
        if(null == t || null == f || value == null) {return false;}
        f.setAccessible(true);
        try {
            f.set(t, convert(value, f.getType()));
            return true;
        } catch (IllegalArgumentException e) {
            LOG.debug(e.getMessage(), e);
        } catch (IllegalAccessException e) {
            LOG.debug(e.getMessage(), e);
        }
        return false;
    }
    
    /**
     * 为对象设值
     * @param t
     * @param f
     * @param value
     */
    public static boolean setVal2(Object t, String fieldName, String value) {
        if(null == t || null == fieldName || "".equals(fieldName) || value == null) {return false;}
        try {
            Field f = t.getClass().getDeclaredField(fieldName);
            return setVal2(t, f, value);
        } catch (IllegalArgumentException e) {
            LOG.debug(e.getMessage(), e);
        } catch (NoSuchFieldException e) {
            LOG.debug(e.getMessage(), e);
        } catch (SecurityException e) {
            LOG.debug(e.getMessage(), e);
        }
        return false;
    }
    /**
     * 字符串转换成 字段 相对应的 类型
     * @param value
     * @param clzss
     * @return
     */
    private static Object convert(String value, Class<?> clzss) {
        if(null == value || null == clzss) { return null; }
        value = value.trim();
        if(clzss.equals(String.class)) { return value; }
        Method method;
        Object val = null;
        if(Number.class.isAssignableFrom(clzss)) {
            value = prepareNumber(value);
            try {
                method = clzss.getMethod("valueOf", String.class);
                val = method.invoke(null, value);
            } catch (NoSuchMethodException e) {
                LOG.debug(e.getMessage(), e);
            } catch (SecurityException e) {
                LOG.debug(e.getMessage(), e);
            } catch (IllegalAccessException e) {
                LOG.debug(e.getMessage(), e);
            } catch (IllegalArgumentException e) {
                LOG.debug(e.getMessage(), e);
            } catch (InvocationTargetException e) {
                LOG.debug(e.getMessage(), e);
            }
        } else if(Date.class.isAssignableFrom(clzss)) {
            val = parseDate(value);
        }
        return val;
    }
    /**
     * 滤除 字符串中 非数字 的部分
     * @param value
     * @return
     */
    private static String prepareNumber(String value) {
        return value.replaceAll("\\D", "");
    }
    /** 日期格式  数组 */
    private static final String[] DATE_FORMAT_ARR = new String[]{"yyyy-MM-dd","yyyy/MM/dd","(yyyy-MM-dd)"};
    /**
     * String 转换 成 Date
     * @param value
     * @param val
     * @return
     */
    private static Date parseDate(String value) {
        for(String format : DATE_FORMAT_ARR){
            try {
                return DateUtils.parse(value, format);
            } catch (ParseException e) {
                LOG.debug(e.getMessage());
            }
        }
        return null;
    }
    /** 需要跳过的 字段 修饰符 数组 */
    private static final String[] SKIP_MOD_ARR = new String[]{"static","final"};
    /**
     * 判断 字段 是否需要 进行赋值操作
     * @param f
     * @return
     */
    private static boolean isSkip(Field f) {
        String mods = Modifier.toString(f.getModifiers());
        for(String mod : SKIP_MOD_ARR) {
            if(mods.contains(mod)) {
                return true;
            }
        }
        return false;
    }
    /**
     * 创建新对象
     * @param clazz
     * @return
     */
    public static <T> T createInstance(Class<T> clazz) {
        if(null == clazz) return null;
        T t = null;
        try {
            t = clazz.newInstance();
        } catch (InstantiationException e) {
            LOG.debug(e.getMessage(), e);
        } catch (IllegalAccessException e) {
            LOG.debug(e.getMessage(), e);
        }
        return t;
    }
    /**
     * 将对象 obj 的属性值  赋给 clazz对象的同名属性
     * @param obj
     * @param clazz
     * @return
     */
    public static <T> T tramsmit(Object obj, Class<T> clazz) {
        obj = getSingleObj(obj);
        if(null == obj || null == clazz) { return null;}
        T t = createInstance(clazz);
        if(null == t) { return null;}
        for(Field f : clazz.getDeclaredFields()) {
            if(isSkip(f)) {
                continue;
            }
            Object value = null;
            try {
                Field of = obj.getClass().getDeclaredField(f.getName());
                if(null == of) {
                    continue;
                }
                of.setAccessible(true);
                value = of.get(obj);
            } catch (IllegalArgumentException e) {
                LOG.debug(e.getMessage(), e);
            } catch (IllegalAccessException e) {
                LOG.debug(e.getMessage(), e);
            } catch (SecurityException e) {
                LOG.debug(e.getMessage(), e);
            } catch (NoSuchFieldException e) {
                LOG.debug(e.getMessage(), e);
            }
            value = getSingleObj(value);
            if(null == value) {
                continue;
            }
            setVal(t, f, String.valueOf(value));
        }
        return t;
    }
    /**
     * read params
     * @param inputStream
     * @return
     */
    public static final String getStrFromStream(InputStream inputStream) {
        StringBuilder sb = new StringBuilder();
        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
            String tmp = null;
            while( (tmp = br.readLine()) != null) {
                sb.append(tmp);
            }
        } catch (IOException e) {
            LOG.debug(e.getMessage(), e);
        } finally {
            try {
                inputStream.close();
            } catch (IOException e) {
                LOG.debug(e.getMessage(), e);
            }
        }
        return sb.toString();
    }
    /**
     * 获取字节码对应的  名
     * @param name
     * @param clzss
     * @return
     */
    public static final Field getDeField(String name, Class<?> clzss) {
        if(null == name || null == clzss || clzss.isArray()) {
            return null;
        }
        Field resumeIdField = null;
        try {
            resumeIdField = clzss.getDeclaredField(name);
        } catch (SecurityException e) {
            LOG.debug(e.getMessage(), e);
        } catch (NoSuchFieldException e) {
            LOG.debug(e.getMessage(), e);
        }
        return resumeIdField;
    }
    /** 判断对象的属性中是否有null值 */
    public static final boolean hasNullValue(Object obj, List<String> list) {
        obj = getSingleObj(obj);
        if(null == obj) return true;
        for(Field f : obj.getClass().getDeclaredFields()) {
            f.setAccessible(true);
            try {
                if(f.get(obj) == null) {
                    if(null != list) {
                        list.add(obj.getClass().getSimpleName() + "_" + f.getName());
                    }
                    return true;
                }
            } catch (IllegalArgumentException e) {
                LOG.debug(e.getMessage(), e);
            } catch (IllegalAccessException e) {
                LOG.debug(e.getMessage(), e);
            }
        }
        return false;
    }
    
    /**
     * 比较两个对象的 名称相同字段 数值是否相同, 返回 数值不同的字段名 数组
     * @param obj
     * @param com
     * @return
     */
    public static final List<String> getDiffFieldNames(Object obj, Object com){
        obj = getSingleObj(obj);
        com = getSingleObj(com);
        if(null == obj || null == com) {return null;}
        List<String> list = new ArrayList<String>();
        for(Field f : obj.getClass().getDeclaredFields()) {
            if(isSkip(f)) { continue;}
            f.setAccessible(true);
            try {
                Field df = com.getClass().getDeclaredField( f.getName() );
                if(null == df) { continue; }
                df.setAccessible(true);
                Object value = f.get(obj);
                Object valueC = df.get(com);
                if(null == value || null == valueC) { continue;}
                if(!String.valueOf(value).equals(String.valueOf(valueC))) {
                    list.add(f.getName());
                }
            } catch (NoSuchFieldException e) {
                LOG.debug(e.getMessage(), e);
            } catch (SecurityException e) {
                LOG.debug(e.getMessage(), e);
            } catch (IllegalArgumentException e) {
                LOG.debug(e.getMessage(), e);
            } catch (IllegalAccessException e) {
                LOG.debug(e.getMessage(), e);
            }
        }
        return list.isEmpty() ? null : list;
    }
    
    /**
     * 简历多条模块比较部分公共函数
     * @param latestResume 最新简历对象
     * @param standardResume 标准简历对象
     * @param moduleName 对比模块名称
     * @return 
     */
    public static final boolean getCompareResult(PreviewResume latestResume,
            PreviewResume standardResume, String moduleName) {
        ResumeCompareModuleEnum val = ResumeCompareModuleEnum
                .valueOf(moduleName.toUpperCase());
        String packageName = ResumeCompareModuleEnum.PACKAGE_NAME.value();
        String className = packageName+val.value();
        List<?> latestModuleInfo = getListObjectValue(latestResume,className);
        List<?> standardModuleInfo = getListObjectValue(standardResume,className);
        boolean isSame = false;
        if (ListUtil.isNotBlank(standardModuleInfo)) {
            /**
             * 标准简历不为空时,最新简历为空或者记录条数不一致,都表示不相等
             */
            if (ListUtil.isBlank(latestModuleInfo) || standardModuleInfo.size() != latestModuleInfo.size()) {
                return isSame;
            }
            
            String mathedName = ResumeCompareModuleEnum.MATHED_NAME.value();
            try {
                //两条比较信息字符串
                String standardCompareInfo;
                String latestCompareInfo;
                for(Object standardObj : standardModuleInfo){
                    //多条数据对比时默认为不相同
                    isSame = false;
                    standardCompareInfo = standardObj.getClass()
                            .getMethod(mathedName).invoke(standardObj)
                            .toString();
                    int index = 0;
                    for(Object latestObj : latestModuleInfo){
                        latestCompareInfo = latestObj.getClass()
                                .getMethod(mathedName).invoke(latestObj)
                                .toString();
                        if(standardCompareInfo.equals(latestCompareInfo)){
                            isSame = true;
                            break;
                        }
                        /**
                         * 与最新简历每条记录对比完成后未发现相同数据,说明有差异数据
                         */
                        if (index + 1 == latestModuleInfo.size() && !isSame) {
                            isSame = false;
                            break;
                        }
                        index++;
                    }
                    /**
                     * 有差异数据时停止循环比较,返回false,两个消息不相等
                     */
                    if (!isSame) {
                        break;
                    }
                }
            }catch (IllegalAccessException e) {
                LOG.debug(e.getMessage(), e);
            } catch (IllegalArgumentException e) {
                LOG.debug(e.getMessage(), e);
            } catch (SecurityException e) {
                LOG.debug(e.getMessage(), e);
            } catch (InvocationTargetException e) {
                LOG.debug(e.getMessage(), e);
            } catch (NoSuchMethodException e) {
                LOG.debug(e.getMessage(), e);
            }
        }else{
            //标准简历为空,不打红框
            isSame = true;
        }
        return isSame;
    }
    /** 获取最底层元素 */
    public static Object getSingleObj(Object obj) {
        if(null == obj) return obj;
        if( obj.getClass().isArray()) {
            if( ((Object[])obj).length > 0) {
                return getSingleObj(((Object[])obj)[0]);
            }
            return null;
        } else if(obj instanceof List) {
            if(((List<?>)obj).size() > 0) {
                return getSingleObj(((List<?>)obj).get(0));
            }
            return null;
        } 
        return obj;
    }
    /**
     * 根据类名获取实例中的List对象值
     * @param obj 对象实例
     * @param className 获取的类
     * @return
     */
    public static List<?> getListObjectValue(Object obj,String className){
        for(Field f : obj.getClass().getDeclaredFields()){
            f.setAccessible(true);
            try {
                Object listObject = f.get(obj);
                if(null == listObject || !(listObject instanceof List)){
                    continue;
                }
                List<?> objValue = (List<?>)listObject;
                if(objValue.isEmpty()){
                    continue;
                }
                if(className.equals(objValue.get(0).getClass().getName())){
                    return objValue;
                }
            } catch (IllegalArgumentException e) {
                LOG.debug(e.getMessage(), e);
            } catch (IllegalAccessException e) {
                LOG.debug(e.getMessage(), e);
            }
        }
        return null;
    }
    /**
     * 实体类对象 转换成 map集合
     * @param obj
     * @return
     */
    public static Map<String, Object> obj2Map(Object obj) {
        Map<String, Object> map = new HashMap<String, Object>();
        if(null == obj){ return map; }
        for(Field f : obj.getClass().getDeclaredFields()) {
            if (Modifier.toString(f.getModifiers()).indexOf("static") > -1 ) {
                continue;
            }
            f.setAccessible(true);
            Object value = null;
            try {
                value = f.get(obj);
            } catch (IllegalArgumentException e) {
                LOG.debug(e.getMessage(), e);
            } catch (IllegalAccessException e) {
                LOG.debug(e.getMessage(), e);
            }
            map.put(f.getName(), value);
        }
        return map;
    }
    /**
     * 过滤 map中的key,将"_"去除,例:"CATEGORY_DESC" >> "categoryDesc"
     * @param map
     * @return
     */
    public static Map<String, Object> format(Map<String, Object> map) { 
        return format( map, 0);
    }
    /**
     * 
     * @param map
     * @param type key的样式 
     *                 0:原样,  例:"CATEGORY_DESC" >> "CATEGORY_DESC"
     *                 1:小写 ;  例:"CATEGORY_DESC" >> "categorydesc"
     *                 2:大写,  例:"CATEGORY_DESC" >> "CATEGORYDESC"
     *                 3:特殊  例:"CATEGORY_DESC" >> "categoryDesc"
     * @return
     */
    public static Map<String, Object> format(Map<String, Object> map, int type) { 
        if(0 == type) return map;
        Map<String, Object> tmp = new HashMap<String, Object>();
        for(Entry<String, Object> en : map.entrySet()) {
            String key = en.getKey();
            if(null == key) continue;
            if(1 == type) {
                key = en.getKey().replace("_", "").toLowerCase();
            } else if(2 == type) {
                key = en.getKey().replace("_", "").toUpperCase();
            } else if(3 == type) {
                StringBuilder sb = new StringBuilder();
                String[] arr = en.getKey().split("_");
                for(String str : arr) {
                    sb.append(Character.toUpperCase(str.charAt(0))).append(str.substring(1).toLowerCase());
                }
                sb.replace(0, 1, Character.toString(Character.toLowerCase( sb.charAt(0) )));
                key = sb.toString();
            }
            tmp.put(key, en.getValue());
        }
        return tmp;
    }

    /**
     * 获取注解配置的表名
     * @param clzss
     * @return
     */
    public static <T> String getTableName(Class<T> clzss) {
        Entity en = null;
        String table = null;
        if(null == clzss || null == (en = clzss.getAnnotation(Entity.class))
                || null == (table = en.name()) || "".equals(table)) {
            return null;
        }
        return table;
    }
    
    /**
     * 获取方法 上 配置的 字段名
     * @param method
     * @return
     */
    public static String getColumnName(Method method) {
        Column en = null;
        if(null == method || null == (en = method.getAnnotation(Column.class))) {
            return null;
        }
        return en.name();
    }

    /**
     * 
     * @param obj
     * @param fieldName
     * @return
     */
    public static Object getValue(Object obj, String fieldName) {
        Object result = null;
        if(StringUtil.hasNull(obj, fieldName)) return result;
        try {
            Field f = obj.getClass().getDeclaredField(fieldName);
            result = getValue(obj, f);
        } catch (NoSuchFieldException e) {
            LOG.debug(e.getMessage(), e);
        } catch (SecurityException e) {
            LOG.debug(e.getMessage(), e);
        }
        return result;
    }
    
    /**
     * 获取对象 相应 字段的 值
     * @param obj
     * @param f
     * @return
     */
    public static Object getValue(Object obj, Field f) {
        if(StringUtil.hasNull(obj, f)) return null;
        f.setAccessible(true);
        try {
            return f.get(obj);
        } catch (IllegalArgumentException e) {
            LOG.debug(e.getMessage(), e);
        } catch (IllegalAccessException e) {
            LOG.debug(e.getMessage(), e);
        }
        return null;
    }
    /**
     * 获取对象 相应 方法的返回值
     * @param obj
     * @param method
     * @return
     */
    public static Object getValue(Object obj, Method method) {
        if(StringUtil.hasNull(obj, method)) return null;
            try {
                return method.invoke(obj);
            } catch (IllegalAccessException e) {
                LOG.debug("obj: [ " + obj + " ] method: [ " + method + " ] " + e.getMessage(), e);
            } catch (IllegalArgumentException e) {
                LOG.debug("obj: [ " + obj + " ] method: [ " + method + " ] " + e.getMessage(), e);
            } catch (InvocationTargetException e) {
                LOG.debug("obj: [ " + obj + " ] method: [ " + method + " ] " + e.getMessage(), e);
            }
        return null;
    }
    /**
     * 反射 根据方法名调用方法  
     * @param obj 对象
     * @param value 方法参数
     * @param methodName 方法名
     * @return
     */
    public static boolean invokeValue(Object obj, Object value, String methodName) {
        boolean result = false;
        if(StringUtil.hasNull(obj, value, methodName)) return result;
        for(Method me : obj.getClass().getDeclaredMethods()) {
            if(!methodName.equals(me.getName())) continue;
            try {
                me.setAccessible(true);
                me.invoke(obj, value);
                result = true;
            } catch (IllegalAccessException e) {
                LOG.debug(e.getMessage(), e);
            } catch (IllegalArgumentException e) {
                LOG.debug(e.getMessage(), e);
            } catch (InvocationTargetException e) {
                LOG.debug(e.getMessage(), e);
            }
        }
        return result;
    }
    /**
     * 反射 根据方法名调用方法  
     * @param obj 对象
     * @param methodName 方法名
     * @return
     */
    public static Object invokeValue(Object obj, String methodName) {
        Object result = null;
        if(StringUtil.hasNull(obj, methodName)) return result;
        for(Method me : obj.getClass().getDeclaredMethods()) {
            if(!methodName.equals(me.getName()) || me.getParameterTypes().length > 0) continue;
            try {
                me.setAccessible(true);
                result = me.invoke(obj);
            } catch (IllegalAccessException e) {
                LOG.debug(e.getMessage(), e);
            } catch (IllegalArgumentException e) {
                LOG.debug(e.getMessage(), e);
            } catch (InvocationTargetException e) {
                LOG.debug(e.getMessage(), e);
            }
        }
        return result;
    }
    
    /**
     * 查询的wheresql语句
     * @param obj
     * @return
     */
    public static String getWhereSql(Object obj) {
        StringBuilder sb =new StringBuilder();
        if(null == obj) return sb.toString();
        for(Method me : obj.getClass().getDeclaredMethods()) {
            String columnName = BeanHelpUtils.getColumnName(me);
            if(null == columnName || "".equals(columnName)) continue;
            Object value = null;
            try {
                value = me.invoke(obj);
            } catch (IllegalAccessException e) {
                LOG.debug(e.getMessage(), e);
            } catch (IllegalArgumentException e) {
                LOG.debug(e.getMessage(), e);
            } catch (InvocationTargetException e) {
                LOG.debug(e.getMessage(), e);
            }
            if(null == value || "".equals(value) || value.equals(ZERO_LONG) || value.equals(ZERP_INT)) continue;
            sb.append(" and ").append(columnName).append(" like '%").append(value.toString().replace("'", "")).append("%'");
        }
        return sb.toString();
    }
    /**
     * 
     * @param t
     * @return
     */
    public static String getIdWhereSql(Object t) {
        if(null == t) { return null; }
        Class<?> clzss = t.getClass();
        StringBuilder whereSql = new StringBuilder();
        for(Method method : clzss.getDeclaredMethods() ) {
            Id annotation = method.getAnnotation(Id.class);
            if(null == annotation) { continue; }
            String columnName = BeanHelpUtils.getColumnName(method);
            if(StringUtil.hasEmpty(columnName)) { continue; }
            whereSql.append(" and ").append(columnName);
            Object value = BeanHelpUtils.getValue(t, method);
            if(null == value) {
                whereSql.append(" is null");
            } else {
                whereSql.append(" = '").append(value).append("'");
            }
        }
        if(whereSql.length() == 0) return null;
        return whereSql.toString();
    }
    /** 特殊字符过滤 */
    private static final Map<String, String> FILTER_LETTER = new HashMap<String, String>();
    static {
        FILTER_LETTER.put("%", "\\%");
        FILTER_LETTER.put("_", "\\_");
        FILTER_LETTER.put("'", "\\'");
        FILTER_LETTER.put("\"", "\\\"");
        FILTER_LETTER.put("\n", " ");
        FILTER_LETTER.put("\r", " ");
        FILTER_LETTER.put("!", "!");
        FILTER_LETTER.put("(", "(");
        FILTER_LETTER.put(")", ")");
    }
    /**
     * 过滤字符中的特殊值
     * @return
     */
    public static String filterStr(String str) {
        if(null == str || "".equals(str)) return str;
        for(Entry<String, String> en : FILTER_LETTER.entrySet()) {
            str = str.replace(en.getKey(), en.getValue());
        }
        return str;
    }/**
     * 
     * @param str
     * @return
     */
    public static int parseInt(String str) {
        int flag = 0;
        try {
            flag = StringUtil.hasNull(str) ? 0 : Integer.parseInt(str);
        } catch(Exception e) {
            LOG.debug(e.getMessage(),e);
        }
        return flag; 
    }
}
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值