| | |
| | | package cn.iocoder.yudao.module.hrm.dal.redis; |
| | | |
| | | import cn.hutool.core.date.DatePattern; |
| | | import cn.hutool.core.date.DateUtil; |
| | | 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; |
| | | |
| | | /** |
| | | * HRM 编号生成的 Redis DAO |
| | |
| | | * 离职申请编号前缀 |
| | | */ |
| | | public static final String RESIGNATION_APPLICATION_NO_PREFIX = "RA"; |
| | | |
| | | /** |
| | | * 员工合同编号前缀 |
| | | */ |
| | | public static final String EMPLOYEE_CONTRACT_NO_PREFIX = "HT"; |
| | | |
| | | @Resource |
| | | private StringRedisTemplate stringRedisTemplate; |
| | |
| | | } |
| | | |
| | | /** |
| | | * 生成员工合同编号,格式:HT + yyyyMMdd + 4位自增 |
| | | * 例如:HT202401010001 |
| | | * |
| | | * @return 员工合同编号 |
| | | */ |
| | | public String generateEmployeeContractNo() { |
| | | return generate(EMPLOYEE_CONTRACT_NO_PREFIX, 4); |
| | | } |
| | | |
| | | /** |
| | | * 生成薪酬核算编号,格式:SC + yyyyMMdd + 4位自增 |
| | | * 例如:SC202401010001 |
| | | * |
| | |
| | | } |
| | | |
| | | /** |
| | | * 生成编号 |
| | | * 生成 16 位纯数字业务编码(全系统唯一)。 |
| | | * |
| | | * @param prefix 前缀 |
| | | * @param digits 位数 |
| | | * @return 编号 |
| | | * <p>为兼容既有调用方,保留入参,但不再参与编码拼接。 |
| | | * |
| | | * @return 16 位纯数字编码 |
| | | */ |
| | | public String generate(String prefix, int digits) { |
| | | String noPrefix = prefix + DateUtil.format(LocalDateTime.now(), DatePattern.PURE_DATE_PATTERN); |
| | | String key = HrmRedisKeyConstants.NO + noPrefix; |
| | | Long no = stringRedisTemplate.opsForValue().increment(key); |
| | | // 设置过期时间 1 天 |
| | | stringRedisTemplate.expire(key, Duration.ofDays(1L)); |
| | | return noPrefix + String.format("%0" + digits + "d", no); |
| | | // 全系统共用同一自增序列,保证编码「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("生成唯一业务编码失败,请重试"); |
| | | } |
| | | |
| | | } |