| | |
| | | import cn.iocoder.yudao.module.mdm.dal.dataobject.unit.MdmUnitMeasureDO; |
| | | import cn.iocoder.yudao.module.mdm.dal.mysql.item.MdmItemBatchConfigMapper; |
| | | import cn.iocoder.yudao.module.mdm.dal.mysql.item.MdmItemMapper; |
| | | import cn.iocoder.yudao.module.mdm.dal.mysql.item.MdmItemCodeSequenceMapper; |
| | | import cn.iocoder.yudao.module.mdm.dal.mysql.unit.MdmUnitMeasureMapper; |
| | | import cn.iocoder.yudao.module.mdm.service.brand.MdmBrandService; |
| | | import cn.iocoder.yudao.module.mdm.service.category.MdmItemCategoryService; |
| | |
| | | private MdmItemMapper itemMapper; |
| | | |
| | | @Resource |
| | | private MdmItemCodeSequenceMapper itemCodeSequenceMapper; |
| | | |
| | | @Resource |
| | | private MdmItemCategoryService itemCategoryService; |
| | | @Resource |
| | | private MdmUnitMeasureService unitMeasureService; |
| | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public Long createItem(MdmItemSaveReqVO createReqVO) { |
| | | createReqVO.setCode(generateItemCode(createReqVO.getItemType())); |
| | | // 校验数据 |
| | | validateItemSaveData(createReqVO); |
| | | |
| | |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public void updateItem(MdmItemSaveReqVO updateReqVO) { |
| | | // 校验存在 |
| | | validateItemExists(updateReqVO.getId()); |
| | | MdmItemDO existingItem = validateItemExists(updateReqVO.getId()); |
| | | // 编号由系统生成,修改产品时保持原编号不变。 |
| | | updateReqVO.setCode(existingItem.getCode()); |
| | | updateReqVO.setItemType(existingItem.getItemType()); |
| | | // 校验数据 |
| | | validateItemSaveData(updateReqVO); |
| | | |
| | |
| | | } |
| | | |
| | | private void validateItemSaveData(MdmItemSaveReqVO reqVO) { |
| | | if (reqVO.getItemType() == null || reqVO.getItemType() < 1 || reqVO.getItemType() > 5) { |
| | | throw exception(MDM_ITEM_TYPE_INVALID); |
| | | } |
| | | // 校验物料编码的唯一性 |
| | | validateItemCodeUnique(reqVO.getId(), reqVO.getCode()); |
| | | } |
| | | |
| | | /** 生成四位类型前缀 + 五位流水号,共九位。流水行在事务内加锁,避免并发重复。 */ |
| | | private String generateItemCode(Integer itemType) { |
| | | String prefix = switch (itemType == null ? 0 : itemType) { |
| | | case 1 -> "1001"; |
| | | case 2 -> "1002"; |
| | | case 3 -> "1003"; |
| | | case 4 -> "1004"; |
| | | case 5 -> "1005"; |
| | | default -> throw exception(MDM_ITEM_TYPE_INVALID); |
| | | }; |
| | | itemCodeSequenceMapper.insertIfAbsent(prefix); |
| | | Integer current = itemCodeSequenceMapper.selectCurrentValueForUpdate(prefix); |
| | | if (current == null) { |
| | | current = itemMapper.selectMaxSequenceByPrefix(prefix); |
| | | itemCodeSequenceMapper.insertIfAbsent(prefix); |
| | | itemCodeSequenceMapper.updateCurrentValue(prefix, current); |
| | | } |
| | | int maxExisting = itemMapper.selectMaxSequenceByPrefix(prefix); |
| | | if (current < maxExisting) { |
| | | current = maxExisting; |
| | | itemCodeSequenceMapper.updateCurrentValue(prefix, current); |
| | | } |
| | | if (current >= 99_999) { |
| | | throw exception(MDM_ITEM_CODE_SEQUENCE_EXHAUSTED); |
| | | } |
| | | itemCodeSequenceMapper.increment(prefix); |
| | | int next = current + 1; |
| | | return prefix + String.format("%05d", next); |
| | | } |
| | | |
| | | /** |
| | |
| | | saveReqVO.setName(importVO.getName()); |
| | | saveReqVO.setBarCode(importVO.getBarCode()); |
| | | saveReqVO.setSpecification(importVO.getSpecification()); |
| | | saveReqVO.setTechnicalParameters(importVO.getTechnicalParameters()); |
| | | saveReqVO.setCategoryId(categoryId); |
| | | saveReqVO.setUnitMeasureId(unitMeasureId); |
| | | saveReqVO.setUnitMeasureId2(unitMeasureId2); |
| | |
| | | StrUtil.blankToDefault(importVO.getName(), "(名称和编码为空的行)")); |
| | | } |
| | | |
| | | } |
| | | } |