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

import org.apache.commons.lang3.time.DateFormatUtils;

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


    /**
     *
     * @param pre 前缀
     * @param maxCode  表的最大流水后缀
     *  addOne  1自动加1,0不加
     * @return
     */
    public static  String createSerialNumber(String pre,String maxCode,int addOne){
        String result=null;
        Date date=new Date();
        String datestr=DateFormatUtils.format(date, "yyyyMMdd");
        SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");
        Integer count=0;
        String code = null;
        if (maxCode!=null&&!maxCode.equals("0") && maxCode.substring(maxCode.length() - 12, maxCode.length() - 4).equals(datestr)) {
            Integer Count = Integer.valueOf(maxCode.substring(maxCode.length() - 4, maxCode.length()));
            if(addOne==1){
                code = pre + df.format(date) + String.format("%04d", Count + 1);
            }else{
                code = pre + df.format(date) + String.format("%04d", Count);
            }

        } else {
            code = pre + df.format(date) + "0001";
        }
/*
        if(StringUtils.isNotEmpty(maxCode)&&!maxCode.equals("0")){
            count = Integer.valueOf(maxCode.substring(maxCode.length() - 4, maxCode.length()));
        }
        if(addOne==1){
            result=pre+datestr+ String.format("%04d", count + 1);
        }else{
            result=pre+datestr+ String.format("%04d", count );
        }*/
        return code;
    }
    /**
     *
     * @param pre 前缀
     * @param maxCode  表的最大流水后缀
     *  addOne  1自动加1,0不加
     * @return
     */
    public static  String createSerialNumberV(String pre,String maxCode){
        String result=null;
        Date date=new Date();
        String datestr=DateFormatUtils.format(date, "yyyyMMdd");
        SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");
        Integer count=0;
        String code = null;
//        System.out.println(maxCode.substring(maxCode.length() - 12, maxCode.length() - 4));
        System.out.println(datestr);
//        String msxcodeplitPre=maxCode.split(pre)[0];
        //如果指定类型的最后的code存在,并且日期一致。那么 code = 入库单类型 + 年月日 + (排序号 + 1)
        if (!maxCode.equals("0") && maxCode.substring(maxCode.length() - 12, maxCode.length() - 4).equals(datestr)) {
            Integer Count = Integer.valueOf(maxCode.substring(maxCode.length() - 4, maxCode.length()));
            code = pre + df.format(date) + String.format("%04d", Count + 1);
        } else {
            code = pre + df.format(date) + "0001";
        }
        return code;
    }


    /**
     *再生批号 addOne为0则是昨天得
     * @param pre 前缀
     *  addOne  1自动加1,0不加
     * @return
     */
    public static  String createSerialNumberForRegenerationCode(String pre,int addOne){
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
        String result=null;
        Date date=new Date();
        String datestr=format.format(date);
        Calendar c = Calendar.getInstance();
        c.add(Calendar.DATE, -1);
        Date start = c.getTime();
        String qyt= format.format(start);//前一天
        if(addOne==1){
            result=pre+datestr;
        }else{
            result=pre+qyt;
        }
        return result;
    }

    public static void main(String[] args) {
        System.out.println(createSerialNumber("P","CGRKD2022042700001",1));
    }

}