|
|
@@ -0,0 +1,144 @@
|
|
|
+package cn.iocoder.yudao.module.pms.service.qhse.hazard;
|
|
|
+
|
|
|
+import cn.hutool.core.util.ObjectUtil;
|
|
|
+import cn.iocoder.yudao.framework.common.exception.ErrorCode;
|
|
|
+import cn.iocoder.yudao.framework.common.pojo.PageResult;
|
|
|
+import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
|
|
|
+import cn.iocoder.yudao.module.pms.controller.admin.qhse.hazard.vo.IotHazardTypePageReqVO;
|
|
|
+import cn.iocoder.yudao.module.pms.controller.admin.qhse.hazard.vo.IotHazardTypeSaveReqVO;
|
|
|
+import cn.iocoder.yudao.module.pms.dal.dataobject.IotProductClassifyDO;
|
|
|
+import cn.iocoder.yudao.module.pms.dal.dataobject.qhse.hazard.IotHazardTypeDO;
|
|
|
+import cn.iocoder.yudao.module.pms.dal.mysql.qhse.hazard.IotHazardTypeMapper;
|
|
|
+import cn.iocoder.yudao.module.pms.enums.ErrorCodeConstant;
|
|
|
+import com.google.common.annotations.VisibleForTesting;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.validation.annotation.Validated;
|
|
|
+
|
|
|
+import javax.annotation.Resource;
|
|
|
+import java.util.Objects;
|
|
|
+
|
|
|
+import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
|
|
|
+import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.*;
|
|
|
+
|
|
|
+/**
|
|
|
+ * QHSE-隐患排查分类 Service 实现类
|
|
|
+ *
|
|
|
+ * @author 超级管理员
|
|
|
+ */
|
|
|
+@Service
|
|
|
+@Validated
|
|
|
+public class IotHazardTypeServiceImpl implements IotHazardTypeService {
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private IotHazardTypeMapper iotHazardTypeMapper;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Long createIotHazardType(IotHazardTypeSaveReqVO createReqVO) {
|
|
|
+ if (createReqVO.getParentId() == null) {
|
|
|
+ createReqVO.setParentId(IotProductClassifyDO.PARENT_ID_ROOT);
|
|
|
+ }
|
|
|
+ // 校验父产品分类的有效性
|
|
|
+ validateParentHazard(null, createReqVO.getParentId());
|
|
|
+ // 校验产品分类名的唯一性
|
|
|
+ validateHazardNameUnique(null, createReqVO.getParentId(), createReqVO.getName());
|
|
|
+ // 插入
|
|
|
+ IotHazardTypeDO iotHazardType = BeanUtils.toBean(createReqVO, IotHazardTypeDO.class);
|
|
|
+
|
|
|
+ iotHazardTypeMapper.insert(iotHazardType);
|
|
|
+ // 返回
|
|
|
+ return iotHazardType.getId();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void updateIotHazardType(IotHazardTypeSaveReqVO updateReqVO) {
|
|
|
+ if (updateReqVO.getParentId() == null) {
|
|
|
+ updateReqVO.setParentId(IotProductClassifyDO.PARENT_ID_ROOT);
|
|
|
+ }
|
|
|
+ // 校验存在
|
|
|
+ validateIotHazardTypeExists(updateReqVO.getId());
|
|
|
+ // 校验父产品分类的有效性
|
|
|
+ validateParentHazard(updateReqVO.getId(), updateReqVO.getParentId());
|
|
|
+ // 校验产品分类名的唯一性
|
|
|
+ validateHazardNameUnique(updateReqVO.getId(), updateReqVO.getParentId(), updateReqVO.getName());
|
|
|
+ // 更新
|
|
|
+ IotHazardTypeDO updateObj = BeanUtils.toBean(updateReqVO, IotHazardTypeDO.class);
|
|
|
+ iotHazardTypeMapper.updateById(updateObj);
|
|
|
+ }
|
|
|
+
|
|
|
+ private void validateIotHazardTypeExists(Long id) {
|
|
|
+ if (iotHazardTypeMapper.selectById(id) == null) {
|
|
|
+ throw exception(new ErrorCode(1,"不存在"));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ @Override
|
|
|
+ public void deleteIotHazardType(Long id) {
|
|
|
+ // 校验存在
|
|
|
+ validateIotHazardTypeExists(id);
|
|
|
+ if (iotHazardTypeMapper.selectCountByParentId(id) > 0) {
|
|
|
+ throw exception(ErrorCodeConstant.PRODUCT_EXITS_CHILDREN);
|
|
|
+ }
|
|
|
+ // 删除
|
|
|
+ iotHazardTypeMapper.deleteById(id);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public IotHazardTypeDO getIotHazardType(Long id) {
|
|
|
+ return iotHazardTypeMapper.selectById(id);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public PageResult<IotHazardTypeDO> getIotHazardTypePage(IotHazardTypePageReqVO pageReqVO) {
|
|
|
+ return iotHazardTypeMapper.selectPage(pageReqVO);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ @VisibleForTesting
|
|
|
+ void validateParentHazard(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. 父产品分类不存在
|
|
|
+ IotHazardTypeDO parentDept = iotHazardTypeMapper.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 = iotHazardTypeMapper.selectById(parentId);
|
|
|
+ if (parentDept == null) {
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @VisibleForTesting
|
|
|
+ void validateHazardNameUnique(Long id, Long parentId, String name) {
|
|
|
+ IotHazardTypeDO dept = iotHazardTypeMapper.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);
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|