| | |
| | | |
| | | import cn.hutool.core.collection.CollUtil; |
| | | import cn.hutool.core.util.ObjUtil; |
| | | import cn.hutool.core.util.StrUtil; |
| | | 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; |
| | |
| | | /** |
| | | * MES 物料产品分类 Service 实现类 |
| | | * |
| | | * @author 芋道源码 |
| | | * 兼容层实现:数据存储在 mes_md_item_type 表,但优先从 MDM 分类 API 获取 |
| | | * 实际分类数据已迁移到 mdm_item_category 表 |
| | | * |
| | | * @author 超级管理员 |
| | | */ |
| | | @Service |
| | | @Validated |
| | | public class MesMdItemTypeServiceImpl implements MesMdItemTypeService { |
| | | |
| | | @Resource |
| | | private MdmItemCategoryApi mdmItemCategoryApi; |
| | | |
| | | @Resource |
| | | private MesMdItemTypeMapper itemTypeMapper; |
| | | |
| | | @Resource |
| | | @Lazy // 延迟加载,避免循环依赖 |
| | | @Lazy |
| | | private MesMdItemService itemService; |
| | | |
| | | @Override |
| | |
| | | // 校验数据 |
| | | validateItemTypeSaveData(createReqVO); |
| | | |
| | | // 插入 |
| | | // 插入到本地表(保持兼容) |
| | | MesMdItemTypeDO itemType = BeanUtils.toBean(createReqVO, MesMdItemTypeDO.class); |
| | | itemTypeMapper.insert(itemType); |
| | | return itemType.getId(); |
| | |
| | | // 校验数据 |
| | | validateItemTypeSaveData(updateReqVO); |
| | | |
| | | // 更新 |
| | | // 更新本地表 |
| | | MesMdItemTypeDO updateObj = BeanUtils.toBean(updateReqVO, MesMdItemTypeDO.class); |
| | | itemTypeMapper.updateById(updateObj); |
| | | } |
| | |
| | | } |
| | | |
| | | private void validateItemTypeExists(Long id) { |
| | | // 优先从 MDM 获取 |
| | | CommonResult<MdmItemCategoryRespDTO> result = mdmItemCategoryApi.getItemCategory(id); |
| | | if (result.isSuccess() && result.getData() != null) { |
| | | return; |
| | | } |
| | | // 回退到本地表 |
| | | if (itemTypeMapper.selectById(id) == null) { |
| | | throw exception(MD_ITEM_TYPE_NOT_EXISTS); |
| | | } |
| | |
| | | if (Objects.equals(id, parentId)) { |
| | | throw exception(MD_ITEM_TYPE_PARENT_ERROR); |
| | | } |
| | | // 2. 父分类不存在 |
| | | MesMdItemTypeDO parentItemType = itemTypeMapper.selectById(parentId); |
| | | // 2. 父分类不存在 - 优先从 MDM 获取 |
| | | CommonResult<MdmItemCategoryRespDTO> result = mdmItemCategoryApi.getItemCategory(parentId); |
| | | MesMdItemTypeDO parentItemType = null; |
| | | if (result.isSuccess() && result.getData() != null) { |
| | | parentItemType = convertToMesDO(result.getData()); |
| | | } else { |
| | | parentItemType = itemTypeMapper.selectById(parentId); |
| | | } |
| | | if (parentItemType == null) { |
| | | throw exception(MD_ITEM_TYPE_PARENT_NOT_EXITS); |
| | | } |
| | | // 3. 递归校验父分类,如果父分类是自己的子分类,则报错,避免形成环路 |
| | | if (id == null) { // id 为空,说明新增,不需要考虑环路 |
| | | if (id == null) { |
| | | return; |
| | | } |
| | | for (int i = 0; i < Short.MAX_VALUE; i++) { |
| | | // 3.1 校验环路 |
| | | parentId = parentItemType.getParentId(); |
| | | if (Objects.equals(id, parentId)) { |
| | | throw exception(MD_ITEM_TYPE_PARENT_IS_CHILD); |
| | | } |
| | | // 3.2 继续递归下一级父分类 |
| | | if (parentId == null || MesMdItemTypeDO.PARENT_ID_ROOT.equals(parentId)) { |
| | | break; |
| | | } |
| | | parentItemType = itemTypeMapper.selectById(parentId); |
| | | // 继续查找父级 |
| | | CommonResult<MdmItemCategoryRespDTO> parentResult = mdmItemCategoryApi.getItemCategory(parentId); |
| | | if (parentResult.isSuccess() && parentResult.getData() != null) { |
| | | parentItemType = convertToMesDO(parentResult.getData()); |
| | | } else { |
| | | parentItemType = itemTypeMapper.selectById(parentId); |
| | | } |
| | | if (parentItemType == null) { |
| | | break; |
| | | } |
| | |
| | | |
| | | @Override |
| | | public MesMdItemTypeDO getItemType(Long id) { |
| | | // 优先从 MDM 获取 |
| | | CommonResult<MdmItemCategoryRespDTO> result = mdmItemCategoryApi.getItemCategory(id); |
| | | if (result.isSuccess() && result.getData() != null) { |
| | | return convertToMesDO(result.getData()); |
| | | } |
| | | // 回退到本地表 |
| | | return itemTypeMapper.selectById(id); |
| | | } |
| | | |
| | | @Override |
| | | public MesMdItemTypeDO getItemTypeByCode(String code) { |
| | | // 从本地表查 |
| | | return itemTypeMapper.selectByCode(code); |
| | | } |
| | | |
| | | @Override |
| | | public List<MesMdItemTypeDO> getItemTypeList(MesMdItemTypeListReqVO listReqVO) { |
| | | // 从本地表查 |
| | | return itemTypeMapper.selectList(listReqVO); |
| | | } |
| | | |
| | |
| | | |
| | | @Override |
| | | public List<MesMdItemTypeDO> getItemTypeChildrenList(Long parentId) { |
| | | List<MesMdItemTypeDO> allList = itemTypeMapper.selectList(); // 分类总量小,全量查 |
| | | // 递归收集所有子分类 |
| | | List<MesMdItemTypeDO> allList = itemTypeMapper.selectList(); |
| | | List<MesMdItemTypeDO> result = new ArrayList<>(); |
| | | collectItemTypeChildren(result, parentId, allList); |
| | | return result; |
| | | } |
| | | |
| | | /** |
| | | * 递归收集所有子分类 |
| | | */ |
| | | private void collectItemTypeChildren(List<MesMdItemTypeDO> result, Long parentId, List<MesMdItemTypeDO> allList) { |
| | | for (MesMdItemTypeDO itemType : allList) { |
| | | if (Objects.equals(itemType.getParentId(), parentId)) { |
| | |
| | | } |
| | | } |
| | | |
| | | } |
| | | /** |
| | | * 转换 MDM 分类 DTO 为 MES DO |
| | | */ |
| | | private MesMdItemTypeDO convertToMesDO(MdmItemCategoryRespDTO dto) { |
| | | MesMdItemTypeDO mesDO = new MesMdItemTypeDO(); |
| | | mesDO.setId(dto.getId()); |
| | | mesDO.setCode(dto.getCode()); |
| | | mesDO.setName(dto.getName()); |
| | | mesDO.setParentId(dto.getParentId()); |
| | | mesDO.setItemOrProduct(dto.getItemOrProduct()); |
| | | mesDO.setSort(dto.getSort()); |
| | | mesDO.setStatus(dto.getStatus()); |
| | | return mesDO; |
| | | } |
| | | |
| | | @Override |
| | | @org.springframework.transaction.annotation.Transactional(rollbackFor = Exception.class) |
| | | public MesMdItemTypeImportRespVO importItemTypeList(List<MesMdItemTypeImportExcelVO> importList, Boolean isUpdateSupport) { |
| | | if (CollUtil.isEmpty(importList)) { |
| | | throw exception(MD_ITEM_TYPE_IMPORT_LIST_IS_EMPTY); |
| | | } |
| | | MesMdItemTypeImportRespVO respVO = MesMdItemTypeImportRespVO.builder() |
| | | .createList(new ArrayList<>()).updateList(new ArrayList<>()) |
| | | .failureList(new LinkedHashMap<>()).build(); |
| | | |
| | | // 1. 建立编码 -> ID 映射(现有分类 + 本次新建分类),用于解析"父分类编码"列 |
| | | Map<String, Long> codeIdMap = new HashMap<>(); |
| | | for (MesMdItemTypeDO itemType : itemTypeMapper.selectList()) { |
| | | codeIdMap.put(itemType.getCode(), itemType.getId()); |
| | | } |
| | | Map<String, Long> importedCodeIdMap = new HashMap<>(); |
| | | |
| | | // 2. 遍历导入 |
| | | for (MesMdItemTypeImportExcelVO importVO : importList) { |
| | | // 2.1 基础校验 |
| | | if (StrUtil.isBlank(importVO.getCode()) || StrUtil.isBlank(importVO.getName())) { |
| | | respVO.getFailureList().put(StrUtil.blankToDefault(importVO.getName(), importVO.getCode()), "分类编码和分类名称不能为空"); |
| | | continue; |
| | | } |
| | | // 2.2 解析父分类 ID |
| | | Long parentId = MesMdItemTypeDO.PARENT_ID_ROOT; |
| | | if (StrUtil.isNotBlank(importVO.getParentCode())) { |
| | | Long pid = importedCodeIdMap.get(importVO.getParentCode()) != null |
| | | ? importedCodeIdMap.get(importVO.getParentCode()) |
| | | : codeIdMap.get(importVO.getParentCode()); |
| | | if (pid == null) { |
| | | respVO.getFailureList().put(importVO.getName(), "父分类编码不存在:" + importVO.getParentCode()); |
| | | continue; |
| | | } |
| | | parentId = pid; |
| | | } |
| | | |
| | | // 2.3 已存在(按编码判断) |
| | | MesMdItemTypeDO existByCode = itemTypeMapper.selectByCode(importVO.getCode()); |
| | | if (existByCode != null) { |
| | | if (!Boolean.TRUE.equals(isUpdateSupport)) { |
| | | respVO.getFailureList().put(importVO.getName(), "分类编码已存在"); |
| | | continue; |
| | | } |
| | | MesMdItemTypeDO updateObj = BeanUtils.toBean(importVO, MesMdItemTypeDO.class); |
| | | updateObj.setId(existByCode.getId()); |
| | | updateObj.setParentId(parentId); |
| | | itemTypeMapper.updateById(updateObj); |
| | | respVO.getUpdateList().add(importVO.getName()); |
| | | } else { |
| | | // 2.4 校验同一父分类下名称唯一 |
| | | MesMdItemTypeDO existByName = itemTypeMapper.selectByParentIdAndName(parentId, importVO.getName()); |
| | | if (existByName != null) { |
| | | respVO.getFailureList().put(importVO.getName(), "同一父分类下已存在该名称的分类"); |
| | | continue; |
| | | } |
| | | // 2.5 校验同一父分类下编码唯一 |
| | | MesMdItemTypeDO existByParentCode = itemTypeMapper.selectByParentIdAndCode(parentId, importVO.getCode()); |
| | | if (existByParentCode != null) { |
| | | respVO.getFailureList().put(importVO.getName(), "同一父分类下已存在该编码的分类"); |
| | | continue; |
| | | } |
| | | // 2.6 新增 |
| | | MesMdItemTypeDO newObj = BeanUtils.toBean(importVO, MesMdItemTypeDO.class); |
| | | newObj.setParentId(parentId); |
| | | itemTypeMapper.insert(newObj); |
| | | respVO.getCreateList().add(importVO.getName()); |
| | | importedCodeIdMap.put(importVO.getCode(), newObj.getId()); |
| | | } |
| | | } |
| | | return respVO; |
| | | } |
| | | |
| | | } |