package cn.iocoder.yudao.module.mdm.service.category; import cn.hutool.core.collection.CollUtil; import cn.hutool.core.util.ObjUtil; import cn.iocoder.yudao.framework.common.pojo.PageResult; import cn.iocoder.yudao.framework.common.util.object.BeanUtils; import cn.iocoder.yudao.module.mdm.controller.admin.category.vo.MdmItemCategoryPageReqVO; import cn.iocoder.yudao.module.mdm.controller.admin.category.vo.MdmItemCategorySaveReqVO; import cn.iocoder.yudao.module.mdm.dal.dataobject.category.MdmItemCategoryDO; import cn.iocoder.yudao.module.mdm.dal.mysql.category.MdmItemCategoryMapper; import cn.iocoder.yudao.module.mdm.dal.mysql.item.MdmItemMapper; import jakarta.annotation.Resource; import org.springframework.stereotype.Service; import org.springframework.validation.annotation.Validated; import java.util.*; import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; import static cn.iocoder.yudao.module.mdm.enums.ErrorCodeConstants.*; /** * MDM 物料分类 Service 实现类 * * @author 芋道源码 */ @Service @Validated public class MdmItemCategoryServiceImpl implements MdmItemCategoryService { @Resource private MdmItemCategoryMapper itemCategoryMapper; @Resource private MdmItemMapper itemMapper; @Override public Long createItemCategory(MdmItemCategorySaveReqVO createReqVO) { // 校验数据 validateItemCategorySaveData(createReqVO); // 插入 MdmItemCategoryDO itemCategory = BeanUtils.toBean(createReqVO, MdmItemCategoryDO.class); itemCategoryMapper.insert(itemCategory); return itemCategory.getId(); } @Override public void updateItemCategory(MdmItemCategorySaveReqVO updateReqVO) { // 校验存在 validateItemCategoryExists(updateReqVO.getId()); // 校验数据 validateItemCategorySaveData(updateReqVO); // 更新 MdmItemCategoryDO updateObj = BeanUtils.toBean(updateReqVO, MdmItemCategoryDO.class); itemCategoryMapper.updateById(updateObj); } @Override public void updateItemCategoryStatus(Long id, Integer status) { // 校验存在 validateItemCategoryExists(id); // 更新状态 itemCategoryMapper.updateById(new MdmItemCategoryDO().setId(id).setStatus(status)); } @Override public void deleteItemCategory(Long id) { // 校验存在 validateItemCategoryExists(id); // 校验是否有子分类 if (itemCategoryMapper.selectCountByParentId(id) > 0) { throw exception(MDM_ITEM_CATEGORY_EXISTS_CHILDREN); } // 校验分类下是否有物料(直接使用 Mapper 避免循环依赖) if (itemMapper.selectCountByCategoryId(id) > 0) { throw exception(MDM_ITEM_CATEGORY_EXISTS_ITEM); } // 删除 itemCategoryMapper.deleteById(id); } @Override public MdmItemCategoryDO validateItemCategoryExists(Long id) { MdmItemCategoryDO itemCategory = itemCategoryMapper.selectById(id); if (itemCategory == null) { throw exception(MDM_ITEM_CATEGORY_NOT_EXISTS); } return itemCategory; } private void validateItemCategorySaveData(MdmItemCategorySaveReqVO reqVO) { // 校验编码唯一 MdmItemCategoryDO category = itemCategoryMapper.selectByCode(reqVO.getCode()); if (category != null && ObjUtil.notEqual(category.getId(), reqVO.getId())) { throw exception(MDM_ITEM_CATEGORY_CODE_DUPLICATE); } // 校验同一父级下名称唯一 MdmItemCategoryDO sameName = itemCategoryMapper.selectByParentIdAndName( reqVO.getParentId() != null ? reqVO.getParentId() : 0L, reqVO.getName()); if (sameName != null && ObjUtil.notEqual(sameName.getId(), reqVO.getId())) { throw exception(MDM_ITEM_CATEGORY_NAME_DUPLICATE); } // 校验父分类存在 if (reqVO.getParentId() != null && reqVO.getParentId() > 0) { if (itemCategoryMapper.selectById(reqVO.getParentId()) == null) { throw exception(MDM_ITEM_CATEGORY_PARENT_NOT_EXISTS); } } } @Override public MdmItemCategoryDO getItemCategory(Long id) { return itemCategoryMapper.selectById(id); } @Override public PageResult getItemCategoryPage(MdmItemCategoryPageReqVO pageReqVO) { return itemCategoryMapper.selectPage(pageReqVO); } @Override public List getItemCategoryList(Collection ids) { if (CollUtil.isEmpty(ids)) { return Collections.emptyList(); } return itemCategoryMapper.selectBatchIds(ids); } @Override public List getItemCategoryChildrenList(Long id) { List result = new ArrayList<>(); // 添加自己 MdmItemCategoryDO self = itemCategoryMapper.selectById(id); if (self != null) { result.add(self); } // 递归获取所有子分类 loadChildren(id, result); return result; } private void loadChildren(Long parentId, List result) { List children = itemCategoryMapper.selectListByParentId(parentId); if (CollUtil.isNotEmpty(children)) { result.addAll(children); for (MdmItemCategoryDO child : children) { loadChildren(child.getId(), result); } } } }