From 6b5f7c66fc40d7f6099d561e31a34fbd50dd20d3 Mon Sep 17 00:00:00 2001
From: 云 <2163098428@qq.com>
Date: 星期三, 01 七月 2026 15:54:57 +0800
Subject: [PATCH] 初始化项目
---
yudao-module-mdm/src/main/java/cn/iocoder/yudao/module/mdm/service/category/MdmItemCategoryServiceImpl.java | 158 ++++++++++++++++++++++++++++++++++++++++++++++++++++
1 files changed, 158 insertions(+), 0 deletions(-)
diff --git a/yudao-module-mdm/src/main/java/cn/iocoder/yudao/module/mdm/service/category/MdmItemCategoryServiceImpl.java b/yudao-module-mdm/src/main/java/cn/iocoder/yudao/module/mdm/service/category/MdmItemCategoryServiceImpl.java
new file mode 100644
index 0000000..ecb240b
--- /dev/null
+++ b/yudao-module-mdm/src/main/java/cn/iocoder/yudao/module/mdm/service/category/MdmItemCategoryServiceImpl.java
@@ -0,0 +1,158 @@
+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));
+ }
+
+}
--
Gitblit v1.9.3