|
@@ -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.IotInfoClassifyListReqVO;
|
|
|
+import cn.iocoder.yudao.module.pms.controller.admin.vo.IotInfoClassifySaveReqVO;
|
|
|
+import cn.iocoder.yudao.module.pms.dal.dataobject.IotInfoClassifyDO;
|
|
|
+import cn.iocoder.yudao.module.pms.dal.mysql.IotInfoClassifyMapper;
|
|
|
+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 IotInfoClassifyServiceImpl implements IotInfoClassifyService {
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private IotInfoClassifyMapper iotInfoClassifyMapper;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Long createIotInfoClassify(IotInfoClassifySaveReqVO createReqVO) {
|
|
|
+ if (createReqVO.getParentId() == null) {
|
|
|
+ createReqVO.setParentId(IotInfoClassifyDO.PARENT_ID_ROOT);
|
|
|
+ }
|
|
|
+ // 校验父产品分类的有效性
|
|
|
+ validateParentInfoClassify(null, createReqVO.getParentId());
|
|
|
+ // 校验产品分类名的唯一性
|
|
|
+ validateInfoClassifyNameUnique(null, createReqVO.getParentId(), createReqVO.getName());
|
|
|
+
|
|
|
+ // 插入产品分类
|
|
|
+ IotInfoClassifyDO dept = BeanUtils.toBean(createReqVO, IotInfoClassifyDO.class);
|
|
|
+ iotInfoClassifyMapper.insert(dept);
|
|
|
+ return dept.getId();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void updateIotInfoClassify(IotInfoClassifySaveReqVO updateReqVO) {
|
|
|
+ if (updateReqVO.getParentId() == null) {
|
|
|
+ updateReqVO.setParentId(IotInfoClassifyDO.PARENT_ID_ROOT);
|
|
|
+ }
|
|
|
+ // 校验自己存在
|
|
|
+ validateIotInfoClassifyExists(updateReqVO.getId());
|
|
|
+ // 校验父产品分类的有效性
|
|
|
+ validateParentInfoClassify(updateReqVO.getId(), updateReqVO.getParentId());
|
|
|
+ // 校验产品分类名的唯一性
|
|
|
+ validateInfoClassifyNameUnique(updateReqVO.getId(), updateReqVO.getParentId(), updateReqVO.getName());
|
|
|
+
|
|
|
+ // 更新产品分类
|
|
|
+ IotInfoClassifyDO updateObj = BeanUtils.toBean(updateReqVO, IotInfoClassifyDO.class);
|
|
|
+ iotInfoClassifyMapper.updateById(updateObj);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void deleteIotInfoClassify(Long id) {
|
|
|
+ // 校验是否存在
|
|
|
+ validateIotInfoClassifyExists(id);
|
|
|
+ // 校验是否有子产品分类
|
|
|
+ if (iotInfoClassifyMapper.selectCountByParentId(id) > 0) {
|
|
|
+ throw exception(DEPT_EXITS_CHILDREN);
|
|
|
+ }
|
|
|
+ // 删除产品分类
|
|
|
+ iotInfoClassifyMapper.deleteById(id);
|
|
|
+ }
|
|
|
+
|
|
|
+ @VisibleForTesting
|
|
|
+ void validateIotInfoClassifyExists(Long id) {
|
|
|
+ if (id == null) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ IotInfoClassifyDO dept = iotInfoClassifyMapper.selectById(id);
|
|
|
+ if (dept == null) {
|
|
|
+ throw exception(DEPT_NOT_FOUND);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @VisibleForTesting
|
|
|
+ void validateParentInfoClassify(Long id, Long parentId) {
|
|
|
+ if (parentId == null || IotInfoClassifyDO.PARENT_ID_ROOT.equals(parentId)) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ // 1. 不能设置自己为父产品分类
|
|
|
+ if (Objects.equals(id, parentId)) {
|
|
|
+ throw exception(DEPT_PARENT_ERROR);
|
|
|
+ }
|
|
|
+ // 2. 父产品分类不存在
|
|
|
+ IotInfoClassifyDO parentDept = iotInfoClassifyMapper.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 || IotInfoClassifyDO.PARENT_ID_ROOT.equals(parentId)) {
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ parentDept = iotInfoClassifyMapper.selectById(parentId);
|
|
|
+ if (parentDept == null) {
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @VisibleForTesting
|
|
|
+ void validateInfoClassifyNameUnique(Long id, Long parentId, String name) {
|
|
|
+ IotInfoClassifyDO dept = iotInfoClassifyMapper.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 IotInfoClassifyDO getIotInfoClassify(Long id) {
|
|
|
+ return iotInfoClassifyMapper.selectById(id);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public List<IotInfoClassifyDO> getIotInfoClassifyList(Collection<Long> ids) {
|
|
|
+ if (CollUtil.isEmpty(ids)) {
|
|
|
+ return Collections.emptyList();
|
|
|
+ }
|
|
|
+ return iotInfoClassifyMapper.selectBatchIds(ids);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public List<IotInfoClassifyDO> getIotInfoClassifyList(IotInfoClassifyListReqVO reqVO) {
|
|
|
+ List<IotInfoClassifyDO> list = iotInfoClassifyMapper.selectList(reqVO);
|
|
|
+ list.sort(Comparator.comparing(IotInfoClassifyDO::getSort));
|
|
|
+ return list;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public List<IotInfoClassifyDO> getChildIotInfoClassifyList(Collection<Long> ids) {
|
|
|
+ List<IotInfoClassifyDO> children = new LinkedList<>();
|
|
|
+ // 遍历每一层
|
|
|
+ Collection<Long> parentIds = ids;
|
|
|
+ for (int i = 0; i < Short.MAX_VALUE; i++) { // 使用 Short.MAX_VALUE 避免 bug 场景下,存在死循环
|
|
|
+ // 查询当前层,所有的子产品分类
|
|
|
+ List<IotInfoClassifyDO> depts = iotInfoClassifyMapper.selectListByParentId(parentIds);
|
|
|
+ // 1. 如果没有子产品分类,则结束遍历
|
|
|
+ if (CollUtil.isEmpty(depts)) {
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ // 2. 如果有子产品分类,继续遍历
|
|
|
+ children.addAll(depts);
|
|
|
+ parentIds = convertSet(depts, IotInfoClassifyDO::getId);
|
|
|
+ }
|
|
|
+ return children;
|
|
|
+ }
|
|
|
+
|
|
|
+}
|