| | |
| | | import cn.hutool.core.lang.Assert; |
| | | import cn.hutool.core.util.StrUtil; |
| | | import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils; |
| | | import cn.iocoder.yudao.framework.common.util.number.BizNoUtils; |
| | | import cn.iocoder.yudao.module.mes.dal.dataobject.md.autocode.MesMdAutoCodePartDO; |
| | | import cn.iocoder.yudao.module.mes.dal.dataobject.md.autocode.MesMdAutoCodeRecordDO; |
| | | import cn.iocoder.yudao.module.mes.dal.dataobject.md.autocode.MesMdAutoCodeRuleDO; |
| | |
| | | import cn.iocoder.yudao.module.mes.service.md.autocode.strategy.MesMdAutoCodePartStrategy; |
| | | import jakarta.annotation.PostConstruct; |
| | | import jakarta.annotation.Resource; |
| | | import org.springframework.data.redis.core.StringRedisTemplate; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | import org.springframework.validation.annotation.Validated; |
| | |
| | | /** |
| | | * MES 编码生成记录 Service 实现类 |
| | | * |
| | | * @author 芋道源码 |
| | | * @author 超级管理员 |
| | | */ |
| | | @Service |
| | | @Validated |
| | |
| | | private MesMdAutoCodeRuleService ruleService; |
| | | @Resource |
| | | private MesMdAutoCodePartService partService; |
| | | |
| | | @Resource |
| | | private StringRedisTemplate stringRedisTemplate; |
| | | |
| | | @Resource |
| | | private List<MesMdAutoCodePartStrategy> strategyList; |
| | |
| | | */ |
| | | private String generateCode(MesMdAutoCodeRuleDO rule, List<MesMdAutoCodePartDO> parts, |
| | | MesMdAutoCodeContext context) { |
| | | StringBuilder codeBuilder = new StringBuilder(); |
| | | for (MesMdAutoCodePartDO part : parts) { |
| | | MesMdAutoCodePartStrategy strategy = strategyMap.get(part.getType()); |
| | | Assert.notNull(strategy, () -> exception(AUTO_CODE_GENERATE_FAILED)); |
| | | // 生成分段编码 |
| | | String partCode = strategy.generate(part, context); |
| | | // 统一截取指定长度 |
| | | partCode = StrUtil.sub(partCode, 0, part.getLength()); |
| | | // 拼接分段编码 |
| | | codeBuilder.append(partCode); |
| | | // 需求:所有业务编码统一为「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; |
| | | } |
| | | } |
| | | // 补齐处理 |
| | | String result = codeBuilder.toString(); |
| | | if (Boolean.TRUE.equals(rule.getPadded()) && rule.getMaxLength() != null) { |
| | | result = padCode(result, rule); |
| | | } |
| | | return result; |
| | | throw new IllegalStateException("生成唯一业务编码失败,请重试"); |
| | | } |
| | | |
| | | /** |