|
@@ -0,0 +1,180 @@
|
|
|
+package cn.iocoder.yudao.module.pms.service;
|
|
|
+
|
|
|
+import cn.hutool.core.collection.CollUtil;
|
|
|
+import cn.hutool.core.util.ObjectUtil;
|
|
|
+import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
|
|
|
+import cn.iocoder.yudao.module.pms.controller.admin.vo.IotProductClassifyListReqVO;
|
|
|
+import cn.iocoder.yudao.module.pms.controller.admin.vo.IotProductClassifySaveReqVO;
|
|
|
+import cn.iocoder.yudao.module.pms.dal.dataobject.IotProductClassifyDO;
|
|
|
+import cn.iocoder.yudao.module.pms.dal.mysql.IotProductClassifyMapper;
|
|
|
+import com.google.common.annotations.VisibleForTesting;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.validation.annotation.Validated;
|
|
|
+
|
|
|
+import javax.annotation.Resource;
|
|
|
+import java.util.*;
|
|
|
+
|
|
|
+import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
|
|
|
+import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertSet;
|
|
|
+import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.*;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 产品分类 Service 实现类
|
|
|
+ *
|
|
|
+ * @author 芋道源码
|
|
|
+ */
|
|
|
+@Service
|
|
|
+@Validated
|
|
|
+@Slf4j
|
|
|
+public class IotProductClassifyServiceImpl implements IotProductClassifyService {
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private IotProductClassifyMapper iotProductClassifyMapper;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Long createIotProductClassify(IotProductClassifySaveReqVO createReqVO) {
|
|
|
+ if (createReqVO.getParentId() == null) {
|
|
|
+ createReqVO.setParentId(IotProductClassifyDO.PARENT_ID_ROOT);
|
|
|
+ }
|
|
|
+ // 校验父产品分类的有效性
|
|
|
+ validateParentProductClassify(null, createReqVO.getParentId());
|
|
|
+ // 校验产品分类名的唯一性
|
|
|
+ validateProductClassifyNameUnique(null, createReqVO.getParentId(), createReqVO.getName());
|
|
|
+
|
|
|
+ // 插入产品分类
|
|
|
+ IotProductClassifyDO dept = BeanUtils.toBean(createReqVO, IotProductClassifyDO.class);
|
|
|
+ iotProductClassifyMapper.insert(dept);
|
|
|
+ return dept.getId();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void updateIotProductClassify(IotProductClassifySaveReqVO updateReqVO) {
|
|
|
+ if (updateReqVO.getParentId() == null) {
|
|
|
+ updateReqVO.setParentId(IotProductClassifyDO.PARENT_ID_ROOT);
|
|
|
+ }
|
|
|
+ // 校验自己存在
|
|
|
+ validateIotProductClassifyExists(updateReqVO.getId());
|
|
|
+ // 校验父产品分类的有效性
|
|
|
+ validateParentProductClassify(updateReqVO.getId(), updateReqVO.getParentId());
|
|
|
+ // 校验产品分类名的唯一性
|
|
|
+ validateProductClassifyNameUnique(updateReqVO.getId(), updateReqVO.getParentId(), updateReqVO.getName());
|
|
|
+
|
|
|
+ // 更新产品分类
|
|
|
+ IotProductClassifyDO updateObj = BeanUtils.toBean(updateReqVO, IotProductClassifyDO.class);
|
|
|
+ iotProductClassifyMapper.updateById(updateObj);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void deleteIotProductClassify(Long id) {
|
|
|
+ // 校验是否存在
|
|
|
+ validateIotProductClassifyExists(id);
|
|
|
+ // 校验是否有子产品分类
|
|
|
+ if (iotProductClassifyMapper.selectCountByParentId(id) > 0) {
|
|
|
+ throw exception(DEPT_EXITS_CHILDREN);
|
|
|
+ }
|
|
|
+ // 删除产品分类
|
|
|
+ iotProductClassifyMapper.deleteById(id);
|
|
|
+ }
|
|
|
+
|
|
|
+ @VisibleForTesting
|
|
|
+ void validateIotProductClassifyExists(Long id) {
|
|
|
+ if (id == null) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ IotProductClassifyDO dept = iotProductClassifyMapper.selectById(id);
|
|
|
+ if (dept == null) {
|
|
|
+ throw exception(DEPT_NOT_FOUND);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @VisibleForTesting
|
|
|
+ void validateParentProductClassify(Long id, Long parentId) {
|
|
|
+ if (parentId == null || IotProductClassifyDO.PARENT_ID_ROOT.equals(parentId)) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ // 1. 不能设置自己为父产品分类
|
|
|
+ if (Objects.equals(id, parentId)) {
|
|
|
+ throw exception(DEPT_PARENT_ERROR);
|
|
|
+ }
|
|
|
+ // 2. 父产品分类不存在
|
|
|
+ IotProductClassifyDO parentDept = iotProductClassifyMapper.selectById(parentId);
|
|
|
+ if (parentDept == null) {
|
|
|
+ throw exception(DEPT_PARENT_NOT_EXITS);
|
|
|
+ }
|
|
|
+ // 3. 递归校验父产品分类,如果父产品分类是自己的子产品分类,则报错,避免形成环路
|
|
|
+ if (id == null) { // id 为空,说明新增,不需要考虑环路
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ for (int i = 0; i < Short.MAX_VALUE; i++) {
|
|
|
+ // 3.1 校验环路
|
|
|
+ parentId = parentDept.getParentId();
|
|
|
+ if (Objects.equals(id, parentId)) {
|
|
|
+ throw exception(DEPT_PARENT_IS_CHILD);
|
|
|
+ }
|
|
|
+ // 3.2 继续递归下一级父产品分类
|
|
|
+ if (parentId == null || IotProductClassifyDO.PARENT_ID_ROOT.equals(parentId)) {
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ parentDept = iotProductClassifyMapper.selectById(parentId);
|
|
|
+ if (parentDept == null) {
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @VisibleForTesting
|
|
|
+ void validateProductClassifyNameUnique(Long id, Long parentId, String name) {
|
|
|
+ IotProductClassifyDO dept = iotProductClassifyMapper.selectByParentIdAndName(parentId, name);
|
|
|
+ if (dept == null) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ // 如果 id 为空,说明不用比较是否为相同 id 的产品分类
|
|
|
+ if (id == null) {
|
|
|
+ throw exception(DEPT_NAME_DUPLICATE);
|
|
|
+ }
|
|
|
+ if (ObjectUtil.notEqual(dept.getId(), id)) {
|
|
|
+ throw exception(DEPT_NAME_DUPLICATE);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public IotProductClassifyDO getIotProductClassify(Long id) {
|
|
|
+ return iotProductClassifyMapper.selectById(id);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public List<IotProductClassifyDO> getIotProductClassifyList(Collection<Long> ids) {
|
|
|
+ if (CollUtil.isEmpty(ids)) {
|
|
|
+ return Collections.emptyList();
|
|
|
+ }
|
|
|
+ return iotProductClassifyMapper.selectBatchIds(ids);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public List<IotProductClassifyDO> getIotProductClassifyList(IotProductClassifyListReqVO reqVO) {
|
|
|
+ List<IotProductClassifyDO> list = iotProductClassifyMapper.selectList(reqVO);
|
|
|
+ list.sort(Comparator.comparing(IotProductClassifyDO::getSort));
|
|
|
+ return list;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public List<IotProductClassifyDO> getChildIotProductClassifyList(Collection<Long> ids) {
|
|
|
+ List<IotProductClassifyDO> children = new LinkedList<>();
|
|
|
+ // 遍历每一层
|
|
|
+ Collection<Long> parentIds = ids;
|
|
|
+ for (int i = 0; i < Short.MAX_VALUE; i++) { // 使用 Short.MAX_VALUE 避免 bug 场景下,存在死循环
|
|
|
+ // 查询当前层,所有的子产品分类
|
|
|
+ List<IotProductClassifyDO> depts = iotProductClassifyMapper.selectListByParentId(parentIds);
|
|
|
+ // 1. 如果没有子产品分类,则结束遍历
|
|
|
+ if (CollUtil.isEmpty(depts)) {
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ // 2. 如果有子产品分类,继续遍历
|
|
|
+ children.addAll(depts);
|
|
|
+ parentIds = convertSet(depts, IotProductClassifyDO::getId);
|
|
|
+ }
|
|
|
+ return children;
|
|
|
+ }
|
|
|
+
|
|
|
+}
|