tuqin
18 小时以前 0ddbb4ec727ffce844ef672107e3410d84b1f11a
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
package cn.iocoder.yudao.module.erp.dal.redis.no;
 
import cn.iocoder.yudao.framework.common.util.number.BizNoUtils;
import jakarta.annotation.Resource;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Repository;
 
 
/**
 * Erp 订单序号的 Redis DAO
 *
 * @author HUIHUI
 */
@Repository
public class ErpNoRedisDAO {
 
    /**
     * 销售订单 {@link cn.iocoder.yudao.module.erp.dal.dataobject.sale.ErpSaleOrderDO}
     */
    public static final String SALE_ORDER_NO_PREFIX = "XSDD";
 
    /**
     * 采购订单 {@link cn.iocoder.yudao.module.erp.dal.dataobject.purchase.ErpPurchaseOrderDO}
     */
    public static final String PURCHASE_ORDER_NO_PREFIX = "CGDD";
 
    /**
     * 采购申请 {@link cn.iocoder.yudao.module.erp.dal.dataobject.purchase.ErpPurchaseRequestDO}
     */
    public static final String PURCHASE_REQUEST_NO_PREFIX = "CGSQ";
 
    /**
     * 采购来票 {@link cn.iocoder.yudao.module.erp.dal.dataobject.purchase.ErpPurchaseInvoiceDO}
     */
    public static final String PURCHASE_INVOICE_NO_PREFIX = "FPD";
 
    /**
     * 付款单 {@link cn.iocoder.yudao.module.erp.dal.dataobject.finance.ErpFinancePaymentDO}
     */
    public static final String FINANCE_PAYMENT_NO_PREFIX = "FKD";
    /**
     * 收款单 {@link cn.iocoder.yudao.module.erp.dal.dataobject.finance.ErpFinanceReceiptDO}
     */
    public static final String FINANCE_RECEIPT_NO_PREFIX = "SKD";
 
    @Resource
    private StringRedisTemplate stringRedisTemplate;
 
    /**
     * 生成 16 位纯数字业务编码(全系统唯一)。
     *
     * <p>为兼容既有调用方,保留 {@code prefix} 入参,但不再参与编码拼接。
     *
     * @param prefix 单据类型前缀(已不再参与编码拼接)
     * @return 16 位纯数字编码
     */
    public String generate(String prefix) {
        // 全系统共用同一自增序列,保证编码「16 位纯数字」且全系统唯一
        // 随机生成 16 位纯数字编码,并用 Redis 集合查重,保证全系统唯一(不使用自增/顺序)
        for (int i = 0; i < BizNoUtils.MAX_RETRY; i++) {
            String code = BizNoUtils.randomCode();
            Long added = stringRedisTemplate.opsForSet().add(BizNoUtils.USED_SET_KEY, code);
            if (added != null && added > 0) {
                return code;
            }
        }
        throw new IllegalStateException("生成唯一业务编码失败,请重试");
    }
 
}