java之流水单号的生成

package com.xp.spo.utils;

import com.xp.spo.model.Express;
import com.xp.spo.service.ExpressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author Czy--feeling
 * @create 2018-09-27 2:11 PM
 * 生成格式:
LL+yyMMdd(时间)+0000(四位数),例如:LL201810100001
 * 生成规则:按当天时间从0001开始累积,今天从LL201810100001开始;明天从LL201810110001开始累积;
 **/
@Component
    public class Odd {

    private int number;

    @Autowired
    private ExpressService expressService;

    /**
     *
     */
    private static Odd odd;

    @PostConstruct
    public void init() {
        odd = this;
    }

    /**
     * 编写测试流水订单号
     * 测试流水订单号规则:自动生成(LL+8位日期+4位流水号)  示例:LL201809270001
     * @param num
     * @return
     */
    public static String getBody(Integer num) {

        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        String str = String.format("%04d", num);
        return sdf.format(new Date()).substring(0, 8) + str;

    }

    /**
     *截取流水尾号转换数字
     * @param string
     * @return
     */
    public static int getOddCode(String string) {
        int oddCode = Integer.parseInt(string.substring(10, 15));
        return oddCode;

    }

    /**
     * 截取流水单号入库日期
     * @param string
     * @return
     */
    public static int getOddSenttime(String string) {
        int oddSenttime = Integer.parseInt(string.substring(2, 10));
        return oddSenttime;

    }

    public static int getNowTimeCode() {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
        int nowTimeCode = Integer.parseInt(simpleDateFormat.format(new Date()).substring(0, 8));
        return nowTimeCode;
    }

//    public static void main(String[] args) {
//        System.out.println(001+1);
//
//        System.out.println("122330003".substring(5,9));
//
//        System.out.println(odd.getOddCode("XP201809270201"));
//
//        System.out.println(getNowTimeCode());
//
//        System.out.println(getOddSenttime("XP201810090201"));
//
//        System.out.println(odd.getBody(01));
//
//    }

    /**
     *生成流水单号
     * @return
     */
    public static String createOdd() {

        String nowOdd=null;
        String oddMaxCode = odd.expressService.findMaxOdd();
//如果最大流水单号不为空
        if (oddMaxCode!=null) {
//如果当前时间不相同,例如:20181009=!20180809,重新开始以当天日期拼流水单号201810100001
            if (getNowTimeCode() != getOddSenttime(oddMaxCode)) {
                int number = 1;
                Express express = new Express();
                express.setOdd("LL"+getBody(number));
                nowOdd = express.getOdd();
                //相同,则加1,例如:201810100002
            }else {
                int number = odd.getOddCode(oddMaxCode);
                Express express = new Express();
                express.setOdd("LL"+getBody(getOddCode(oddMaxCode)+1));
                nowOdd = express.getOdd();
            }
            //如果没有流水单号,以当前日期重新开始生成流水单号
        }else  {
            int number = 1;
            Express express = new Express();

            express.setOdd("LL"+getBody(number));

            nowOdd = express.getOdd();
        }

        return nowOdd;
    }

}

你可能感兴趣的:(java之流水单号的生成)