| | |
| | | package cn.iocoder.yudao.module.crm.dal.redis.no; |
| | | |
| | | import cn.hutool.core.date.DatePattern; |
| | | import cn.hutool.core.date.DateUtil; |
| | | import cn.iocoder.yudao.module.crm.dal.redis.RedisKeyConstants; |
| | | 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; |
| | | |
| | | import java.time.Duration; |
| | | import java.time.LocalDateTime; |
| | | |
| | | |
| | | /** |
| | |
| | | private StringRedisTemplate stringRedisTemplate; |
| | | |
| | | /** |
| | | * 生成序号,使用当前日期,格式为 {PREFIX} + yyyyMMdd + 6 位自增 |
| | | * 例如说:QTRK 202109 000001 (没有中间空格) |
| | | * 生成 16 位纯数字业务编码(全系统唯一)。 |
| | | * |
| | | * @param prefix 前缀 |
| | | * @return 序号 |
| | | * <p>为兼容既有调用方,保留 {@code prefix} 入参,但不再参与编码拼接。 |
| | | * |
| | | * @param prefix 单据类型前缀(已不再参与编码拼接) |
| | | * @return 16 位纯数字编码 |
| | | */ |
| | | public String generate(String prefix) { |
| | | // 递增序号 |
| | | String noPrefix = prefix + DateUtil.format(LocalDateTime.now(), DatePattern.PURE_DATE_PATTERN); |
| | | String key = RedisKeyConstants.NO + noPrefix; |
| | | Long no = stringRedisTemplate.opsForValue().increment(key); |
| | | // 设置过期时间 |
| | | stringRedisTemplate.expire(key, Duration.ofDays(1L)); |
| | | return noPrefix + String.format("%06d", no); |
| | | // 全系统共用同一自增序列,保证编码「16 位纯数字」且全系统唯一 |
| | | Long no = stringRedisTemplate.opsForValue().increment(BizNoUtils.GLOBAL_SEQ_KEY); |
| | | return BizNoUtils.format(no); |
| | | } |
| | | |
| | | } |