云
2026-10-25 d54fbc9d4f8981fa0f7f613b33a28a8cd4f764ea
yudao-module-mes/src/main/java/cn/iocoder/yudao/module/mes/service/md/item/MesMdItemTypeServiceImpl.java
@@ -2,10 +2,14 @@
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.mdm.api.category.MdmItemCategoryApi;
import cn.iocoder.yudao.module.mdm.api.category.dto.MdmItemCategoryRespDTO;
import cn.iocoder.yudao.module.mes.controller.admin.md.item.vo.type.MesMdItemTypeImportExcelVO;
import cn.iocoder.yudao.module.mes.controller.admin.md.item.vo.type.MesMdItemTypeImportRespVO;
import cn.iocoder.yudao.module.mes.controller.admin.md.item.vo.type.MesMdItemTypeListReqVO;
import cn.iocoder.yudao.module.mes.controller.admin.md.item.vo.type.MesMdItemTypeSaveReqVO;
import cn.iocoder.yudao.module.mes.dal.dataobject.md.item.MesMdItemTypeDO;
@@ -13,9 +17,11 @@
import jakarta.annotation.Resource;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.mes.enums.ErrorCodeConstants.*;
@@ -26,7 +32,7 @@
 * 兼容层实现:数据存储在 mes_md_item_type 表,但优先从 MDM 分类 API 获取
 * 实际分类数据已迁移到 mdm_item_category 表
 *
 * @author 芋道源码
 * @author 超级管理员
 */
@Service
@Validated
@@ -230,4 +236,67 @@
        return mesDO;
    }
    @Override
    @Transactional(rollbackFor = Exception.class)
    public MesMdItemTypeImportRespVO importItemTypeList(List<MesMdItemTypeImportExcelVO> importItemTypeList,
                                                        boolean updateSupport) {
        // 1. 校验非空
        if (CollUtil.isEmpty(importItemTypeList)) {
            throw exception(MD_ITEM_TYPE_IMPORT_LIST_IS_EMPTY);
        }
        // 2. 逐行处理
        MesMdItemTypeImportRespVO respVO = MesMdItemTypeImportRespVO.builder()
                .createCodes(new ArrayList<>()).updateCodes(new ArrayList<>())
                .failureCodes(new LinkedHashMap<>()).build();
        AtomicInteger index = new AtomicInteger(1);
        importItemTypeList.forEach(importItem -> {
            int currentIndex = index.getAndIncrement();
            String key = StrUtil.blankToDefault(importItem.getCode(), "第 " + currentIndex + " 行");
            if (StrUtil.isBlank(importItem.getCode())) {
                respVO.getFailureCodes().put(key, "分类编码不能为空");
                return;
            }
            if (StrUtil.isBlank(importItem.getName())) {
                respVO.getFailureCodes().put(key, "分类名称不能为空");
                return;
            }
            if (StrUtil.isBlank(importItem.getItemOrProduct())) {
                respVO.getFailureCodes().put(key, "物料/产品标识不能为空");
                return;
            }
            // 2.1 解析父分类(为空则默认为根分类)
            Long parentId = MesMdItemTypeDO.PARENT_ID_ROOT;
            if (StrUtil.isNotBlank(importItem.getParentCode())) {
                MesMdItemTypeDO parentItemType = itemTypeMapper.selectByCode(importItem.getParentCode());
                if (parentItemType == null) {
                    respVO.getFailureCodes().put(key, "父分类编码[" + importItem.getParentCode() + "]不存在");
                    return;
                }
                parentId = parentItemType.getId();
            }
            // 2.2 判断是否存在(同一父分类下编码唯一)
            MesMdItemTypeDO existItemType = itemTypeMapper.selectByParentIdAndCode(parentId, importItem.getCode());
            if (existItemType == null) {
                // 2.2.1 新增
                MesMdItemTypeDO itemType = BeanUtils.toBean(importItem, MesMdItemTypeDO.class);
                itemType.setParentId(parentId);
                itemType.setSort(ObjUtil.defaultIfNull(importItem.getSort(), 0));
                itemType.setStatus(ObjUtil.defaultIfNull(importItem.getStatus(), CommonStatusEnum.ENABLE.getStatus()));
                itemTypeMapper.insert(itemType);
                respVO.getCreateCodes().add(importItem.getCode());
            } else if (updateSupport) {
                // 2.2.2 更新
                MesMdItemTypeDO updateObj = BeanUtils.toBean(importItem, MesMdItemTypeDO.class);
                updateObj.setId(existItemType.getId());
                updateObj.setParentId(parentId);
                itemTypeMapper.updateById(updateObj);
                respVO.getUpdateCodes().add(importItem.getCode());
            } else {
                respVO.getFailureCodes().put(key, "分类编码已存在");
            }
        });
        return respVO;
    }
}