昨天 eccad5a129106377a275be4f7cdc58e99e9b95d4
yudao-module-mdm/src/main/java/cn/iocoder/yudao/module/mdm/service/category/MdmItemCategoryServiceImpl.java
¶Ô±ÈÐÂÎļþ
@@ -0,0 +1,163 @@
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<MdmItemCategoryDO> getItemCategoryPage(MdmItemCategoryPageReqVO pageReqVO) {
        return itemCategoryMapper.selectPage(pageReqVO);
    }
    @Override
    public List<MdmItemCategoryDO> getItemCategoryList(Collection<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return Collections.emptyList();
        }
        return itemCategoryMapper.selectBatchIds(ids);
    }
    @Override
    public List<MdmItemCategoryDO> getItemCategoryChildrenList(Long id) {
        List<MdmItemCategoryDO> 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<MdmItemCategoryDO> result) {
        List<MdmItemCategoryDO> children = itemCategoryMapper.selectListByParentId(parentId);
        if (CollUtil.isNotEmpty(children)) {
            result.addAll(children);
            for (MdmItemCategoryDO child : children) {
                loadChildren(child.getId(), result);
            }
        }
    }
    @Override
    public List<MdmItemCategoryDO> getSimpleItemList() {
        return itemCategoryMapper.selectList(new MdmItemCategoryDO().setStatus(0));
    }
    @Override
    public MdmItemCategoryDO getItemCategoryByItemOrProduct(String itemOrProduct) {
        return itemCategoryMapper.selectByItemOrProduct(itemOrProduct);
    }
}