2026-07-09 d41fe2e95f3f64c6e3a7229acd9e74e673513a0a
yudao-module-erp/src/main/java/cn/iocoder/yudao/module/erp/service/product/ErpProductServiceImpl.java
@@ -4,19 +4,16 @@
import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.collection.MapUtils;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.erp.controller.admin.product.vo.product.ErpProductPageReqVO;
import cn.iocoder.yudao.module.erp.controller.admin.product.vo.product.ErpProductRespVO;
import cn.iocoder.yudao.module.erp.controller.admin.product.vo.product.ProductSaveReqVO;
import cn.iocoder.yudao.module.erp.dal.dataobject.product.ErpProductDO;
import cn.iocoder.yudao.module.erp.dal.mysql.product.ErpProductMapper;
import cn.iocoder.yudao.module.mdm.api.item.MdmItemApi;
import cn.iocoder.yudao.module.mdm.api.item.dto.MdmItemCreateReqDTO;
import cn.iocoder.yudao.module.mdm.api.item.dto.MdmItemRespDTO;
import cn.iocoder.yudao.module.mdm.api.item.dto.MdmItemUpdateReqDTO;
import cn.iocoder.yudao.module.mdm.api.category.MdmItemCategoryApi;
import cn.iocoder.yudao.module.mdm.api.category.dto.MdmItemCategoryRespDTO;
import cn.iocoder.yudao.module.mdm.api.unit.MdmUnitMeasureApi;
import cn.iocoder.yudao.module.mdm.api.unit.dto.MdmUnitMeasureRespDTO;
import cn.iocoder.yudao.module.mdm.enums.MdmItemTypeEnum;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
@@ -30,9 +27,9 @@
/**
 * ERP 产品 Service 实现类
 *
 * 改造说明:ERP 产品数据来源于 MDM 物料主数据
 * - 所有产品操作均调用 MDM API
 * - ERP 产品对应 MDM 物料类型 = 成品(itemType = 2)
 * 改造说明:产品数据统一使用 MDM 主数据管理(itemType=2 产品类型)
 * - 创建、更新、删除操作通过 MDM API 进行
 * - 查询操作从 MDM 获取数据
 *
 * @author 芋道源码
 */
@@ -40,46 +37,80 @@
@Validated
public class ErpProductServiceImpl implements ErpProductService {
    /** ERP 产品对应的 MDM 物料类型:成品 */
    private static final int MDM_ITEM_TYPE_PRODUCT = 2;
    @Resource
    private ErpProductMapper productMapper;
    @Resource
    private MdmItemApi mdmItemApi;
    @Resource
    private MdmItemCategoryApi mdmItemCategoryApi;
    @Resource
    private MdmUnitMeasureApi mdmUnitMeasureApi;
    @Override
    public Long createProduct(ProductSaveReqVO createReqVO) {
        // 1. 校验条码唯一
        validateProductBarCodeUnique(null, createReqVO.getBarCode());
        // 2. 构建创建请求并调用 MDM API
        MdmItemCreateReqDTO createReqDTO = buildCreateReqDTO(createReqVO);
        // 1. 校验条码是否重复
        MdmItemRespDTO existItem = mdmItemApi.getItemByBarCode(createReqVO.getBarCode()).getCheckedData();
        if (existItem != null) {
            throw exception(PRODUCT_BAR_CODE_EXISTS);
        }
        // 2. 通过 MDM API 创建产品
        MdmItemCreateReqDTO createReqDTO = new MdmItemCreateReqDTO();
        createReqDTO.setCode(createReqVO.getBarCode()); // 使用条码作为编码
        createReqDTO.setName(createReqVO.getName());
        createReqDTO.setBarCode(createReqVO.getBarCode());
        createReqDTO.setSpecification(createReqVO.getStandard());
        createReqDTO.setCategoryId(createReqVO.getCategoryId());
        createReqDTO.setUnitMeasureId(createReqVO.getUnitId());
        createReqDTO.setItemType(MdmItemTypeEnum.PRODUCT.getType()); // 产品类型
        createReqDTO.setStatus(createReqVO.getStatus() != null ? createReqVO.getStatus() : CommonStatusEnum.ENABLE.getStatus());
        createReqDTO.setPurchasePrice(createReqVO.getPurchasePrice());
        createReqDTO.setSalesPrice(createReqVO.getSalePrice());
        createReqDTO.setCostPrice(createReqVO.getMinPrice());
        createReqDTO.setExpiryDay(createReqVO.getExpiryDay());
        createReqDTO.setWeight(createReqVO.getWeight());
        createReqDTO.setRemark(createReqVO.getRemark());
        return mdmItemApi.createItem(createReqDTO).getCheckedData();
    }
    @Override
    public void updateProduct(ProductSaveReqVO updateReqVO) {
        // 1. 校验存在
        validateProductExists(updateReqVO.getId());
        // 2. 校验条码唯一
        validateProductBarCodeUnique(updateReqVO.getId(), updateReqVO.getBarCode());
        // 3. 构建更新请求并调用 MDM API
        MdmItemUpdateReqDTO updateReqDTO = buildUpdateReqDTO(updateReqVO);
        MdmItemRespDTO existItem = mdmItemApi.getItem(updateReqVO.getId()).getCheckedData();
        if (existItem == null) {
            throw exception(PRODUCT_NOT_EXISTS);
        }
        // 2. 校验条码是否重复
        if (!existItem.getBarCode().equals(updateReqVO.getBarCode())) {
            MdmItemRespDTO barCodeItem = mdmItemApi.getItemByBarCode(updateReqVO.getBarCode()).getCheckedData();
            if (barCodeItem != null) {
                throw exception(PRODUCT_BAR_CODE_EXISTS);
            }
        }
        // 3. 通过 MDM API 更新产品
        MdmItemUpdateReqDTO updateReqDTO = new MdmItemUpdateReqDTO();
        updateReqDTO.setId(updateReqVO.getId());
        updateReqDTO.setCode(updateReqVO.getBarCode());
        updateReqDTO.setName(updateReqVO.getName());
        updateReqDTO.setBarCode(updateReqVO.getBarCode());
        updateReqDTO.setSpecification(updateReqVO.getStandard());
        updateReqDTO.setCategoryId(updateReqVO.getCategoryId());
        updateReqDTO.setUnitMeasureId(updateReqVO.getUnitId());
        updateReqDTO.setItemType(MdmItemTypeEnum.PRODUCT.getType());
        updateReqDTO.setStatus(updateReqVO.getStatus());
        updateReqDTO.setPurchasePrice(updateReqVO.getPurchasePrice());
        updateReqDTO.setSalesPrice(updateReqVO.getSalePrice());
        updateReqDTO.setCostPrice(updateReqVO.getMinPrice());
        updateReqDTO.setExpiryDay(updateReqVO.getExpiryDay());
        updateReqDTO.setWeight(updateReqVO.getWeight());
        updateReqDTO.setRemark(updateReqVO.getRemark());
        mdmItemApi.updateItem(updateReqDTO);
    }
    @Override
    public void deleteProduct(Long id) {
        // 校验存在
        validateProductExists(id);
        // ERP 产品删除实际为禁用(MDM 物料不能物理删除)
        MdmItemRespDTO item = mdmItemApi.getItem(id).getCheckedData();
        if (item == null) {
            throw exception(PRODUCT_NOT_EXISTS);
        }
        // 通过 MDM API 删除(更新状态为禁用)
        mdmItemApi.updateItemStatus(id, CommonStatusEnum.DISABLE.getStatus());
    }
@@ -88,12 +119,10 @@
        if (CollUtil.isEmpty(ids)) {
            return Collections.emptyList();
        }
        // 1. 调用 MDM API 获取物料列表
        List<MdmItemRespDTO> itemList = mdmItemApi.getItemList(ids).getCheckedData();
        Map<Long, MdmItemRespDTO> itemMap = convertMap(itemList, MdmItemRespDTO::getId);
        // 2. 校验每个产品
        // 从 MDM 获取产品数据
        List<MdmItemRespDTO> items = mdmItemApi.getItemList(ids).getCheckedData();
        Map<Long, MdmItemRespDTO> itemMap = convertMap(items, MdmItemRespDTO::getId);
        List<ErpProductDO> result = new ArrayList<>();
        for (Long id : ids) {
            MdmItemRespDTO item = itemMap.get(id);
            if (item == null) {
@@ -102,22 +131,78 @@
            if (CommonStatusEnum.isDisable(item.getStatus())) {
                throw exception(PRODUCT_NOT_ENABLE, item.getName());
            }
            // 转换为 ErpProductDO
            result.add(convertToErpProduct(item));
        }
        return result;
    }
        // 3. 转换为 ERP Product DO(手动映射字段)
        return convertToErpProductDOList(itemList);
    private void validateProductExists(Long id) {
        // 从 MDM 校验
        MdmItemRespDTO item = mdmItemApi.getItem(id).getCheckedData();
        if (item == null) {
            throw exception(PRODUCT_NOT_EXISTS);
        }
    }
    @Override
    public ErpProductDO getProduct(Long id) {
        // 从 MDM 获取产品数据
        MdmItemRespDTO item = mdmItemApi.getItem(id).getCheckedData();
        return convertToErpProductDO(item);
        if (item == null) {
            return null;
        }
        // 检查是否为产品类型
        if (!MdmItemTypeEnum.PRODUCT.getType().equals(item.getItemType())) {
            return null;
        }
        return convertToErpProduct(item);
    }
    @Override
    public ErpProductRespVO getProductVO(Long id) {
        // 从 MDM 获取产品数据
        MdmItemRespDTO item = mdmItemApi.getItem(id).getCheckedData();
        if (item == null) {
            return null;
        }
        // 检查是否为产品类型
        if (!MdmItemTypeEnum.PRODUCT.getType().equals(item.getItemType())) {
            return null;
        }
        return convertToErpProductVO(item);
    }
    /**
     * 将 MDM 物料转换为 ERP 产品 VO
     */
    private ErpProductRespVO convertToErpProductVO(MdmItemRespDTO item) {
        ErpProductRespVO vo = new ErpProductRespVO();
        vo.setId(item.getId());
        vo.setName(item.getName());
        vo.setBarCode(item.getBarCode());
        vo.setStandard(item.getSpecification());
        vo.setCategoryId(item.getCategoryId());
        vo.setUnitId(item.getUnitMeasureId());
        vo.setStatus(item.getStatus());
        vo.setExpiryDay(item.getExpiryDay());
        vo.setWeight(item.getWeight());
        vo.setPurchasePrice(item.getPurchasePrice());
        vo.setSalePrice(item.getSalesPrice());
        vo.setMinPrice(item.getCostPrice());
        vo.setRemark(item.getRemark());
        return vo;
    }
    @Override
    public List<ErpProductRespVO> getProductVOListByStatus(Integer status) {
        List<MdmItemRespDTO> itemList = mdmItemApi.getItemListByStatus(status).getCheckedData();
        return buildProductVOList(itemList);
        // 从 MDM 获取产品数据(itemType=2)
        List<MdmItemRespDTO> items = mdmItemApi.getItemListByStatus(status).getCheckedData();
        // 过滤产品类型
        items = items.stream()
                .filter(item -> MdmItemTypeEnum.PRODUCT.getType().equals(item.getItemType()))
                .toList();
        return buildProductVOListFromMdm(items);
    }
    @Override
@@ -125,22 +210,68 @@
        if (CollUtil.isEmpty(ids)) {
            return Collections.emptyList();
        }
        List<MdmItemRespDTO> itemList = mdmItemApi.getItemList(ids).getCheckedData();
        return buildProductVOList(itemList);
        // 从 MDM 获取产品数据
        List<MdmItemRespDTO> items = mdmItemApi.getItemList(ids).getCheckedData();
        return buildProductVOListFromMdm(items);
    }
    @Override
    public PageResult<ErpProductRespVO> getProductVOPage(ErpProductPageReqVO pageReqVO) {
        // 调用 MDM API 分页查询
        PageResult<MdmItemRespDTO> mdmPageResult = mdmItemApi.getItemPage(
                MDM_ITEM_TYPE_PRODUCT,
                pageReqVO.getStatus(),
                pageReqVO.getPageNo(),
                pageReqVO.getPageSize()
        ).getCheckedData();
        // 从 MDM 获取产品分页数据
        cn.iocoder.yudao.framework.common.pojo.PageResult<MdmItemRespDTO> mdmPageResult =
                mdmItemApi.getItemPage(MdmItemTypeEnum.PRODUCT.getType(), null,
                        pageReqVO.getPageNo(), pageReqVO.getPageSize()).getCheckedData();
        return new PageResult<>(buildProductVOListFromMdm(mdmPageResult.getList()), mdmPageResult.getTotal());
    }
        List<ErpProductRespVO> productVOList = buildProductVOList(mdmPageResult.getList());
        return new PageResult<>(productVOList, mdmPageResult.getTotal());
    /**
     * 从 MDM 数据构建 ERP 产品 VO 列表
     */
    private List<ErpProductRespVO> buildProductVOListFromMdm(List<MdmItemRespDTO> items) {
        if (CollUtil.isEmpty(items)) {
            return Collections.emptyList();
        }
        List<ErpProductRespVO> result = new ArrayList<>();
        for (MdmItemRespDTO item : items) {
            ErpProductRespVO vo = new ErpProductRespVO();
            vo.setId(item.getId());
            vo.setName(item.getName());
            vo.setBarCode(item.getBarCode());
            vo.setStandard(item.getSpecification());
            vo.setCategoryId(item.getCategoryId());
            vo.setUnitId(item.getUnitMeasureId());
            vo.setStatus(item.getStatus());
            vo.setExpiryDay(item.getExpiryDay());
            vo.setWeight(item.getWeight());
            vo.setPurchasePrice(item.getPurchasePrice());
            vo.setSalePrice(item.getSalesPrice());
            vo.setMinPrice(item.getCostPrice()); // 使用成本价作为最低价
            vo.setRemark(item.getRemark());
            // 分类名称和单位名称需要额外查询(简化处理,后续可优化)
            result.add(vo);
        }
        return result;
    }
    /**
     * 将 MDM 物料转换为 ERP 产品 DO
     */
    private ErpProductDO convertToErpProduct(MdmItemRespDTO item) {
        ErpProductDO product = new ErpProductDO();
        product.setId(item.getId());
        product.setName(item.getName());
        product.setBarCode(item.getBarCode());
        product.setStandard(item.getSpecification());
        product.setCategoryId(item.getCategoryId());
        product.setUnitId(item.getUnitMeasureId());
        product.setStatus(item.getStatus());
        product.setExpiryDay(item.getExpiryDay());
        product.setWeight(item.getWeight());
        product.setPurchasePrice(item.getPurchasePrice());
        product.setSalePrice(item.getSalesPrice());
        product.setMinPrice(item.getCostPrice());
        product.setRemark(item.getRemark());
        return product;
    }
    @Override
@@ -153,160 +284,4 @@
        return mdmItemApi.getItemCountByUnitMeasureId(unitId).getCheckedData();
    }
    // ==================== 私有方法 ====================
    /**
     * 校验产品存在
     */
    private void validateProductExists(Long id) {
        mdmItemApi.validateItemExists(id).getCheckedData();
    }
    /**
     * 校验产品条码唯一
     */
    private void validateProductBarCodeUnique(Long id, String barCode) {
        if (barCode == null) {
            return;
        }
        MdmItemRespDTO existItem = mdmItemApi.getItemByBarCode(barCode).getCheckedData();
        if (existItem != null && !existItem.getId().equals(id)) {
            throw exception(PRODUCT_BAR_CODE_EXISTS);
        }
    }
    /**
     * 构建创建请求 DTO
     * 注意:ProductSaveReqVO 使用 standard 字段作为规格,MDM 使用 specification
     */
    private MdmItemCreateReqDTO buildCreateReqDTO(ProductSaveReqVO saveReqVO) {
        MdmItemCreateReqDTO createReqDTO = new MdmItemCreateReqDTO();
        createReqDTO.setCode(saveReqVO.getBarCode()); // 使用条码作为编码
        createReqDTO.setName(saveReqVO.getName());
        createReqDTO.setBarCode(saveReqVO.getBarCode());
        createReqDTO.setSpecification(saveReqVO.getStandard()); // standard -> specification
        createReqDTO.setCategoryId(saveReqVO.getCategoryId());
        createReqDTO.setUnitMeasureId(saveReqVO.getUnitId()); // unitId -> unitMeasureId
        createReqDTO.setItemType(MDM_ITEM_TYPE_PRODUCT);
        createReqDTO.setStatus(saveReqVO.getStatus() != null ? saveReqVO.getStatus() : CommonStatusEnum.ENABLE.getStatus());
        createReqDTO.setPurchasePrice(saveReqVO.getPurchasePrice());
        createReqDTO.setSalesPrice(saveReqVO.getSalePrice());
        createReqDTO.setCostPrice(saveReqVO.getMinPrice()); // minPrice -> costPrice
        createReqDTO.setExpiryDay(saveReqVO.getExpiryDay());
        createReqDTO.setWeight(saveReqVO.getWeight());
        createReqDTO.setRemark(saveReqVO.getRemark());
        return createReqDTO;
    }
    /**
     * 构建更新请求 DTO
     */
    private MdmItemUpdateReqDTO buildUpdateReqDTO(ProductSaveReqVO saveReqVO) {
        MdmItemUpdateReqDTO updateReqDTO = new MdmItemUpdateReqDTO();
        updateReqDTO.setId(saveReqVO.getId());
        updateReqDTO.setCode(saveReqVO.getBarCode());
        updateReqDTO.setName(saveReqVO.getName());
        updateReqDTO.setBarCode(saveReqVO.getBarCode());
        updateReqDTO.setSpecification(saveReqVO.getStandard()); // standard -> specification
        updateReqDTO.setCategoryId(saveReqVO.getCategoryId());
        updateReqDTO.setUnitMeasureId(saveReqVO.getUnitId()); // unitId -> unitMeasureId
        updateReqDTO.setStatus(saveReqVO.getStatus());
        updateReqDTO.setPurchasePrice(saveReqVO.getPurchasePrice());
        updateReqDTO.setSalesPrice(saveReqVO.getSalePrice());
        updateReqDTO.setCostPrice(saveReqVO.getMinPrice());
        updateReqDTO.setExpiryDay(saveReqVO.getExpiryDay());
        updateReqDTO.setWeight(saveReqVO.getWeight());
        updateReqDTO.setRemark(saveReqVO.getRemark());
        return updateReqDTO;
    }
    /**
     * 构建产品 VO 列表(包含分类和单位名称)
     * 注意:MDM 和 ERP 字段名有差异,需要手动映射
     */
    private List<ErpProductRespVO> buildProductVOList(List<MdmItemRespDTO> itemList) {
        if (CollUtil.isEmpty(itemList)) {
            return Collections.emptyList();
        }
        // 1. 获取分类和单位信息
        Set<Long> categoryIds = convertSet(itemList, MdmItemRespDTO::getCategoryId);
        Set<Long> unitIds = convertSet(itemList, MdmItemRespDTO::getUnitMeasureId);
        Map<Long, MdmItemCategoryRespDTO> categoryMap = CollUtil.isEmpty(categoryIds)
                ? Collections.emptyMap()
                : convertMap(mdmItemCategoryApi.getItemCategoryList(categoryIds).getCheckedData(), MdmItemCategoryRespDTO::getId);
        Map<Long, MdmUnitMeasureRespDTO> unitMap = CollUtil.isEmpty(unitIds)
                ? Collections.emptyMap()
                : convertMap(mdmUnitMeasureApi.getUnitMeasureList(unitIds).getCheckedData(), MdmUnitMeasureRespDTO::getId);
        // 2. 手动转换并填充关联信息(处理字段名差异)
        List<ErpProductRespVO> result = new ArrayList<>();
        for (MdmItemRespDTO mdmItem : itemList) {
            ErpProductRespVO product = new ErpProductRespVO();
            product.setId(mdmItem.getId());
            product.setName(mdmItem.getName());
            product.setBarCode(mdmItem.getBarCode());
            product.setStandard(mdmItem.getSpecification()); // specification -> standard
            product.setCategoryId(mdmItem.getCategoryId());
            product.setUnitId(mdmItem.getUnitMeasureId()); // unitMeasureId -> unitId
            product.setStatus(mdmItem.getStatus());
            product.setPurchasePrice(mdmItem.getPurchasePrice());
            product.setSalePrice(mdmItem.getSalesPrice()); // salesPrice -> salePrice
            product.setMinPrice(mdmItem.getCostPrice()); // costPrice -> minPrice
            product.setExpiryDay(mdmItem.getExpiryDay());
            product.setWeight(mdmItem.getWeight());
            product.setRemark(mdmItem.getRemark());
            // 设置分类和单位名称
            MapUtils.findAndThen(categoryMap, mdmItem.getCategoryId(),
                    category -> product.setCategoryName(category.getName()));
            MapUtils.findAndThen(unitMap, mdmItem.getUnitMeasureId(),
                    unit -> product.setUnitName(unit.getName()));
            result.add(product);
        }
        return result;
    }
    /**
     * 转换 MDM 物料为 ERP 产品 DO
     * 注意:字段名差异需要手动映射
     */
    private ErpProductDO convertToErpProductDO(MdmItemRespDTO mdmItem) {
        if (mdmItem == null) {
            return null;
        }
        ErpProductDO product = new ErpProductDO();
        product.setId(mdmItem.getId());
        product.setName(mdmItem.getName());
        product.setBarCode(mdmItem.getBarCode());
        product.setStandard(mdmItem.getSpecification()); // specification -> standard
        product.setCategoryId(mdmItem.getCategoryId());
        product.setUnitId(mdmItem.getUnitMeasureId()); // unitMeasureId -> unitId
        product.setStatus(mdmItem.getStatus());
        product.setPurchasePrice(mdmItem.getPurchasePrice());
        product.setSalePrice(mdmItem.getSalesPrice()); // salesPrice -> salePrice
        product.setMinPrice(mdmItem.getCostPrice()); // costPrice -> minPrice
        product.setExpiryDay(mdmItem.getExpiryDay());
        product.setWeight(mdmItem.getWeight());
        product.setRemark(mdmItem.getRemark());
        return product;
    }
    /**
     * 批量转换 MDM 物料列表为 ERP 产品列表
     */
    private List<ErpProductDO> convertToErpProductDOList(List<MdmItemRespDTO> mdmItemList) {
        if (CollUtil.isEmpty(mdmItemList)) {
            return Collections.emptyList();
        }
        List<ErpProductDO> result = new ArrayList<>();
        for (MdmItemRespDTO mdmItem : mdmItemList) {
            result.add(convertToErpProductDO(mdmItem));
        }
        return result;
    }
}
}