当前位置:   article > 正文

生成订单编号(唯一流水码)实际应用_数据库订单编号怎么设计

数据库订单编号怎么设计

在开发中,我们需要生成一些流水号,订单编号,等等一些东西。这个时候需要我们使用锁来控制流水码的唯一。
不啰嗦。直接上代码。另外大家觉得好看有用,给点个赞呗。

import com.mindskip.sdd.service.*;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.locks.ReentrantLock;

/**
 * 下单
 *
 * @author 567
 */
@RestController
@RequestMapping(value = "/api/order")
public class OrderController{
	/**
	 * 流水码表,只有四个字段:id,类型,流水码,日期(定时任务,每天24时都会清空这张表)
	 */
	private SerialNumberService serialNumberService;

    /**
     * 指针1:记录已有流水号的单号
     */
    public static int num = 0;
       /**
     * 指针2:记录时间每天重新生成流水单号
     */    
    public static String nowDay = "";
    /**
     * 锁,多线程/并发下,保障单号生成唯一
     */    
    private static ReentrantLock lock = new ReentrantLock();
  
    /**
     * 机构编码(10位前置字符,不够补零)+yyyyMMdd+ 6位流水号
     *
     * @return
     */
    private String generatorOrderCode() {
    	//getCurrentUser 为获取用户
        String domain ="想要的前置字符";
        //不够10位,补0
        for (int i = domain.length(); i < 10; i++) {
            domain = String.format("%s0", domain);
        }
        return getNum(serialNumberService, domain).toUpperCase(Locale.ROOT);
    }

    /**
     * 生成订单的流水码
     *
     * @param code
     * @return
     */

    private static String getNum(SerialNumberService serialNumberService, String code) {
        //指针,最后生成的数
        String unique = "";
        //上锁
        lock.lock();
        try {
            //现在的日期
            String newDay = new SimpleDateFormat("yyyyMMdd").format(new Date());
            if (StringUtils.isBlank(nowDay)) {
                //nowDay 是第一次启动。给与当天的日期
                nowDay = newDay;
            }
            if (!nowDay.equals(newDay)) {
                //记录的天,跟现在的日期不一致。则赋值num重新生成编号。
                //用于上线后,一直运行不曾中断,第二天申请开班的情况
                num = 0;
                nowDay = newDay;
            }
            //5位流水号
            if (num == 0) {
                //当天第一份流水单号
                //查询当天最大id的流水单号。
                //如果大家觉得ReentrantLock锁不保险,可以尝试数据库加锁
                String k = serialNumberService.selectNowDayMaxId();
                if (k == null) {
                    unique = nowDay + "000001";
                } else {
	                //从k的流水单号里,截取字符,获取单号。其实使用id可是可以的。
                    num = Integer.parseInt(k.substring(19, 24));
                    unique = unique();
                }
            } else {
                unique = unique();
            }
            //已有流水单+1
            num++;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //释放锁
            lock.unlock();
        }
        return code + unique;
    }

    private static String unique() {
        //当天最后的订单流水号累加1
        String nums = String.valueOf(num + 1);
        //设定具体流水为两位数,单数则补齐前面的0
        StringBuilder sb = new StringBuilder(nums);
        for (int i = nums.length(); i < 6; i++) {
            sb.insert(0, "0");
        }
        return nowDay + sb;
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111

对了,多谢这位的博客:https://blog.csdn.net/weixin_38256539/article/details/125393024

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/954340
推荐阅读
相关标签
  

闽ICP备14008679号