| ¶Ô±ÈÐÂÎļþ |
| | |
| | | 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); |
| | | } |
| | | |
| | | } |