Commit 98e4f94c6897cf93401c5af2a7ddea7011a6fa3b

Authored by YunaiV
1 parent 8b94433f

✨ feat(mes): 优化条码生成与校验逻辑

重构条码创建和更新流程,合并数据校验与内容生成逻辑,确保条码内容唯一性和配置一致性。新增校验方法以提高代码可读性和维护性。
yudao-module-mes/src/main/java/cn/iocoder/yudao/module/mes/service/wm/barcode/MesWmBarcodeServiceImpl.java
... ... @@ -34,25 +34,13 @@ public class MesWmBarcodeServiceImpl implements MesWmBarcodeService {
34 34  
35 35 @Override
36 36 public Long createBarcode(MesWmBarcodeSaveReqVO createReqVO) {
37   - // 1.1 校验条码配置
38   - MesWmBarcodeConfigDO config = barcodeConfigService.validateBarcodeConfigByBizType(createReqVO.getBizType());
39   - // 1.2 校验是否已存在条码
40   - MesWmBarcodeDO existBarcode = barcodeMapper.selectByBizTypeAndBizId(
  37 + // 1. 校验数据
  38 + MesWmBarcodeConfigDO config = validateBarcodeSaveData(createReqVO.getId(),
41 39 createReqVO.getBizType(), createReqVO.getBizId());
42   - if (existBarcode != null) {
43   - throw exception(WM_BARCODE_ALREADY_EXISTS);
44   - }
45 40  
46   - // 2.1 生成条码内容(如果前端未传递,则自动生成)
47   - String content = createReqVO.getContent();
48   - if (StrUtil.isBlank(content)) {
49   - content = generateBarcodeContent(config.getContentFormat(), createReqVO.getBizCode());
50   - }
51   - // 2.2 校验条码内容唯一性
52   - MesWmBarcodeDO contentBarcode = barcodeMapper.selectByContent(content);
53   - if (contentBarcode != null) {
54   - throw exception(WM_BARCODE_CONTENT_DUPLICATE);
55   - }
  41 + // 2. 生成条码内容,并校验唯一性
  42 + String content = generateAndValidateContent(createReqVO.getId(),
  43 + createReqVO.getContent(), config.getContentFormat(), createReqVO.getBizCode());
56 44  
57 45 // 3. 保存条码记录
58 46 MesWmBarcodeDO barcode = BeanUtils.toBean(createReqVO, MesWmBarcodeDO.class).setContent(content)
... ... @@ -77,45 +65,96 @@ public class MesWmBarcodeServiceImpl implements MesWmBarcodeService {
77 65 return generateBarcodeContent(config.getContentFormat(), bizCode);
78 66 }
79 67  
  68 + @Override
  69 + public void updateBarcode(MesWmBarcodeSaveReqVO updateReqVO) {
  70 + // 1.1 校验存在
  71 + validateBarcodeExists(updateReqVO.getId());
  72 + // 1.2 校验数据
  73 + MesWmBarcodeConfigDO config = validateBarcodeSaveData(updateReqVO.getId(),
  74 + updateReqVO.getBizType(), updateReqVO.getBizId());
  75 +
  76 + // 2. 生成条码内容,并校验唯一性
  77 + String content = generateAndValidateContent(updateReqVO.getId(),
  78 + updateReqVO.getContent(), config.getContentFormat(), updateReqVO.getBizCode());
  79 +
  80 + // 3. 更新(刷新 configId 和 format,确保与最新配置一致)
  81 + MesWmBarcodeDO updateObj = BeanUtils.toBean(updateReqVO, MesWmBarcodeDO.class)
  82 + .setConfigId(config.getId()).setFormat(config.getFormat());
  83 + if (StrUtil.isNotBlank(content)) {
  84 + updateObj.setContent(content);
  85 + }
  86 + barcodeMapper.updateById(updateObj);
  87 + }
  88 +
80 89 /**
81   - * 生成条码内容
  90 + * 校验条码保存数据:条码配置 + 业务对象唯一性
82 91 *
83   - * @param contentFormat 内容格式模板
84   - * @param bizCode 业务编码
85   - * @return 条码内容
  92 + * @param id 条码编号(新增时为 null)
  93 + * @param bizType 业务类型
  94 + * @param bizId 业务编号
  95 + * @return 条码配置
86 96 */
87   - private String generateBarcodeContent(String contentFormat, String bizCode) {
88   - if (StrUtil.isBlank(contentFormat)) {
89   - return bizCode;
90   - }
91   - return contentFormat.replace(MesWmBarcodeConfigDO.PLACEHOLDER_BUSINESS_CODE, bizCode);
  97 + private MesWmBarcodeConfigDO validateBarcodeSaveData(Long id, Integer bizType, Long bizId) {
  98 + // 1. 校验条码配置
  99 + MesWmBarcodeConfigDO config = barcodeConfigService.validateBarcodeConfigByBizType(bizType);
  100 + // 2. 校验业务对象唯一性
  101 + validateBarcodeUnique(id, bizType, bizId);
  102 + return config;
92 103 }
93 104  
94   - @Override
95   - public void updateBarcode(MesWmBarcodeSaveReqVO updateReqVO) {
96   - // 1. 校验存在
97   - validateBarcodeExists(updateReqVO.getId());
  105 + /**
  106 + * 校验业务对象(bizType + bizId)唯一性
  107 + *
  108 + * @param id 条码编号(新增时为 null,更新时排除自身)
  109 + * @param bizType 业务类型
  110 + * @param bizId 业务编号
  111 + */
  112 + private void validateBarcodeUnique(Long id, Integer bizType, Long bizId) {
  113 + MesWmBarcodeDO barcode = barcodeMapper.selectByBizTypeAndBizId(bizType, bizId);
  114 + if (barcode == null) {
  115 + return;
  116 + }
  117 + if (ObjUtil.notEqual(barcode, id)) {
  118 + throw exception(WM_BARCODE_ALREADY_EXISTS);
  119 + }
  120 + }
98 121  
99   - // 2.1 生成条码内容(如果前端未传递,则自动生成)
100   - String content = updateReqVO.getContent();
  122 + /**
  123 + * 生成条码内容(如果前端未传递则自动生成),并校验内容唯一性
  124 + *
  125 + * @param id 条码编号(新增时为 null,更新时排除自身)
  126 + * @param content 前端传递的条码内容(可为空)
  127 + * @param contentFormat 内容格式模板
  128 + * @param bizCode 业务编码
  129 + * @return 最终条码内容
  130 + */
  131 + private String generateAndValidateContent(Long id, String content, String contentFormat, String bizCode) {
  132 + // 1. 如果前端未传递内容,则自动生成
101 133 if (StrUtil.isBlank(content)) {
102   - MesWmBarcodeConfigDO config = barcodeConfigService.validateBarcodeConfigByBizType(updateReqVO.getBizType());
103   - content = generateBarcodeContent(config.getContentFormat(), updateReqVO.getBizCode());
  134 + content = generateBarcodeContent(contentFormat, bizCode);
104 135 }
105   - // 2.2 校验条码内容唯一性(排除自己)
  136 + // 2. 校验条码内容唯一性(排除自身)
106 137 if (StrUtil.isNotBlank(content)) {
107 138 MesWmBarcodeDO contentBarcode = barcodeMapper.selectByContent(content);
108   - if (contentBarcode != null && ObjUtil.notEqual(contentBarcode.getId(), updateReqVO.getId())) {
  139 + if (contentBarcode != null && ObjUtil.notEqual(contentBarcode.getId(), id)) {
109 140 throw exception(WM_BARCODE_CONTENT_DUPLICATE);
110 141 }
111 142 }
  143 + return content;
  144 + }
112 145  
113   - // 3. 更新
114   - MesWmBarcodeDO updateObj = BeanUtils.toBean(updateReqVO, MesWmBarcodeDO.class);
115   - if (StrUtil.isNotBlank(content)) {
116   - updateObj.setContent(content);
  146 + /**
  147 + * 生成条码内容
  148 + *
  149 + * @param contentFormat 内容格式模板
  150 + * @param bizCode 业务编码
  151 + * @return 条码内容
  152 + */
  153 + private String generateBarcodeContent(String contentFormat, String bizCode) {
  154 + if (StrUtil.isBlank(contentFormat)) {
  155 + return bizCode;
117 156 }
118   - barcodeMapper.updateById(updateObj);
  157 + return contentFormat.replace(MesWmBarcodeConfigDO.PLACEHOLDER_BUSINESS_CODE, bizCode);
119 158 }
120 159  
121 160 @Override
... ...