DataUtils.java 5.1 KB
package com.huaheng.common.utils;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;

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);
    }



    /**
     * 流水码
     * 根据类型生成单号  规则:入库单类型 + 年月日 + 5位(排序号 + 1)
     * @dataType code的前缀
     * @maxCode  code当前表的最后一个值
     * @return
     */
    public String createCode(String dataType,String maxCode) {
        String code = null;
        int i = 1;
        Date now = new Date();
        SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");
        if (maxCode != null && maxCode.substring(maxCode.length() - 13, maxCode.length() - 5).equals(df.format(now))) {
            Integer Count = Integer.valueOf(maxCode.substring(maxCode.length() - 5, maxCode.length()));
            code =dataType+ df.format(now) + String.format("%05d", Count + i++);
        } else {
            code =dataType + df.format(now) + String.format("%05d", 0000+i++);
        }
        return code;
    }
}