| | |
| | | |
| | | import cn.hutool.core.collection.CollUtil; |
| | | import cn.hutool.core.util.ObjUtil; |
| | | import cn.iocoder.yudao.framework.common.pojo.CommonResult; |
| | | import cn.iocoder.yudao.framework.common.pojo.PageResult; |
| | | import cn.iocoder.yudao.framework.common.util.object.BeanUtils; |
| | | import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX; |
| | | import cn.iocoder.yudao.module.mdm.controller.admin.item.vo.MdmItemPageReqVO; |
| | | import cn.iocoder.yudao.module.mdm.controller.admin.item.vo.MdmItemSaveReqVO; |
| | | import cn.iocoder.yudao.module.mdm.dal.dataobject.category.MdmItemCategoryDO; |
| | | import cn.iocoder.yudao.module.mdm.dal.dataobject.item.MdmItemBatchConfigDO; |
| | | import cn.iocoder.yudao.module.mdm.dal.dataobject.item.MdmItemDO; |
| | | 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.service.brand.MdmBrandService; |
| | | import cn.iocoder.yudao.module.mdm.service.category.MdmItemCategoryService; |
| | | import cn.iocoder.yudao.module.mdm.service.unit.MdmUnitMeasureService; |
| | | import cn.iocoder.yudao.module.mes.api.item.MesMdItemApi; |
| | | import cn.iocoder.yudao.module.mes.api.item.MesMdItemTypeApi; |
| | | import cn.iocoder.yudao.module.mes.api.item.dto.MesMdItemSyncReqDTO; |
| | | import cn.iocoder.yudao.module.mes.api.item.dto.MesMdItemTypeRespDTO; |
| | | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
| | | import jakarta.annotation.Resource; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.context.annotation.Lazy; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | |
| | | import java.util.stream.Collectors; |
| | | |
| | | import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; |
| | | import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertSet; |
| | | import static cn.iocoder.yudao.module.mdm.enums.ErrorCodeConstants.*; |
| | | |
| | | /** |
| | | * MDM 物料主数据 Service 实现类 |
| | | * |
| | | * @author 芋道源码 |
| | | * @author 超级管理员 |
| | | */ |
| | | @Service |
| | | @Validated |
| | |
| | | @Resource |
| | | private MdmItemMapper itemMapper; |
| | | |
| | | @Resource |
| | | private MdmItemCategoryService itemCategoryService; |
| | | @Resource |
| | | private MdmUnitMeasureService unitMeasureService; |
| | | @Resource |
| | |
| | | private MesMdItemApi mesMdItemApi; |
| | | @Resource |
| | | private MdmItemBatchConfigMapper itemBatchConfigMapper; |
| | | |
| | | @Resource |
| | | private MesMdItemTypeApi mesMdItemTypeApi; |
| | | |
| | | |
| | | @Override |
| | |
| | | // 保存批次配置(如果有) |
| | | saveBatchConfig(item.getId(), createReqVO); |
| | | |
| | | // 同步到 MES(勾选 syncMes 时) |
| | | createReqVO.setId(item.getId()); |
| | | // 勾选同步时,同步到 MES |
| | | if (Boolean.TRUE.equals(createReqVO.getSyncMes())) { |
| | | createReqVO.setId(item.getId()); |
| | | syncItemToMes(createReqVO); |
| | | } |
| | | return item.getId(); |
| | |
| | | // 保存批次配置(如果有) |
| | | saveBatchConfig(updateReqVO.getId(), updateReqVO); |
| | | |
| | | // 同步到 MES(勾选 syncMes 时) |
| | | // 根据同步开关决定:打开则同步到 MES,关闭则取消同步(从 MES 删除) |
| | | if (Boolean.TRUE.equals(updateReqVO.getSyncMes())) { |
| | | syncItemToMes(updateReqVO); |
| | | } else { |
| | | mesMdItemApi.deleteItem(updateReqVO.getId()); |
| | | } |
| | | } |
| | | |
| | |
| | | } |
| | | |
| | | /** |
| | | * 同步物料到 MES(单位/分类按 code 映射) |
| | | * 同步物料到 MES(单位按 code 映射) |
| | | * |
| | | * MDM 的 unitMeasureId/categoryId 与 MES 的 unitMeasureId/itemTypeId 分属两套主数据, |
| | | * 这里先在 MDM 侧解析出 code,再交由 MesMdItemApi 在 MES 侧解析为对应 id。 |
| | | * 品种管理已去掉「品种分类」字段,同步不再传递 MES 物料分类(item_type_id 留空,可在 MES 侧补填)。 |
| | | * 同步失败将抛出异常,触发外层 @Transactional 整体回滚(MDM 操作一并回滚)。 |
| | | */ |
| | | private void syncItemToMes(MdmItemSaveReqVO reqVO) { |
| | |
| | | } |
| | | unitCode = unitMeasure.getCode(); |
| | | } |
| | | // 解析分类 id -> code(使用 MDM 本地分类服务) |
| | | String itemTypeCode = null; |
| | | if (reqVO.getCategoryId() != null) { |
| | | CommonResult<MesMdItemTypeRespDTO> itemType = mesMdItemTypeApi.getItemType(reqVO.getCategoryId()); |
| | | itemTypeCode = itemType.getData().getCode(); |
| | | } |
| | | // 组装同步 DTO |
| | | MesMdItemSyncReqDTO syncReqDTO = new MesMdItemSyncReqDTO(); |
| | | syncReqDTO.setCode(reqVO.getCode()); |
| | |
| | | syncReqDTO.setName(reqVO.getName()); |
| | | syncReqDTO.setSpecification(reqVO.getSpecification()); |
| | | syncReqDTO.setUnitCode(unitCode); |
| | | syncReqDTO.setItemTypeCode(itemTypeCode); |
| | | syncReqDTO.setSafeStockFlag(Boolean.TRUE); // 保留 MDM 的 minStock/maxStock,避免被 MES 清零 |
| | | syncReqDTO.setMinStock(reqVO.getMinStock()); |
| | | syncReqDTO.setMaxStock(reqVO.getMaxStock()); |
| | | syncReqDTO.setHighValue(reqVO.getIsHighValue()); |
| | | syncReqDTO.setBatchFlag(reqVO.getIsBatchManaged()); |
| | | syncReqDTO.setStatus(reqVO.getStatus()); |
| | | syncReqDTO.setRemark(reqVO.getRemark()); |
| | | // 批次配置字段 |
| | | syncReqDTO.setProduceDateFlag(reqVO.getProduceDateFlag()); |
| | |
| | | } |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public void updateItemStatus(Long id, Integer status) { |
| | | // 校验存在 |
| | | validateItemExists(id); |
| | | // 更新状态 |
| | | itemMapper.updateById(new MdmItemDO().setId(id).setStatus(status)); |
| | | // 同步状态到 MES |
| | | mesMdItemApi.updateItemStatus(id, status); |
| | | } |
| | | |
| | | @Override |
| | |
| | | public void deleteItem(Long id) { |
| | | // 校验存在 |
| | | MdmItemDO item = validateItemExists(id); |
| | | // 解决软删除唯一键冲突:如果已存在相同 code 且已软删除的记录,先物理删除它 |
| | | // MdmItemDO existingDeleted = itemMapper.selectByCodeAndDeleted(item.getCode(), true); |
| | | // if (existingDeleted != null && !existingDeleted.getId().equals(id)) { |
| | | // itemMapper.physicalDeleteById(existingDeleted.getId()); |
| | | // } |
| | | // 校验物料是否被业务引用 |
| | | validateItemNoReference(id); |
| | | // 软删除当前记录 |
| | | itemMapper.deleteById(id); |
| | | // 同步删除到 MES |
| | | mesMdItemApi.deleteItem(id); |
| | | } |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public void updateItemSync(Long id, Boolean sync) { |
| | | // 校验存在 |
| | | MdmItemDO item = validateItemExists(id); |
| | | if (Boolean.TRUE.equals(sync)) { |
| | | // 同步到 MES:从 DB 读取完整物料数据组装后走 syncItemToMes |
| | | MdmItemSaveReqVO reqVO = BeanUtils.toBean(item, MdmItemSaveReqVO.class); |
| | | // DO 字段名为 highValue,与 VO 的 isHighValue 不自动匹配,手动补齐 |
| | | reqVO.setIsHighValue(item.getHighValue()); |
| | | // 补齐批次属性配置 |
| | | MdmItemBatchConfigDO config = itemBatchConfigMapper.selectOne( |
| | | MdmItemBatchConfigDO::getItemId, id); |
| | | if (config != null) { |
| | | reqVO.setProduceDateFlag(config.getProduceDateFlag()); |
| | | reqVO.setExpireDateFlag(config.getExpireDateFlag()); |
| | | reqVO.setReceiptDateFlag(config.getReceiptDateFlag()); |
| | | reqVO.setVendorFlag(config.getVendorFlag()); |
| | | reqVO.setPurchaseOrderCodeFlag(config.getPurchaseOrderCodeFlag()); |
| | | reqVO.setLotNumberFlag(config.getLotNumberFlag()); |
| | | reqVO.setQualityStatusFlag(config.getQualityStatusFlag()); |
| | | } |
| | | syncItemToMes(reqVO); |
| | | } else { |
| | | // 取消同步:从 MES 删除该物料(MES 无此物料时内部为空操作) |
| | | mesMdItemApi.deleteItem(id); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 校验物料未被业务引用(销售、采购、生产、质检、库存等) |
| | | * |
| | | * @param id 物料编号 |
| | | */ |
| | | private void validateItemNoReference(Long id) { |
| | | List<Map<String, Object>> references = itemMapper.selectItemReferenceCount(id); |
| | | if (CollUtil.isNotEmpty(references)) { |
| | | String refDesc = references.stream() |
| | | .map(r -> r.get("source") + "(" + r.get("cnt") + ")") |
| | | .collect(Collectors.joining("、")); |
| | | throw exception(MDM_ITEM_EXISTS_REFERENCE, refDesc); |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public void deleteItemList(Collection<Long> ids) { |
| | | if (CollUtil.isEmpty(ids)) { |
| | | return; |
| | | } |
| | | // 逐个删除(复用单条删除逻辑,保持校验) |
| | | ids.forEach(this::deleteItem); |
| | | } |
| | | |
| | | @Override |
| | |
| | | } |
| | | if (ObjUtil.notEqual(item.getId(), id)) { |
| | | throw exception(MDM_ITEM_CODE_DUPLICATE); |
| | | } |
| | | } |
| | | |
| | | private void validateCategoryExists(Long categoryId) { |
| | | if (categoryId == null) { |
| | | return; |
| | | } |
| | | // 使用 MDM 本地分类服务校验 |
| | | MdmItemCategoryDO category = itemCategoryService.getItemCategory(categoryId); |
| | | if (category == null) { |
| | | throw exception(MDM_ITEM_CATEGORY_NOT_EXISTS); |
| | | } |
| | | } |
| | | |
| | |
| | | |
| | | @Override |
| | | public PageResult<MdmItemDO> getItemPage(MdmItemPageReqVO pageReqVO) { |
| | | // 查找时,如果查找某个分类编号,则包含它的子分类 |
| | | Set<Long> categoryIds = null; |
| | | if (pageReqVO.getCategoryId() != null) { |
| | | categoryIds = new HashSet<>(); |
| | | categoryIds.add(pageReqVO.getCategoryId()); |
| | | List<cn.iocoder.yudao.module.mdm.dal.dataobject.category.MdmItemCategoryDO> children = |
| | | itemCategoryService.getItemCategoryChildrenList(pageReqVO.getCategoryId()); |
| | | categoryIds.addAll(convertSet(children, cn.iocoder.yudao.module.mdm.dal.dataobject.category.MdmItemCategoryDO::getId)); |
| | | } |
| | | // 分页查询 |
| | | return itemMapper.selectPage(pageReqVO, categoryIds); |
| | | return itemMapper.selectPage(pageReqVO, (Collection<Long>) null); |
| | | } |
| | | |
| | | @Override |
| | |
| | | return itemMapper.selectByCode(code); |
| | | } |
| | | |
| | | @Override |
| | | public List<MdmItemDO> getItemSimpleList() { |
| | | return itemMapper.selectList(MdmItemDO::getStatus, |
| | | cn.iocoder.yudao.framework.common.enums.CommonStatusEnum.ENABLE.getStatus()); |
| | | } |
| | | |
| | | /** |
| | | * 保存批次配置(到 MDM 本地表) |
| | | */ |